youssefleb commited on
Commit
e74609a
·
verified ·
1 Parent(s): 92321d7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import httpx
3
+ import os
4
+ import json
5
+
6
+ # --- Config ---
7
+ # We read the backend URL from the HF Space Secrets
8
+ BLAXEL_BACKEND_URL = os.environ.get("BLAXEL_BACKEND_URL", "")
9
+
10
+ # --- Backend Client ---
11
+ def call_blaxel_backend(user_problem, run_count, do_calibrate):
12
+ """
13
+ Sends the user's problem to our Blaxel backend and streams the response.
14
+ """
15
+
16
+ if not BLAXEL_BACKEND_URL:
17
+ yield "ERROR: 'BLAXEL_BACKEND_URL' is not set in the Space Secrets. Backend is offline."
18
+ return
19
+
20
+ # 1. Show a "thinking" message to the user.
21
+ yield "[1/6] 🧠 Agent activated. Connecting to Blaxel backend..."
22
+
23
+ # 2. Package the request
24
+ request_data = {
25
+ "problem": user_problem,
26
+ "run_count": run_count,
27
+ "do_calibrate": do_calibrate
28
+ # We will add framework selection later
29
+ }
30
+
31
+ # 3. Make a streaming API call to the backend.
32
+ try:
33
+ with httpx.stream("POST", BLAXEL_BACKEND_URL + "/solve_problem", json=request_data, timeout=300) as response:
34
+
35
+ final_json_string = ""
36
+ for chunk in response.iter_text():
37
+ if chunk.startswith("FINAL:"):
38
+ # This is our special prefix for the final data package
39
+ final_json_string = chunk.replace("FINAL:", "")
40
+ else:
41
+ # This is a live log update
42
+ yield chunk
43
+
44
+ # 4. Once streaming is done, parse the final JSON
45
+ if final_json_string:
46
+ final_data = json.loads(final_json_string)
47
+ # This is a "hack" to update all fields from one function
48
+ # We yield a final dictionary that maps components to values
49
+ yield {
50
+ status_output: "✅ Solution Complete. See final brief below.",
51
+ final_text_output: final_data.get("text", "Error: No text received."),
52
+ final_audio_output: final_data.get("audio", None)
53
+ }
54
+ else:
55
+ yield "Error: Backend finished without providing a final solution."
56
+
57
+ # 5. Handle errors.
58
+ except httpx.ConnectError:
59
+ yield f"ERROR: Could not connect to Blaxel backend at: {BLAXEL_BACKEND_URL}. Is it running?"
60
+ except httpx.ReadTimeout:
61
+ yield "Error: The agent backend timed out (300 seconds)."
62
+ except Exception as e:
63
+ yield f"An unexpected error occurred: {str(e)}"
64
+
65
+ # --- Gradio UI (Our Full Plan) ---
66
+ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
67
+ gr.Markdown("# The Self-Calibrating Strategic Selector")
68
+ gr.Markdown("Based on the research by Youssef Hariri (Papers 1 & 2)")
69
+
70
+ with gr.Tabs():
71
+ # --- Main Tab ---
72
+ with gr.TabItem("🚀 Deploy Agent"):
73
+ gr.Markdown("## 1. Enter Your 'Wicked Problem'")
74
+ problem_input = gr.Textbox(
75
+ label="Problem Brief",
76
+ placeholder="e.g., 'Draft a novel, feasible, and culturally sensitive marketing plan for our new app in Japan...'",
77
+ lines=5
78
+ )
79
+
80
+ gr.Markdown("## 2. (Optional) Select Frameworks")
81
+ # We will add gr.Dropdown() here later
82
+
83
+ gr.Markdown("## 3. (Optional) Advanced Settings")
84
+ with gr.Accordion("Advanced Run Options", open=False):
85
+ run_count = gr.Slider(
86
+ label="Number of Runs (Leverages AI stochasticity)",
87
+ minimum=1, maximum=5, step=1, value=1
88
+ )
89
+ do_calibrate = gr.Checkbox(
90
+ label="Run Live Ensemble Calibration (Slower, highest accuracy)",
91
+ value=False
92
+ )
93
+
94
+ gr.Markdown("## 4. Execute")
95
+ submit_button = gr.Button("Deploy Agent", variant="primary")
96
+
97
+ # --- Outputs ---
98
+ with gr.TabItem("📊 Live Log & Final Output"):
99
+ gr.Markdown("## Agent's Internal Monologue (Live Log)")
100
+ status_output = gr.Textbox(label="Agent Status Log", interactive=False, lines=10)
101
+
102
+ gr.Markdown("## Final Briefing")
103
+ final_text_output = gr.Markdown()
104
+
105
+ gr.Markdown("## Audio Presentation")
106
+ final_audio_output = gr.Audio(label="Audio Presentation", type="filepath")
107
+
108
+ # Wire up the UI
109
+ submit_button.click(
110
+ fn=call_blaxel_backend,
111
+ inputs=[problem_input, run_count, do_calibrate],
112
+ outputs=[status_output, final_text_output, final_audio_output]
113
+ )
114
+
115
+ if __name__ == "__main__":
116
+ demo.launch()