Spaces:
Sleeping
Sleeping
first shot at entertaining random fact agent ^^
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
@@ -7,16 +7,15 @@ 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
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
@@ -25,28 +24,67 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
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 f"Error fetching time for timezone '{timezone}': {str(e)}"
|
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=2096,
|
44 |
-
temperature=0.5,
|
45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
46 |
-
custom_role_conversions=None,
|
47 |
)
|
48 |
|
49 |
-
|
50 |
# Import tool from Hub
|
51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
52 |
|
@@ -55,7 +93,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
@@ -65,5 +103,4 @@ agent = CodeAgent(
|
|
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
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
|
|
10 |
@tool
|
11 |
+
def get_random_fact() -> str:
|
12 |
+
"""Returns a fun random fact fetched from an API."""
|
13 |
+
try:
|
14 |
+
response = requests.get("https://uselessfacts.jsph.pl/random.json?language=en")
|
15 |
+
fact = response.json().get("text", "No fact available.")
|
16 |
+
return fact
|
17 |
+
except Exception as e:
|
18 |
+
return f"Error fetching fact: {str(e)}"
|
19 |
|
20 |
@tool
|
21 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
24 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
25 |
"""
|
26 |
try:
|
|
|
27 |
tz = pytz.timezone(timezone)
|
28 |
+
local_time = datetime.datetime.now(tz)
|
29 |
+
formatted_time = local_time.strftime("%Y-%m-%d %H:%M:%S")
|
30 |
+
return formatted_time
|
31 |
except Exception as e:
|
32 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
33 |
|
34 |
+
@tool
|
35 |
+
def get_fact_with_greeting(timezone: str) -> str:
|
36 |
+
"""
|
37 |
+
Returns an entertaining message that includes an appropriate greeting based on the
|
38 |
+
current time in the given timezone along with a fun random fact.
|
39 |
+
|
40 |
+
Args:
|
41 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
42 |
+
"""
|
43 |
+
# Get the current time string and also a datetime object for hour extraction.
|
44 |
+
try:
|
45 |
+
tz = pytz.timezone(timezone)
|
46 |
+
local_time = datetime.datetime.now(tz)
|
47 |
+
formatted_time = local_time.strftime("%Y-%m-%d %H:%M:%S")
|
48 |
+
except Exception as e:
|
49 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
50 |
+
|
51 |
+
# Determine an appropriate greeting based on the hour of the day.
|
52 |
+
hour = local_time.hour
|
53 |
+
if hour < 12:
|
54 |
+
greeting = "Good morning"
|
55 |
+
elif 12 <= hour < 18:
|
56 |
+
greeting = "Good afternoon"
|
57 |
+
else:
|
58 |
+
greeting = "Good evening"
|
59 |
+
|
60 |
+
# Fetch a fun fact.
|
61 |
+
fact = get_random_fact()
|
62 |
+
# If get_random_fact returned an error, it will already be a message indicating the error.
|
63 |
+
if fact.startswith("Error"):
|
64 |
+
fun_fact_message = fact
|
65 |
+
else:
|
66 |
+
fun_fact_message = f"Here's a fun fact: {fact}"
|
67 |
+
|
68 |
+
# Create the final entertaining message.
|
69 |
+
message = (
|
70 |
+
f"{greeting}! The current local time in {timezone} is {formatted_time}. "
|
71 |
+
f"{fun_fact_message}"
|
72 |
+
)
|
73 |
+
|
74 |
+
return message
|
75 |
|
76 |
final_answer = FinalAnswerTool()
|
77 |
|
78 |
# 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:
|
79 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
80 |
|
81 |
model = HfApiModel(
|
82 |
+
max_tokens=2096,
|
83 |
+
temperature=0.5,
|
84 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
|
85 |
+
custom_role_conversions=None,
|
86 |
)
|
87 |
|
|
|
88 |
# Import tool from Hub
|
89 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
90 |
|
|
|
93 |
|
94 |
agent = CodeAgent(
|
95 |
model=model,
|
96 |
+
tools=[final_answer, get_random_fact, get_current_time_in_timezone, get_fact_with_greeting],
|
97 |
max_steps=6,
|
98 |
verbosity_level=1,
|
99 |
grammar=None,
|
|
|
103 |
prompt_templates=prompt_templates
|
104 |
)
|
105 |
|
106 |
+
GradioUI(agent).launch()
|
|