Sugamdeol commited on
Commit
0483ea7
·
verified ·
1 Parent(s): a6146e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -2,6 +2,7 @@ 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):
@@ -16,7 +17,7 @@ def download_image(prompt, index):
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 needed
20
  # Wrap text for multiple lines
21
  lines = textwrap.fill(text, width=40)
22
  draw.text((10, 10), lines, font=font, fill="white")
@@ -30,17 +31,36 @@ def visualize_story_lines(story):
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
  images = visualize_story_lines(story)
43
- return images[0] # Return first image for now
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- iface = gr.Interface(fn=visualize_story_images, inputs="text", outputs="image")
46
  iface.launch(share=True)
 
2
  import requests
3
  from PIL import Image, ImageDraw, ImageFont
4
  import textwrap
5
+ import os
6
 
7
  # Function to download image based on prompt
8
  def download_image(prompt, index):
 
17
  def add_text_to_image(image_path, text):
18
  img = Image.open(image_path)
19
  draw = ImageDraw.Draw(img)
20
+ font = ImageFont.load_default() # Load default font
21
  # Wrap text for multiple lines
22
  lines = textwrap.fill(text, width=40)
23
  draw.text((10, 10), lines, font=font, fill="white")
 
31
  images_with_text = []
32
 
33
  for idx, line in enumerate(lines):
34
+ prompt = line.replace(" ", "_") # Adjust prompt formatting
35
  img_file = download_image(prompt, idx)
36
+ img_with_text = add_text_to_image(img_file, line) # Add line text to image
37
  images_with_text.append(img_with_text)
38
 
39
  return images_with_text # Return list of images with text
40
 
41
+ # Gradio interface with state management
42
+ def visualize_story_images(story, img_index):
43
  images = visualize_story_lines(story)
44
+ if 0 <= img_index < len(images):
45
+ return images[img_index] # Return the image based on index
46
+ return None
47
+
48
+ # Create a function for navigation
49
+ def navigate_story(story, img_index):
50
+ images = visualize_story_lines(story)
51
+ if img_index < len(images):
52
+ return images[img_index]
53
+ return images[-1] # Return the last image if index exceeds
54
+
55
+ # Gradio interface setup
56
+ story_input = gr.inputs.Textbox(lines=10, placeholder="Enter your story here...")
57
+ image_output = gr.outputs.Image(type="pil")
58
+
59
+ iface = gr.Interface(
60
+ fn=visualize_story_images,
61
+ inputs=[story_input, gr.inputs.Slider(minimum=0, maximum=9, step=1, label="Image Index")],
62
+ outputs=image_output,
63
+ live=True
64
+ )
65
 
 
66
  iface.launch(share=True)