Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio_client import Client, file
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import requests
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
|
| 7 |
+
# Initialize the Hugging Face API clients
|
| 8 |
+
captioning_client = Client("fancyfeast/joy-caption-pre-alpha")
|
| 9 |
+
generation_client = Client("black-forest-labs/FLUX.1-dev")
|
| 10 |
+
|
| 11 |
+
# Function to caption an image
|
| 12 |
+
def caption_image(image):
|
| 13 |
+
caption = captioning_client.predict(
|
| 14 |
+
input_image=image,
|
| 15 |
+
api_name="/stream_chat"
|
| 16 |
+
)
|
| 17 |
+
return caption
|
| 18 |
+
|
| 19 |
+
# Function to generate an image from a text prompt using Hugging Face API
|
| 20 |
+
def generate_image_from_caption(caption):
|
| 21 |
+
image = generation_client.predict(
|
| 22 |
+
prompt=caption,
|
| 23 |
+
seed=0,
|
| 24 |
+
randomize_seed=True,
|
| 25 |
+
width=1024,
|
| 26 |
+
height=1024,
|
| 27 |
+
guidance_scale=3.5,
|
| 28 |
+
num_inference_steps=28,
|
| 29 |
+
api_name="/infer"
|
| 30 |
+
)
|
| 31 |
+
return image
|
| 32 |
+
|
| 33 |
+
# Main function to handle the upload and generate images and captions in a loop
|
| 34 |
+
def process_image(image, iterations):
|
| 35 |
+
generated_images = []
|
| 36 |
+
captions = []
|
| 37 |
+
|
| 38 |
+
current_image = image
|
| 39 |
+
|
| 40 |
+
for i in range(iterations):
|
| 41 |
+
# Caption the current image
|
| 42 |
+
caption = caption_image(current_image)
|
| 43 |
+
captions.append(caption)
|
| 44 |
+
|
| 45 |
+
# Generate a new image based on the caption
|
| 46 |
+
new_image = generate_image_from_caption(caption)
|
| 47 |
+
generated_images.append(new_image)
|
| 48 |
+
|
| 49 |
+
# Set the newly generated image as the current image for the next iteration
|
| 50 |
+
current_image = new_image
|
| 51 |
+
|
| 52 |
+
return generated_images, captions
|
| 53 |
+
|
| 54 |
+
# Gradio Interface
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
with gr.Row():
|
| 57 |
+
image_input = gr.Image(type="pil", label="Upload an Image")
|
| 58 |
+
iterations_input = gr.Number(value=3, label="Number of Iterations")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
output_images = gr.Gallery(label="Generated Images")
|
| 62 |
+
output_captions = gr.Textbox(label="Generated Captions")
|
| 63 |
+
|
| 64 |
+
generate_button = gr.Button("Generate")
|
| 65 |
+
|
| 66 |
+
generate_button.click(
|
| 67 |
+
fn=process_image,
|
| 68 |
+
inputs=[image_input, iterations_input],
|
| 69 |
+
outputs=[output_images, output_captions]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Launch the app
|
| 73 |
+
demo.launch()
|