Spaces:
Running
Running
import gradio as gr | |
import os | |
from dotenv import load_dotenv | |
# Import necessary functions/variables from run.py | |
from run import create_agent | |
load_dotenv() | |
CONFIG_FILE = ".user_config.env" | |
def save_env_vars_to_file(env_vars): | |
"""Saves environment variables to a file.""" | |
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") | |
def launch_interface(): | |
"""Launches the Gradio interface for configuring and running the agent.""" | |
def setup_agent(question, model_id, hf_token, serpapi_key, use_custom_endpoint, | |
custom_api_endpoint, custom_api_key, search_provider, search_api_key, custom_search_url): | |
"""Sets up the agent with the given configuration and runs it with the provided question.""" | |
print("[DEBUG] Setting up agent with input question:", question) | |
if question.strip() == "": | |
return "Please enter a question." | |
endpoint = custom_api_endpoint if use_custom_endpoint else None | |
api_key = custom_api_key if use_custom_endpoint else None | |
save_env_vars_to_file({ | |
"HF_TOKEN": hf_token, | |
"SERPAPI_API_KEY": serpapi_key, | |
}) | |
print("[DEBUG] Instantiating agent with UI configuration") | |
agent = create_agent( | |
model_id=model_id, | |
hf_token=hf_token, | |
serpapi_key=serpapi_key, | |
custom_api_endpoint=endpoint, | |
custom_api_key=api_key, | |
search_provider=search_provider, | |
search_api_key=search_api_key, | |
custom_search_url=custom_search_url | |
) | |
return agent.run(question) | |
with gr.Blocks(theme=gr.themes.Base(), css=".gr-box { border-radius: 15px; padding: 20px; }") as demo: | |
gr.Markdown("# 🤖 SmolAgent Configurable Interface") | |
with gr.Row(): | |
with gr.Column(scale=2): | |
question = gr.Textbox(label="🧠 Question", placeholder="Ask me anything...") | |
model_id = gr.Textbox(value="gpt-4o-mini", label="🧬 Model ID") | |
hf_token = gr.Textbox(value=os.getenv("HF_TOKEN", ""), label="🔑 HuggingFace Token", type="password") | |
serpapi_key = gr.Textbox(value=os.getenv("SERPAPI_API_KEY", ""), label="🔍 Serper API Key", | |
type="password", visible=True) | |
use_custom_endpoint = gr.Checkbox(label="🌐 Use Custom API Endpoint") | |
custom_api_endpoint = gr.Textbox(label="🔌 Custom API Endpoint URL", | |
placeholder="https://your-api-endpoint.com", visible=False) | |
custom_api_key = gr.Textbox(label="🔐 Custom API Endpoint Key", type="password", visible=False) | |
search_provider = gr.Dropdown(label="🔎 Search Provider", choices=["serper", "searxng"], value="serper") | |
search_api_key = gr.Textbox(label="🔑 Search Provider API Key (optional)", type="password", | |
visible=False) | |
custom_search_url = gr.Textbox(label="🌐 Custom SearxNG Instance URL", | |
placeholder="https://your-searxng-instance/search", visible=False) | |
submit_btn = gr.Button("🚀 Run Agent") | |
with gr.Column(scale=1): | |
output = gr.Textbox(label="📤 Answer", lines=15) | |
def update_search_visibility(provider): | |
"""Updates the visibility of search-related textboxes based on the selected search provider.""" | |
return { | |
serpapi_key: gr.update(visible=(provider == "serper")), | |
custom_search_url: gr.update(visible=(provider == "searxng")), | |
search_api_key: gr.update(visible=(provider == "searxng")), | |
} | |
def update_custom_endpoint_visibility(checked): | |
"""Updates the visibility of custom API endpoint textboxes based on the checkbox state.""" | |
return { | |
custom_api_endpoint: gr.update(visible=checked), | |
custom_api_key: gr.update(visible=checked), | |
} | |
search_provider.change(fn=update_search_visibility, inputs=search_provider, | |
outputs=[serpapi_key, custom_search_url, search_api_key]) | |
use_custom_endpoint.change(fn=update_custom_endpoint_visibility, inputs=use_custom_endpoint, | |
outputs=[custom_api_endpoint, custom_api_key]) | |
submit_btn.click(fn=setup_agent, | |
inputs=[question, model_id, hf_token, serpapi_key, | |
use_custom_endpoint, custom_api_endpoint, custom_api_key, | |
search_provider, search_api_key, custom_search_url], | |
outputs=output) | |
print("[DEBUG] Launching Gradio interface") | |
demo.launch() | |
if __name__ == "__main__": | |
launch_interface() |