Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,98 +1,52 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import httpx
|
| 3 |
-
import os
|
| 4 |
import json
|
| 5 |
|
| 6 |
-
# ---
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
if not BLAXEL_BACKEND_URL:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
request_data = {
|
| 19 |
-
"problem": user_problem,
|
| 20 |
-
"run_count": run_count,
|
| 21 |
-
"do_calibrate": do_calibrate
|
| 22 |
-
}
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
response.raise_for_status()
|
| 33 |
-
final_data = response.json()
|
| 34 |
-
status_log = "\n".join(final_data.get("status_log", []))
|
| 35 |
-
|
| 36 |
-
yield (
|
| 37 |
-
status_log,
|
| 38 |
-
final_data.get("text", "Error: No text received."),
|
| 39 |
-
final_data.get("audio", None)
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
except httpx.HTTPStatusError as e:
|
| 43 |
-
yield f"HTTP Error: {e.response.status_code}
|
| 44 |
-
except httpx.ConnectError:
|
| 45 |
-
yield f"ERROR: Could not connect to Blaxel backend at: {BLAXEL_BACKEND_URL}", None, None
|
| 46 |
except httpx.ReadTimeout:
|
| 47 |
-
yield "Error: The
|
| 48 |
except Exception as e:
|
| 49 |
-
yield f"An
|
| 50 |
-
|
| 51 |
-
# --- Gradio UI
|
| 52 |
-
with gr.Blocks(
|
| 53 |
-
gr.Markdown("# MudabbirAI")
|
| 54 |
-
gr.
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
with gr.Tabs():
|
| 57 |
-
with gr.TabItem("🚀 Deploy Agent"):
|
| 58 |
-
gr.Markdown("## 1. Enter Your 'Wicked Problem'")
|
| 59 |
-
problem_input = gr.Textbox(
|
| 60 |
-
label="Problem Brief",
|
| 61 |
-
placeholder="e.g., 'Draft a novel, feasible, and culturally sensitive marketing plan for our new app in Japan...'",
|
| 62 |
-
lines=5
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
gr.Markdown("## 2. (Optional) Select Frameworks")
|
| 66 |
-
|
| 67 |
-
gr.Markdown("## 3. (Optional) Advanced Settings")
|
| 68 |
-
with gr.Accordion("Advanced Run Options", open=False):
|
| 69 |
-
run_count = gr.Slider(
|
| 70 |
-
label="Number of Runs (Leverages AI stochasticity)",
|
| 71 |
-
minimum=1, maximum=5, step=1, value=1
|
| 72 |
-
)
|
| 73 |
-
do_calibrate = gr.Checkbox(
|
| 74 |
-
label="Run Live Ensemble Calibration (Slower, highest accuracy)",
|
| 75 |
-
value=False
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
gr.Markdown("## 4. Execute")
|
| 79 |
-
submit_button = gr.Button("Deploy Agent", variant="primary")
|
| 80 |
-
|
| 81 |
-
with gr.TabItem("📊 Live Log & Final Output"):
|
| 82 |
-
gr.Markdown("## Agent's Internal Monologue (Live Log)")
|
| 83 |
-
status_output = gr.Textbox(label="Agent Status Log", interactive=False, lines=10)
|
| 84 |
-
|
| 85 |
-
gr.Markdown("## Final Briefing")
|
| 86 |
-
final_text_output = gr.Markdown()
|
| 87 |
-
|
| 88 |
-
gr.Markdown("## Audio Presentation")
|
| 89 |
-
final_audio_output = gr.Audio(label="Audio Presentation", type="filepath")
|
| 90 |
-
|
| 91 |
submit_button.click(
|
| 92 |
fn=call_blaxel_backend,
|
| 93 |
-
inputs=
|
| 94 |
-
outputs=
|
| 95 |
)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py (Smoke Test - Now with Secrets)
|
| 2 |
import gradio as gr
|
| 3 |
import httpx
|
| 4 |
+
import os # <-- 1. Import the os module
|
| 5 |
import json
|
| 6 |
|
| 7 |
+
# --- Config ---
|
| 8 |
+
# !!! 2. Read the URL from Hugging Face secrets (environment variables)
|
| 9 |
+
BLAXEL_BACKEND_URL = os.getenv("BLAXEL_BACKEND_URL")
|
| 10 |
|
| 11 |
+
# --- Backend Client ---
|
| 12 |
+
def call_blaxel_backend(user_problem):
|
| 13 |
+
|
| 14 |
+
# 3. Add a check to see if the secret was loaded
|
| 15 |
if not BLAXEL_BACKEND_URL:
|
| 16 |
+
yield "Configuration Error: BLAXEL_BACKEND_URL secret is not set in Hugging Face."
|
| 17 |
+
yield "Please add your Blaxel URL to the 'Secrets' section of your Space settings."
|
| 18 |
+
return # Stop execution if the URL isn't found
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# 1. Show a "thinking" message
|
| 21 |
+
yield f"Attempting to connect to MudabbirAI (at {BLAXEL_BACKEND_URL})..."
|
| 22 |
+
|
| 23 |
+
# 2. Make a streaming API call
|
| 24 |
try:
|
| 25 |
+
with httpx.stream("POST", BLAXEL_BACKEND_URL, json={"problem": user_problem}, timeout=60) as response:
|
| 26 |
+
response.raise_for_status() # Raise an exception for bad statuses (4xx, 5xx)
|
| 27 |
+
for chunk in response.iter_text():
|
| 28 |
+
# Each chunk is a status update
|
| 29 |
+
yield chunk
|
| 30 |
+
except httpx.ConnectError as e:
|
| 31 |
+
yield f"Connection Error: Could not connect to Blaxel. Is the URL correct and the backend running?\nDetails: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except httpx.HTTPStatusError as e:
|
| 33 |
+
yield f"HTTP Error: Received status code {e.response.status_code} from Blaxel.\nDetails: {e.response.text}"
|
|
|
|
|
|
|
| 34 |
except httpx.ReadTimeout:
|
| 35 |
+
yield "Error: The connection timed out."
|
| 36 |
except Exception as e:
|
| 37 |
+
yield f"An unknown error occurred: {str(e)}"
|
| 38 |
+
|
| 39 |
+
# --- Gradio UI ---
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("# MudabbirAI - Connection Test")
|
| 42 |
+
problem_input = gr.Textbox(label="Enter any text to test")
|
| 43 |
+
submit_button = gr.Button("Test Connection")
|
| 44 |
+
status_output = gr.Textbox(label="Live Log from Blaxel", interactive=False)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
submit_button.click(
|
| 47 |
fn=call_blaxel_backend,
|
| 48 |
+
inputs=problem_input,
|
| 49 |
+
outputs=status_output
|
| 50 |
)
|
| 51 |
|
| 52 |
+
demo.launch()
|
|
|