yougandar commited on
Commit
fa431aa
·
verified ·
1 Parent(s): dd9a069

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -2,30 +2,29 @@ import gradio as gr
2
  from diffusers import StableDiffusionPipeline
3
  import torch
4
 
5
- # Load model and specify device (GPU if available, otherwise CPU)
6
  device = "cuda" if torch.cuda.is_available() else "cpu"
7
 
8
- # Load the pipeline with accelerate support for memory optimization
9
  pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2")
10
  pipe = pipe.to(device)
11
 
12
  def generate_comic_images(story_text):
13
- # Split the story into sentences
14
- scenes = story_text.split(".")
15
 
16
- # Generate an image for each scene using the pipeline
17
  comic_images = []
18
  for scene in scenes:
19
- scene = scene.strip()
20
  if scene:
21
  print(f"Generating image for scene: {scene}")
22
  try:
23
- # Lower number of steps for faster generation
24
  image = pipe(scene, num_inference_steps=20, height=512, width=512).images[0]
 
25
  comic_images.append(image)
26
  except Exception as e:
27
  print(f"Error generating image for '{scene}': {e}")
28
- comic_images.append(f"Error generating image for '{scene}'")
29
 
30
  return comic_images
31
 
@@ -42,9 +41,10 @@ with gr.Blocks() as demo:
42
 
43
  generate_button = gr.Button("Generate Comic")
44
 
 
45
  comic_output = gr.Gallery(label="Generated Comic", show_label=True, elem_id="comic-gallery")
46
 
47
  generate_button.click(fn=comic_generation_interface, inputs=story_input, outputs=comic_output)
48
 
49
- # Launch the app
50
  demo.launch()
 
2
  from diffusers import StableDiffusionPipeline
3
  import torch
4
 
5
+ # Load the model and specify device (GPU if available)
6
  device = "cuda" if torch.cuda.is_available() else "cpu"
7
 
8
+ # Load the pipeline
9
  pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2")
10
  pipe = pipe.to(device)
11
 
12
  def generate_comic_images(story_text):
13
+ # Split the story into sentences (scenes)
14
+ scenes = [scene.strip() for scene in story_text.split(".") if scene.strip()]
15
 
 
16
  comic_images = []
17
  for scene in scenes:
 
18
  if scene:
19
  print(f"Generating image for scene: {scene}")
20
  try:
21
+ # Generate the image for each scene
22
  image = pipe(scene, num_inference_steps=20, height=512, width=512).images[0]
23
+ # Convert the image to a format Gradio can handle (like PIL or numpy array)
24
  comic_images.append(image)
25
  except Exception as e:
26
  print(f"Error generating image for '{scene}': {e}")
27
+ comic_images.append(None)
28
 
29
  return comic_images
30
 
 
41
 
42
  generate_button = gr.Button("Generate Comic")
43
 
44
+ # Gallery component to display generated images
45
  comic_output = gr.Gallery(label="Generated Comic", show_label=True, elem_id="comic-gallery")
46
 
47
  generate_button.click(fn=comic_generation_interface, inputs=story_input, outputs=comic_output)
48
 
49
+ # Launch the Gradio app
50
  demo.launch()