Spaces:
Running
Running
| # app.py (Updated with Debug Log Tab) | |
| import gradio as gr | |
| import httpx | |
| import os | |
| import json | |
| # --- Config --- | |
| BLAXEL_BASE_URL = os.getenv("BLAXEL_BACKEND_URL") | |
| BLAXEL_API_KEY = os.getenv("BLAXEL_API_KEY") | |
| # --- Backend Client --- | |
| def call_blaxel_backend( | |
| user_problem: str, | |
| google_key: str, | |
| anthropic_key: str, | |
| sambanova_key: str | |
| ): | |
| if not BLAXEL_BASE_URL or not BLAXEL_API_KEY: | |
| yield {status_output: "Configuration Error: Secrets not set."} | |
| return | |
| if not google_key: | |
| yield {status_output: "Input Error: Google API Key is required."} | |
| return | |
| full_endpoint_url = f"{BLAXEL_BASE_URL.rstrip('/')}/solve_problem" | |
| headers = { | |
| "Authorization": f"Bearer {BLAXEL_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "problem": user_problem, | |
| "keys": { | |
| "google": google_key, | |
| "anthropic": anthropic_key or None, | |
| "sambanova": sambanova_key or None, | |
| } | |
| } | |
| yield {status_output: f"Connecting to MudabbirAI..."} | |
| try: | |
| with httpx.stream("POST", full_endpoint_url, json=payload, headers=headers, timeout=300.0) as response: | |
| if response.status_code != 200: | |
| yield {status_output: f"HTTP Error: {response.status_code}"} | |
| return | |
| final_json = None | |
| full_log = "" | |
| for chunk in response.iter_text(): | |
| if chunk.startswith("FINAL:"): | |
| final_json = json.loads(chunk.replace("FINAL:", "")) | |
| else: | |
| full_log += chunk + "\n" | |
| yield {status_output: full_log} | |
| if final_json: | |
| # --- UPDATE: Extract "log" from response --- | |
| yield { | |
| status_output: full_log + "\nDone!", | |
| final_text_output: final_json.get("text"), | |
| final_audio_output: final_json.get("audio"), | |
| final_json_log: final_json.get("log") # Populate the new JSON component | |
| } | |
| except Exception as e: | |
| yield {status_output: f"Error: {str(e)}"} | |
| # --- Gradio UI --- | |
| with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo: | |
| gr.Markdown("# MudabbirAI: The Self-Calibrating Strategic Selector") | |
| with gr.Group(): | |
| problem_input = gr.Textbox(label="1. Enter Your 'Wicked Problem'", lines=5) | |
| with gr.Accordion("2. Sponsor API Keys", open=False): | |
| google_key_input = gr.Textbox(label="Google API Key", type="password") | |
| with gr.Row(): | |
| anthropic_key_input = gr.Textbox(label="Anthropic API Key", type="password") | |
| sambanova_key_input = gr.Textbox(label="SambaNova API Key", type="password") | |
| submit_button = gr.Button("Deploy Agent", variant="primary") | |
| with gr.Tabs(): | |
| with gr.TabItem("Agent's Internal Monologue", id="log_tab"): | |
| status_output = gr.Textbox(label="Live Log", lines=20, interactive=False, autoscroll=True) | |
| with gr.TabItem("Final Briefing", id="result_tab"): | |
| final_text_output = gr.Markdown(label="Final Polished Briefing") | |
| final_audio_output = gr.Audio(label="Audio Presentation") | |
| # --- NEW TAB FOR DEBUG LOG --- | |
| with gr.TabItem("Debug Data (JSON)", id="debug_tab"): | |
| final_json_log = gr.JSON(label="Full Execution Trace") | |
| submit_button.click( | |
| fn=call_blaxel_backend, | |
| inputs=[problem_input, google_key_input, anthropic_key_input, sambanova_key_input], | |
| outputs=[status_output, final_text_output, final_audio_output, final_json_log] # Added output here | |
| ) | |
| demo.launch() |