CultriX commited on
Commit
3b8d6af
·
verified ·
1 Parent(s): b4d7541

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -140
app.py CHANGED
@@ -1,140 +1,153 @@
1
- from run import create_agent, run_agent_with_streaming import gradio as gr import os import threading import time from dotenv import load_dotenv
2
-
3
- load_dotenv() CONFIG_FILE = ".user_config.env"
4
-
5
- def save_env_vars_to_file(env_vars): print("[DEBUG] Saving user config to file") with open(CONFIG_FILE, "w") as f: for key, value in env_vars.items(): f.write(f"{key}={value}\n")
6
-
7
- def launch_interface(): def setup_agent_streaming(question, model_id, hf_token, openai_api_key, serpapi_key, api_endpoint, use_custom_endpoint, custom_api_endpoint, custom_api_key, search_provider, search_api_key, custom_search_url): print("[DEBUG] Setting up agent with input question:", question)
8
-
9
- if question.strip() == "":
10
- yield "Please enter a question.", ""
11
- return
12
-
13
- endpoint = custom_api_endpoint if use_custom_endpoint else api_endpoint
14
- api_key = custom_api_key if use_custom_endpoint else openai_api_key
15
-
16
- save_env_vars_to_file({
17
- "HF_TOKEN": hf_token,
18
- "SERPAPI_API_KEY": serpapi_key,
19
- "API_ENDPOINT": api_endpoint,
20
- "OPENAI_API_KEY": openai_api_key
21
- })
22
-
23
- print("[DEBUG] Instantiating agent with UI configuration")
24
- agent = create_agent(
25
- model_id=model_id,
26
- hf_token=hf_token,
27
- serpapi_key=serpapi_key,
28
- openai_api_key=openai_api_key,
29
- api_endpoint=api_endpoint,
30
- custom_api_endpoint=endpoint,
31
- custom_api_key=api_key,
32
- search_provider=search_provider,
33
- search_api_key=search_api_key,
34
- custom_search_url=custom_search_url
35
- )
36
-
37
- output_buffer = []
38
- final_answer = ""
39
- is_complete = False
40
-
41
- def highlight_text(text):
42
- if "[COMPLETED] Final answer:" in text:
43
- return f"<span style='color:#10b981;font-weight:bold;'>[FINAL]</span> <mark>{text.split(':', 1)[1].strip()}</mark>"
44
- elif "[ERROR]" in text:
45
- return f"<span style='color:#ef4444;font-weight:bold;'>[ERROR]</span> <pre>{text.strip()}</pre>"
46
- elif "[STARTING]" in text:
47
- return f"<span style='color:#f59e0b;font-weight:bold;'>[STEP]</span> {text.strip()}"
48
- elif text.strip():
49
- return f"<details><summary><span style='color:#f59e0b;'>Step</span></summary>\n<pre>{text.strip()}</pre>\n</details>"
50
- return ""
51
-
52
- def stream_callback(text):
53
- nonlocal final_answer
54
- if "[COMPLETED] Final answer:" in text:
55
- final_answer = text.split("[COMPLETED] Final answer:", 1)[1].strip()
56
- formatted = highlight_text(text)
57
- if formatted:
58
- output_buffer.append(formatted)
59
-
60
- def run_agent_async():
61
- nonlocal is_complete
62
- try:
63
- _ = run_agent_with_streaming(agent, question, stream_callback)
64
- except Exception as e:
65
- output_buffer.append(highlight_text(f"[ERROR] {str(e)}"))
66
- finally:
67
- is_complete = True
68
-
69
- agent_thread = threading.Thread(target=run_agent_async)
70
- agent_thread.start()
71
-
72
- last_length = 0
73
- while not is_complete or agent_thread.is_alive():
74
- current_output = "\n".join(output_buffer)
75
- if len(current_output) > last_length:
76
- yield current_output, ""
77
- last_length = len(current_output)
78
- time.sleep(0.1)
79
-
80
- final_output = "\n".join(output_buffer)
81
- yield final_output, final_answer
82
-
83
- with gr.Blocks(title="SmolAgent - Streaming AI") as demo:
84
- gr.Markdown("# SmolAgent - Intelligent AI with Web Tools")
85
-
86
- with gr.Row():
87
- with gr.Column():
88
- question = gr.Textbox(label="Your Question", lines=3)
89
- model_id = gr.Textbox(label="Model ID", value="gpt-4.1-nano")
90
- hf_token = gr.Textbox(label="HF Token", type="password", value=os.getenv("HF_TOKEN", ""))
91
- openai_api_key = gr.Textbox(label="OpenAI API Key", type="password", value=os.getenv("OPENAI_API_KEY", ""))
92
- api_endpoint = gr.Textbox(label="API Endpoint", value=os.getenv("API_ENDPOINT", "https://api.openai.com/v1"))
93
- use_custom_endpoint = gr.Checkbox(label="Use Custom API Endpoint")
94
- custom_api_endpoint = gr.Textbox(label="Custom API URL", visible=False)
95
- custom_api_key = gr.Textbox(label="Custom API Key", type="password", visible=False)
96
- serpapi_key = gr.Textbox(label="SerpAPI Key", type="password", value=os.getenv("SERPAPI_API_KEY", ""))
97
- search_provider = gr.Dropdown(choices=["serper", "searxng"], value="searxng", label="Search Provider")
98
- search_api_key = gr.Textbox(label="Search Provider API Key", type="password", visible=True)
99
- custom_search_url = gr.Textbox(label="Custom SearxNG URL", value="https://search.endorisk.nl/search", visible=True)
100
- submit_btn = gr.Button("Submit")
101
-
102
- with gr.Column():
103
- output = gr.Markdown(label="Live Agent Output")
104
- final = gr.Textbox(label="Final Answer", interactive=False)
105
- copy_btn = gr.Button("Copy Final Answer")
106
-
107
- def update_visibility(provider):
108
- return {
109
- custom_search_url: gr.update(visible=(provider == "searxng")),
110
- search_api_key: gr.update(visible=(provider == "searxng"))
111
- }
112
-
113
- def update_custom_fields(checked):
114
- return {
115
- custom_api_endpoint: gr.update(visible=checked),
116
- custom_api_key: gr.update(visible=checked)
117
- }
118
-
119
- search_provider.change(fn=update_visibility, inputs=search_provider, outputs=[custom_search_url, search_api_key])
120
- use_custom_endpoint.change(fn=update_custom_fields, inputs=use_custom_endpoint, outputs=[custom_api_endpoint, custom_api_key])
121
-
122
- submit_btn.click(
123
- fn=setup_agent_streaming,
124
- inputs=[question, model_id, hf_token, openai_api_key, serpapi_key, api_endpoint, use_custom_endpoint, custom_api_endpoint, custom_api_key, search_provider, search_api_key, custom_search_url],
125
- outputs=[output, final],
126
- show_progress=True
127
- )
128
-
129
- copy_btn.click(
130
- fn=lambda txt: gr.Textbox.update(value=txt),
131
- inputs=final,
132
- outputs=final,
133
- show_progress=False
134
- )
135
-
136
- print("[DEBUG] Launching updated Gradio interface")
137
- demo.launch()
138
-
139
- if name == "main": launch_interface()
140
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from run import create_agent, run_agent_with_streaming
2
+ import gradio as gr
3
+ import os
4
+ import threading
5
+ import time
6
+ from dotenv import load_dotenv
7
+
8
+ load_dotenv()
9
+ CONFIG_FILE = ".user_config.env"
10
+
11
+ def save_env_vars_to_file(env_vars):
12
+ print("[DEBUG] Saving user config to file")
13
+ with open(CONFIG_FILE, "w") as f:
14
+ for key, value in env_vars.items():
15
+ f.write(f"{key}={value}\n")
16
+
17
+ def launch_interface():
18
+ def setup_agent_streaming(question, model_id, hf_token, openai_api_key, serpapi_key, api_endpoint, use_custom_endpoint,
19
+ custom_api_endpoint, custom_api_key, search_provider, search_api_key, custom_search_url):
20
+ print("[DEBUG] Setting up agent with input question:", question)
21
+
22
+ if question.strip() == "":
23
+ yield "Please enter a question.", ""
24
+ return
25
+
26
+ endpoint = custom_api_endpoint if use_custom_endpoint else api_endpoint
27
+ api_key = custom_api_key if use_custom_endpoint else openai_api_key
28
+
29
+ save_env_vars_to_file({
30
+ "HF_TOKEN": hf_token,
31
+ "SERPAPI_API_KEY": serpapi_key,
32
+ "API_ENDPOINT": api_endpoint,
33
+ "OPENAI_API_KEY": openai_api_key
34
+ })
35
+
36
+ print("[DEBUG] Instantiating agent with UI configuration")
37
+ agent = create_agent(
38
+ model_id=model_id,
39
+ hf_token=hf_token,
40
+ serpapi_key=serpapi_key,
41
+ openai_api_key=openai_api_key,
42
+ api_endpoint=api_endpoint,
43
+ custom_api_endpoint=endpoint,
44
+ custom_api_key=api_key,
45
+ search_provider=search_provider,
46
+ search_api_key=search_api_key,
47
+ custom_search_url=custom_search_url
48
+ )
49
+
50
+ output_buffer = []
51
+ final_answer = ""
52
+ is_complete = False
53
+
54
+ def highlight_text(text):
55
+ if "[COMPLETED] Final answer:" in text:
56
+ return f"<span style='color:#10b981;font-weight:bold;'>[FINAL]</span> <mark>{text.split(':', 1)[1].strip()}</mark>"
57
+ elif "[ERROR]" in text:
58
+ return f"<span style='color:#ef4444;font-weight:bold;'>[ERROR]</span> <pre>{text.strip()}</pre>"
59
+ elif "[STARTING]" in text:
60
+ return f"<span style='color:#f59e0b;font-weight:bold;'>[STEP]</span> {text.strip()}"
61
+ elif text.strip():
62
+ return f"<details><summary><span style='color:#f59e0b;'>Step</span></summary>\n<pre>{text.strip()}</pre>\n</details>"
63
+ return ""
64
+
65
+ def stream_callback(text):
66
+ nonlocal final_answer
67
+ if "[COMPLETED] Final answer:" in text:
68
+ final_answer = text.split("[COMPLETED] Final answer:", 1)[1].strip()
69
+ formatted = highlight_text(text)
70
+ if formatted:
71
+ output_buffer.append(formatted)
72
+
73
+ def run_agent_async():
74
+ nonlocal is_complete
75
+ try:
76
+ _ = run_agent_with_streaming(agent, question, stream_callback)
77
+ except Exception as e:
78
+ output_buffer.append(highlight_text(f"[ERROR] {str(e)}"))
79
+ finally:
80
+ is_complete = True
81
+
82
+ agent_thread = threading.Thread(target=run_agent_async)
83
+ agent_thread.start()
84
+
85
+ last_length = 0
86
+ while not is_complete or agent_thread.is_alive():
87
+ current_output = "\n".join(output_buffer)
88
+ if len(current_output) > last_length:
89
+ yield current_output, ""
90
+ last_length = len(current_output)
91
+ time.sleep(0.1)
92
+
93
+ final_output = "\n".join(output_buffer)
94
+ yield final_output, final_answer
95
+
96
+ with gr.Blocks(title="SmolAgent - Streaming AI") as demo:
97
+ gr.Markdown("# SmolAgent - Intelligent AI with Web Tools")
98
+
99
+ with gr.Row():
100
+ with gr.Column():
101
+ question = gr.Textbox(label="Your Question", lines=3)
102
+ model_id = gr.Textbox(label="Model ID", value="gpt-4.1-nano")
103
+ hf_token = gr.Textbox(label="HF Token", type="password", value=os.getenv("HF_TOKEN", ""))
104
+ openai_api_key = gr.Textbox(label="OpenAI API Key", type="password", value=os.getenv("OPENAI_API_KEY", ""))
105
+ api_endpoint = gr.Textbox(label="API Endpoint", value=os.getenv("API_ENDPOINT", "https://api.openai.com/v1"))
106
+ use_custom_endpoint = gr.Checkbox(label="Use Custom API Endpoint")
107
+ custom_api_endpoint = gr.Textbox(label="Custom API URL", visible=False)
108
+ custom_api_key = gr.Textbox(label="Custom API Key", type="password", visible=False)
109
+ serpapi_key = gr.Textbox(label="SerpAPI Key", type="password", value=os.getenv("SERPAPI_API_KEY", ""))
110
+ search_provider = gr.Dropdown(choices=["serper", "searxng"], value="searxng", label="Search Provider")
111
+ search_api_key = gr.Textbox(label="Search Provider API Key", type="password", visible=True)
112
+ custom_search_url = gr.Textbox(label="Custom SearxNG URL", value="https://search.endorisk.nl/search", visible=True)
113
+ submit_btn = gr.Button("Submit")
114
+
115
+ with gr.Column():
116
+ output = gr.Markdown(label="Live Agent Output")
117
+ final = gr.Textbox(label="Final Answer", interactive=False)
118
+ copy_btn = gr.Button("Copy Final Answer")
119
+
120
+ def update_visibility(provider):
121
+ return {
122
+ custom_search_url: gr.update(visible=(provider == "searxng")),
123
+ search_api_key: gr.update(visible=(provider == "searxng"))
124
+ }
125
+
126
+ def update_custom_fields(checked):
127
+ return {
128
+ custom_api_endpoint: gr.update(visible=checked),
129
+ custom_api_key: gr.update(visible=checked)
130
+ }
131
+
132
+ search_provider.change(fn=update_visibility, inputs=search_provider, outputs=[custom_search_url, search_api_key])
133
+ use_custom_endpoint.change(fn=update_custom_fields, inputs=use_custom_endpoint, outputs=[custom_api_endpoint, custom_api_key])
134
+
135
+ submit_btn.click(
136
+ fn=setup_agent_streaming,
137
+ inputs=[question, model_id, hf_token, openai_api_key, serpapi_key, api_endpoint, use_custom_endpoint, custom_api_endpoint, custom_api_key, search_provider, search_api_key, custom_search_url],
138
+ outputs=[output, final],
139
+ show_progress=True
140
+ )
141
+
142
+ copy_btn.click(
143
+ fn=lambda txt: gr.Textbox.update(value=txt),
144
+ inputs=final,
145
+ outputs=final,
146
+ show_progress=False
147
+ )
148
+
149
+ print("[DEBUG] Launching updated Gradio interface")
150
+ demo.launch()
151
+
152
+ if __name__ == "__main__":
153
+ launch_interface()