Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +33 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
4 |
+
|
5 |
+
class ImageCaption:
|
6 |
+
def __init__(self):
|
7 |
+
self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
8 |
+
self.model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
9 |
+
|
10 |
+
def generate(self,img):
|
11 |
+
if isinstance(img,str):
|
12 |
+
img = Image.open(img)
|
13 |
+
|
14 |
+
input = self.processor(img,return_tensors='pt')
|
15 |
+
print(**input)
|
16 |
+
output = self.model.generate(**input)
|
17 |
+
caption = self.processor.decode(output[0],skip_special_tokens = True)
|
18 |
+
return caption
|
19 |
+
|
20 |
+
|
21 |
+
ic = ImageCaption()
|
22 |
+
app = gr.Interface(
|
23 |
+
fn = ic.generate,
|
24 |
+
inputs=gr.Image(type='pil'),
|
25 |
+
outputs="text",
|
26 |
+
description="upload image to generate caption"
|
27 |
+
|
28 |
+
)
|
29 |
+
|
30 |
+
app.launch()
|
31 |
+
|
32 |
+
|
33 |
+
# print(ic.generate(input("Enter the source of image: ")))
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
transformers
|
4 |
+
Pillow
|