Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,37 @@
|
|
| 1 |
# Import necessary libraries
|
| 2 |
import torch
|
| 3 |
-
from diffusers import
|
| 4 |
import gradio as gr
|
| 5 |
import time
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
device = "
|
|
|
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Define the image generation function
|
| 23 |
def generate_image(prompt):
|
|
@@ -27,31 +41,33 @@ def generate_image(prompt):
|
|
| 27 |
# Add a consistent style to all prompts to get a children's book look
|
| 28 |
enhanced_prompt = f"children's book illustration, watercolor style, cute, whimsical, {prompt}"
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
image = pipe(
|
| 33 |
prompt=enhanced_prompt,
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
| 38 |
return image
|
| 39 |
|
| 40 |
# Create the Gradio Interface
|
| 41 |
-
# We are creating a simple one-input (prompt) one-output (image) interface
|
| 42 |
demo = gr.Interface(
|
| 43 |
-
fn=generate_image,
|
| 44 |
-
inputs=gr.Textbox(
|
| 45 |
label="Enter your scene description",
|
| 46 |
lines=2,
|
| 47 |
placeholder="A brave little mouse exploring a giant forest..."
|
| 48 |
),
|
| 49 |
-
outputs=gr.Image(label="Generated Illustration", type="pil"),
|
| 50 |
-
title="Children's Book Illustrator 🤖🎨",
|
| 51 |
-
description="
|
| 52 |
)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
# Setting `api_name="generate"` creates an API endpoint at `/api/generate/`
|
| 57 |
-
demo.launch(debug=True, server_name="0.0.0.0", api_name="generate")
|
|
|
|
| 1 |
# Import necessary libraries
|
| 2 |
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
|
| 4 |
import gradio as gr
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
# Force CPU usage
|
| 8 |
+
device = "cpu"
|
| 9 |
+
print(f"Using device: {device}")
|
| 10 |
|
| 11 |
+
# Load a smaller, CPU-friendly model
|
| 12 |
+
# Using 'Oppenheimer/DALL-E_1.5' - a fine-tuned SD 1.5 model known for good results
|
| 13 |
+
model_id = "Oppenheimer/DALL-E_1.5"
|
| 14 |
+
|
| 15 |
+
print("Loading pipeline... This may take a few minutes.")
|
| 16 |
+
try:
|
| 17 |
+
# Use torch.float32 for CPU compatibility. DO NOT use float16.
|
| 18 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 19 |
+
model_id,
|
| 20 |
+
torch_dtype=torch.float32, # Critical for CPU
|
| 21 |
+
use_safetensors=True
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Optional: Use a faster scheduler for quicker generation
|
| 25 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 26 |
+
|
| 27 |
+
# Move the pipeline to the CPU
|
| 28 |
+
pipe = pipe.to(device)
|
| 29 |
+
print("Model loaded successfully on CPU!")
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Error loading model: {e}")
|
| 33 |
+
# Provide a more helpful error message
|
| 34 |
+
raise e
|
| 35 |
|
| 36 |
# Define the image generation function
|
| 37 |
def generate_image(prompt):
|
|
|
|
| 41 |
# Add a consistent style to all prompts to get a children's book look
|
| 42 |
enhanced_prompt = f"children's book illustration, watercolor style, cute, whimsical, {prompt}"
|
| 43 |
|
| 44 |
+
print(f"Generating image for prompt: {enhanced_prompt}")
|
| 45 |
+
|
| 46 |
+
# Generate the image. Use a lower resolution for speed on CPU.
|
| 47 |
image = pipe(
|
| 48 |
prompt=enhanced_prompt,
|
| 49 |
+
width=512, # Smaller image = faster generation
|
| 50 |
+
height=512,
|
| 51 |
+
guidance_scale=7.5,
|
| 52 |
+
num_inference_steps=20, # Fewer steps = much faster
|
| 53 |
+
generator=torch.Generator(device=device) # Ensure generator is on CPU
|
| 54 |
+
).images[0]
|
| 55 |
|
| 56 |
+
print("Image generated successfully!")
|
| 57 |
return image
|
| 58 |
|
| 59 |
# Create the Gradio Interface
|
|
|
|
| 60 |
demo = gr.Interface(
|
| 61 |
+
fn=generate_image,
|
| 62 |
+
inputs=gr.Textbox(
|
| 63 |
label="Enter your scene description",
|
| 64 |
lines=2,
|
| 65 |
placeholder="A brave little mouse exploring a giant forest..."
|
| 66 |
),
|
| 67 |
+
outputs=gr.Image(label="Generated Illustration", type="pil"),
|
| 68 |
+
title="Children's Book Illustrator (CPU Edition) 🤖🎨",
|
| 69 |
+
description="This free version runs on CPU. It's slower but gets the job done! Enter a scene description."
|
| 70 |
)
|
| 71 |
|
| 72 |
+
# Launch the app
|
| 73 |
+
demo.launch(debug=True, server_name="0.0.0.0")
|
|
|
|
|
|