1Noura commited on
Commit
0f9e8fd
·
verified ·
1 Parent(s): 4785e03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -20
app.py CHANGED
@@ -1,16 +1,14 @@
1
  import gradio as gr
 
2
  from transformers import pipeline
3
  from diffusers import StableDiffusionPipeline
4
  import torch
5
- import wget
6
 
7
  # Define the device to use (either "cuda" for GPU or "cpu" for CPU)
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,9 +19,16 @@ translator = pipeline(
21
  device=device
22
  )
23
 
24
- # Download the image
25
  url1 = "https://github.com/Shahad-b/Image-database/blob/main/sea.jpg?raw=true"
26
  sea = wget.download(url1)
 
 
 
 
 
 
 
27
  # Function to generate images based on the image's caption
28
  def generate_image_and_translate(image, num_images=1):
29
  # Generate caption in English from the uploaded image
@@ -44,28 +49,24 @@ def generate_image_and_translate(image, num_images=1):
44
 
45
  # Set up the Gradio interface
46
  interface = gr.Interface(
47
- fn=generate_image_and_translate, # Function to call when processing input
48
  inputs=[
49
- gr.Image(type="pil", label="Upload Image"), # Input for image upload
50
- gr.Slider(minimum=1, maximum=10, label="Number of Images", value=1, step=1) # Slider to select number of images
51
  ],
52
  outputs=[
53
- gr.Gallery(label="Generated Images"), # Output for displaying generated images
54
- gr.Textbox(label="Generated Caption (English)", interactive=False), # Output for English caption
55
- gr.Textbox(label="Translated Caption (Arabic)", interactive=False)# Output for Arabic caption
56
-
57
  ],
58
- title="Image Generation and Translation", # Title of the interface
59
- description="Upload an image to generate new images based on its caption and translate the caption into Arabic.", # Description
60
- examples=[ # Example input
61
- ["sea.jpg", 3]
62
- # ["Cat.jpeg", 4],
63
- # ["Car.jpeg", 2]
64
  ]
65
  )
66
 
67
  # Launch the Gradio application
68
  interface.launch()
69
-
70
- if __name__ == "__main__":
71
- app.run(host="0.0.0.0" , port=7860)
 
1
  import gradio as gr
2
+ import wget
3
  from transformers import pipeline
4
  from diffusers import StableDiffusionPipeline
5
  import torch
 
6
 
7
  # Define the device to use (either "cuda" for GPU or "cpu" for CPU)
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
  device=device
20
  )
21
 
22
+ # Download the images
23
  url1 = "https://github.com/Shahad-b/Image-database/blob/main/sea.jpg?raw=true"
24
  sea = wget.download(url1)
25
+
26
+ url2 = "https://github.com/Shahad-b/Image-database/blob/main/Cat.jpeg?raw=true"
27
+ Cat = wget.download(url2)
28
+
29
+ url3 = "https://github.com/Shahad-b/Image-database/blob/main/Car.jpeg?raw=true"
30
+ Car = wget.download(url3)
31
+
32
  # Function to generate images based on the image's caption
33
  def generate_image_and_translate(image, num_images=1):
34
  # Generate caption in English from the uploaded image
 
49
 
50
  # Set up the Gradio interface
51
  interface = gr.Interface(
52
+ fn=generate_image_and_translate,
53
  inputs=[
54
+ gr.Image(type="pil", label="Upload Image"),
55
+ gr.Slider(minimum=1, maximum=10, label="Number of Images", value=1, step=1)
56
  ],
57
  outputs=[
58
+ gr.Gallery(label="Generated Images"),
59
+ gr.Textbox(label="Generated Caption (English)", interactive=False),
60
+ gr.Textbox(label="Translated Caption (Arabic)", interactive=False)
 
61
  ],
62
+ title="Image Generation and Translation",
63
+ description="Upload an image to generate new images based on its caption and translate the caption into Arabic.",
64
+ examples=[
65
+ ["sea.jpg", 3],
66
+ ["Cat.jpeg", 4],
67
+ ["Car.jpeg", 2]
68
  ]
69
  )
70
 
71
  # Launch the Gradio application
72
  interface.launch()