geethareddy commited on
Commit
52844a5
·
verified ·
1 Parent(s): c41b0a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -28,12 +28,22 @@ def generate_comic_strip(story):
28
  # Split the story into sentences to identify key scenes
29
  scenes = split_into_sentences(story)
30
 
 
 
 
 
31
  comic_strip = []
32
 
33
  for idx, scene in enumerate(scenes):
34
  try:
35
- # Generate image for each scene
36
- image = pipe(scene, num_inference_steps=50).images[0]
 
 
 
 
 
 
37
  comic_strip.append(image)
38
  except Exception as e:
39
  # In case of any error during image generation, append a placeholder image
@@ -73,7 +83,7 @@ def main():
73
  ["An astronaut lands on a distant planet and encounters alien life forms."]
74
  ],
75
  allow_flagging="never", # Disable flagging of outputs
76
- theme="default" # You can choose other themes like "huggingface"
77
  )
78
 
79
  # Launch the Gradio app
@@ -83,10 +93,13 @@ def main():
83
  pipe = None
84
  try:
85
  print("Loading Stable Diffusion model...")
86
- # Initialize the Stable Diffusion pipeline
87
  pipe = StableDiffusionPipeline.from_pretrained(
88
  "stabilityai/stable-diffusion-2-1-base", # Model name
89
  torch_dtype=torch.float32, # Use float32 for CPU
 
 
 
90
  )
91
  pipe = pipe.to("cpu") # Move the model to CPU
92
  pipe.enable_attention_slicing() # Reduce memory usage
 
28
  # Split the story into sentences to identify key scenes
29
  scenes = split_into_sentences(story)
30
 
31
+ # Limit the number of scenes to prevent excessive image generation
32
+ max_scenes = 3
33
+ scenes = scenes[:max_scenes]
34
+
35
  comic_strip = []
36
 
37
  for idx, scene in enumerate(scenes):
38
  try:
39
+ # Generate image for each scene with optimizations
40
+ image = pipe(
41
+ scene,
42
+ num_inference_steps=20, # Reduced steps for faster generation
43
+ height=256, # Reduced resolution
44
+ width=256, # Reduced resolution
45
+ guidance_scale=7.5, # Default guidance scale
46
+ ).images[0]
47
  comic_strip.append(image)
48
  except Exception as e:
49
  # In case of any error during image generation, append a placeholder image
 
83
  ["An astronaut lands on a distant planet and encounters alien life forms."]
84
  ],
85
  allow_flagging="never", # Disable flagging of outputs
86
+ theme="default", # You can choose other themes like "huggingface"
87
  )
88
 
89
  # Launch the Gradio app
 
93
  pipe = None
94
  try:
95
  print("Loading Stable Diffusion model...")
96
+ # Initialize the Stable Diffusion pipeline with optimizations
97
  pipe = StableDiffusionPipeline.from_pretrained(
98
  "stabilityai/stable-diffusion-2-1-base", # Model name
99
  torch_dtype=torch.float32, # Use float32 for CPU
100
+ low_cpu_mem_usage=True, # Optimize for CPU usage
101
+ safety_checker=None, # Disable safety checker to speed up loading
102
+ force_download=True # Force download to avoid resume_download warning
103
  )
104
  pipe = pipe.to("cpu") # Move the model to CPU
105
  pipe.enable_attention_slicing() # Reduce memory usage