Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,9 @@ import wget
|
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
|
10 |
# Load the models
|
|
|
11 |
caption_image = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large", device=device)
|
|
|
12 |
sd_pipeline = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5").to(device)
|
13 |
|
14 |
# Load the translation model (English to Arabic)
|
@@ -19,9 +21,23 @@ translator = pipeline(
|
|
19 |
device=device
|
20 |
)
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Function to generate images based on the image's caption
|
27 |
def generate_image_and_translate(image, num_images=1):
|
@@ -43,24 +59,20 @@ def generate_image_and_translate(image, num_images=1):
|
|
43 |
|
44 |
# Set up the Gradio interface
|
45 |
interface = gr.Interface(
|
46 |
-
fn=generate_image_and_translate,
|
47 |
inputs=[
|
48 |
-
gr.Image(type="pil", label="Upload Image"),
|
49 |
-
gr.Slider(minimum=1, maximum=10, label="Number of Images", value=1, step=1)
|
50 |
],
|
51 |
outputs=[
|
52 |
-
gr.Gallery(label="Generated Images"),
|
53 |
-
gr.Textbox(label="Generated Caption (English)", interactive=False),
|
54 |
-
gr.Textbox(label="Translated Caption (Arabic)", interactive=False)
|
55 |
],
|
56 |
-
title="Image Generation and
|
57 |
-
description="Upload an image to
|
58 |
-
|
59 |
-
["sea.jpg", 3]
|
60 |
-
]
|
61 |
)
|
62 |
|
63 |
# Launch the Gradio application
|
64 |
-
interface.launch()
|
65 |
-
if __name__ == "__main__":
|
66 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
|
10 |
# Load the models
|
11 |
+
# Image captioning model to generate captions from uploaded images
|
12 |
caption_image = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large", device=device)
|
13 |
+
# Stable Diffusion model for generating new images based on captions
|
14 |
sd_pipeline = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5").to(device)
|
15 |
|
16 |
# Load the translation model (English to Arabic)
|
|
|
21 |
device=device
|
22 |
)
|
23 |
|
24 |
+
# Function to generate images based on the image's caption
|
25 |
+
def generate_image_and_translate(image, num_images=1):
|
26 |
+
# Generate caption in English from the uploaded image
|
27 |
+
caption_en = caption_image(image)[0]['generated_text']
|
28 |
+
|
29 |
+
# Translate the English caption to Arabic
|
30 |
+
caption_ar = translator(caption_en, src_lang="eng_Latn", tgt_lang="arb_Arab")[0]['translation_text']
|
31 |
+
|
32 |
+
generated_images = []
|
33 |
+
|
34 |
+
# Generate the specified number of images based on the English caption
|
35 |
+
for _ in range(num_images):
|
36 |
+
generated_image = sd_pipeline(prompt=caption_en).images[0]
|
37 |
+
generated_images.append(generated_image)
|
38 |
+
|
39 |
+
# Return the generated images along with both captions
|
40 |
+
return generated_images, caption_en, caption_ar
|
41 |
|
42 |
# Function to generate images based on the image's caption
|
43 |
def generate_image_and_translate(image, num_images=1):
|
|
|
59 |
|
60 |
# Set up the Gradio interface
|
61 |
interface = gr.Interface(
|
62 |
+
fn=generate_image_and_translate, # Function to call when processing input
|
63 |
inputs=[
|
64 |
+
gr.Image(type="pil", label="📤 Upload Image"), # Input for image upload
|
65 |
+
gr.Slider(minimum=1, maximum=10, label="🔢 Number of Images", value=1, step=1) # Slider to select number of images
|
66 |
],
|
67 |
outputs=[
|
68 |
+
gr.Gallery(label="🖼️ Generated Images"),
|
69 |
+
gr.Textbox(label="📝 Generated Caption (English)", interactive=False),
|
70 |
+
gr.Textbox(label="🌍 Translated Caption (Arabic)", interactive=False)
|
71 |
],
|
72 |
+
title="Image Generation and Captioning", # Title of the interface
|
73 |
+
description="Upload an image to extract a caption and display it in both Arabic and English. Then, a new image will be generated based on that caption.", # Description
|
74 |
+
theme='freddyaboulton/dracula_revamped' # Determine theme
|
|
|
|
|
75 |
)
|
76 |
|
77 |
# Launch the Gradio application
|
78 |
+
interface.launch()
|
|
|
|