Spaces:
Running
Running
File size: 5,197 Bytes
f060f46 038f212 f060f46 9d531d5 f060f46 9d531d5 f060f46 9d531d5 18cac97 f060f46 9d531d5 e216d73 f060f46 25bef75 f060f46 eaac079 f060f46 9d531d5 eaac079 f060f46 eaac079 f060f46 eaac079 f060f46 eaac079 f060f46 eaac079 f060f46 9d531d5 f060f46 b7cb9b4 330f630 f060f46 |
1 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 |
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, 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)
if question.strip() == "":
return "Please enter a question."
endpoint = custom_api_endpoint if use_custom_endpoint else api_endpoint
api_key = custom_api_key if use_custom_endpoint else openai_api_key
save_env_vars_to_file({
"HF_TOKEN": hf_token,
"SERPAPI_API_KEY": serpapi_key,
"API_ENDPOINT": api_endpoint,
"OPENAI_API_KEY": openai_api_key
})
print("[DEBUG] Instantiating agent with UI configuration")
agent = create_agent(
model_id=model_id,
hf_token=hf_token,
serpapi_key=serpapi_key,
openai_api_key=openai_api_key,
api_endpoint=api_endpoint,
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...", visible=True)
model_id = gr.Textbox(value="gpt-4o-mini", label="𧬠Model ID", visible=True)
hf_token = gr.Textbox(value=os.getenv("HF_TOKEN", ""), label="π HuggingFace Token", type="password", visible=True)
openai_api_key = gr.Textbox(value=os.getenv("OPENAI_API_KEY", ""), label="π OpenAI API Key", type="password", visible=True)
serpapi_key = gr.Textbox(value=os.getenv("SERPAPI_API_KEY", ""), label="π Serper API Key", type="password", visible=True)
api_endpoint = gr.Textbox(value=os.getenv("API_ENDPOINT", "https://api.openai.com"), label="Default API Endpoint", placeholder="https://api.openai.com", visible=True)
custom_api_endpoint = gr.Textbox(label="π Custom API Endpoint URL", placeholder="https://your-api-endpoint.com", visible=False)
use_custom_endpoint = gr.Checkbox(label="π Use Custom API Endpoint", visible=True)
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", visible=True)
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", visible=True)
with gr.Column(scale=1):
output = gr.Textbox(label="π€ Answer", lines=20)
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=[api_endpoint, openai_api_key, custom_api_endpoint, custom_api_key])
submit_btn.click(fn=setup_agent,
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],
outputs=output)
print("[DEBUG] Launching Gradio interface")
demo.launch()
if __name__ == "__main__":
launch_interface()
|