Sugamdeol commited on
Commit
269525c
·
verified ·
1 Parent(s): dc2831e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import requests
3
- import imageio
4
- from PIL import Image
5
 
6
  # Function to download image based on prompt
7
  def download_image(prompt, index):
@@ -12,24 +12,34 @@ def download_image(prompt, index):
12
  file.write(response.content)
13
  return image_filename
14
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # Function to visualize each line of the story
16
  def visualize_story_lines(story):
17
  lines = story.split('\n') # Split story into lines
18
- images = []
19
 
20
  for idx, line in enumerate(lines):
21
  prompt = line.replace(" ", "_") # You can adjust prompt formatting
22
  img_file = download_image(prompt, idx)
23
- images.append(imageio.imread(img_file)) # Append image to list
 
24
 
25
- # Create a GIF as a slideshow from the images
26
- gif_filename = 'slideshow.gif'
27
- imageio.mimsave(gif_filename, images, duration=1) # 1 second per image
28
- return gif_filename
29
 
30
  # Gradio interface
31
- def visualize_story_slideshow(story):
32
  return visualize_story_lines(story)
33
 
34
- iface = gr.Interface(fn=visualize_story_slideshow, inputs="text", outputs="file")
35
  iface.launch()
 
1
  import gradio as gr
2
  import requests
3
+ from PIL import Image, ImageDraw, ImageFont
4
+ import textwrap
5
 
6
  # Function to download image based on prompt
7
  def download_image(prompt, index):
 
12
  file.write(response.content)
13
  return image_filename
14
 
15
+ # Function to add text to image
16
+ def add_text_to_image(image_path, text):
17
+ img = Image.open(image_path)
18
+ draw = ImageDraw.Draw(img)
19
+ font = ImageFont.load_default() # You can load a custom font if you want
20
+ # Wrap text for multiple lines
21
+ lines = textwrap.fill(text, width=40)
22
+ draw.text((10, 10), lines, font=font, fill="white")
23
+ img_with_text = f'text_added_{image_path}'
24
+ img.save(img_with_text)
25
+ return img_with_text
26
+
27
  # Function to visualize each line of the story
28
  def visualize_story_lines(story):
29
  lines = story.split('\n') # Split story into lines
30
+ images_with_text = []
31
 
32
  for idx, line in enumerate(lines):
33
  prompt = line.replace(" ", "_") # You can adjust prompt formatting
34
  img_file = download_image(prompt, idx)
35
+ img_with_text = add_text_to_image(img_file, line) # Add line as text to image
36
+ images_with_text.append(img_with_text)
37
 
38
+ return images_with_text # Return list of images with text
 
 
 
39
 
40
  # Gradio interface
41
+ def visualize_story_images(story):
42
  return visualize_story_lines(story)
43
 
44
+ iface = gr.Interface(fn=visualize_story_images, inputs="text", outputs="image")
45
  iface.launch()