SpyCoder77 commited on
Commit
cfb5c17
·
verified ·
1 Parent(s): cfb8c8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -4
app.py CHANGED
@@ -4,6 +4,7 @@ from PIL import Image
4
  import tempfile
5
  import requests
6
  from io import BytesIO
 
7
 
8
  # Initialize the Hugging Face API clients
9
  captioning_client = Client("fancyfeast/joy-caption-pre-alpha")
@@ -31,12 +32,23 @@ def generate_image_from_caption(caption):
31
  num_inference_steps=28,
32
  api_name="/infer"
33
  )
34
- image_url = image[0] # Assuming the API returns a URL to the image
 
 
 
 
 
 
 
 
35
  response = requests.get(image_url)
36
  return Image.open(BytesIO(response.content))
37
 
38
  # Main function to handle the upload and generate images and captions in a loop
39
  def process_image(image, iterations):
 
 
 
40
  generated_images = []
41
  captions = []
42
 
@@ -47,31 +59,41 @@ def process_image(image, iterations):
47
  caption = caption_image(current_image)
48
  captions.append(caption)
49
 
 
 
 
50
  # Generate a new image based on the caption
51
  new_image = generate_image_from_caption(caption)
52
  generated_images.append(new_image)
53
 
 
 
 
54
  # Set the newly generated image as the current image for the next iteration
55
  current_image = new_image
56
 
57
- return generated_images, captions
 
 
 
58
 
59
  # Gradio Interface
60
  with gr.Blocks() as demo:
61
  with gr.Row():
62
  image_input = gr.Image(type="pil", label="Upload an Image")
63
- iterations_input = gr.Number(value=3, label="Number of Iterations")
64
 
65
  with gr.Row():
66
  output_images = gr.Gallery(label="Generated Images")
67
  output_captions = gr.Textbox(label="Generated Captions")
 
68
 
69
  generate_button = gr.Button("Generate")
70
 
71
  generate_button.click(
72
  fn=process_image,
73
  inputs=[image_input, iterations_input],
74
- outputs=[output_images, output_captions]
75
  )
76
 
77
  # Launch the app
 
4
  import tempfile
5
  import requests
6
  from io import BytesIO
7
+ import os
8
 
9
  # Initialize the Hugging Face API clients
10
  captioning_client = Client("fancyfeast/joy-caption-pre-alpha")
 
32
  num_inference_steps=28,
33
  api_name="/infer"
34
  )
35
+
36
+ # Check if the response is a URL or a local path
37
+ image_url = image[0]
38
+ if not image_url.startswith("http"):
39
+ # Handle local file path
40
+ with open(image_url, "rb") as file:
41
+ return Image.open(file)
42
+
43
+ # Fetch image from URL
44
  response = requests.get(image_url)
45
  return Image.open(BytesIO(response.content))
46
 
47
  # Main function to handle the upload and generate images and captions in a loop
48
  def process_image(image, iterations):
49
+ # Ensure iterations is an integer
50
+ iterations = int(round(iterations))
51
+
52
  generated_images = []
53
  captions = []
54
 
 
59
  caption = caption_image(current_image)
60
  captions.append(caption)
61
 
62
+ # Notify that the caption has been made
63
+ status = f"Caption made: {caption}"
64
+
65
  # Generate a new image based on the caption
66
  new_image = generate_image_from_caption(caption)
67
  generated_images.append(new_image)
68
 
69
+ # Notify that the image has been generated
70
+ status += f"\nImage generated for iteration {i+1}"
71
+
72
  # Set the newly generated image as the current image for the next iteration
73
  current_image = new_image
74
 
75
+ # Notify that the process is completed
76
+ status += "\nProcessing complete!"
77
+
78
+ return generated_images, captions, status
79
 
80
  # Gradio Interface
81
  with gr.Blocks() as demo:
82
  with gr.Row():
83
  image_input = gr.Image(type="pil", label="Upload an Image")
84
+ iterations_input = gr.Number(value=3, label="Number of Iterations", precision=0)
85
 
86
  with gr.Row():
87
  output_images = gr.Gallery(label="Generated Images")
88
  output_captions = gr.Textbox(label="Generated Captions")
89
+ status_output = gr.Textbox(label="Status Updates", lines=10)
90
 
91
  generate_button = gr.Button("Generate")
92
 
93
  generate_button.click(
94
  fn=process_image,
95
  inputs=[image_input, iterations_input],
96
+ outputs=[output_images, output_captions, status_output]
97
  )
98
 
99
  # Launch the app