Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,155 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
print("--- Starting Direct HF Hub Test ---")
|
6 |
-
token = os.environ.get("HF_TOKEN")
|
7 |
|
8 |
-
if token:
|
9 |
-
print("HF_TOKEN secret FOUND.")
|
10 |
-
else:
|
11 |
-
print("!!! HF_TOKEN secret IS *NOT* SET! !!!")
|
12 |
|
13 |
try:
|
14 |
-
client = InferenceClient(token=token)
|
15 |
-
print("InferenceClient initialized.")
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
max_tokens=10,
|
21 |
-
)
|
22 |
-
print("Direct client test SUCCESS:", response)
|
23 |
|
24 |
except Exception as e:
|
25 |
-
print("Direct client test FAILED:")
|
26 |
-
traceback.print_exc() # <--- This will print the full error
|
27 |
|
28 |
-
print("
|
|
|
|
|
29 |
|
30 |
-
#
|
31 |
-
# from smolagents import CodeAgent, HfApiModel # etc...
|
32 |
-
# agent = CodeAgent(...)
|
33 |
-
# agent.run(...)
|
|
|
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 |
+
"""A tool that fetches the current local time in a specified timezone.
|
44 |
+
|
45 |
+
Args:
|
46 |
+
|
47 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
48 |
+
|
49 |
+
"""
|
50 |
+
|
51 |
+
try:
|
52 |
+
|
53 |
+
# Create timezone object
|
54 |
+
|
55 |
+
tz = pytz.timezone(timezone)
|
56 |
+
|
57 |
+
# Get current time in that timezone
|
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 |
+
# 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:
|
76 |
+
|
77 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
model = HfApiModel(
|
82 |
+
|
83 |
+
max_tokens=2096,
|
84 |
+
|
85 |
+
temperature=0.5,
|
86 |
+
|
87 |
+
model_id='mistralai/Mistral-7B-Instruct-v0.2',# it is possible that this model may be overloaded
|
88 |
+
|
89 |
+
custom_role_conversions=None,
|
90 |
+
|
91 |
+
)
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
# Import tool from Hub
|
98 |
+
|
99 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
with open("prompts.yaml", 'r') as stream:
|
104 |
+
|
105 |
+
prompt_templates = yaml.safe_load(stream)
|
106 |
+
|
107 |
+
|
108 |
+
|
109 |
+
agent = CodeAgent(
|
110 |
+
|
111 |
+
model=model,
|
112 |
+
|
113 |
+
tools=[final_answer,
|
114 |
+
|
115 |
+
get_current_time_in_timezone,
|
116 |
+
|
117 |
+
search_tool], ## add your tools here (don't remove final answer)
|
118 |
+
|
119 |
+
max_steps=6,
|
120 |
+
|
121 |
+
verbosity_level=1,
|
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
|
|
|
|
|
|