Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,18 @@
|
|
| 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_custom_tool(arg1:str, arg2:int)-> str:
|
| 13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
arg1: the first argument
|
|
@@ -25,37 +27,126 @@ 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 |
-
# 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
@@ -65,5 +156,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
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
+
import random
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# Set Hugging Face API key (ensure this is handled securely in production)
|
| 12 |
+
# os.environ["HF_API_KEY"] = "HF_TOKEN"
|
| 13 |
|
|
|
|
| 14 |
@tool
|
| 15 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
|
|
| 16 |
"""A tool that does nothing yet
|
| 17 |
Args:
|
| 18 |
arg1: the first argument
|
|
|
|
| 27 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 28 |
"""
|
| 29 |
try:
|
|
|
|
| 30 |
tz = pytz.timezone(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 |
+
# --- Creative Tool 1: Random Creative & Smart Prompt Generator ---
|
| 37 |
+
@tool
|
| 38 |
+
def creative_prompt_generator(category: str = "any") -> str:
|
| 39 |
+
"""
|
| 40 |
+
Generates a random, creative, and smart prompt for storytelling, art, or brainstorming.
|
| 41 |
+
Args:
|
| 42 |
+
category: The creative domain for the prompt (e.g., 'story', 'art', 'startup', 'game', 'science', 'any').
|
| 43 |
+
"""
|
| 44 |
+
prompts = {
|
| 45 |
+
"story": [
|
| 46 |
+
"Write a short story where the main character wakes up in a world where everyone speaks in rhymes.",
|
| 47 |
+
"Describe a day in the life of a detective who can hear people's thoughts, but only in a foreign language.",
|
| 48 |
+
"Imagine a city where dreams and reality merge every midnight. What happens tonight?"
|
| 49 |
+
],
|
| 50 |
+
"art": [
|
| 51 |
+
"Draw or describe a landscape where gravity works sideways.",
|
| 52 |
+
"Create an artwork inspired by the concept of 'time as a river'.",
|
| 53 |
+
"Design a futuristic vehicle powered by a mysterious, glowing crystal."
|
| 54 |
+
],
|
| 55 |
+
"startup": [
|
| 56 |
+
"Pitch a startup that helps people swap skills instead of money.",
|
| 57 |
+
"Invent a wearable device that translates animal sounds into human language.",
|
| 58 |
+
"Describe a mobile app that helps users find lost memories."
|
| 59 |
+
],
|
| 60 |
+
"game": [
|
| 61 |
+
"Design a board game where alliances change every round based on a secret code.",
|
| 62 |
+
"Invent a video game where the main character can only move when music plays.",
|
| 63 |
+
"Describe the rules for a card game inspired by quantum mechanics."
|
| 64 |
+
],
|
| 65 |
+
"science": [
|
| 66 |
+
"Propose an experiment to test if plants can communicate through sound.",
|
| 67 |
+
"Imagine a new element discovered on a distant planet. What are its properties?",
|
| 68 |
+
"Describe a futuristic city powered entirely by algae."
|
| 69 |
+
],
|
| 70 |
+
"any": [
|
| 71 |
+
"Invent a new holiday and describe how people celebrate it.",
|
| 72 |
+
"Describe a world where pets are the teachers and humans are the students.",
|
| 73 |
+
"Imagine a conversation between a cloud and a mountain."
|
| 74 |
+
]
|
| 75 |
+
}
|
| 76 |
+
all_categories = list(prompts.keys())
|
| 77 |
+
chosen_category = category.lower() if category.lower() in prompts else "any"
|
| 78 |
+
prompt = random.choice(prompts[chosen_category])
|
| 79 |
+
return f"Creative Prompt [{chosen_category.capitalize()}]: {prompt}"
|
| 80 |
|
| 81 |
+
# --- Creative Tool 2: Health Tip Generator Based on Symptoms ---
|
| 82 |
+
@tool
|
| 83 |
+
def health_tip(symptoms: str) -> str:
|
| 84 |
+
"""
|
| 85 |
+
Provides a general health tip based on the described symptoms.
|
| 86 |
+
Args:
|
| 87 |
+
symptoms: A brief description of the symptoms (e.g., 'headache and fatigue').
|
| 88 |
+
"""
|
| 89 |
+
# Simple keyword-based matching for demonstration (not medical advice!)
|
| 90 |
+
tips = [
|
| 91 |
+
(["headache", "migraine"], "Stay hydrated, rest in a quiet and dark room, and avoid screen time. If headaches persist or worsen, consult a healthcare professional."),
|
| 92 |
+
(["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."),
|
| 93 |
+
(["cough", "sore throat"], "Drink warm fluids, avoid irritants, and rest your voice. If symptoms last more than a week, seek medical advice."),
|
| 94 |
+
(["fever", "temperature"], "Stay hydrated, rest, and monitor your temperature. If the fever is high or lasts more than 2 days, consult a healthcare provider."),
|
| 95 |
+
(["stomach", "nausea", "vomit"], "Eat light, bland foods, avoid dehydration, and rest. If symptoms are severe or persistent, seek medical attention."),
|
| 96 |
+
(["anxiety", "stress", "nervous"], "Practice deep breathing, mindfulness, and take regular breaks. Reach out for support if needed."),
|
| 97 |
+
(["back pain", "muscle ache"], "Apply a warm compress, gently stretch, and avoid heavy lifting. See a doctor if pain is severe or persistent.")
|
| 98 |
+
]
|
| 99 |
+
symptoms_lower = symptoms.lower()
|
| 100 |
+
for keywords, tip in tips:
|
| 101 |
+
if any(word in symptoms_lower for word in keywords):
|
| 102 |
+
return f"Health Tip: {tip}\n\n(Always consult a healthcare professional for serious or persistent symptoms.)"
|
| 103 |
+
return (
|
| 104 |
+
"Health Tip: Maintain a balanced diet, stay hydrated, get enough rest, and consult a healthcare professional if symptoms persist or worsen.\n\n"
|
| 105 |
+
"(This is not medical advice. For emergencies or serious symptoms, seek professional help immediately.)"
|
| 106 |
+
)
|
| 107 |
|
| 108 |
+
# --- Creative Tool 3: Daily Motivation Quote ---
|
| 109 |
+
@tool
|
| 110 |
+
def get_motivation_quote() -> str:
|
| 111 |
+
"""Returns a random motivational quote.
|
| 112 |
+
Args:
|
| 113 |
+
None
|
| 114 |
+
"""
|
| 115 |
+
quotes = [
|
| 116 |
+
"Believe you can and you're halfway there. – Theodore Roosevelt",
|
| 117 |
+
"The only way to do great work is to love what you do. – Steve Jobs",
|
| 118 |
+
"You are never too old to set another goal or to dream a new dream. – C.S. Lewis",
|
| 119 |
+
"Success is not final, failure is not fatal: It is the courage to continue that counts. – Winston Churchill",
|
| 120 |
+
"What you get by achieving your goals is not as important as what you become by achieving your goals. – Zig Ziglar"
|
| 121 |
+
]
|
| 122 |
+
return random.choice(quotes)
|
| 123 |
+
|
| 124 |
+
final_answer = FinalAnswerTool()
|
| 125 |
|
| 126 |
model = HfApiModel(
|
| 127 |
+
max_tokens=2096,
|
| 128 |
+
temperature=0.5,
|
| 129 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 130 |
+
custom_role_conversions=None,
|
| 131 |
)
|
| 132 |
|
|
|
|
| 133 |
# Import tool from Hub
|
| 134 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 135 |
|
| 136 |
with open("prompts.yaml", 'r') as stream:
|
| 137 |
prompt_templates = yaml.safe_load(stream)
|
| 138 |
+
|
| 139 |
agent = CodeAgent(
|
| 140 |
model=model,
|
| 141 |
+
tools=[
|
| 142 |
+
final_answer,
|
| 143 |
+
my_custom_tool,
|
| 144 |
+
get_current_time_in_timezone,
|
| 145 |
+
creative_prompt_generator,
|
| 146 |
+
health_tip,
|
| 147 |
+
get_motivation_quote,
|
| 148 |
+
image_generation_tool
|
| 149 |
+
],
|
| 150 |
max_steps=6,
|
| 151 |
verbosity_level=1,
|
| 152 |
grammar=None,
|
|
|
|
| 156 |
prompt_templates=prompt_templates
|
| 157 |
)
|
| 158 |
|
| 159 |
+
GradioUI(agent).launch()
|
|
|