SailajaS commited on
Commit
21dd73a
·
verified ·
1 Parent(s): 1dad66e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -23
app.py CHANGED
@@ -1,47 +1,61 @@
 
1
  import gradio as gr
2
- from diffusers import StableDiffusionPipeline
3
- import torch
4
  from PIL import Image
 
5
 
6
- # Initialize the Stable Diffusion pipeline (optimized for CPU)
7
- pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float32)
8
- pipe = pipe.to("cpu") # Move model to CPU
9
 
10
- # Function to split the story into key scenes
11
- def split_story_into_scenes(story):
12
- # Split the story into sentences (scenes) for generating images
13
- scenes = story.split(". ") # Splitting based on period and space
14
- return [scene for scene in scenes if scene] # Remove empty scenes
15
 
16
- # Function to generate images for each scene using Stable Diffusion
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def generate_images(scenes):
18
  images = []
19
  for scene in scenes:
20
- # Generate image for each scene (text prompt)
21
  print(f"Generating image for scene: {scene}")
22
- image = pipe(scene).images[0] # Get the first generated image
23
- images.append(image)
 
24
  return images
25
 
26
  # Function to create the comic-strip layout
27
  def generate_comic_strip(story):
28
- scenes = split_story_into_scenes(story) # Split the story into scenes
29
- images = generate_images(scenes) # Generate images for each scene
30
- return images # Return the images for display in the gallery
31
 
32
  # Gradio Interface
33
  description = "GenArt Narrative: Turn your story descriptions into comic-strip visuals!"
34
 
35
  interface = gr.Interface(
36
- fn=generate_comic_strip, # Function to run
37
- inputs=gr.Textbox(lines=5, placeholder="Enter your short story here..."), # Input: user story
38
- outputs=gr.Gallery(label="Generated Comic Strip"), # Output: gallery of images
39
  title="GenArt Narrative",
40
  description=description,
41
- examples=[["A brave knight sets out on a quest to rescue a princess from a dragon."]] # Example story
 
 
42
  )
43
 
44
- # Launch the Gradio app
45
  if __name__ == "__main__":
46
  interface.launch()
47
-
 
1
+
2
  import gradio as gr
3
+ import requests
 
4
  from PIL import Image
5
+ from io import BytesIO
6
 
7
+ # Hugging Face Inference API token (you need to get your own token from Hugging Face)
8
+ API_TOKEN = "your_huggingface_api_token_here"
9
+ API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
10
 
11
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
 
 
 
 
12
 
13
+ # Function to split story into key scenes
14
+ def split_story_into_scenes(story):
15
+ scenes = story.strip().split(". ")
16
+ return [scene.strip() for scene in scenes if scene.strip()] # Return non-empty, stripped scenes
17
+
18
+ # Function to generate images using the Hugging Face Inference API
19
+ def generate_image_from_prompt(prompt):
20
+ payload = {"inputs": prompt}
21
+ response = requests.post(API_URL, headers=headers, json=payload)
22
+ if response.status_code == 200:
23
+ image = Image.open(BytesIO(response.content))
24
+ return image
25
+ else:
26
+ print(f"Error {response.status_code}: {response.content}")
27
+ return None
28
+
29
+ # Function to generate images for all scenes
30
  def generate_images(scenes):
31
  images = []
32
  for scene in scenes:
 
33
  print(f"Generating image for scene: {scene}")
34
+ image = generate_image_from_prompt(scene)
35
+ if image:
36
+ images.append(image)
37
  return images
38
 
39
  # Function to create the comic-strip layout
40
  def generate_comic_strip(story):
41
+ scenes = split_story_into_scenes(story)
42
+ images = generate_images(scenes)
43
+ return images
44
 
45
  # Gradio Interface
46
  description = "GenArt Narrative: Turn your story descriptions into comic-strip visuals!"
47
 
48
  interface = gr.Interface(
49
+ fn=generate_comic_strip,
50
+ inputs=gr.Textbox(lines=5, placeholder="Enter your short story here..."),
51
+ outputs=gr.Gallery(label="Comic Strip"),
52
  title="GenArt Narrative",
53
  description=description,
54
+ examples=[
55
+ ["Once upon a time, a dragon guarded a secret treasure in a mystical land."]
56
+ ],
57
  )
58
 
59
+ # Launch the app
60
  if __name__ == "__main__":
61
  interface.launch()