Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Tiny model that fits in free tier memory
|
7 |
+
MODEL_NAME = "OFA-Sys/small-stable-diffusion-v0"
|
8 |
+
|
9 |
+
# Load model (will cache after first run)
|
10 |
+
@gr.cache()
|
11 |
+
def load_model():
|
12 |
+
return StableDiffusionPipeline.from_pretrained(
|
13 |
+
MODEL_NAME,
|
14 |
+
torch_dtype=torch.float16,
|
15 |
+
safety_checker=None
|
16 |
+
).to("cpu")
|
17 |
+
|
18 |
+
def generate_character(description, seed=42):
|
19 |
+
try:
|
20 |
+
pipe = load_model()
|
21 |
+
|
22 |
+
# Reduce memory usage
|
23 |
+
torch.manual_seed(seed)
|
24 |
+
with torch.inference_mode():
|
25 |
+
image = pipe(
|
26 |
+
prompt=f"pixel art character, {description}",
|
27 |
+
num_inference_steps=15, # Fewer steps = less memory
|
28 |
+
guidance_scale=7.0,
|
29 |
+
width=256, # Smaller resolution
|
30 |
+
height=256
|
31 |
+
).images[0]
|
32 |
+
|
33 |
+
return image
|
34 |
+
except Exception as e:
|
35 |
+
return f"Error: {str(e)}\nTry a simpler description or different words."
|
36 |
+
|
37 |
+
# Create simple animation effect by generating variations
|
38 |
+
def generate_animation(description, frames=3):
|
39 |
+
images = []
|
40 |
+
for i in range(frames):
|
41 |
+
img = generate_character(description, seed=i)
|
42 |
+
if isinstance(img, str): # If error returned
|
43 |
+
return img
|
44 |
+
images.append(img)
|
45 |
+
|
46 |
+
# Create simple animation (GIF)
|
47 |
+
images[0].save(
|
48 |
+
"animation.gif",
|
49 |
+
save_all=True,
|
50 |
+
append_images=images[1:],
|
51 |
+
duration=500,
|
52 |
+
loop=0
|
53 |
+
)
|
54 |
+
return "animation.gif"
|
55 |
+
|
56 |
+
# Minimal interface
|
57 |
+
with gr.Blocks(title="Tiny Character Animator") as demo:
|
58 |
+
gr.Markdown("""
|
59 |
+
# 🎮 Tiny Character Animator
|
60 |
+
*Free-tier optimized for Hugging Face Spaces*
|
61 |
+
""")
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
desc = gr.Textbox(
|
65 |
+
label="Describe your character",
|
66 |
+
placeholder="e.g., 'blue robot with antennae'",
|
67 |
+
max_lines=2
|
68 |
+
)
|
69 |
+
|
70 |
+
with gr.Row():
|
71 |
+
btn_still = gr.Button("Generate Still", variant="secondary")
|
72 |
+
btn_animate = gr.Button("Generate Animation", variant="primary")
|
73 |
+
|
74 |
+
with gr.Row():
|
75 |
+
output_still = gr.Image(label="Character", shape=(256, 256))
|
76 |
+
output_anim = gr.Image(label="Animation", format="gif", visible=False)
|
77 |
+
|
78 |
+
# Button actions
|
79 |
+
btn_still.click(
|
80 |
+
generate_character,
|
81 |
+
inputs=desc,
|
82 |
+
outputs=output_still
|
83 |
+
)
|
84 |
+
|
85 |
+
btn_animate.click(
|
86 |
+
lambda: (gr.Image(visible=False), gr.Image(visible=True)), # Toggle visibility
|
87 |
+
None,
|
88 |
+
[output_still, output_anim]
|
89 |
+
).then(
|
90 |
+
generate_animation,
|
91 |
+
inputs=desc,
|
92 |
+
outputs=output_anim
|
93 |
+
)
|
94 |
+
|
95 |
+
demo.launch(debug=False)
|