youssefleb commited on
Commit
7833f1c
·
verified ·
1 Parent(s): fe6cb14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -1,9 +1,8 @@
1
- # app.py (Clean Version - Imports Visuals)
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
8
 
9
  # --- Config ---
@@ -46,20 +45,39 @@ def call_blaxel_backend(user_problem, google_key, anthropic_key, sambanova_key):
46
 
47
  final_json = None
48
  full_log = ""
49
-
50
- # Stream the text updates
 
51
  for chunk in response.iter_text():
52
- if chunk.startswith("FINAL:"):
53
- final_json = json.loads(chunk.replace("FINAL:", ""))
54
- else:
55
- full_log += chunk + "\n"
56
- yield {status_output: full_log}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- # Once finished, process the JSON log to create visuals
59
  if final_json:
60
  log_data = final_json.get("log")
61
 
62
- # Generate Visuals using the imported functions
63
  chart = create_progress_chart(log_data)
64
  calib_table = create_calibration_table(log_data)
65
 
 
1
+ # app.py (Fixed SSE Parsing for Large JSON Logs)
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 ---
 
45
 
46
  final_json = None
47
  full_log = ""
48
+ buffer = ""
49
+
50
+ # Iterate over the stream
51
  for chunk in response.iter_text():
52
+ buffer += chunk
53
+
54
+ # Process complete SSE events (separated by double newline)
55
+ while "\n\n" in buffer:
56
+ message, buffer = buffer.split("\n\n", 1)
57
+
58
+ # Remove "data: " prefix if present
59
+ if message.startswith("data: "):
60
+ content = message.replace("data: ", "", 1)
61
+ else:
62
+ content = message
63
+
64
+ # Check if this is the FINAL payload
65
+ if content.startswith("FINAL:"):
66
+ try:
67
+ json_str = content.replace("FINAL:", "", 1)
68
+ final_json = json.loads(json_str)
69
+ except json.JSONDecodeError as e:
70
+ full_log += f"\n[Error Parsing Final JSON]: {e}"
71
+ else:
72
+ # Standard log update
73
+ full_log += content + "\n"
74
+ yield {status_output: full_log}
75
 
76
+ # Once stream ends, check if we got the JSON and render visuals
77
  if final_json:
78
  log_data = final_json.get("log")
79
 
80
+ # Generate Visuals
81
  chart = create_progress_chart(log_data)
82
  calib_table = create_calibration_table(log_data)
83