Spaces:
Sleeping
Sleeping
| 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 | |
| # ----------------------- | |
| 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 | |
| # ----------------------- | |
| 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 | |
| # ----------------------- | |
| 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() |