DemahAlmutairi commited on
Commit
473cd7a
·
verified ·
1 Parent(s): 97ef1fe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline
3
+ from PIL import Image
4
+ import gradio as gr
5
+ import os
6
+
7
+ # Specify the device (CPU or GPU)
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+
10
+ # Load the image-to-text pipeline
11
+ caption_image = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large", device=device)
12
+
13
+ # List of local image paths
14
+ example_images = ["image1.jpeg"]
15
+
16
+ # Function to process the image
17
+ def process_image(image):
18
+ caption = caption_image(image)[0]['generated_text']
19
+ return caption
20
+
21
+ # Create Gradio interface with example images
22
+ iface = gr.Interface(
23
+ fn=process_image,
24
+ inputs=gr.Image(type="pil"),
25
+ outputs=gr.Textbox(label="Generated Caption"),
26
+ examples=example_images # Use local images as examples
27
+ )
28
+
29
+ # Launch the interface
30
+ iface.launch()