Spaces:
Running
Running
Upload 2 files
Browse files- app.py +30 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, BlipForConditionalGeneration
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Load BLIP model
|
7 |
+
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
8 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
9 |
+
|
10 |
+
def caption_image(input_image: np.ndarray):
|
11 |
+
# Convert numpy array to PIL Image
|
12 |
+
raw_image = Image.fromarray(input_image).convert('RGB')
|
13 |
+
|
14 |
+
# Generate caption
|
15 |
+
inputs = processor(images=raw_image, text="a photo of", return_tensors="pt")
|
16 |
+
outputs = model.generate(**inputs, max_new_tokens=50)
|
17 |
+
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
18 |
+
|
19 |
+
return caption
|
20 |
+
|
21 |
+
# Gradio interface
|
22 |
+
iface = gr.Interface(
|
23 |
+
fn=caption_image,
|
24 |
+
inputs=gr.Image(),
|
25 |
+
outputs="text",
|
26 |
+
title="Image Captioning",
|
27 |
+
examples=["example1.jpg", "example2.jpg"] # Optional: Add sample images
|
28 |
+
)
|
29 |
+
|
30 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch>=2.0.0
|
2 |
+
transformers>=4.30.0
|
3 |
+
gradio>=3.0.0
|
4 |
+
Pillow>=9.0.0
|