Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py (New Secure & Robust Version)
|
| 2 |
import gradio as gr
|
| 3 |
import httpx
|
| 4 |
import os
|
|
@@ -17,14 +17,13 @@ def call_blaxel_backend(
|
|
| 17 |
sambanova_key: str
|
| 18 |
):
|
| 19 |
|
| 20 |
-
# --- 1. Validate Keys ---
|
| 21 |
if not BLAXEL_BASE_URL or not BLAXEL_API_KEY:
|
| 22 |
-
yield "Configuration Error: The app's own BLAXEL secrets are not set."
|
| 23 |
return
|
| 24 |
|
| 25 |
-
# We require at least the Google key
|
| 26 |
if not google_key:
|
| 27 |
-
yield {status_output: "Input Error: Please enter your Google API Key
|
| 28 |
return
|
| 29 |
|
| 30 |
# --- 2. Set up connection details ---
|
|
@@ -40,12 +39,13 @@ def call_blaxel_backend(
|
|
| 40 |
"problem": user_problem,
|
| 41 |
"keys": {
|
| 42 |
"google": google_key,
|
| 43 |
-
"anthropic": anthropic_key or None,
|
| 44 |
-
"sambanova": sambanova_key or None,
|
| 45 |
}
|
| 46 |
}
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
try:
|
| 51 |
# Set a long timeout for all the LLM calls
|
|
@@ -53,6 +53,7 @@ def call_blaxel_backend(
|
|
| 53 |
|
| 54 |
if response.status_code != 200:
|
| 55 |
error_details = response.read().decode()
|
|
|
|
| 56 |
yield {status_output: f"HTTP Error: Received status code {response.status_code} from Blaxel.\nDetails: {error_details}"}
|
| 57 |
return
|
| 58 |
|
|
@@ -60,12 +61,11 @@ def call_blaxel_backend(
|
|
| 60 |
full_log = ""
|
| 61 |
for chunk in response.iter_text():
|
| 62 |
if chunk.startswith("FINAL:"):
|
| 63 |
-
# This is our final package, don't yield it
|
| 64 |
final_json = json.loads(chunk.replace("FINAL:", ""))
|
| 65 |
else:
|
| 66 |
-
# This is a live log update
|
| 67 |
full_log += chunk + "\n"
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
# --- 4. Return final results to all UI components ---
|
| 71 |
if final_json:
|
|
@@ -75,6 +75,7 @@ def call_blaxel_backend(
|
|
| 75 |
final_audio_output: final_json.get("audio")
|
| 76 |
}
|
| 77 |
|
|
|
|
| 78 |
except httpx.ConnectError as e:
|
| 79 |
yield {status_output: f"Connection Error: Could not connect to Blaxel.\nDetails: {str(e)}"}
|
| 80 |
except httpx.ReadTimeout:
|
|
@@ -118,7 +119,7 @@ with gr.Blocks() as demo:
|
|
| 118 |
with gr.Column(scale=1):
|
| 119 |
status_output = gr.Textbox(
|
| 120 |
label="Agent's Internal Monologue (Live Log)",
|
| 121 |
-
lines=20,
|
| 122 |
interactive=False
|
| 123 |
)
|
| 124 |
final_text_output = gr.Markdown(label="Final Briefing")
|
|
@@ -131,16 +132,17 @@ with gr.Blocks() as demo:
|
|
| 131 |
sambanova_key_input
|
| 132 |
]
|
| 133 |
|
| 134 |
-
|
|
|
|
| 135 |
status_output,
|
| 136 |
final_text_output,
|
| 137 |
final_audio_output
|
| 138 |
-
|
| 139 |
|
| 140 |
submit_button.click(
|
| 141 |
fn=call_blaxel_backend,
|
| 142 |
inputs=all_inputs,
|
| 143 |
-
outputs=
|
| 144 |
)
|
| 145 |
|
| 146 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py (New Secure & Robust Version - Gradio FIX)
|
| 2 |
import gradio as gr
|
| 3 |
import httpx
|
| 4 |
import os
|
|
|
|
| 17 |
sambanova_key: str
|
| 18 |
):
|
| 19 |
|
| 20 |
+
# --- 1. Validate Keys (FIX: All yields are now dictionaries) ---
|
| 21 |
if not BLAXEL_BASE_URL or not BLAXEL_API_KEY:
|
| 22 |
+
yield {status_output: "Configuration Error: The app's own BLAXEL secrets are not set in Hugging Face."}
|
| 23 |
return
|
| 24 |
|
|
|
|
| 25 |
if not google_key:
|
| 26 |
+
yield {status_output: "Input Error: Please enter your Google API Key. It is required for the 'Judge' agent."}
|
| 27 |
return
|
| 28 |
|
| 29 |
# --- 2. Set up connection details ---
|
|
|
|
| 39 |
"problem": user_problem,
|
| 40 |
"keys": {
|
| 41 |
"google": google_key,
|
| 42 |
+
"anthropic": anthropic_key or None,
|
| 43 |
+
"sambanova": sambanova_key or None,
|
| 44 |
}
|
| 45 |
}
|
| 46 |
|
| 47 |
+
# FIX: Yield a dictionary
|
| 48 |
+
yield {status_output: f"Connecting to MudabbirAI (at {full_endpoint_url})..."}
|
| 49 |
|
| 50 |
try:
|
| 51 |
# Set a long timeout for all the LLM calls
|
|
|
|
| 53 |
|
| 54 |
if response.status_code != 200:
|
| 55 |
error_details = response.read().decode()
|
| 56 |
+
# FIX: Yield a dictionary
|
| 57 |
yield {status_output: f"HTTP Error: Received status code {response.status_code} from Blaxel.\nDetails: {error_details}"}
|
| 58 |
return
|
| 59 |
|
|
|
|
| 61 |
full_log = ""
|
| 62 |
for chunk in response.iter_text():
|
| 63 |
if chunk.startswith("FINAL:"):
|
|
|
|
| 64 |
final_json = json.loads(chunk.replace("FINAL:", ""))
|
| 65 |
else:
|
|
|
|
| 66 |
full_log += chunk + "\n"
|
| 67 |
+
# FIX: Yield a dictionary to update just the log
|
| 68 |
+
yield {status_output: full_log}
|
| 69 |
|
| 70 |
# --- 4. Return final results to all UI components ---
|
| 71 |
if final_json:
|
|
|
|
| 75 |
final_audio_output: final_json.get("audio")
|
| 76 |
}
|
| 77 |
|
| 78 |
+
# FIX: All except blocks now yield dictionaries
|
| 79 |
except httpx.ConnectError as e:
|
| 80 |
yield {status_output: f"Connection Error: Could not connect to Blaxel.\nDetails: {str(e)}"}
|
| 81 |
except httpx.ReadTimeout:
|
|
|
|
| 119 |
with gr.Column(scale=1):
|
| 120 |
status_output = gr.Textbox(
|
| 121 |
label="Agent's Internal Monologue (Live Log)",
|
| 122 |
+
lines=20,
|
| 123 |
interactive=False
|
| 124 |
)
|
| 125 |
final_text_output = gr.Markdown(label="Final Briefing")
|
|
|
|
| 132 |
sambanova_key_input
|
| 133 |
]
|
| 134 |
|
| 135 |
+
# FIX: Define outputs as a stable LIST, not an unordered set
|
| 136 |
+
all_outputs = [
|
| 137 |
status_output,
|
| 138 |
final_text_output,
|
| 139 |
final_audio_output
|
| 140 |
+
]
|
| 141 |
|
| 142 |
submit_button.click(
|
| 143 |
fn=call_blaxel_backend,
|
| 144 |
inputs=all_inputs,
|
| 145 |
+
outputs=all_outputs # Pass the list
|
| 146 |
)
|
| 147 |
|
| 148 |
demo.launch()
|