yukee1992 commited on
Commit
e77b7eb
·
verified ·
1 Parent(s): d3cd31d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -2,6 +2,8 @@
2
  import torch
3
  from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
4
  import gradio as gr
 
 
5
  import time
6
 
7
  # Force CPU usage
@@ -9,21 +11,20 @@ device = "cpu"
9
  print(f"Using device: {device}")
10
 
11
  # Load a smaller, CPU-friendly PUBLIC model
12
- # Using 'dreamlike-art/dreamlike-diffusion-1.0' - a public fine-tuned model
13
  model_id = "dreamlike-art/dreamlike-diffusion-1.0"
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
- safety_checker=None, # Disable safety checker to avoid NSFW filters that can cause issues
23
- requires_safety_checker=False # Newer versions of diffusers need this
24
  )
25
 
26
- # Optional: Use a faster scheduler for quicker generation
27
  pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
28
 
29
  # Move the pipeline to the CPU
@@ -32,7 +33,6 @@ try:
32
 
33
  except Exception as e:
34
  print(f"Error loading model: {e}")
35
- # Provide a more helpful error message
36
  raise e
37
 
38
  # Define the image generation function
@@ -40,21 +40,24 @@ def generate_image(prompt):
40
  """
41
  This function takes a text prompt and returns a generated image.
42
  """
43
- # Add a consistent style to all prompts to get a children's book look
44
  enhanced_prompt = f"children's book illustration, watercolor style, cute, whimsical, {prompt}"
45
-
46
  print(f"Generating image for prompt: {enhanced_prompt}")
47
 
48
- # Generate the image. Use a lower resolution for speed on CPU.
49
  image = pipe(
50
  prompt=enhanced_prompt,
51
- width=512, # Smaller image = faster generation
52
  height=512,
53
  guidance_scale=7.5,
54
- num_inference_steps=20, # Fewer steps = much faster
55
- generator=torch.Generator(device=device) # Ensure generator is on CPU
56
  ).images[0]
57
-
 
 
 
 
58
  print("Image generated successfully!")
59
  return image
60
 
@@ -71,5 +74,10 @@ demo = gr.Interface(
71
  description="This free version runs on CPU. It's slower but gets the job done! Enter a scene description."
72
  )
73
 
74
- # Launch the app
75
- demo.launch(debug=True, server_name="0.0.0.0")
 
 
 
 
 
 
2
  import torch
3
  from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
4
  import gradio as gr
5
+ from PIL import Image
6
+ import io
7
  import time
8
 
9
  # Force CPU usage
 
11
  print(f"Using device: {device}")
12
 
13
  # Load a smaller, CPU-friendly PUBLIC model
 
14
  model_id = "dreamlike-art/dreamlike-diffusion-1.0"
15
 
16
  print("Loading pipeline... This may take a few minutes.")
17
  try:
18
+ # Use torch.float32 for CPU compatibility
19
  pipe = StableDiffusionPipeline.from_pretrained(
20
  model_id,
21
+ torch_dtype=torch.float32,
22
  use_safetensors=True,
23
+ safety_checker=None,
24
+ requires_safety_checker=False
25
  )
26
 
27
+ # Use a faster scheduler for quicker generation
28
  pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
29
 
30
  # Move the pipeline to the CPU
 
33
 
34
  except Exception as e:
35
  print(f"Error loading model: {e}")
 
36
  raise e
37
 
38
  # Define the image generation function
 
40
  """
41
  This function takes a text prompt and returns a generated image.
42
  """
43
+ # Add a consistent style to all prompts
44
  enhanced_prompt = f"children's book illustration, watercolor style, cute, whimsical, {prompt}"
 
45
  print(f"Generating image for prompt: {enhanced_prompt}")
46
 
47
+ # Generate the image
48
  image = pipe(
49
  prompt=enhanced_prompt,
50
+ width=512,
51
  height=512,
52
  guidance_scale=7.5,
53
+ num_inference_steps=20,
54
+ generator=torch.Generator(device=device)
55
  ).images[0]
56
+
57
+ # Convert to RGB to ensure proper color format
58
+ if image.mode != 'RGB':
59
+ image = image.convert('RGB')
60
+
61
  print("Image generated successfully!")
62
  return image
63
 
 
74
  description="This free version runs on CPU. It's slower but gets the job done! Enter a scene description."
75
  )
76
 
77
+ # Launch the app with more robust settings
78
+ demo.launch(
79
+ debug=True,
80
+ server_name="0.0.0.0",
81
+ share=False,
82
+ enable_queue=True # This helps handle multiple requests properly
83
+ )