Commit
·
7ebe50e
1
Parent(s):
75a1171
commit
Browse files
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
+
|
| 4 |
+
# Load the model and tokenizer
|
| 5 |
+
model_name = "Salesforce/blip-image-captioning-large"
|
| 6 |
+
processor = BlipProcessor.from_pretrained(model_name)
|
| 7 |
+
model = BlipForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
def generate_caption(image):
|
| 10 |
+
# Preprocess the image
|
| 11 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 12 |
+
|
| 13 |
+
# Generate caption using the model
|
| 14 |
+
caption = model.generate(**inputs)
|
| 15 |
+
|
| 16 |
+
# Decode the output caption
|
| 17 |
+
decoded_caption = processor.decode(caption[0], skip_special_tokens=True)
|
| 18 |
+
return decoded_caption
|
| 19 |
+
|
| 20 |
+
# Define the Gradio interface
|
| 21 |
+
inputs = gr.inputs.Image(label="Upload an image")
|
| 22 |
+
outputs = gr.outputs.Textbox(label="Generated Caption")
|
| 23 |
+
|
| 24 |
+
# Create the Gradio app
|
| 25 |
+
gr.Interface(fn=generate_caption, inputs=inputs, outputs=outputs).launch()
|