|
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool |
|
import datetime |
|
import requests |
|
import pytz |
|
import yaml |
|
from tools.final_answer import FinalAnswerTool |
|
from Gradio_UI import GradioUI |
|
import random |
|
import os |
|
|
|
|
|
|
|
|
|
@tool |
|
def my_custom_tool(arg1: str, arg2: int) -> str: |
|
"""A tool that does nothing yet |
|
Args: |
|
arg1: the first argument |
|
arg2: the second argument |
|
""" |
|
return "What magic will you build ?" |
|
|
|
@tool |
|
def get_current_time_in_timezone(timezone: str) -> str: |
|
"""A tool that fetches the current local time in a specified timezone. |
|
Args: |
|
timezone: A string representing a valid timezone (e.g., 'America/New_York'). |
|
""" |
|
try: |
|
tz = pytz.timezone(timezone) |
|
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") |
|
return f"The current local time in {timezone} is: {local_time}" |
|
except Exception as e: |
|
return f"Error fetching time for timezone '{timezone}': {str(e)}" |
|
|
|
|
|
@tool |
|
def creative_prompt_generator(category: str = "any") -> str: |
|
""" |
|
Generates a random, creative, and smart prompt for storytelling, art, or brainstorming. |
|
Args: |
|
category: The creative domain for the prompt (e.g., 'story', 'art', 'startup', 'game', 'science', 'any'). |
|
""" |
|
prompts = { |
|
"story": [ |
|
"Write a short story where the main character wakes up in a world where everyone speaks in rhymes.", |
|
"Describe a day in the life of a detective who can hear people's thoughts, but only in a foreign language.", |
|
"Imagine a city where dreams and reality merge every midnight. What happens tonight?" |
|
], |
|
"art": [ |
|
"Draw or describe a landscape where gravity works sideways.", |
|
"Create an artwork inspired by the concept of 'time as a river'.", |
|
"Design a futuristic vehicle powered by a mysterious, glowing crystal." |
|
], |
|
"startup": [ |
|
"Pitch a startup that helps people swap skills instead of money.", |
|
"Invent a wearable device that translates animal sounds into human language.", |
|
"Describe a mobile app that helps users find lost memories." |
|
], |
|
"game": [ |
|
"Design a board game where alliances change every round based on a secret code.", |
|
"Invent a video game where the main character can only move when music plays.", |
|
"Describe the rules for a card game inspired by quantum mechanics." |
|
], |
|
"science": [ |
|
"Propose an experiment to test if plants can communicate through sound.", |
|
"Imagine a new element discovered on a distant planet. What are its properties?", |
|
"Describe a futuristic city powered entirely by algae." |
|
], |
|
"any": [ |
|
"Invent a new holiday and describe how people celebrate it.", |
|
"Describe a world where pets are the teachers and humans are the students.", |
|
"Imagine a conversation between a cloud and a mountain." |
|
] |
|
} |
|
all_categories = list(prompts.keys()) |
|
chosen_category = category.lower() if category.lower() in prompts else "any" |
|
prompt = random.choice(prompts[chosen_category]) |
|
return f"Creative Prompt [{chosen_category.capitalize()}]: {prompt}" |
|
|
|
|
|
@tool |
|
def health_tip(symptoms: str) -> str: |
|
""" |
|
Provides a general health tip based on the described symptoms. |
|
Args: |
|
symptoms: A brief description of the symptoms (e.g., 'headache and fatigue'). |
|
""" |
|
|
|
tips = [ |
|
(["headache", "migraine"], "Stay hydrated, rest in a quiet and dark room, and avoid screen time. If headaches persist or worsen, consult a healthcare professional."), |
|
(["fatigue", "tired", "exhausted"], "Ensure you get enough sleep, eat nutritious meals, and take short breaks during work. Persistent fatigue should be discussed with a doctor."), |
|
(["cough", "sore throat"], "Drink warm fluids, avoid irritants, and rest your voice. If symptoms last more than a week, seek medical advice."), |
|
(["fever", "temperature"], "Stay hydrated, rest, and monitor your temperature. If the fever is high or lasts more than 2 days, consult a healthcare provider."), |
|
(["stomach", "nausea", "vomit"], "Eat light, bland foods, avoid dehydration, and rest. If symptoms are severe or persistent, seek medical attention."), |
|
(["anxiety", "stress", "nervous"], "Practice deep breathing, mindfulness, and take regular breaks. Reach out for support if needed."), |
|
(["back pain", "muscle ache"], "Apply a warm compress, gently stretch, and avoid heavy lifting. See a doctor if pain is severe or persistent.") |
|
] |
|
symptoms_lower = symptoms.lower() |
|
for keywords, tip in tips: |
|
if any(word in symptoms_lower for word in keywords): |
|
return f"Health Tip: {tip}\n\n(Always consult a healthcare professional for serious or persistent symptoms.)" |
|
return ( |
|
"Health Tip: Maintain a balanced diet, stay hydrated, get enough rest, and consult a healthcare professional if symptoms persist or worsen.\n\n" |
|
"(This is not medical advice. For emergencies or serious symptoms, seek professional help immediately.)" |
|
) |
|
|
|
|
|
@tool |
|
def get_motivation_quote() -> str: |
|
"""Returns a random motivational quote. |
|
Args: |
|
None |
|
""" |
|
quotes = [ |
|
"Believe you can and you're halfway there. – Theodore Roosevelt", |
|
"The only way to do great work is to love what you do. – Steve Jobs", |
|
"You are never too old to set another goal or to dream a new dream. – C.S. Lewis", |
|
"Success is not final, failure is not fatal: It is the courage to continue that counts. – Winston Churchill", |
|
"What you get by achieving your goals is not as important as what you become by achieving your goals. – Zig Ziglar" |
|
] |
|
return random.choice(quotes) |
|
|
|
final_answer = FinalAnswerTool() |
|
|
|
model = HfApiModel( |
|
max_tokens=2096, |
|
temperature=0.5, |
|
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', |
|
custom_role_conversions=None, |
|
) |
|
|
|
|
|
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) |
|
|
|
with open("prompts.yaml", 'r') as stream: |
|
prompt_templates = yaml.safe_load(stream) |
|
|
|
agent = CodeAgent( |
|
model=model, |
|
tools=[ |
|
final_answer, |
|
my_custom_tool, |
|
get_current_time_in_timezone, |
|
creative_prompt_generator, |
|
health_tip, |
|
get_motivation_quote, |
|
image_generation_tool |
|
], |
|
max_steps=6, |
|
verbosity_level=1, |
|
grammar=None, |
|
planning_interval=None, |
|
name=None, |
|
description=None, |
|
prompt_templates=prompt_templates |
|
) |
|
|
|
GradioUI(agent).launch() |
|
|