Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,72 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
-
# Example custom tool: a magic placeholder for creative ideas!
|
| 10 |
@tool
|
| 11 |
-
def
|
| 12 |
-
"""A tool that
|
| 13 |
-
|
| 14 |
-
Args:
|
| 15 |
-
arg1: the first argument.
|
| 16 |
-
arg2: the second argument.
|
| 17 |
-
"""
|
| 18 |
-
return "What magic will you build?"
|
| 19 |
-
|
| 20 |
-
@tool
|
| 21 |
-
def get_current_time_in_timezone(timezone: str) -> str:
|
| 22 |
-
"""A tool that fetches the current local time in a specified timezone.
|
| 23 |
-
|
| 24 |
Args:
|
| 25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
tz = pytz.timezone(timezone)
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
return f"
|
| 33 |
|
| 34 |
final_answer = FinalAnswerTool()
|
| 35 |
-
|
| 36 |
-
# Using a free model (e.g., 'gpt2') instead of a paid one.
|
| 37 |
model = HfApiModel(
|
| 38 |
max_tokens=2096,
|
| 39 |
temperature=0.5,
|
| 40 |
-
model_id='
|
| 41 |
-
custom_role_conversions=None,
|
| 42 |
)
|
| 43 |
|
| 44 |
-
# Import
|
| 45 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 46 |
|
| 47 |
with open("prompts.yaml", 'r') as stream:
|
| 48 |
prompt_templates = yaml.safe_load(stream)
|
| 49 |
-
|
| 50 |
agent = CodeAgent(
|
| 51 |
model=model,
|
| 52 |
-
tools=[final_answer,
|
| 53 |
max_steps=6,
|
| 54 |
verbosity_level=1,
|
| 55 |
-
grammar=None,
|
| 56 |
-
planning_interval=None,
|
| 57 |
-
name=None,
|
| 58 |
-
description=None,
|
| 59 |
prompt_templates=prompt_templates
|
| 60 |
)
|
| 61 |
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
+
import random
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
|
|
|
| 9 |
@tool
|
| 10 |
+
def time_traveling_fortune_teller(timezone: str) -> str:
|
| 11 |
+
"""A fun tool that tells the current time in a given timezone and provides a random fortune.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
Args:
|
| 13 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 14 |
"""
|
| 15 |
try:
|
| 16 |
tz = pytz.timezone(timezone)
|
| 17 |
+
current_time = datetime.datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')
|
| 18 |
+
current_hour = datetime.datetime.now(tz).hour
|
| 19 |
+
|
| 20 |
+
# Fun fortunes based on time of day
|
| 21 |
+
morning_fortunes = [
|
| 22 |
+
"☀️ Rise and shine! Today is a blank canvas, so go paint your masterpiece!",
|
| 23 |
+
"🌅 A surprise awaits you this morning… maybe it's coffee, maybe it's chaos. Who knows?",
|
| 24 |
+
"🍩 Today might be sweet! Maybe a donut is in your future? (Or at least a snack!)"
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
afternoon_fortunes = [
|
| 28 |
+
"🌞 Power through! Your afternoon holds the promise of success (or at least a good nap).",
|
| 29 |
+
"🍕 Lunchtime decisions determine your fate—choose wisely!",
|
| 30 |
+
"🚀 Something unexpected will happen soon. Could be good, could be hilarious!"
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
evening_fortunes = [
|
| 34 |
+
"🌙 The stars say… it's time to relax! Or binge-watch your favorite show.",
|
| 35 |
+
"🔥 A late-night snack might be calling your name. The universe approves.",
|
| 36 |
+
"🌌 Your dreams tonight will hold a secret message. If you remember them!"
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
# Choose fortune based on time of day
|
| 40 |
+
if 5 <= current_hour < 12:
|
| 41 |
+
fortune = random.choice(morning_fortunes)
|
| 42 |
+
elif 12 <= current_hour < 18:
|
| 43 |
+
fortune = random.choice(afternoon_fortunes)
|
| 44 |
+
else:
|
| 45 |
+
fortune = random.choice(evening_fortunes)
|
| 46 |
+
|
| 47 |
+
return f"⏳ The local time in {timezone} is {current_time}.\n🔮 Your fortune: {fortune}"
|
| 48 |
+
|
| 49 |
except Exception as e:
|
| 50 |
+
return f"⚠️ Oops! The time-traveling fortune teller got lost in another dimension. Error: {str(e)}"
|
| 51 |
|
| 52 |
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
| 53 |
model = HfApiModel(
|
| 54 |
max_tokens=2096,
|
| 55 |
temperature=0.5,
|
| 56 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# Import tool from Hub
|
| 60 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 61 |
|
| 62 |
with open("prompts.yaml", 'r') as stream:
|
| 63 |
prompt_templates = yaml.safe_load(stream)
|
| 64 |
+
|
| 65 |
agent = CodeAgent(
|
| 66 |
model=model,
|
| 67 |
+
tools=[final_answer, time_traveling_fortune_teller],
|
| 68 |
max_steps=6,
|
| 69 |
verbosity_level=1,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
prompt_templates=prompt_templates
|
| 71 |
)
|
| 72 |
|