Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,118 @@
|
|
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_cutom_tool(arg1:str, arg2:int)-> str:
|
13 |
-
|
14 |
-
|
15 |
Args:
|
16 |
-
arg1:
|
17 |
-
arg2:
|
|
|
|
|
|
|
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"The current local time in {timezone} is: {local_time}"
|
33 |
except Exception as e:
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
final_answer = FinalAnswerTool()
|
|
|
|
|
38 |
model = HfApiModel(
|
39 |
-
max_tokens=2096,
|
40 |
-
temperature=0.5,
|
41 |
-
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud'
|
42 |
-
custom_role_conversions=None,
|
43 |
)
|
44 |
|
45 |
-
|
46 |
-
# Import tool from Hub
|
47 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
48 |
|
|
|
49 |
with open("prompts.yaml", 'r') as stream:
|
50 |
prompt_templates = yaml.safe_load(stream)
|
51 |
-
|
|
|
52 |
agent = CodeAgent(
|
53 |
model=model,
|
54 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
max_steps=6,
|
56 |
verbosity_level=1,
|
57 |
grammar=None,
|
58 |
planning_interval=None,
|
59 |
-
name=
|
60 |
-
description=
|
61 |
prompt_templates=prompt_templates
|
62 |
)
|
63 |
|
64 |
-
|
65 |
-
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 random
|
7 |
from tools.final_answer import FinalAnswerTool
|
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
|
|
10 |
@tool
|
11 |
+
def my_cutom_tool(arg1: str, arg2: int) -> str:
|
12 |
+
"""A magical tool that conjures a mystical incantation.
|
13 |
+
|
14 |
Args:
|
15 |
+
arg1: A mystical phrase or keyword.
|
16 |
+
arg2: An intensity level of magic.
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
A randomly generated incantation string.
|
20 |
"""
|
21 |
+
magical_templates = [
|
22 |
+
f"By the light of the moon and stars, '{arg1}' awakens with intensity {arg2}!",
|
23 |
+
f"Summoning ancient powers: '{arg1}' transforms into cosmic energy of level {arg2}.",
|
24 |
+
f"Let the magic flow! '{arg1}' reverberates with a force of {arg2} magnitudes."
|
25 |
+
]
|
26 |
+
return random.choice(magical_templates)
|
27 |
|
28 |
@tool
|
29 |
def get_current_time_in_timezone(timezone: str) -> str:
|
30 |
+
"""Fetches the current local time in a specified timezone.
|
31 |
+
|
32 |
Args:
|
33 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
34 |
+
|
35 |
+
Returns:
|
36 |
+
A string with the current time in the given timezone or an error message.
|
37 |
"""
|
38 |
try:
|
|
|
39 |
tz = pytz.timezone(timezone)
|
|
|
40 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
41 |
return f"The current local time in {timezone} is: {local_time}"
|
42 |
except Exception as e:
|
43 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
44 |
|
45 |
+
@tool
|
46 |
+
def get_random_joke() -> str:
|
47 |
+
"""Fetches a random joke from an online API.
|
48 |
+
|
49 |
+
Returns:
|
50 |
+
A string containing a joke.
|
51 |
+
"""
|
52 |
+
try:
|
53 |
+
response = requests.get("https://official-joke-api.appspot.com/random_joke")
|
54 |
+
data = response.json()
|
55 |
+
joke = f"{data['setup']} ... {data['punchline']}"
|
56 |
+
return joke
|
57 |
+
except Exception as e:
|
58 |
+
return f"Error fetching joke: {str(e)}"
|
59 |
|
60 |
+
@tool
|
61 |
+
def get_motivational_quote() -> str:
|
62 |
+
"""Fetches a motivational quote from an online API.
|
63 |
+
|
64 |
+
Returns:
|
65 |
+
A string containing an inspirational quote.
|
66 |
+
"""
|
67 |
+
try:
|
68 |
+
response = requests.get("https://zenquotes.io/api/random")
|
69 |
+
data = response.json()
|
70 |
+
if isinstance(data, list) and len(data) > 0:
|
71 |
+
quote = data[0].get('q', 'Stay positive!')
|
72 |
+
author = data[0].get('a', 'Unknown')
|
73 |
+
return f"\"{quote}\" - {author}"
|
74 |
+
return "No quote found."
|
75 |
+
except Exception as e:
|
76 |
+
return f"Error fetching quote: {str(e)}"
|
77 |
+
|
78 |
+
# This tool always has to remain in your toolkit
|
79 |
final_answer = FinalAnswerTool()
|
80 |
+
|
81 |
+
# Set up the model with your desired parameters
|
82 |
model = HfApiModel(
|
83 |
+
max_tokens=2096,
|
84 |
+
temperature=0.5,
|
85 |
+
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud', # Note: This model may be overloaded at times.
|
86 |
+
custom_role_conversions=None,
|
87 |
)
|
88 |
|
89 |
+
# Load the image generation tool from the Hub
|
|
|
90 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
91 |
|
92 |
+
# Load your prompt templates from a YAML file
|
93 |
with open("prompts.yaml", 'r') as stream:
|
94 |
prompt_templates = yaml.safe_load(stream)
|
95 |
+
|
96 |
+
# Create the awesome agent with a suite of magical, informative, and fun tools.
|
97 |
agent = CodeAgent(
|
98 |
model=model,
|
99 |
+
tools=[
|
100 |
+
final_answer,
|
101 |
+
my_cutom_tool,
|
102 |
+
get_current_time_in_timezone,
|
103 |
+
image_generation_tool,
|
104 |
+
get_random_joke,
|
105 |
+
get_motivational_quote,
|
106 |
+
DuckDuckGoSearchTool(), # Enables web search capabilities
|
107 |
+
],
|
108 |
max_steps=6,
|
109 |
verbosity_level=1,
|
110 |
grammar=None,
|
111 |
planning_interval=None,
|
112 |
+
name="AwesomeAgent",
|
113 |
+
description="A highly creative and magical agent with time, image, search, and humor capabilities.",
|
114 |
prompt_templates=prompt_templates
|
115 |
)
|
116 |
|
117 |
+
# Launch the agent in a Gradio interface
|
118 |
+
GradioUI(agent).launch()
|