Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py (Fixed
|
| 2 |
import gradio as gr
|
| 3 |
import httpx
|
| 4 |
import os
|
|
@@ -38,30 +38,33 @@ def call_blaxel_backend(user_problem, google_key, anthropic_key, sambanova_key):
|
|
| 38 |
yield {status_output: f"Connecting to MudabbirAI..."}
|
| 39 |
|
| 40 |
try:
|
| 41 |
-
|
|
|
|
| 42 |
if response.status_code != 200:
|
| 43 |
yield {status_output: f"HTTP Error: {response.status_code}"}
|
| 44 |
return
|
| 45 |
|
| 46 |
final_json = None
|
| 47 |
full_log = ""
|
| 48 |
-
buffer = ""
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
for
|
| 52 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
if content.startswith("FINAL:"):
|
| 66 |
try:
|
| 67 |
json_str = content.replace("FINAL:", "", 1)
|
|
@@ -69,11 +72,12 @@ def call_blaxel_backend(user_problem, google_key, anthropic_key, sambanova_key):
|
|
| 69 |
except json.JSONDecodeError as e:
|
| 70 |
full_log += f"\n[Error Parsing Final JSON]: {e}"
|
| 71 |
else:
|
| 72 |
-
#
|
|
|
|
| 73 |
full_log += content + "\n"
|
| 74 |
yield {status_output: full_log}
|
| 75 |
|
| 76 |
-
#
|
| 77 |
if final_json:
|
| 78 |
log_data = final_json.get("log")
|
| 79 |
|
|
@@ -89,9 +93,12 @@ def call_blaxel_backend(user_problem, google_key, anthropic_key, sambanova_key):
|
|
| 89 |
progress_plot: chart,
|
| 90 |
calibration_data: calib_table
|
| 91 |
}
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
except Exception as e:
|
| 94 |
-
yield {status_output: f"Error: {str(e)}"}
|
| 95 |
|
| 96 |
# --- Gradio UI ---
|
| 97 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", secondary_hue="slate")) as demo:
|
|
|
|
| 1 |
+
# app.py (Fixed: Robust Line-by-Line Streaming)
|
| 2 |
import gradio as gr
|
| 3 |
import httpx
|
| 4 |
import os
|
|
|
|
| 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}"}
|
| 45 |
return
|
| 46 |
|
| 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 |
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 |
|
|
|
|
| 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:
|
| 101 |
+
yield {status_output: f"Connection Error: {str(e)}"}
|
| 102 |
|
| 103 |
# --- Gradio UI ---
|
| 104 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", secondary_hue="slate")) as demo:
|