Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,29 @@
|
|
1 |
-
from run import create_agent
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
from dotenv import load_dotenv
|
5 |
|
|
|
|
|
|
|
6 |
load_dotenv()
|
|
|
7 |
CONFIG_FILE = ".user_config.env"
|
8 |
|
|
|
9 |
def save_env_vars_to_file(env_vars):
|
|
|
10 |
print("[DEBUG] Saving user config to file")
|
11 |
with open(CONFIG_FILE, "w") as f:
|
12 |
for key, value in env_vars.items():
|
13 |
f.write(f"{key}={value}\n")
|
14 |
|
|
|
15 |
def launch_interface():
|
|
|
|
|
16 |
def setup_agent(question, model_id, hf_token, serpapi_key, use_custom_endpoint,
|
17 |
custom_api_endpoint, custom_api_key, search_provider, search_api_key, custom_search_url):
|
|
|
18 |
print("[DEBUG] Setting up agent with input question:", question)
|
19 |
|
20 |
if question.strip() == "":
|
@@ -43,40 +52,49 @@ def launch_interface():
|
|
43 |
return agent.run(question)
|
44 |
|
45 |
with gr.Blocks(theme=gr.themes.Base(), css=".gr-box { border-radius: 15px; padding: 20px; }") as demo:
|
46 |
-
gr.Markdown("#
|
47 |
|
48 |
with gr.Row():
|
49 |
with gr.Column(scale=2):
|
50 |
-
question = gr.Textbox(label="
|
51 |
-
model_id = gr.Textbox(value="gpt-4o-mini", label="
|
52 |
-
hf_token = gr.Textbox(value=os.getenv("HF_TOKEN", ""), label="
|
53 |
-
serpapi_key = gr.Textbox(value=os.getenv("SERPAPI_API_KEY", ""), label="
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
61 |
with gr.Column(scale=1):
|
62 |
-
output = gr.Textbox(label="
|
|
|
63 |
|
64 |
def update_search_visibility(provider):
|
|
|
65 |
return {
|
66 |
serpapi_key: gr.update(visible=(provider == "serper")),
|
67 |
custom_search_url: gr.update(visible=(provider == "searxng")),
|
68 |
search_api_key: gr.update(visible=(provider == "searxng")),
|
69 |
}
|
70 |
|
|
|
71 |
def update_custom_endpoint_visibility(checked):
|
|
|
72 |
return {
|
73 |
custom_api_endpoint: gr.update(visible=checked),
|
74 |
custom_api_key: gr.update(visible=checked),
|
75 |
}
|
76 |
|
|
|
77 |
search_provider.change(fn=update_search_visibility, inputs=search_provider,
|
78 |
outputs=[serpapi_key, custom_search_url, search_api_key])
|
79 |
-
|
80 |
use_custom_endpoint.change(fn=update_custom_endpoint_visibility, inputs=use_custom_endpoint,
|
81 |
outputs=[custom_api_endpoint, custom_api_key])
|
82 |
|
@@ -89,6 +107,6 @@ def launch_interface():
|
|
89 |
print("[DEBUG] Launching Gradio interface")
|
90 |
demo.launch()
|
91 |
|
92 |
-
if __name__ == "__main__":
|
93 |
-
launch_interface()
|
94 |
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
from dotenv import load_dotenv
|
4 |
|
5 |
+
# Import necessary functions/variables from run.py
|
6 |
+
from run import create_agent
|
7 |
+
|
8 |
load_dotenv()
|
9 |
+
|
10 |
CONFIG_FILE = ".user_config.env"
|
11 |
|
12 |
+
|
13 |
def save_env_vars_to_file(env_vars):
|
14 |
+
"""Saves environment variables to a file."""
|
15 |
print("[DEBUG] Saving user config to file")
|
16 |
with open(CONFIG_FILE, "w") as f:
|
17 |
for key, value in env_vars.items():
|
18 |
f.write(f"{key}={value}\n")
|
19 |
|
20 |
+
|
21 |
def launch_interface():
|
22 |
+
"""Launches the Gradio interface for configuring and running the agent."""
|
23 |
+
|
24 |
def setup_agent(question, model_id, hf_token, serpapi_key, use_custom_endpoint,
|
25 |
custom_api_endpoint, custom_api_key, search_provider, search_api_key, custom_search_url):
|
26 |
+
"""Sets up the agent with the given configuration and runs it with the provided question."""
|
27 |
print("[DEBUG] Setting up agent with input question:", question)
|
28 |
|
29 |
if question.strip() == "":
|
|
|
52 |
return agent.run(question)
|
53 |
|
54 |
with gr.Blocks(theme=gr.themes.Base(), css=".gr-box { border-radius: 15px; padding: 20px; }") as demo:
|
55 |
+
gr.Markdown("# 🤖 SmolAgent Configurable Interface")
|
56 |
|
57 |
with gr.Row():
|
58 |
with gr.Column(scale=2):
|
59 |
+
question = gr.Textbox(label="🧠 Question", placeholder="Ask me anything...")
|
60 |
+
model_id = gr.Textbox(value="gpt-4o-mini", label="🧬 Model ID")
|
61 |
+
hf_token = gr.Textbox(value=os.getenv("HF_TOKEN", ""), label="🔑 HuggingFace Token", type="password")
|
62 |
+
serpapi_key = gr.Textbox(value=os.getenv("SERPAPI_API_KEY", ""), label="🔍 Serper API Key",
|
63 |
+
type="password", visible=True)
|
64 |
+
use_custom_endpoint = gr.Checkbox(label="🌐 Use Custom API Endpoint")
|
65 |
+
custom_api_endpoint = gr.Textbox(label="🔌 Custom API Endpoint URL",
|
66 |
+
placeholder="https://your-api-endpoint.com", visible=False)
|
67 |
+
custom_api_key = gr.Textbox(label="🔐 Custom API Endpoint Key", type="password", visible=False)
|
68 |
+
search_provider = gr.Dropdown(label="🔎 Search Provider", choices=["serper", "searxng"], value="serper")
|
69 |
+
search_api_key = gr.Textbox(label="🔑 Search Provider API Key (optional)", type="password",
|
70 |
+
visible=False)
|
71 |
+
custom_search_url = gr.Textbox(label="🌐 Custom SearxNG Instance URL",
|
72 |
+
placeholder="https://your-searxng-instance/search", visible=False)
|
73 |
+
submit_btn = gr.Button("🚀 Run Agent")
|
74 |
+
|
75 |
with gr.Column(scale=1):
|
76 |
+
output = gr.Textbox(label="📤 Answer", lines=15)
|
77 |
+
|
78 |
|
79 |
def update_search_visibility(provider):
|
80 |
+
"""Updates the visibility of search-related textboxes based on the selected search provider."""
|
81 |
return {
|
82 |
serpapi_key: gr.update(visible=(provider == "serper")),
|
83 |
custom_search_url: gr.update(visible=(provider == "searxng")),
|
84 |
search_api_key: gr.update(visible=(provider == "searxng")),
|
85 |
}
|
86 |
|
87 |
+
|
88 |
def update_custom_endpoint_visibility(checked):
|
89 |
+
"""Updates the visibility of custom API endpoint textboxes based on the checkbox state."""
|
90 |
return {
|
91 |
custom_api_endpoint: gr.update(visible=checked),
|
92 |
custom_api_key: gr.update(visible=checked),
|
93 |
}
|
94 |
|
95 |
+
|
96 |
search_provider.change(fn=update_search_visibility, inputs=search_provider,
|
97 |
outputs=[serpapi_key, custom_search_url, search_api_key])
|
|
|
98 |
use_custom_endpoint.change(fn=update_custom_endpoint_visibility, inputs=use_custom_endpoint,
|
99 |
outputs=[custom_api_endpoint, custom_api_key])
|
100 |
|
|
|
107 |
print("[DEBUG] Launching Gradio interface")
|
108 |
demo.launch()
|
109 |
|
|
|
|
|
110 |
|
111 |
+
if __name__ == "__main__":
|
112 |
+
launch_interface()
|