Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,140 +1,153 @@
|
|
1 |
-
from run import create_agent, run_agent_with_streaming
|
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 |
-
outputs=
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
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()
|