Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,166 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Create a simple
|
| 7 |
with gr.Blocks() as demo:
|
| 8 |
-
gr.Markdown("#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
print("Test successful! API info can be generated without errors.")
|
| 21 |
-
except Exception as e:
|
| 22 |
-
print(f"Error when trying to get API info: {type(e).__name__}: {str(e)}")
|
| 23 |
-
print("This is likely where your original error is coming from.")
|
| 24 |
-
|
| 25 |
-
# Try to pinpoint the exact schema processing issue
|
| 26 |
-
try:
|
| 27 |
-
from gradio.blocks import Blocks
|
| 28 |
-
from gradio_client import utils as client_utils
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
try:
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
except Exception as deep_err:
|
| 47 |
-
print(f"Error during deeper diagnosis: {type(deep_err).__name__}: {str(deep_err)}")
|
| 48 |
-
|
| 49 |
-
# Don't launch the app, we just want to test the API info generation
|
| 50 |
-
print("\nGradio version:", gr.__version__)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageDraw
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
def generate_passover_coloring_page(age):
|
| 7 |
+
"""
|
| 8 |
+
Generates a simple Passover-themed coloring page based on age.
|
| 9 |
+
"""
|
| 10 |
+
try:
|
| 11 |
+
age = int(float(age))
|
| 12 |
+
if not (1 <= age <= 12):
|
| 13 |
+
return "Please enter a valid age between 1 and 12.", None
|
| 14 |
+
except (ValueError, TypeError):
|
| 15 |
+
return "Please enter a valid number for age.", None
|
| 16 |
+
|
| 17 |
+
# Define complexity based on age
|
| 18 |
+
if 1 <= age <= 3:
|
| 19 |
+
complexity = "simple"
|
| 20 |
+
elif 4 <= age <= 6:
|
| 21 |
+
complexity = "basic"
|
| 22 |
+
elif 7 <= age <= 9:
|
| 23 |
+
complexity = "intermediate"
|
| 24 |
+
else:
|
| 25 |
+
complexity = "advanced"
|
| 26 |
+
|
| 27 |
+
# Passover themes based on complexity
|
| 28 |
+
themes = {
|
| 29 |
+
"simple": [
|
| 30 |
+
"Matzah", "Seder Plate", "Baby Moses", "Frog (Plague)"
|
| 31 |
+
],
|
| 32 |
+
"basic": [
|
| 33 |
+
"Parting of the Red Sea", "Ten Plagues", "Four Questions", "Afikoman Hunt"
|
| 34 |
+
],
|
| 35 |
+
"intermediate": [
|
| 36 |
+
"Seder Table", "Exodus Story", "Pesach in Hebrew", "Egyptian Pyramids"
|
| 37 |
+
],
|
| 38 |
+
"advanced": [
|
| 39 |
+
"Haggadah Illustrations", "Passover Symbols", "Story Panels", "Hebrew Calligraphy"
|
| 40 |
+
]
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
selected_theme = themes[complexity][0] # Just use the first theme for simplicity
|
| 44 |
+
|
| 45 |
+
# Create a simple coloring page
|
| 46 |
+
img = Image.new('RGB', (500, 400), color='white')
|
| 47 |
+
d = ImageDraw.Draw(img)
|
| 48 |
+
|
| 49 |
+
# Draw a border
|
| 50 |
+
d.rectangle([(20, 20), (480, 380)], outline='black', width=3)
|
| 51 |
+
|
| 52 |
+
# Add title
|
| 53 |
+
d.text((50, 40), f"Passover Coloring Page", fill='black')
|
| 54 |
+
d.text((50, 70), f"Theme: {selected_theme}", fill='black')
|
| 55 |
+
d.text((50, 100), f"For age: {age} (Complexity: {complexity})", fill='black')
|
| 56 |
+
|
| 57 |
+
# Draw a simple shape based on the theme
|
| 58 |
+
if "Matzah" in selected_theme:
|
| 59 |
+
# Simple rectangle with dots for matzah
|
| 60 |
+
d.rectangle([(150, 150), (350, 250)], outline='black', width=2)
|
| 61 |
+
for i in range(160, 340, 20):
|
| 62 |
+
for j in range(160, 240, 20):
|
| 63 |
+
d.ellipse([(i, j), (i+5, j+5)], fill='black')
|
| 64 |
+
|
| 65 |
+
elif "Seder" in selected_theme:
|
| 66 |
+
# Simple circle for seder plate
|
| 67 |
+
d.ellipse([(150, 150), (350, 250)], outline='black', width=2)
|
| 68 |
+
# Divide into sections
|
| 69 |
+
d.line([(250, 150), (250, 250)], fill='black', width=2)
|
| 70 |
+
d.line([(150, 200), (350, 200)], fill='black', width=2)
|
| 71 |
+
|
| 72 |
+
elif "Moses" in selected_theme or "Exodus" in selected_theme:
|
| 73 |
+
# Simple boat/basket shape
|
| 74 |
+
d.arc([(150, 150), (350, 250)], 0, 180, fill='black', width=2)
|
| 75 |
+
d.line([(150, 200), (350, 200)], fill='black', width=2)
|
| 76 |
+
|
| 77 |
+
elif "Plague" in selected_theme or "Plagues" in selected_theme:
|
| 78 |
+
# Simple frog outline
|
| 79 |
+
d.ellipse([(180, 170), (250, 220)], outline='black', width=2) # Body
|
| 80 |
+
d.ellipse([(250, 170), (320, 220)], outline='black', width=2) # Body
|
| 81 |
+
d.ellipse([(200, 150), (230, 170)], outline='black', width=2) # Eye
|
| 82 |
+
d.ellipse([(300, 150), (330, 170)], outline='black', width=2) # Eye
|
| 83 |
+
|
| 84 |
+
else:
|
| 85 |
+
# Generic Star of David
|
| 86 |
+
points = [
|
| 87 |
+
(250, 150), (300, 200), (250, 250),
|
| 88 |
+
(200, 200), (250, 150), (250, 250)
|
| 89 |
+
]
|
| 90 |
+
d.line(points, fill='black', width=2)
|
| 91 |
+
points2 = [(200, 200), (300, 200)]
|
| 92 |
+
d.line(points2, fill='black', width=2)
|
| 93 |
+
|
| 94 |
+
# Save to a temporary file
|
| 95 |
+
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_file:
|
| 96 |
+
temp_path = temp_file.name
|
| 97 |
+
img.save(temp_path)
|
| 98 |
+
|
| 99 |
+
# Create the response text
|
| 100 |
+
response = f"## Passover Coloring Page for a {age}-year-old\n\n"
|
| 101 |
+
response += f"I've created a {complexity} level coloring page with the theme: **{selected_theme}**\n\n"
|
| 102 |
+
response += "Happy Passover! Print this page for coloring."
|
| 103 |
+
|
| 104 |
+
return response, temp_path
|
| 105 |
|
| 106 |
+
# Create a simple UI that avoids the API issue
|
| 107 |
with gr.Blocks() as demo:
|
| 108 |
+
gr.Markdown("# Passover Coloring Pages Generator")
|
| 109 |
+
|
| 110 |
+
with gr.Row():
|
| 111 |
+
with gr.Column():
|
| 112 |
+
age_input = gr.Slider(
|
| 113 |
+
minimum=1,
|
| 114 |
+
maximum=12,
|
| 115 |
+
step=1,
|
| 116 |
+
value=6,
|
| 117 |
+
label="Child's Age"
|
| 118 |
+
)
|
| 119 |
+
generate_btn = gr.Button("Generate Coloring Page")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
with gr.Row():
|
| 122 |
+
with gr.Column():
|
| 123 |
+
output_text = gr.Markdown()
|
| 124 |
+
|
| 125 |
+
with gr.Row():
|
| 126 |
+
output_image = gr.Image(label="Coloring Page")
|
| 127 |
+
|
| 128 |
+
generate_btn.click(
|
| 129 |
+
generate_passover_coloring_page,
|
| 130 |
+
inputs=age_input,
|
| 131 |
+
outputs=[output_text, output_image]
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# Launch with a specific server port to avoid potential conflicts
|
| 135 |
+
if __name__ == "__main__":
|
| 136 |
+
# The magic line that avoids API processing issues:
|
| 137 |
+
# Override the app.get_blocks().get_api_info() method that's causing the error
|
| 138 |
+
original_launch = demo.launch
|
| 139 |
+
|
| 140 |
+
def patched_launch(*args, **kwargs):
|
| 141 |
+
# Skip the problematic API processing in routes.py
|
| 142 |
+
try:
|
| 143 |
+
import types
|
| 144 |
+
from gradio import routes
|
| 145 |
+
|
| 146 |
+
# Create a dummy method that returns an empty dict to avoid the error
|
| 147 |
+
def dummy_api_info(self):
|
| 148 |
+
return {"endpoints": {}, "components": [], "dependencies": []}
|
| 149 |
+
|
| 150 |
+
# Try to patch FastAPI.api_info
|
| 151 |
try:
|
| 152 |
+
if hasattr(routes, 'FastAPI'):
|
| 153 |
+
original_api_info = routes.FastAPI.api_info
|
| 154 |
+
routes.FastAPI.api_info = types.MethodType(dummy_api_info, routes.FastAPI)
|
| 155 |
+
except:
|
| 156 |
+
pass
|
| 157 |
|
| 158 |
+
except:
|
| 159 |
+
pass
|
| 160 |
+
|
| 161 |
+
# Call the original launch method
|
| 162 |
+
return original_launch(*args, **kwargs)
|
| 163 |
+
|
| 164 |
+
# Replace the launch method with our patched version
|
| 165 |
+
demo.launch = patched_launch
|
| 166 |
+
demo.launch(share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|