Spaces:
Running
Running
from run import create_agent | |
import gradio as gr | |
import os | |
from dotenv import load_dotenv | |
load_dotenv() | |
CONFIG_FILE = ".user_config.env" | |
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") | |
def launch_interface(): | |
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): | |
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): | |
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): | |
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() | |