youssefleb commited on
Commit
3f3a77b
·
verified ·
1 Parent(s): 3b50dc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -23
app.py CHANGED
@@ -1,9 +1,10 @@
1
- # app.py (Fixed: Robust Line-by-Line Streaming)
2
  import gradio as gr
3
  import httpx
4
  import os
5
  import json
6
- from visuals import create_progress_chart, create_calibration_table
 
7
 
8
  # --- Config ---
9
  BLAXEL_BASE_URL = os.getenv("BLAXEL_BACKEND_URL")
@@ -38,7 +39,7 @@ def call_blaxel_backend(user_problem, google_key, anthropic_key, sambanova_key):
38
  yield {status_output: f"Connecting to MudabbirAI..."}
39
 
40
  try:
41
- # Increased timeout to 600s because complex agents can take time
42
  with httpx.stream("POST", full_endpoint_url, json=payload, headers=headers, timeout=600.0) as response:
43
  if response.status_code != 200:
44
  yield {status_output: f"HTTP Error: {response.status_code}"}
@@ -47,24 +48,16 @@ def call_blaxel_backend(user_problem, google_key, anthropic_key, sambanova_key):
47
  final_json = None
48
  full_log = ""
49
 
50
- # --- FIX: Use iter_lines() for robust SSE handling ---
51
  for line in response.iter_lines():
52
- if not line:
53
- continue # Skip empty keep-alive lines
 
54
 
55
- # Decode line (httpx yields strings in newer versions, but safe to check)
56
- if hasattr(line, "decode"):
57
- line = line.decode("utf-8")
58
-
59
- # Filter out keep-alive comments (lines starting with :)
60
- if line.startswith(":"):
61
- continue
62
-
63
- # Process data lines
64
  if line.startswith("data: "):
65
  content = line.replace("data: ", "", 1).strip()
66
 
67
- # Check for the FINAL payload
68
  if content.startswith("FINAL:"):
69
  try:
70
  json_str = content.replace("FINAL:", "", 1)
@@ -72,18 +65,18 @@ def call_blaxel_backend(user_problem, google_key, anthropic_key, sambanova_key):
72
  except json.JSONDecodeError as e:
73
  full_log += f"\n[Error Parsing Final JSON]: {e}"
74
  else:
75
- # Normal log update
76
- # We add a newline to separate log entries cleanly
77
  full_log += content + "\n"
78
  yield {status_output: full_log}
79
 
80
- # --- Render Results ---
81
  if final_json:
82
  log_data = final_json.get("log")
83
 
84
- # Generate Visuals
85
  chart = create_progress_chart(log_data)
86
  calib_table = create_calibration_table(log_data)
 
87
 
88
  yield {
89
  status_output: full_log + "\nDone!",
@@ -91,10 +84,10 @@ def call_blaxel_backend(user_problem, google_key, anthropic_key, sambanova_key):
91
  final_audio_output: final_json.get("audio"),
92
  final_json_log: log_data,
93
  progress_plot: chart,
94
- calibration_data: calib_table
 
95
  }
96
  else:
97
- # If stream ended but no FINAL json, show what we got
98
  yield {status_output: full_log + "\n[Stream ended without final payload]"}
99
 
100
  except Exception as e:
@@ -125,6 +118,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", secondary_hue="slate"
125
  progress_plot = gr.Plot(label="Improvement Trajectory (Draft 1 vs. Final)")
126
  with gr.Row():
127
  calibration_data = gr.Dataframe(label="Team Calibration Results (Model Scores)")
 
 
128
 
129
  with gr.TabItem("📝 Final Briefing", id="result_tab"):
130
  final_text_output = gr.Markdown(label="Strategic Report")
@@ -137,7 +132,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", secondary_hue="slate"
137
  submit_button.click(
138
  fn=call_blaxel_backend,
139
  inputs=[problem_input, google_key_input, anthropic_key_input, sambanova_key_input],
140
- outputs=[status_output, final_text_output, final_audio_output, final_json_log, progress_plot, calibration_data]
141
  )
142
 
143
  demo.launch()
 
1
+ # app.py (Final Version: Visuals + Financials + Robust Streaming)
2
  import gradio as gr
3
  import httpx
4
  import os
5
  import json
6
+ # Import the visualization logic from our new module
7
+ from visuals import create_progress_chart, create_calibration_table, create_cost_summary
8
 
9
  # --- Config ---
10
  BLAXEL_BASE_URL = os.getenv("BLAXEL_BACKEND_URL")
 
39
  yield {status_output: f"Connecting to MudabbirAI..."}
40
 
41
  try:
42
+ # High timeout for complex agentic workflows
43
  with httpx.stream("POST", full_endpoint_url, json=payload, headers=headers, timeout=600.0) as response:
44
  if response.status_code != 200:
45
  yield {status_output: f"HTTP Error: {response.status_code}"}
 
48
  final_json = None
49
  full_log = ""
50
 
51
+ # Robust SSE Line Parsing
52
  for line in response.iter_lines():
53
+ if not line: continue
54
+ if hasattr(line, "decode"): line = line.decode("utf-8")
55
+ if line.startswith(":"): continue # Skip keep-alive comments
56
 
 
 
 
 
 
 
 
 
 
57
  if line.startswith("data: "):
58
  content = line.replace("data: ", "", 1).strip()
59
 
60
+ # Check for FINAL payload
61
  if content.startswith("FINAL:"):
62
  try:
63
  json_str = content.replace("FINAL:", "", 1)
 
65
  except json.JSONDecodeError as e:
66
  full_log += f"\n[Error Parsing Final JSON]: {e}"
67
  else:
68
+ # Streaming Log Update
 
69
  full_log += content + "\n"
70
  yield {status_output: full_log}
71
 
72
+ # Final Rendering
73
  if final_json:
74
  log_data = final_json.get("log")
75
 
76
+ # Create Visuals & Cost Report
77
  chart = create_progress_chart(log_data)
78
  calib_table = create_calibration_table(log_data)
79
+ cost_md = create_cost_summary(log_data)
80
 
81
  yield {
82
  status_output: full_log + "\nDone!",
 
84
  final_audio_output: final_json.get("audio"),
85
  final_json_log: log_data,
86
  progress_plot: chart,
87
+ calibration_data: calib_table,
88
+ cost_display: cost_md
89
  }
90
  else:
 
91
  yield {status_output: full_log + "\n[Stream ended without final payload]"}
92
 
93
  except Exception as e:
 
118
  progress_plot = gr.Plot(label="Improvement Trajectory (Draft 1 vs. Final)")
119
  with gr.Row():
120
  calibration_data = gr.Dataframe(label="Team Calibration Results (Model Scores)")
121
+ with gr.Row():
122
+ cost_display = gr.Markdown(label="Financial Intelligence")
123
 
124
  with gr.TabItem("📝 Final Briefing", id="result_tab"):
125
  final_text_output = gr.Markdown(label="Strategic Report")
 
132
  submit_button.click(
133
  fn=call_blaxel_backend,
134
  inputs=[problem_input, google_key_input, anthropic_key_input, sambanova_key_input],
135
+ outputs=[status_output, final_text_output, final_audio_output, final_json_log, progress_plot, calibration_data, cost_display]
136
  )
137
 
138
  demo.launch()