Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from diffusers import StableDiffusionPipeline
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
@gr.cache()
|
| 10 |
def load_model():
|
| 11 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 12 |
-
|
| 13 |
torch_dtype=torch.float16,
|
| 14 |
safety_checker=None,
|
| 15 |
use_safetensors=True
|
| 16 |
)
|
| 17 |
-
pipe.scheduler = DPMSolverSinglestepScheduler.from_config(pipe.scheduler.config)
|
| 18 |
pipe = pipe.to("cpu")
|
| 19 |
-
pipe.enable_attention_slicing() # Reduces memory
|
| 20 |
-
pipe.enable_model_cpu_offload() # Only loads needed components
|
| 21 |
return pipe
|
| 22 |
|
| 23 |
def generate_character(prompt, seed=42):
|
|
@@ -25,66 +23,36 @@ def generate_character(prompt, seed=42):
|
|
| 25 |
pipe = load_model()
|
| 26 |
generator = torch.Generator(device="cpu").manual_seed(seed)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
generator=generator
|
| 37 |
-
).images[0]
|
| 38 |
|
| 39 |
return image
|
| 40 |
except Exception as e:
|
| 41 |
-
return f"Error: {str(e)}\nTry
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
base_image = generate_character(prompt)
|
| 46 |
-
if isinstance(base_image, str): # If error
|
| 47 |
-
return base_image
|
| 48 |
-
|
| 49 |
-
images = [base_image]
|
| 50 |
-
pipe = load_model()
|
| 51 |
-
|
| 52 |
-
for i in range(1, frames):
|
| 53 |
-
result = pipe(
|
| 54 |
-
prompt=prompt,
|
| 55 |
-
image=images[-1],
|
| 56 |
-
strength=0.3, # Small changes per frame
|
| 57 |
-
generator=torch.Generator().manual_seed(i)
|
| 58 |
-
)
|
| 59 |
-
images.append(result.images[0])
|
| 60 |
-
|
| 61 |
-
images[0].save(
|
| 62 |
-
"animation.gif",
|
| 63 |
-
save_all=True,
|
| 64 |
-
append_images=images[1:],
|
| 65 |
-
duration=500,
|
| 66 |
-
loop=0
|
| 67 |
-
)
|
| 68 |
-
return "animation.gif"
|
| 69 |
-
|
| 70 |
-
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 71 |
-
gr.Markdown("# 🎬 Character Animator (12GB Optimized)")
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
prompt = gr.Textbox(
|
| 75 |
-
label="
|
| 76 |
-
placeholder="e.g. '
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
gen_btn = gr.Button("Generate")
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
anim_btn.click(generate_animation, inputs=prompt, outputs=anim_out)
|
| 89 |
|
| 90 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Use a smaller SD model variant that fits within free tier
|
| 7 |
+
MODEL_ID = "CompVis/ldm-super-resolution-4x-openimages" # Only ~1.4GB
|
| 8 |
|
| 9 |
@gr.cache()
|
| 10 |
def load_model():
|
| 11 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 12 |
+
MODEL_ID,
|
| 13 |
torch_dtype=torch.float16,
|
| 14 |
safety_checker=None,
|
| 15 |
use_safetensors=True
|
| 16 |
)
|
|
|
|
| 17 |
pipe = pipe.to("cpu")
|
| 18 |
+
pipe.enable_attention_slicing() # Reduces memory usage
|
|
|
|
| 19 |
return pipe
|
| 20 |
|
| 21 |
def generate_character(prompt, seed=42):
|
|
|
|
| 23 |
pipe = load_model()
|
| 24 |
generator = torch.Generator(device="cpu").manual_seed(seed)
|
| 25 |
|
| 26 |
+
image = pipe(
|
| 27 |
+
prompt=f"pixel art {prompt}, clean lines, vibrant colors",
|
| 28 |
+
num_inference_steps=20,
|
| 29 |
+
guidance_scale=7.0,
|
| 30 |
+
width=256,
|
| 31 |
+
height=256,
|
| 32 |
+
generator=generator
|
| 33 |
+
).images[0]
|
|
|
|
|
|
|
| 34 |
|
| 35 |
return image
|
| 36 |
except Exception as e:
|
| 37 |
+
return f"Error: {str(e)}\nTry a simpler prompt."
|
| 38 |
|
| 39 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
| 40 |
+
gr.Markdown("# 🎮 Lightweight Character Generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
with gr.Row():
|
| 43 |
prompt = gr.Textbox(
|
| 44 |
+
label="Describe your character",
|
| 45 |
+
placeholder="e.g. 'robot pirate with laser eye'",
|
| 46 |
+
max_lines=2
|
| 47 |
)
|
| 48 |
|
| 49 |
+
generate_btn = gr.Button("Generate", variant="primary")
|
| 50 |
+
output = gr.Image(label="Your Character", type="pil")
|
|
|
|
| 51 |
|
| 52 |
+
generate_btn.click(
|
| 53 |
+
generate_character,
|
| 54 |
+
inputs=prompt,
|
| 55 |
+
outputs=output
|
| 56 |
+
)
|
|
|
|
| 57 |
|
| 58 |
+
demo.launch(debug=False)
|