Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,239 +1,232 @@
|
|
| 1 |
-
from
|
| 2 |
-
|
| 3 |
-
import tempfile
|
| 4 |
-
import os
|
| 5 |
import requests
|
| 6 |
-
import
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# Draw content based on the theme
|
| 54 |
-
if theme == "Matzah":
|
| 55 |
-
# Matzah pattern
|
| 56 |
-
d.rectangle([(150, 180), (450, 380)], outline='black', width=2)
|
| 57 |
-
for i in range(160, 440, 25):
|
| 58 |
-
for j in range(190, 370, 25):
|
| 59 |
-
d.ellipse([(i, j), (i+5, j+5)], fill='black')
|
| 60 |
-
|
| 61 |
-
elif theme == "Seder Plate":
|
| 62 |
-
# Seder plate
|
| 63 |
-
d.ellipse([(150, 180), (450, 380)], outline='black', width=2)
|
| 64 |
-
# Divide into sections
|
| 65 |
-
d.line([(300, 180), (300, 380)], fill='black', width=2)
|
| 66 |
-
d.line([(150, 280), (450, 280)], fill='black', width=2)
|
| 67 |
-
d.ellipse([(275, 255), (325, 305)], outline='black', width=2)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
center_x, center_y = 300, 280
|
| 80 |
-
radius = 80
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
(center_x, center_y - radius),
|
| 85 |
-
(center_x + radius * 0.866, center_y + radius * 0.5),
|
| 86 |
-
(center_x - radius * 0.866, center_y + radius * 0.5),
|
| 87 |
-
(center_x, center_y - radius)
|
| 88 |
-
]
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
img.save(buffered, format="PNG")
|
| 104 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 105 |
-
|
| 106 |
-
# Create the response text
|
| 107 |
-
response = {
|
| 108 |
-
"age": age,
|
| 109 |
-
"theme": theme,
|
| 110 |
-
"description": description,
|
| 111 |
-
"complexity": complexity,
|
| 112 |
-
"image": img_str
|
| 113 |
-
}
|
| 114 |
-
|
| 115 |
-
return response
|
| 116 |
-
|
| 117 |
-
# Home page
|
| 118 |
-
@app.route('/', methods=['GET', 'POST'])
|
| 119 |
-
def home():
|
| 120 |
-
result = None
|
| 121 |
-
|
| 122 |
-
if request.method == 'POST':
|
| 123 |
-
try:
|
| 124 |
-
age = request.form.get('age')
|
| 125 |
-
if age:
|
| 126 |
-
result = generate_passover_coloring_page(age)
|
| 127 |
-
except Exception as e:
|
| 128 |
-
return render_template('index.html', error=str(e))
|
| 129 |
-
|
| 130 |
-
return render_template('index.html', result=result)
|
| 131 |
-
|
| 132 |
-
# Create temporary templates directory and files if they don't exist
|
| 133 |
-
def create_templates():
|
| 134 |
-
templates_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
|
| 135 |
-
if not os.path.exists(templates_dir):
|
| 136 |
-
os.makedirs(templates_dir)
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
<head>
|
| 143 |
-
<title>Passover Coloring Pages Generator</title>
|
| 144 |
-
<style>
|
| 145 |
-
body {
|
| 146 |
-
font-family: Arial, sans-serif;
|
| 147 |
-
max-width: 800px;
|
| 148 |
-
margin: 0 auto;
|
| 149 |
-
padding: 20px;
|
| 150 |
-
}
|
| 151 |
-
h1 {
|
| 152 |
-
color: #2c3e50;
|
| 153 |
-
}
|
| 154 |
-
.form-container {
|
| 155 |
-
margin: 20px 0;
|
| 156 |
-
padding: 15px;
|
| 157 |
-
border: 1px solid #ddd;
|
| 158 |
-
border-radius: 5px;
|
| 159 |
-
}
|
| 160 |
-
label {
|
| 161 |
-
display: block;
|
| 162 |
-
margin-bottom: 5px;
|
| 163 |
-
font-weight: bold;
|
| 164 |
-
}
|
| 165 |
-
input, button {
|
| 166 |
-
padding: 8px;
|
| 167 |
-
margin-bottom: 15px;
|
| 168 |
-
}
|
| 169 |
-
button {
|
| 170 |
-
background-color: #3498db;
|
| 171 |
-
color: white;
|
| 172 |
-
border: none;
|
| 173 |
-
cursor: pointer;
|
| 174 |
-
border-radius: 4px;
|
| 175 |
-
}
|
| 176 |
-
button:hover {
|
| 177 |
-
background-color: #2980b9;
|
| 178 |
-
}
|
| 179 |
-
.result-container {
|
| 180 |
-
margin-top: 20px;
|
| 181 |
-
padding: 15px;
|
| 182 |
-
border: 1px solid #ddd;
|
| 183 |
-
border-radius: 5px;
|
| 184 |
-
}
|
| 185 |
-
.image-container {
|
| 186 |
-
margin-top: 20px;
|
| 187 |
-
text-align: center;
|
| 188 |
-
}
|
| 189 |
-
.image-container img {
|
| 190 |
-
max-width: 100%;
|
| 191 |
-
border: 1px solid #ddd;
|
| 192 |
-
}
|
| 193 |
-
.error {
|
| 194 |
-
color: red;
|
| 195 |
-
font-weight: bold;
|
| 196 |
-
}
|
| 197 |
-
</style>
|
| 198 |
-
</head>
|
| 199 |
-
<body>
|
| 200 |
-
<h1>Passover Coloring Pages Generator</h1>
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
</form>
|
| 208 |
-
</div>
|
| 209 |
|
| 210 |
-
|
| 211 |
-
<div class="error">
|
| 212 |
-
{{ error }}
|
| 213 |
-
</div>
|
| 214 |
-
{% endif %}
|
| 215 |
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
-
<div class="
|
| 224 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
</div>
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
+
import datetime
|
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
+
import pytz
|
| 5 |
+
import yaml
|
| 6 |
+
import re
|
| 7 |
+
from tools.final_answer import FinalAnswerTool
|
| 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 create_new_vday_card_tool(theme: str) -> str:
|
| 13 |
+
# it's import to specify the return type
|
| 14 |
+
# Keep this format for the description / args / args description but feel free to modify the tool
|
| 15 |
+
"""A tool that generates a custom Valentine's Day card based on a given theme.
|
| 16 |
+
Args:
|
| 17 |
+
theme: A string representing the theme for the Valentine's Day card.
|
| 18 |
"""
|
| 19 |
try:
|
| 20 |
+
# Import tool from Hub
|
| 21 |
+
'''
|
| 22 |
+
image_generation_tool = Tool.from_space(
|
| 23 |
+
space_id="black-forest-labs/FLUX.1-schnell",
|
| 24 |
+
name="generate-image",
|
| 25 |
+
description="Generate image from prompt"
|
| 26 |
+
)
|
| 27 |
+
'''
|
| 28 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 29 |
+
image = image_generation_tool(theme)
|
| 30 |
+
return final_answer(image)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error creating card for theme '{theme}': {str(e)}"
|
| 33 |
+
|
| 34 |
+
# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 35 |
+
final_answer = FinalAnswerTool()
|
| 36 |
+
|
| 37 |
+
# Using Mistral-7B-Instruct-v0.1 instead of Qwen2.5-Coder-32B-Instruct
|
| 38 |
+
model = HfApiModel(
|
| 39 |
+
max_tokens=2096,
|
| 40 |
+
temperature=0.7, # Slightly higher temperature for Mistral as it's smaller
|
| 41 |
+
model_id='mistralai/Mistral-7B-Instruct-v0.1', # Changed to Mistral model
|
| 42 |
+
# custom_role_conversions can be adjusted for Mistral
|
| 43 |
+
custom_role_conversions={
|
| 44 |
+
"system": "<s>[INST] <<SYS>>\n{message}\n<</SYS>>\n\n",
|
| 45 |
+
"human": "{message} [/INST]",
|
| 46 |
+
"assistant": "{message} </s><s>[INST] "
|
| 47 |
+
},
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Let's add a fix for the Gradio issue by patching the launch method
|
| 51 |
+
def modified_launch():
|
| 52 |
+
try:
|
| 53 |
+
# Original code
|
| 54 |
+
with open("prompts.yaml", 'r') as stream:
|
| 55 |
+
prompt_templates = yaml.safe_load(stream)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
agent = CodeAgent(
|
| 58 |
+
model=model,
|
| 59 |
+
tools=[final_answer, create_new_vday_card_tool], # add your tools here (don't remove final answer)
|
| 60 |
+
max_steps=6,
|
| 61 |
+
verbosity_level=1,
|
| 62 |
+
grammar=None,
|
| 63 |
+
planning_interval=None,
|
| 64 |
+
name=None,
|
| 65 |
+
description=None,
|
| 66 |
+
prompt_templates=prompt_templates
|
| 67 |
+
)
|
| 68 |
|
| 69 |
+
# Get the Gradio UI instance but don't launch yet
|
| 70 |
+
ui = GradioUI(agent)
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# Patch the launch method to avoid API issues
|
| 73 |
+
original_launch = ui.launch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
def patched_launch(*args, **kwargs):
|
| 76 |
+
# Try to patch the problematic API processing in routes.py
|
| 77 |
+
try:
|
| 78 |
+
import types
|
| 79 |
+
from gradio import routes
|
| 80 |
+
|
| 81 |
+
# Create a dummy method that returns an empty dict to avoid the error
|
| 82 |
+
def dummy_api_info(self):
|
| 83 |
+
return {"endpoints": {}, "components": [], "dependencies": []}
|
| 84 |
+
|
| 85 |
+
# Try to patch FastAPI.api_info
|
| 86 |
+
try:
|
| 87 |
+
if hasattr(routes, 'FastAPI'):
|
| 88 |
+
routes.FastAPI.api_info = types.MethodType(dummy_api_info, routes.FastAPI)
|
| 89 |
+
except:
|
| 90 |
+
pass
|
| 91 |
+
|
| 92 |
+
except:
|
| 93 |
+
pass
|
| 94 |
+
|
| 95 |
+
# Call the original launch method
|
| 96 |
+
return original_launch(*args, **kwargs)
|
| 97 |
|
| 98 |
+
# Replace the launch method with our patched version
|
| 99 |
+
ui.launch = patched_launch
|
| 100 |
+
|
| 101 |
+
# Now launch the UI with our patched method
|
| 102 |
+
return ui.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
except Exception as e:
|
| 105 |
+
# If there's an error with Gradio, fall back to a Flask interface
|
| 106 |
+
print(f"Error with Gradio: {str(e)}")
|
| 107 |
+
print("Falling back to Flask interface...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
# Import Flask and create a simple interface
|
| 110 |
+
from flask import Flask, request, render_template_string, jsonify
|
| 111 |
+
import base64
|
| 112 |
+
from PIL import Image
|
| 113 |
+
from io import BytesIO
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
# Simple HTML template
|
| 118 |
+
html_template = """
|
| 119 |
+
<!DOCTYPE html>
|
| 120 |
+
<html>
|
| 121 |
+
<head>
|
| 122 |
+
<title>Valentine's Day Card Generator</title>
|
| 123 |
+
<style>
|
| 124 |
+
body {
|
| 125 |
+
font-family: Arial, sans-serif;
|
| 126 |
+
max-width: 800px;
|
| 127 |
+
margin: 0 auto;
|
| 128 |
+
padding: 20px;
|
| 129 |
+
}
|
| 130 |
+
h1 {
|
| 131 |
+
color: #e91e63;
|
| 132 |
+
}
|
| 133 |
+
.form-container {
|
| 134 |
+
margin: 20px 0;
|
| 135 |
+
padding: 15px;
|
| 136 |
+
border: 1px solid #ffcccc;
|
| 137 |
+
border-radius: 5px;
|
| 138 |
+
background-color: #fff0f5;
|
| 139 |
+
}
|
| 140 |
+
label {
|
| 141 |
+
display: block;
|
| 142 |
+
margin-bottom: 5px;
|
| 143 |
+
font-weight: bold;
|
| 144 |
+
}
|
| 145 |
+
input {
|
| 146 |
+
width: 100%;
|
| 147 |
+
padding: 8px;
|
| 148 |
+
margin-bottom: 15px;
|
| 149 |
+
border: 1px solid #ccc;
|
| 150 |
+
border-radius: 4px;
|
| 151 |
+
}
|
| 152 |
+
button {
|
| 153 |
+
background-color: #e91e63;
|
| 154 |
+
color: white;
|
| 155 |
+
border: none;
|
| 156 |
+
padding: 10px 15px;
|
| 157 |
+
cursor: pointer;
|
| 158 |
+
border-radius: 4px;
|
| 159 |
+
}
|
| 160 |
+
button:hover {
|
| 161 |
+
background-color: #c2185b;
|
| 162 |
+
}
|
| 163 |
+
.result-container {
|
| 164 |
+
margin-top: 20px;
|
| 165 |
+
text-align: center;
|
| 166 |
+
}
|
| 167 |
+
.card-image {
|
| 168 |
+
max-width: 100%;
|
| 169 |
+
border: 10px solid #fff;
|
| 170 |
+
box-shadow: 0 0 10px rgba(0,0,0,0.2);
|
| 171 |
+
}
|
| 172 |
+
</style>
|
| 173 |
+
</head>
|
| 174 |
+
<body>
|
| 175 |
+
<h1>Valentine's Day Card Generator</h1>
|
| 176 |
+
<p>Enter a theme to generate a custom Valentine's Day card.</p>
|
| 177 |
|
| 178 |
+
<div class="form-container">
|
| 179 |
+
<form method="POST">
|
| 180 |
+
<label for="theme">Card Theme:</label>
|
| 181 |
+
<input type="text" id="theme" name="theme" placeholder="e.g., romantic sunset, cute animals, etc." required>
|
| 182 |
+
<button type="submit">Generate Card</button>
|
| 183 |
+
</form>
|
| 184 |
</div>
|
| 185 |
|
| 186 |
+
{% if error %}
|
| 187 |
+
<div style="color: red; margin-top: 20px;">
|
| 188 |
+
<p>{{ error }}</p>
|
| 189 |
+
</div>
|
| 190 |
+
{% endif %}
|
| 191 |
+
|
| 192 |
+
{% if image_data %}
|
| 193 |
+
<div class="result-container">
|
| 194 |
+
<h2>Your Valentine's Day Card</h2>
|
| 195 |
+
<p>Theme: {{ theme }}</p>
|
| 196 |
+
<img src="data:image/png;base64,{{ image_data }}" alt="Valentine's Day Card" class="card-image">
|
| 197 |
+
</div>
|
| 198 |
+
{% endif %}
|
| 199 |
+
</body>
|
| 200 |
+
</html>
|
| 201 |
+
"""
|
| 202 |
+
|
| 203 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 204 |
+
def home():
|
| 205 |
+
if request.method == 'POST':
|
| 206 |
+
theme = request.form.get('theme', '')
|
| 207 |
+
if theme:
|
| 208 |
+
try:
|
| 209 |
+
# Add Valentine's context to the theme
|
| 210 |
+
valentine_theme = f"Valentine's Day card with theme: {theme}, romantic, love, hearts"
|
| 211 |
+
|
| 212 |
+
# Use the agent to generate the card
|
| 213 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 214 |
+
image = image_generation_tool(valentine_theme)
|
| 215 |
+
|
| 216 |
+
# Convert PIL Image to base64 string
|
| 217 |
+
buffered = BytesIO()
|
| 218 |
+
image.save(buffered, format="PNG")
|
| 219 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 220 |
+
|
| 221 |
+
return render_template_string(html_template, image_data=img_str, theme=theme)
|
| 222 |
+
except Exception as e:
|
| 223 |
+
return render_template_string(html_template, error=f"Error: {str(e)}")
|
| 224 |
+
|
| 225 |
+
return render_template_string(html_template)
|
| 226 |
+
|
| 227 |
+
# Run the Flask app
|
| 228 |
+
return app.run(debug=True, host='0.0.0.0', port=8080)
|
| 229 |
|
| 230 |
+
# Run the modified launch function
|
| 231 |
+
if __name__ == "__main__":
|
| 232 |
+
modified_launch()
|