Spaces:
Sleeping
Sleeping
File size: 1,998 Bytes
3a27a7a 10e9b7d 3a27a7a c4a78f5 4a13179 dfe2aeb 3a27a7a dfe2aeb 3a27a7a 31243f4 3a27a7a 31243f4 3a27a7a 3c4371f 3a27a7a 3c4371f 3a27a7a e80aab9 3a27a7a 7d65c66 3a27a7a e80aab9 3a27a7a e80aab9 c4a78f5 2c07f80 c4a78f5 2bd6361 3a27a7a 2bd6361 3a27a7a e80aab9 3a27a7a e80aab9 3a27a7a | 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 103 104 105 106 107 | import math
from datetime import datetime
import gradio as gr
from duckduckgo_search import DDGS
from smolagents import CodeAgent, tool
from smolagents import InferenceClientModel
from smolagents import FinalAnswerTool
# from huggingface_hub import login
# login()
# -----------------------
# 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()
# -----------------------
# AGENT
# -----------------------
model = InferenceClientModel(
model_id="HuggingFaceH4/zephyr-7b-beta",
token=None
)
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() |