Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,121 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
-
@tool
|
| 12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
-
Args:
|
| 16 |
-
arg1: the first argument
|
| 17 |
-
arg2: the second argument
|
| 18 |
-
"""
|
| 19 |
-
return "What magic will you build ?"
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""
|
| 24 |
-
Args:
|
| 25 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
-
"""
|
| 27 |
try:
|
| 28 |
-
# Create timezone object
|
| 29 |
tz = pytz.timezone(timezone)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
return
|
| 35 |
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=
|
| 44 |
-
temperature=0.
|
| 45 |
-
model_id=
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
verbosity_level=1,
|
| 61 |
-
grammar=None,
|
| 62 |
-
planning_interval=None,
|
| 63 |
-
name=None,
|
| 64 |
-
description=None,
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
+
import requests
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import json
|
| 8 |
|
| 9 |
+
from tools.final_answer import FinalAnswerTool
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# -----------------------------
|
| 14 |
+
# TIME TOOL
|
| 15 |
+
# -----------------------------
|
| 16 |
@tool
|
| 17 |
+
def get_time(timezone: str) -> str:
|
| 18 |
+
"""Get current time in timezone like Asia/Kathmandu"""
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
|
|
|
| 20 |
tz = pytz.timezone(timezone)
|
| 21 |
+
return datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return str(e)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# -----------------------------
|
| 27 |
+
# CALCULATOR TOOL
|
| 28 |
+
# -----------------------------
|
| 29 |
+
@tool
|
| 30 |
+
def calc(expression: str) -> str:
|
| 31 |
+
"""Safe math calculator"""
|
| 32 |
+
try:
|
| 33 |
+
return str(eval(expression))
|
| 34 |
+
except:
|
| 35 |
+
return "error"
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# -----------------------------
|
| 39 |
+
# CSV / FILE TOOL (GAIA CRITICAL)
|
| 40 |
+
# -----------------------------
|
| 41 |
+
@tool
|
| 42 |
+
def read_csv(url: str) -> str:
|
| 43 |
+
"""Read CSV from URL and return structured summary"""
|
| 44 |
+
try:
|
| 45 |
+
df = pd.read_csv(url)
|
| 46 |
+
return df.to_string()
|
| 47 |
except Exception as e:
|
| 48 |
+
return str(e)
|
| 49 |
|
| 50 |
|
| 51 |
+
# -----------------------------
|
| 52 |
+
# JSON TOOL (VERY USEFUL FOR GAIA)
|
| 53 |
+
# -----------------------------
|
| 54 |
+
@tool
|
| 55 |
+
def read_json(url: str) -> str:
|
| 56 |
+
"""Read JSON from URL"""
|
| 57 |
+
try:
|
| 58 |
+
r = requests.get(url)
|
| 59 |
+
return json.dumps(r.json(), indent=2)[:3000]
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return str(e)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# -----------------------------
|
| 65 |
+
# FINAL ANSWER TOOL
|
| 66 |
+
# -----------------------------
|
| 67 |
final_answer = FinalAnswerTool()
|
| 68 |
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
# -----------------------------
|
| 71 |
+
# MODEL (lower temperature = better GAIA)
|
| 72 |
+
# -----------------------------
|
| 73 |
model = HfApiModel(
|
| 74 |
+
max_tokens=2048,
|
| 75 |
+
temperature=0.1,
|
| 76 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
|
| 80 |
+
# -----------------------------
|
| 81 |
+
# PROMPT IMPROVEMENT (VERY IMPORTANT)
|
| 82 |
+
# -----------------------------
|
| 83 |
+
with open("prompts.yaml", "r") as f:
|
| 84 |
+
prompt_templates = yaml.safe_load(f)
|
| 85 |
+
|
| 86 |
+
prompt_templates["system_prompt"] = """
|
| 87 |
+
You are a high-precision GAIA agent.
|
| 88 |
+
|
| 89 |
+
STRICT RULES:
|
| 90 |
+
1. Use tools whenever helpful.
|
| 91 |
+
2. Break problem into steps internally.
|
| 92 |
+
3. Always verify with tools if possible.
|
| 93 |
+
4. FINAL OUTPUT = ONLY FINAL ANSWER.
|
| 94 |
+
5. No explanation, no extra words.
|
| 95 |
+
6. If multiple options exist, choose most likely correct.
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
|
| 99 |
+
# -----------------------------
|
| 100 |
+
# AGENT (UPGRADED STRATEGY)
|
| 101 |
+
# -----------------------------
|
| 102 |
agent = CodeAgent(
|
| 103 |
model=model,
|
| 104 |
+
tools=[
|
| 105 |
+
final_answer,
|
| 106 |
+
DuckDuckGoSearchTool(),
|
| 107 |
+
get_time,
|
| 108 |
+
calc,
|
| 109 |
+
read_csv,
|
| 110 |
+
read_json
|
| 111 |
+
],
|
| 112 |
+
max_steps=15,
|
| 113 |
verbosity_level=1,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
prompt_templates=prompt_templates
|
| 115 |
)
|
| 116 |
|
| 117 |
|
| 118 |
+
# -----------------------------
|
| 119 |
+
# LAUNCH
|
| 120 |
+
# -----------------------------
|
| 121 |
GradioUI(agent).launch()
|