Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +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: ")))
|
|
|
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 |
+
text = "Explain this image,that what she is doing and what kind of expression is it"
|
14 |
+
input = self.processor(img,text=text,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: ")))
|