Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,24 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
+
from PIL import Image
|
| 4 |
|
| 5 |
+
# Load model and processor
|
| 6 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 7 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 8 |
+
|
| 9 |
+
# Inference function
|
| 10 |
+
def generate_caption(image):
|
| 11 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 12 |
+
out = model.generate(**inputs)
|
| 13 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 14 |
+
return caption
|
| 15 |
+
|
| 16 |
+
# Gradio interface
|
| 17 |
+
with gr.Blocks() as demo:
|
| 18 |
+
gr.Markdown("## 🖼️ Upload an image to generate a caption using BLIP")
|
| 19 |
+
image_input = gr.Image(type="pil", label="Image")
|
| 20 |
+
caption_output = gr.Textbox(label="Caption")
|
| 21 |
+
btn = gr.Button("Generate")
|
| 22 |
+
btn.click(fn=generate_caption, inputs=image_input, outputs=caption_output)
|
| 23 |
+
|
| 24 |
+
demo.launch()
|