Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,194 @@
|
|
1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
-
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
#
|
11 |
@tool
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
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 |
-
# Get current time in that timezone
|
31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
32 |
-
return f"
|
33 |
except Exception as e:
|
34 |
-
return f"Error
|
35 |
-
|
36 |
|
|
|
37 |
final_answer = FinalAnswerTool()
|
|
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
#
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
prompt_templates = yaml.safe_load(stream)
|
55 |
-
|
56 |
agent = CodeAgent(
|
57 |
-
model=
|
58 |
-
tools=[final_answer
|
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,load_tool,tool
|
2 |
+
# import datetime
|
3 |
+
# import requests
|
4 |
+
# import pytz
|
5 |
+
# import yaml
|
6 |
+
# from tools.final_answer import FinalAnswerTool
|
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 |
+
|
20 |
+
|
21 |
+
# # return "What magic will you build ?"
|
22 |
+
|
23 |
+
# @tool
|
24 |
+
# def countries_by_currency_and_language(currency: str, language: str) -> str:
|
25 |
+
# """
|
26 |
+
# Fetches countries that use a specified currency and speak a specified language via API.
|
27 |
+
# Uses targeted API calls to avoid fetching all countries unnecessarily.
|
28 |
+
|
29 |
+
# Args:
|
30 |
+
# currency: A 3-letter currency code (e.g., 'USD', 'EUR', 'NGN').
|
31 |
+
# language: A language name (e.g., 'English', 'French', 'Swahili').
|
32 |
+
# """
|
33 |
+
# import requests
|
34 |
+
|
35 |
+
# # Step 1: Fetch countries by currency
|
36 |
+
# try:
|
37 |
+
# currency_response = requests.get(f"https://restcountries.com/v3.1/currency/{currency.upper()}")
|
38 |
+
# currency_response.raise_for_status()
|
39 |
+
# currency_countries = currency_response.json()
|
40 |
+
# except requests.exceptions.RequestException as e:
|
41 |
+
# return f"Failed to fetch countries by currency: {str(e)}"
|
42 |
+
|
43 |
+
# # Step 2: Fetch countries by language
|
44 |
+
# try:
|
45 |
+
# language_response = requests.get(f"https://restcountries.com/v3.1/lang/{language.lower()}")
|
46 |
+
# language_response.raise_for_status()
|
47 |
+
# language_countries = language_response.json()
|
48 |
+
# except requests.exceptions.RequestException as e:
|
49 |
+
# return f"Failed to fetch countries by language: {str(e)}"
|
50 |
+
|
51 |
+
# # Step 3: Find intersection of countries (matches both currency and language)
|
52 |
+
# currency_set = {c["name"]["common"] for c in currency_countries}
|
53 |
+
# language_set = {c["name"]["common"] for c in language_countries}
|
54 |
+
# matches = list(currency_set & language_set)
|
55 |
+
|
56 |
+
# # Step 4: Return results
|
57 |
+
# if matches:
|
58 |
+
# return f"Countries using {currency.upper()} and speaking {language.capitalize()}: {', '.join(matches)}."
|
59 |
+
# else:
|
60 |
+
# return f"No countries found using {currency.upper()} and speaking {language.capitalize()}."
|
61 |
+
|
62 |
+
# @tool
|
63 |
+
# def get_current_time_in_timezone(timezone: str) -> str:
|
64 |
+
# """A tool that fetches the current local time in a specified timezone.
|
65 |
+
# Args:
|
66 |
+
# timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
67 |
+
# """
|
68 |
+
# try:
|
69 |
+
# # Create timezone object
|
70 |
+
# tz = pytz.timezone(timezone)
|
71 |
+
# # Get current time in that timezone
|
72 |
+
# local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
73 |
+
# return f"The current local time in {timezone} is: {local_time}"
|
74 |
+
# except Exception as e:
|
75 |
+
# return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
76 |
+
|
77 |
+
|
78 |
+
# final_answer = FinalAnswerTool()
|
79 |
+
|
80 |
+
# # 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:
|
81 |
+
# # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
82 |
+
|
83 |
+
# model = HfApiModel(
|
84 |
+
# max_tokens=2096,
|
85 |
+
# temperature=0.5,
|
86 |
+
# model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
87 |
+
# custom_role_conversions=None,
|
88 |
+
# )
|
89 |
+
|
90 |
+
|
91 |
+
# # Import tool from Hub
|
92 |
+
# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
93 |
+
|
94 |
+
# with open("prompts.yaml", 'r') as stream:
|
95 |
+
# prompt_templates = yaml.safe_load(stream)
|
96 |
+
|
97 |
+
# agent = CodeAgent(
|
98 |
+
# model=model,
|
99 |
+
# tools=[final_answer], ## add your tools here (don't remove final answer)
|
100 |
+
# max_steps=6,
|
101 |
+
# verbosity_level=1,
|
102 |
+
# grammar=None,
|
103 |
+
# planning_interval=None,
|
104 |
+
# name=None,
|
105 |
+
# description=None,
|
106 |
+
# prompt_templates=prompt_templates
|
107 |
+
# )
|
108 |
+
|
109 |
+
|
110 |
+
# GradioUI(agent).launch()
|
111 |
+
|
112 |
+
|
113 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
114 |
import datetime
|
115 |
import requests
|
116 |
import pytz
|
117 |
import yaml
|
118 |
from tools.final_answer import FinalAnswerTool
|
|
|
119 |
from Gradio_UI import GradioUI
|
120 |
|
121 |
+
# Custom Tools
|
122 |
@tool
|
123 |
+
def countries_by_currency_and_language(currency: str, language: str) -> str:
|
124 |
+
"""Fetches countries using specified currency and language."""
|
125 |
+
try:
|
126 |
+
# Fetch by currency
|
127 |
+
currency_response = requests.get(f"https://restcountries.com/v3.1/currency/{currency.upper()}")
|
128 |
+
currency_response.raise_for_status()
|
129 |
+
currency_countries = currency_response.json()
|
130 |
+
|
131 |
+
# Fetch by language
|
132 |
+
language_response = requests.get(f"https://restcountries.com/v3.1/lang/{language.lower()}")
|
133 |
+
language_response.raise_for_status()
|
134 |
+
language_countries = language_response.json()
|
135 |
+
|
136 |
+
# Find intersection
|
137 |
+
currency_set = {c["name"]["common"] for c in currency_countries}
|
138 |
+
language_set = {c["name"]["common"] for c in language_countries}
|
139 |
+
matches = list(currency_set & language_set)
|
140 |
+
|
141 |
+
return f"Countries using {currency.upper()} and speaking {language.capitalize()}: {', '.join(matches)}." if matches else f"No matches found."
|
142 |
+
except Exception as e:
|
143 |
+
return f"Error: {str(e)}"
|
144 |
|
145 |
@tool
|
146 |
def get_current_time_in_timezone(timezone: str) -> str:
|
147 |
+
"""Returns current time in specified timezone."""
|
|
|
|
|
|
|
148 |
try:
|
|
|
149 |
tz = pytz.timezone(timezone)
|
|
|
150 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
151 |
+
return f"Current time in {timezone}: {local_time}"
|
152 |
except Exception as e:
|
153 |
+
return f"Error: {str(e)}"
|
|
|
154 |
|
155 |
+
# Initialize tools
|
156 |
final_answer = FinalAnswerTool()
|
157 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
158 |
|
159 |
+
# Model configuration with fallback
|
160 |
+
try:
|
161 |
+
primary_model = HfApiModel(
|
162 |
+
max_tokens=2096,
|
163 |
+
temperature=0.5,
|
164 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
165 |
+
custom_role_conversions=None,
|
166 |
+
)
|
167 |
+
except Exception:
|
168 |
+
primary_model = HfApiModel(
|
169 |
+
max_tokens=2096,
|
170 |
+
temperature=0.5,
|
171 |
+
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
172 |
+
custom_role_conversions=None,
|
173 |
+
)
|
174 |
|
175 |
+
# Load prompts
|
176 |
+
try:
|
177 |
+
with open("prompts.yaml", 'r') as stream:
|
178 |
+
prompt_templates = yaml.safe_load(stream)
|
179 |
+
except FileNotFoundError:
|
180 |
+
prompt_templates = None
|
181 |
+
print("Warning: prompts.yaml not found, using default prompts")
|
182 |
|
183 |
+
# Create agent with all tools
|
|
|
|
|
184 |
agent = CodeAgent(
|
185 |
+
model=primary_model,
|
186 |
+
tools=[final_answer, countries_by_currency_and_language,
|
187 |
+
get_current_time_in_timezone, image_generation_tool],
|
188 |
+
max_steps=10, # Increased for more complex tasks
|
189 |
verbosity_level=1,
|
|
|
|
|
|
|
|
|
190 |
prompt_templates=prompt_templates
|
191 |
)
|
192 |
|
193 |
+
# Launch UI
|
194 |
GradioUI(agent).launch()
|