Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 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 |
|
|
@@ -13,17 +13,16 @@ from Gradio_UI import GradioUI
|
|
| 13 |
# -----------------------------
|
| 14 |
# TIME TOOL
|
| 15 |
# -----------------------------
|
| 16 |
-
|
| 17 |
@tool
|
| 18 |
def get_time(timezone: str) -> str:
|
| 19 |
"""
|
| 20 |
-
Get current time in a
|
| 21 |
|
| 22 |
Args:
|
| 23 |
-
timezone
|
| 24 |
|
| 25 |
Returns:
|
| 26 |
-
|
| 27 |
"""
|
| 28 |
try:
|
| 29 |
tz = pytz.timezone(timezone)
|
|
@@ -37,7 +36,15 @@ def get_time(timezone: str) -> str:
|
|
| 37 |
# -----------------------------
|
| 38 |
@tool
|
| 39 |
def calc(expression: str) -> str:
|
| 40 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
try:
|
| 42 |
return str(eval(expression))
|
| 43 |
except:
|
|
@@ -45,11 +52,19 @@ def calc(expression: str) -> str:
|
|
| 45 |
|
| 46 |
|
| 47 |
# -----------------------------
|
| 48 |
-
# CSV
|
| 49 |
# -----------------------------
|
| 50 |
@tool
|
| 51 |
def read_csv(url: str) -> str:
|
| 52 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
try:
|
| 54 |
df = pd.read_csv(url)
|
| 55 |
return df.to_string()
|
|
@@ -58,11 +73,19 @@ def read_csv(url: str) -> str:
|
|
| 58 |
|
| 59 |
|
| 60 |
# -----------------------------
|
| 61 |
-
# JSON TOOL
|
| 62 |
# -----------------------------
|
| 63 |
@tool
|
| 64 |
def read_json(url: str) -> str:
|
| 65 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
try:
|
| 67 |
r = requests.get(url)
|
| 68 |
return json.dumps(r.json(), indent=2)[:3000]
|
|
@@ -70,44 +93,31 @@ def read_json(url: str) -> str:
|
|
| 70 |
return str(e)
|
| 71 |
|
| 72 |
|
| 73 |
-
# -----------------------------
|
| 74 |
-
# FINAL ANSWER TOOL
|
| 75 |
-
# -----------------------------
|
| 76 |
final_answer = FinalAnswerTool()
|
| 77 |
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
#
|
| 81 |
-
|
| 82 |
model = HfApiModel(
|
| 83 |
-
max_tokens=
|
| 84 |
temperature=0.1,
|
| 85 |
-
model_id=
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
|
| 95 |
-
prompt_templates["system_prompt"] = """
|
| 96 |
-
You are a high-precision GAIA agent.
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
2. Break problem into steps internally.
|
| 101 |
-
3. Always verify with tools if possible.
|
| 102 |
-
4. FINAL OUTPUT = ONLY FINAL ANSWER.
|
| 103 |
-
5. No explanation, no extra words.
|
| 104 |
-
6. If multiple options exist, choose most likely correct.
|
| 105 |
-
"""
|
| 106 |
|
| 107 |
|
| 108 |
-
# -----------------------------
|
| 109 |
-
# AGENT (UPGRADED STRATEGY)
|
| 110 |
-
# -----------------------------
|
| 111 |
agent = CodeAgent(
|
| 112 |
model=model,
|
| 113 |
tools=[
|
|
@@ -118,13 +128,14 @@ agent = CodeAgent(
|
|
| 118 |
read_csv,
|
| 119 |
read_json
|
| 120 |
],
|
| 121 |
-
max_steps=
|
| 122 |
verbosity_level=1,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
prompt_templates=prompt_templates
|
| 124 |
)
|
| 125 |
|
| 126 |
|
| 127 |
-
# -----------------------------
|
| 128 |
-
# LAUNCH
|
| 129 |
-
# -----------------------------
|
| 130 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
+
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
| 6 |
import pandas as pd
|
| 7 |
import json
|
| 8 |
|
|
|
|
| 13 |
# -----------------------------
|
| 14 |
# TIME TOOL
|
| 15 |
# -----------------------------
|
|
|
|
| 16 |
@tool
|
| 17 |
def get_time(timezone: str) -> str:
|
| 18 |
"""
|
| 19 |
+
Get current time in a timezone.
|
| 20 |
|
| 21 |
Args:
|
| 22 |
+
timezone: Timezone name like Asia/Kathmandu
|
| 23 |
|
| 24 |
Returns:
|
| 25 |
+
Current time string
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
tz = pytz.timezone(timezone)
|
|
|
|
| 36 |
# -----------------------------
|
| 37 |
@tool
|
| 38 |
def calc(expression: str) -> str:
|
| 39 |
+
"""
|
| 40 |
+
Evaluate math expression.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
expression: Math expression like 2+2*5
|
| 44 |
+
|
| 45 |
+
Returns:
|
| 46 |
+
Result of calculation
|
| 47 |
+
"""
|
| 48 |
try:
|
| 49 |
return str(eval(expression))
|
| 50 |
except:
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
# -----------------------------
|
| 55 |
+
# CSV TOOL
|
| 56 |
# -----------------------------
|
| 57 |
@tool
|
| 58 |
def read_csv(url: str) -> str:
|
| 59 |
+
"""
|
| 60 |
+
Read CSV file from URL.
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
url: CSV file URL
|
| 64 |
+
|
| 65 |
+
Returns:
|
| 66 |
+
CSV content
|
| 67 |
+
"""
|
| 68 |
try:
|
| 69 |
df = pd.read_csv(url)
|
| 70 |
return df.to_string()
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
# -----------------------------
|
| 76 |
+
# JSON TOOL
|
| 77 |
# -----------------------------
|
| 78 |
@tool
|
| 79 |
def read_json(url: str) -> str:
|
| 80 |
+
"""
|
| 81 |
+
Read JSON file from URL.
|
| 82 |
+
|
| 83 |
+
Args:
|
| 84 |
+
url: JSON file URL
|
| 85 |
+
|
| 86 |
+
Returns:
|
| 87 |
+
JSON content
|
| 88 |
+
"""
|
| 89 |
try:
|
| 90 |
r = requests.get(url)
|
| 91 |
return json.dumps(r.json(), indent=2)[:3000]
|
|
|
|
| 93 |
return str(e)
|
| 94 |
|
| 95 |
|
|
|
|
|
|
|
|
|
|
| 96 |
final_answer = FinalAnswerTool()
|
| 97 |
|
| 98 |
|
| 99 |
+
# If model overloads use endpoint:
|
| 100 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 101 |
+
|
| 102 |
model = HfApiModel(
|
| 103 |
+
max_tokens=2096,
|
| 104 |
temperature=0.1,
|
| 105 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 106 |
+
custom_role_conversions=None,
|
| 107 |
)
|
| 108 |
|
| 109 |
|
| 110 |
+
# optional image tool
|
| 111 |
+
image_generation_tool = load_tool(
|
| 112 |
+
"agents-course/text-to-image",
|
| 113 |
+
trust_remote_code=True
|
| 114 |
+
)
|
| 115 |
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
with open("prompts.yaml", 'r') as stream:
|
| 118 |
+
prompt_templates = yaml.safe_load(stream)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
|
|
|
|
|
|
|
|
|
|
| 121 |
agent = CodeAgent(
|
| 122 |
model=model,
|
| 123 |
tools=[
|
|
|
|
| 128 |
read_csv,
|
| 129 |
read_json
|
| 130 |
],
|
| 131 |
+
max_steps=12,
|
| 132 |
verbosity_level=1,
|
| 133 |
+
grammar=None,
|
| 134 |
+
planning_interval=None,
|
| 135 |
+
name=None,
|
| 136 |
+
description=None,
|
| 137 |
prompt_templates=prompt_templates
|
| 138 |
)
|
| 139 |
|
| 140 |
|
|
|
|
|
|
|
|
|
|
| 141 |
GradioUI(agent).launch()
|