Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,131 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
-
import datetime
|
| 3 |
-
import requests
|
| 4 |
-
import pytz
|
| 5 |
import yaml
|
| 6 |
-
import
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
-
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
-
|
| 12 |
@tool
|
| 13 |
-
def
|
| 14 |
"""
|
| 15 |
-
Generates
|
| 16 |
-
|
| 17 |
Args:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
Returns:
|
| 21 |
-
str: A
|
| 22 |
"""
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
-
complexity = next(value for (low, high), value in complexity_levels.items() if low <= age <= high)
|
| 34 |
-
|
| 35 |
-
passover_pages = {
|
| 36 |
-
"simple": ["Simple matzah", "Basic Seder plate", "Moses in a basket", "Simple frog (plague)"],
|
| 37 |
-
"basic": ["Parting of the Red Sea", "Ten Plagues scenes", "Four Questions illustrations", "Afikoman hunt"],
|
| 38 |
-
"intermediate": ["Detailed Seder table", "Exodus story scenes", "Hebrew letter decorations", "Egyptian pyramids"],
|
| 39 |
-
"advanced": ["Intricate Haggadah illustrations", "Detailed symbolism scene", "Combined Passover story panels", "Hebrew calligraphy art"]
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
selected_pages = passover_pages[complexity]
|
| 43 |
-
|
| 44 |
-
# Попытка сгенерировать изображения
|
| 45 |
-
try:
|
| 46 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 47 |
-
images = [image_generation_tool(page) for page in selected_pages]
|
| 48 |
-
return final_answer(images)
|
| 49 |
-
except Exception as e:
|
| 50 |
-
return f"Error generating images: {str(e)}"
|
| 51 |
-
|
| 52 |
-
response = f"Passover Coloring Pages for a {age}-year-old:\n\n"
|
| 53 |
-
response += "\n".join(f"- {page}" for page in selected_pages)
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
#
|
| 58 |
final_answer = FinalAnswerTool()
|
| 59 |
|
| 60 |
model = HfApiModel(
|
|
@@ -63,16 +134,75 @@ model = HfApiModel(
|
|
| 63 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 64 |
)
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
agent = CodeAgent(
|
| 70 |
model=model,
|
| 71 |
-
tools=[final_answer,
|
| 72 |
-
max_steps=
|
| 73 |
verbosity_level=1,
|
| 74 |
prompt_templates=prompt_templates
|
| 75 |
)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, tool
|
|
|
|
|
|
|
|
|
|
| 2 |
import yaml
|
| 3 |
+
import random
|
| 4 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 5 |
from Gradio_UI import GradioUI
|
| 6 |
|
|
|
|
| 7 |
@tool
|
| 8 |
+
def generate_famous_quote(topic: str) -> str:
|
| 9 |
"""
|
| 10 |
+
Generates a famous quote related to the provided topic.
|
|
|
|
| 11 |
Args:
|
| 12 |
+
topic (str): The topic or theme for the quote.
|
|
|
|
| 13 |
Returns:
|
| 14 |
+
str: A famous quote related to the topic and its author.
|
| 15 |
"""
|
| 16 |
+
# Dictionary of quotes by topic
|
| 17 |
+
quotes_by_topic = {
|
| 18 |
+
"love": [
|
| 19 |
+
"The greatest happiness of life is the conviction that we are loved; loved for ourselves, or rather, loved in spite of ourselves. — Victor Hugo",
|
| 20 |
+
"Love is composed of a single soul inhabiting two bodies. — Aristotle",
|
| 21 |
+
"Being deeply loved by someone gives you strength, while loving someone deeply gives you courage. — Lao Tzu",
|
| 22 |
+
"Where there is love, there is life. — Mahatma Gandhi",
|
| 23 |
+
"The best thing to hold onto in life is each other. — Audrey Hepburn"
|
| 24 |
+
],
|
| 25 |
+
"wisdom": [
|
| 26 |
+
"The only true wisdom is in knowing you know nothing. — Socrates",
|
| 27 |
+
"Wisdom is not a product of schooling but of the lifelong attempt to acquire it. — Albert Einstein",
|
| 28 |
+
"The fool doth think he is wise, but the wise man knows himself to be a fool. — William Shakespeare",
|
| 29 |
+
"By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest. — Confucius",
|
| 30 |
+
"The beginning of wisdom is the definition of terms. — Socrates"
|
| 31 |
+
],
|
| 32 |
+
"success": [
|
| 33 |
+
"Success is not final, failure is not fatal: It is the courage to continue that counts. — Winston Churchill",
|
| 34 |
+
"The way to get started is to quit talking and begin doing. — Walt Disney",
|
| 35 |
+
"Don't be distracted by criticism. Remember, the only taste of success some people get is to take a bite out of you. — Zig Ziglar",
|
| 36 |
+
"Success usually comes to those who are too busy to be looking for it. — Henry David Thoreau",
|
| 37 |
+
"I never dreamed about success, I worked for it. — Estée Lauder"
|
| 38 |
+
],
|
| 39 |
+
"happiness": [
|
| 40 |
+
"Happiness is not something ready-made. It comes from your own actions. — Dalai Lama",
|
| 41 |
+
"The happiness of your life depends upon the quality of your thoughts. — Marcus Aurelius",
|
| 42 |
+
"The most important thing is to enjoy your life—to be happy—it's all that matters. — Audrey Hepburn",
|
| 43 |
+
"Happiness is when what you think, what you say, and what you do are in harmony. — Mahatma Gandhi",
|
| 44 |
+
"Count your age by friends, not years. Count your life by smiles, not tears. — John Lennon"
|
| 45 |
+
],
|
| 46 |
+
"time": [
|
| 47 |
+
"The two most powerful warriors are patience and time. — Leo Tolstoy",
|
| 48 |
+
"Time is what we want most, but what we use worst. — William Penn",
|
| 49 |
+
"Yesterday is gone. Tomorrow has not yet come. We have only today. Let us begin. — Mother Teresa",
|
| 50 |
+
"Time you enjoy wasting is not wasted time. — Marthe Troly-Curtin",
|
| 51 |
+
"The future starts today, not tomorrow. — Pope John Paul II"
|
| 52 |
+
],
|
| 53 |
+
"friendship": [
|
| 54 |
+
"A real friend is one who walks in when the rest of the world walks out. — Walter Winchell",
|
| 55 |
+
"Friendship is born at that moment when one person says to another, 'What! You too? I thought I was the only one.' — C.S. Lewis",
|
| 56 |
+
"A friend is someone who knows all about you and still loves you. — Elbert Hubbard",
|
| 57 |
+
"A single rose can be my garden... a single friend, my world. — Leo Buscaglia",
|
| 58 |
+
"Friendship is the only cement that will ever hold the world together. — Woodrow Wilson"
|
| 59 |
+
],
|
| 60 |
+
"courage": [
|
| 61 |
+
"Courage is resistance to fear, mastery of fear, not absence of fear. — Mark Twain",
|
| 62 |
+
"It takes courage to grow up and become who you really are. — E.E. Cummings",
|
| 63 |
+
"Success is not final, failure is not fatal: It is the courage to continue that counts. — Winston Churchill",
|
| 64 |
+
"Courage is found in unlikely places. — J.R.R. Tolkien",
|
| 65 |
+
"You cannot swim for new horizons until you have courage to lose sight of the shore. ��� William Faulkner"
|
| 66 |
+
],
|
| 67 |
+
"change": [
|
| 68 |
+
"The only way to make sense out of change is to plunge into it, move with it, and join the dance. — Alan Watts",
|
| 69 |
+
"Change is the law of life. And those who look only to the past or present are certain to miss the future. — John F. Kennedy",
|
| 70 |
+
"Everyone thinks of changing the world, but no one thinks of changing himself. — Leo Tolstoy",
|
| 71 |
+
"Be the change that you wish to see in the world. — Mahatma Gandhi",
|
| 72 |
+
"Those who cannot change their minds cannot change anything. — George Bernard Shaw"
|
| 73 |
+
],
|
| 74 |
+
"education": [
|
| 75 |
+
"Education is the most powerful weapon which you can use to change the world. — Nelson Mandela",
|
| 76 |
+
"The function of education is to teach one to think intensively and to think critically. — Martin Luther King, Jr.",
|
| 77 |
+
"Education is not the filling of a pail, but the lighting of a fire. — W.B. Yeats",
|
| 78 |
+
"The roots of education are bitter, but the fruit is sweet. — Aristotle",
|
| 79 |
+
"An investment in knowledge pays the best interest. — Benjamin Franklin"
|
| 80 |
+
],
|
| 81 |
+
"faith": [
|
| 82 |
+
"Faith is taking the first step even when you don't see the whole staircase. — Martin Luther King, Jr.",
|
| 83 |
+
"Faith consists in believing when it is beyond the power of reason to believe. — Voltaire",
|
| 84 |
+
"Keep your faith in all beautiful things; in the sun when it is hidden, in the Spring when it is gone. — Roy R. Gilson",
|
| 85 |
+
"Faith is the bird that feels the light when the dawn is still dark. — Rabindranath Tagore",
|
| 86 |
+
"Faith is to believe what you do not see; the reward of this faith is to see what you believe. — Saint Augustine"
|
| 87 |
+
],
|
| 88 |
+
"hope": [
|
| 89 |
+
"Hope is being able to see that there is light despite all of the darkness. — Desmond Tutu",
|
| 90 |
+
"We must accept finite disappointment, but never lose infinite hope. — Martin Luther King, Jr.",
|
| 91 |
+
"Hope is the thing with feathers that perches in the soul. — Emily Dickinson",
|
| 92 |
+
"Where there is no vision, there is no hope. — George Washington Carver",
|
| 93 |
+
"A leader is a dealer in hope. — Napoleon Bonaparte"
|
| 94 |
+
],
|
| 95 |
+
"perseverance": [
|
| 96 |
+
"It does not matter how slowly you go as long as you do not stop. — Confucius",
|
| 97 |
+
"Perseverance is not a long race; it is many short races one after the other. — Walter Elliot",
|
| 98 |
+
"Many of life's failures are people who did not realize how close they were to success when they gave up. — Thomas Edison",
|
| 99 |
+
"Through perseverance many people win success out of what seemed destined to be certain failure. — Benjamin Disraeli",
|
| 100 |
+
"The difference between a successful person and others is not a lack of strength, not a lack of knowledge, but rather a lack in will. — Vince Lombardi"
|
| 101 |
+
]
|
| 102 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
# Normalize the topic (convert to lowercase)
|
| 105 |
+
topic_lower = topic.lower()
|
| 106 |
+
|
| 107 |
+
# Find the closest matching topic
|
| 108 |
+
if topic_lower in quotes_by_topic:
|
| 109 |
+
# Topic directly matches
|
| 110 |
+
relevant_quotes = quotes_by_topic[topic_lower]
|
| 111 |
+
else:
|
| 112 |
+
# Search for partial matches in topic keywords
|
| 113 |
+
matched_topics = []
|
| 114 |
+
for key in quotes_by_topic:
|
| 115 |
+
if key in topic_lower or topic_lower in key:
|
| 116 |
+
matched_topics.append(key)
|
| 117 |
+
|
| 118 |
+
if matched_topics:
|
| 119 |
+
# Use quotes from the first matching topic
|
| 120 |
+
relevant_quotes = quotes_by_topic[matched_topics[0]]
|
| 121 |
+
else:
|
| 122 |
+
# No match found - use a generic response
|
| 123 |
+
return f"No famous quotes found for '{topic}'. Try topics like: love, wisdom, success, happiness, time, friendship, courage, change, education, faith, hope, or perseverance."
|
| 124 |
+
|
| 125 |
+
# Return a random quote from the relevant ones
|
| 126 |
+
return random.choice(relevant_quotes)
|
| 127 |
|
| 128 |
+
# Setup agent
|
| 129 |
final_answer = FinalAnswerTool()
|
| 130 |
|
| 131 |
model = HfApiModel(
|
|
|
|
| 134 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 135 |
)
|
| 136 |
|
| 137 |
+
# Custom prompt templates for the quote generator agent
|
| 138 |
+
quote_prompt_templates = {
|
| 139 |
+
"react-system": """You are a helpful agent that provides famous quotes on various topics.
|
| 140 |
+
You have access to the following tools:
|
| 141 |
+
|
| 142 |
+
{tools_string}
|
| 143 |
+
|
| 144 |
+
When a user enters a topic, use the generate_famous_quote tool to find a relevant quote on that topic.
|
| 145 |
+
Return the quote to the user using the final_answer tool.
|
| 146 |
+
|
| 147 |
+
To use a tool, output an action in this format:
|
| 148 |
+
```py
|
| 149 |
+
action = tool_name(arg1=value1, arg2=value2, ...)
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
Always use Python syntax for calling tools. After using a tool, analyze the result to determine what to do next. Use final_answer to return your answer when complete.
|
| 153 |
+
""",
|
| 154 |
+
"react-human": "{input_string}",
|
| 155 |
+
"react-assistant": "{agent_response}",
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
# Load prompts.yaml if it exists, otherwise use our custom templates
|
| 159 |
+
try:
|
| 160 |
+
with open("prompts.yaml", 'r') as stream:
|
| 161 |
+
prompt_templates = yaml.safe_load(stream)
|
| 162 |
+
prompt_templates.update(quote_prompt_templates)
|
| 163 |
+
except:
|
| 164 |
+
prompt_templates = quote_prompt_templates
|
| 165 |
+
|
| 166 |
+
# Create the agent
|
| 167 |
agent = CodeAgent(
|
| 168 |
model=model,
|
| 169 |
+
tools=[final_answer, generate_famous_quote],
|
| 170 |
+
max_steps=3, # Reduced steps since this is a simple task
|
| 171 |
verbosity_level=1,
|
| 172 |
prompt_templates=prompt_templates
|
| 173 |
)
|
| 174 |
|
| 175 |
+
# Create a custom Gradio UI
|
| 176 |
+
class QuoteGeneratorUI(GradioUI):
|
| 177 |
+
def launch(self, **kwargs):
|
| 178 |
+
import gradio as gr
|
| 179 |
+
|
| 180 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 181 |
+
gr.Markdown("# Famous Quotes Generator")
|
| 182 |
+
gr.Markdown("Enter a topic to receive a famous quote related to it.")
|
| 183 |
+
|
| 184 |
+
with gr.Row():
|
| 185 |
+
with gr.Column():
|
| 186 |
+
topic_input = gr.Textbox(label="Enter a topic (e.g., love, wisdom, success)")
|
| 187 |
+
submit_btn = gr.Button("Get Quote", variant="primary")
|
| 188 |
+
|
| 189 |
+
with gr.Row():
|
| 190 |
+
with gr.Column():
|
| 191 |
+
quote_output = gr.Markdown(label="Your Quote")
|
| 192 |
+
|
| 193 |
+
def get_quote(topic):
|
| 194 |
+
if not topic or topic.strip() == "":
|
| 195 |
+
return "Please enter a topic to receive a quote."
|
| 196 |
+
|
| 197 |
+
try:
|
| 198 |
+
quote = generate_famous_quote(topic)
|
| 199 |
+
return f"### Quote on '{topic}':\n\n> {quote}"
|
| 200 |
+
except Exception as e:
|
| 201 |
+
return f"Error generating quote: {str(e)}"
|
| 202 |
+
|
| 203 |
+
submit_btn.click(get_quote, inputs=topic_input, outputs=quote_output)
|
| 204 |
+
|
| 205 |
+
demo.launch(**kwargs)
|
| 206 |
+
|
| 207 |
+
# Launch the custom UI
|
| 208 |
+
QuoteGeneratorUI(agent).launch()
|