Update app.py
Browse files
app.py
CHANGED
@@ -1,155 +1,106 @@
|
|
1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
-
|
3 |
import datetime
|
4 |
-
|
5 |
import requests
|
6 |
-
|
7 |
import pytz
|
8 |
-
|
9 |
import yaml
|
10 |
-
|
11 |
from tools.final_answer import FinalAnswerTool
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
from Gradio_UI import GradioUI
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
20 |
-
|
21 |
-
@tool
|
22 |
-
|
23 |
-
def search(arg:str)-> str: #it's import to specify the return type
|
24 |
-
|
25 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
26 |
-
|
27 |
-
"""A tool that searches on the web and return the results
|
28 |
-
|
29 |
-
Args:
|
30 |
-
|
31 |
-
arg: the topic to search about
|
32 |
-
|
33 |
-
"""
|
34 |
-
|
35 |
-
return DuckDuckGoSearchTool(arg)
|
36 |
-
|
37 |
-
|
38 |
|
39 |
@tool
|
40 |
-
|
41 |
def get_current_time_in_timezone(timezone: str) -> str:
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
60 |
-
|
61 |
-
return f"The current local time in {timezone} is: {local_time}"
|
62 |
-
|
63 |
-
except Exception as e:
|
64 |
-
|
65 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
search_tool = DuckDuckGoSearchTool()
|
70 |
|
|
|
71 |
final_answer = FinalAnswerTool()
|
72 |
|
|
|
73 |
|
74 |
-
|
75 |
-
#
|
76 |
-
|
77 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
model = HfApiModel(
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
88 |
-
|
89 |
-
custom_role_conversions=None,
|
90 |
-
|
91 |
)
|
92 |
|
|
|
93 |
|
|
|
|
|
94 |
|
|
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
agent = CodeAgent(
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
grammar=None,
|
124 |
-
|
125 |
-
planning_interval=None,
|
126 |
-
|
127 |
-
name=None,
|
128 |
-
|
129 |
-
description=None,
|
130 |
-
|
131 |
-
prompt_templates=prompt_templates
|
132 |
-
|
133 |
)
|
134 |
|
|
|
135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
-
|
138 |
-
|
|
|
139 |
# GradioUI(agent).launch()
|
140 |
|
141 |
-
|
142 |
-
|
143 |
try:
|
144 |
-
|
145 |
result = agent.run("what is the weather in aleppo city in syria?") # Or another test message
|
146 |
-
|
147 |
print("Agent Result:", result)
|
148 |
-
|
149 |
except Exception as e:
|
150 |
-
|
151 |
print("An error occurred during agent run:", e)
|
152 |
-
|
153 |
import traceback
|
154 |
-
|
155 |
traceback.print_exc() # <--- This will print the full error
|
|
|
|
|
|
|
1 |
import datetime
|
|
|
2 |
import requests
|
|
|
3 |
import pytz
|
|
|
4 |
import yaml
|
5 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
+
# from Gradio_UI import GradioUI # Keep this commented out while debugging via logs
|
8 |
|
9 |
+
# --- Define Your Tools ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
@tool
|
|
|
12 |
def get_current_time_in_timezone(timezone: str) -> str:
|
13 |
+
"""A tool that fetches the current local time in a specified timezone.
|
14 |
+
Args:
|
15 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York', 'Europe/London').
|
16 |
+
"""
|
17 |
+
try:
|
18 |
+
# Create timezone object
|
19 |
+
tz = pytz.timezone(timezone)
|
20 |
+
# Get current time in that timezone
|
21 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
22 |
+
return f"The current local time in {timezone} is: {local_time}"
|
23 |
+
except Exception as e:
|
24 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
25 |
+
|
26 |
+
# --- Instantiate Your Tools ---
|
27 |
+
|
28 |
+
# Instantiate the DuckDuckGo search tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
search_tool = DuckDuckGoSearchTool()
|
30 |
|
31 |
+
# Instantiate the tool for giving the final answer
|
32 |
final_answer = FinalAnswerTool()
|
33 |
|
34 |
+
# --- Configure the Language Model ---
|
35 |
|
36 |
+
# Use a reliable model for testing first
|
37 |
+
# You can switch back to larger models or endpoints later once this works.
|
|
|
|
|
|
|
|
|
|
|
38 |
model = HfApiModel(
|
39 |
+
max_tokens=2096,
|
40 |
+
temperature=0.5,
|
41 |
+
model_id='mistralai/Mistral-7B-Instruct-v0.2', # Using Mistral 7B for stability
|
42 |
+
custom_role_conversions=None,
|
|
|
|
|
|
|
|
|
|
|
43 |
)
|
44 |
|
45 |
+
# --- Load Extra Tools (Optional) ---
|
46 |
|
47 |
+
# Import tool from Hub (Keep it here, but we won't add it to the agent yet)
|
48 |
+
# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
49 |
|
50 |
+
# --- Load Prompts ---
|
51 |
|
52 |
+
try:
|
53 |
+
with open("prompts.yaml", 'r') as stream:
|
54 |
+
prompt_templates = yaml.safe_load(stream)
|
55 |
+
except FileNotFoundError:
|
56 |
+
print("ERROR: prompts.yaml not found. Please ensure the file exists.")
|
57 |
+
exit() # Exit if prompts are missing, as the agent can't work.
|
58 |
+
except yaml.YAMLError as e:
|
59 |
+
print(f"ERROR: Could not parse prompts.yaml: {e}")
|
60 |
+
exit()
|
61 |
+
|
62 |
+
# --- Create the Agent ---
|
63 |
+
|
64 |
+
# Create the agent with the tools it should use
|
65 |
agent = CodeAgent(
|
66 |
+
model=model,
|
67 |
+
tools=[
|
68 |
+
final_answer,
|
69 |
+
get_current_time_in_timezone,
|
70 |
+
search_tool # Add the search tool instance here
|
71 |
+
],
|
72 |
+
max_steps=6,
|
73 |
+
verbosity_level=2, # Set to 2 for more detailed logs during debugging
|
74 |
+
grammar=None,
|
75 |
+
planning_interval=None,
|
76 |
+
name=None,
|
77 |
+
description=None,
|
78 |
+
prompt_templates=prompt_templates
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
)
|
80 |
|
81 |
+
# --- Run the Agent (Directly for Debugging) ---
|
82 |
|
83 |
+
print("--- Starting Agent Run ---")
|
84 |
+
try:
|
85 |
+
# Use a query that requires search, like your weather question
|
86 |
+
# Or test the time tool: "What time is it in Europe/London?"
|
87 |
+
result = agent.run("what is the weather in aleppo city in syria?")
|
88 |
+
print("--- Agent Run Finished ---")
|
89 |
+
print("Agent Result:", result)
|
90 |
+
except Exception as e:
|
91 |
+
print("--- An error occurred during agent run: ---")
|
92 |
+
import traceback
|
93 |
+
traceback.print_exc() # Print the full error traceback
|
94 |
|
95 |
+
# --- Launch Gradio UI (Commented Out) ---
|
96 |
+
# Once the agent works reliably via logs, you can uncomment this
|
97 |
+
# print("--- Launching Gradio UI ---")
|
98 |
# GradioUI(agent).launch()
|
99 |
|
|
|
|
|
100 |
try:
|
|
|
101 |
result = agent.run("what is the weather in aleppo city in syria?") # Or another test message
|
|
|
102 |
print("Agent Result:", result)
|
|
|
103 |
except Exception as e:
|
|
|
104 |
print("An error occurred during agent run:", e)
|
|
|
105 |
import traceback
|
|
|
106 |
traceback.print_exc() # <--- This will print the full error
|