File size: 5,393 Bytes
a0b6231
e74609a
 
621674a
b7595da
a0b6231
3f3a77b
a0b6231
e74609a
1d2aadc
621674a
fcb40b8
e74609a
1d2aadc
0eedaf2
a0b6231
 
 
 
 
 
b7595da
 
a0b6231
b7595da
a0b6231
fcb40b8
 
621674a
 
fcb40b8
 
 
 
b7595da
a0b6231
b7595da
 
 
 
5a58fd1
 
7cf4b10
0eedaf2
b7595da
 
fcb40b8
a0b6231
1d2aadc
e74609a
a0b6231
621674a
a0b6231
fcb40b8
621674a
b7595da
 
7833f1c
6f5d11e
3f3a77b
 
a0b6231
7833f1c
6f5d11e
 
7833f1c
 
 
 
 
a0b6231
7833f1c
 
 
b7595da
 
20f4e43
b7595da
a0b6231
b7595da
20f4e43
a0b6231
 
 
b7595da
621674a
e74609a
a0b6231
1d2aadc
a0b6231
 
 
 
 
b7595da
a0b6231
 
 
604b73b
a0b6231
 
 
 
65177e3
a0b6231
 
65177e3
 
a0b6231
65177e3
 
a0b6231
 
 
65177e3
b7595da
a0b6231
ea5d6dc
a0b6231
 
 
 
 
 
 
 
 
 
e74609a
 
0eedaf2
531ede7
e74609a
 
a0b6231
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# app.py (Neutral Version: No Theme to prevent version conflicts)
import gradio as gr
import httpx
import os
import json
import subprocess
from visuals import create_progress_chart, create_calibration_table, create_cost_summary
import config 

# --- 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, google_key, anthropic_key, sambanova_key, openai_key, nebius_key):
    # 1. DEBUG: Check Server Config
    if not BLAXEL_BASE_URL:
        yield {status_output: "❌ ERROR: 'BLAXEL_BACKEND_URL' is missing from Secrets."}
        return
    if not BLAXEL_API_KEY:
        yield {status_output: "❌ ERROR: 'BLAXEL_API_KEY' is missing from Secrets."}
        return
    
    # 2. DEBUG: Check User Input
    if not google_key:
        yield {status_output: "❌ ERROR: You must enter a Google API Key in the text box."}
        return

    full_endpoint_url = f"{BLAXEL_BASE_URL.rstrip('/')}/solve_problem"
    
    headers = {
        "Authorization": f"Bearer {BLAXEL_API_KEY}",
        "Content-Type": "application/json"
    }
    
    # 3. Construct Payload
    payload = {
        "problem": user_problem,
        "keys": {
            "google": google_key,
            "anthropic": anthropic_key or None,
            "sambanova": sambanova_key or None,
            "openai": openai_key or None,
            "nebius": nebius_key or None,
        }
    }

    yield {status_output: f"Connecting to Agent at: {full_endpoint_url}..."}
    
    try:
        with httpx.stream("POST", full_endpoint_url, json=payload, headers=headers, timeout=120.0) as response:
            if response.status_code != 200:
                yield {status_output: f"❌ HTTP Error {response.status_code}: {response.text}"}
                return 

            final_json = None
            full_log = ""

            for line in response.iter_lines():
                if not line: continue 
                if hasattr(line, "decode"): line = line.decode("utf-8")
                if line.startswith(":"): continue 

                if line.startswith("data: "):
                    content = line.replace("data: ", "", 1).strip()
                    if content.startswith("FINAL:"):
                        try:
                            json_str = content.replace("FINAL:", "", 1)
                            final_json = json.loads(json_str)
                        except json.JSONDecodeError as e:
                            full_log += f"\n[JSON Error]: {e}"
                    else:
                        full_log += content + "\n"
                        yield {status_output: full_log}

            if final_json:
                log_data = final_json.get("log")
                yield {
                    status_output: full_log + "\nβœ… Done!",
                    final_text_output: final_json.get("text"),
                    final_json_log: log_data,
                    progress_plot: create_progress_chart(log_data),
                    calibration_data: create_calibration_table(log_data),
                    cost_display: create_cost_summary(log_data)
                }

    except Exception as e:
        yield {status_output: f"❌ Connection Failed: {str(e)}"}

# --- UI Layout ---
def get_model_info():
    # Use safe access to config in case keys are missing
    judge = config.MODELS.get('Gemini', {}).get('judge', 'Gemini 2.0 Flash')
    return f"### πŸ€– System Config\nJudge: {judge}"

# REMOVED THEME ARGUMENT BELOW TO FIX CRASH
with gr.Blocks() as demo:
    gr.Markdown("# 🧠 MudabbirAI")
    
    with gr.Row():
        with gr.Column(scale=2):
            problem_input = gr.Textbox(label="Business Problem", lines=4)
            with gr.Accordion("API Keys", open=True):
                google_key_input = gr.Textbox(label="Google API Key (Gemini 2.0 Flash) Required", type="password")
                # New providers
                with gr.Row():
                    anthropic_key_input = gr.Textbox(label="Anthropic Key (Claude 3.5 Haiku 202421022)", type="password")
                    sambanova_key_input = gr.Textbox(label="SambaNova Key (Llama 4 Maverick 17B 128E Instruct)", type="password")
                with gr.Row():
                    openai_key_input = gr.Textbox(label="OpenAI Key (GPT-4o-mini)", type="password")
                    nebius_key_input = gr.Textbox(label="Nebius Key (Qwen 3 235B A22B Thinking 2507)", type="password")
        
        with gr.Column(scale=1):
            gr.Markdown(get_model_info())
            submit_button = gr.Button("🧠 Launch System", variant="primary")

    # Outputs
    with gr.Tabs():
        with gr.TabItem("Dashboard"):
            progress_plot = gr.Plot()
            calibration_data = gr.Dataframe()
            cost_display = gr.Markdown()
        with gr.TabItem("Result"):
            final_text_output = gr.Markdown()
        with gr.TabItem("Logs"):
            status_output = gr.Textbox(lines=10, autoscroll=True)
            final_json_log = gr.JSON()

    submit_button.click(
        fn=call_blaxel_backend,
        inputs=[problem_input, google_key_input, anthropic_key_input, sambanova_key_input, openai_key_input, nebius_key_input],
        outputs=[status_output, final_text_output, final_json_log, progress_plot, calibration_data, cost_display]
    )

demo.launch()