spatil50's picture
Update app.py
467eb8d verified
raw
history blame
1.88 kB
import math
from datetime import datetime
import gradio as gr
from duckduckgo_search import DDGS
from smolagents import CodeAgent, tool
from smolagents.models import LiteLLMModel
from smolagents.tools import FinalAnswerTool
# -----------------------
# TOOL 1: WEB SEARCH
# -----------------------
@tool
def web_search(query: str) -> str:
"""
Search the web for information.
Args:
query: search query
"""
results = DDGS().text(query, max_results=5)
formatted = []
for r in results:
formatted.append(f"{r['title']} - {r['body']}")
return "\n".join(formatted)
# -----------------------
# TOOL 2: CALCULATOR
# -----------------------
@tool
def calculator(expression: str) -> str:
"""
Evaluate a mathematical expression.
Args:
expression: math expression like 12*45+2
"""
try:
result = eval(expression, {"math": math})
return str(result)
except Exception as e:
return f"Error: {str(e)}"
# -----------------------
# TOOL 3: CURRENT TIME
# -----------------------
@tool
def current_datetime() -> str:
"""
Returns current date and time.
"""
return datetime.now().isoformat()
model = LiteLLMModel(
model_id="ollama/qwen2:7b",
temperature=0.2,
)
agent = CodeAgent(
model=model,
tools=[
web_search,
calculator,
current_datetime,
FinalAnswerTool()
],
max_steps=12,
verbosity_level=1
)
def run_agent(question):
try:
answer = agent.run(question)
return answer
except Exception as e:
return str(e)
demo = gr.Interface(
fn=run_agent,
inputs=gr.Textbox(label="Ask the GAIA Agent"),
outputs="text",
title="GAIA Agent",
description="Agent built with smolagents for the Hugging Face Agents Course"
)
if __name__ == "__main__":
demo.launch()