yukee1992 commited on
Commit
ca6bc6f
·
verified ·
1 Parent(s): c9c6c13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -29
app.py CHANGED
@@ -1,23 +1,37 @@
1
  # Import necessary libraries
2
  import torch
3
- from diffusers import StableDiffusionXLPipeline
4
  import gradio as gr
5
  import time
6
 
7
- # Check if we have a GPU (CUDA) or need to use the CPU
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
9
 
10
- # Load the Stable Diffusion XL pipeline
11
- # We use torch_dtype=torch.float16 for faster generation and less memory usage
12
- print("Loading Stable Diffusion XL pipeline... This may take a few minutes.")
13
- pipe = StableDiffusionXLPipeline.from_pretrained(
14
- "stabilityai/stable-diffusion-xl-base-1.0",
15
- torch_dtype=torch.float16,
16
- use_safetensors=True
17
- )
18
- # Move the pipeline to the chosen device (GPU or CPU)
19
- pipe = pipe.to(device)
20
- print("Model loaded successfully!")
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # Generate the image with some default parameters
31
- # guidance_scale controls how closely the image follows the prompt
 
32
  image = pipe(
33
  prompt=enhanced_prompt,
34
- guidance_scale=9.5,
35
- num_inference_steps=25 # More steps can mean higher quality, but is slower
36
- ).images[0] # We get the first (and only) image from the result
 
 
 
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, # The function to call
44
- inputs=gr.Textbox( # The input is a text box for the prompt
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"), # The output is an image
50
- title="Children's Book Illustrator 🤖🎨",
51
- description="Generate beautiful illustrations for your children's story. Enter a description of a scene."
52
  )
53
 
54
- # This is the key part for making the API work correctly with n8n.
55
- # We launch the Gradio app with a custom `api_name`.
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")