|
|
import gradio as gr |
|
|
import os |
|
|
import spaces |
|
|
import logging |
|
|
from smolagents import CodeAgent, WebSearchTool, InferenceClientModel |
|
|
from hf_tools import SearchHfSpacesTool, CallHfSpaceApiTool |
|
|
|
|
|
|
|
|
logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
|
|
logging.info("Application starting...") |
|
|
|
|
|
try: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model = InferenceClientModel( |
|
|
model_id="Qwen/Qwen2.5-Coder-32B-Instruct", |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
tools = [WebSearchTool(), SearchHfSpacesTool(), CallHfSpaceApiTool()] |
|
|
|
|
|
|
|
|
agent = CodeAgent( |
|
|
tools=tools, |
|
|
model=model, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
@spaces.GPU |
|
|
def run_agent(prompt): |
|
|
""" |
|
|
This function runs the SmolAgent with the user's prompt. |
|
|
The @spaces.GPU decorator ensures that this function runs on a GPU. |
|
|
""" |
|
|
try: |
|
|
|
|
|
answer = agent.run(prompt) |
|
|
return answer |
|
|
except Exception as e: |
|
|
return f"An error occurred: {e}" |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=run_agent, |
|
|
inputs=gr.Textbox(lines=4, label="Your Prompt", placeholder="Enter your request for the agent..."), |
|
|
outputs=gr.Markdown(label="Agent's Response"), |
|
|
title="SmolMCP: A Hugging Face Agent", |
|
|
description="This agent can use tools to answer your questions. Enter your Hugging Face Hub token in the settings to allow the agent to use Hugging Face tools.", |
|
|
) |
|
|
|
|
|
|
|
|
logging.info("Launching Gradio app...") |
|
|
iface.launch() |
|
|
logging.info("Gradio app launched.") |
|
|
|
|
|
except Exception as e: |
|
|
logging.error(f"An unhandled exception occurred during setup: {e}", exc_info=True) |
|
|
|
|
|
print(f"An unhandled exception occurred during setup: {e}") |
|
|
|