Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
from transformers import BlipForConditionalGeneration, BlipForQuestionAnswering, BlipProcessor
|
6 |
+
|
7 |
+
|
8 |
+
## Loading BLIP image caption model
|
9 |
+
processor = BlipProcessor.from_pretrained("adit94/blip_humour")
|
10 |
+
model = BlipForConditionalGeneration.from_pretrained("adit94/blip_humour")
|
11 |
+
|
12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
|
14 |
+
|
15 |
+
def blip_generate(prompt, img):
|
16 |
+
#raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
|
17 |
+
img = img.convert('RGB')
|
18 |
+
inputs = processor(img, return_tensors="pt").to(device)
|
19 |
+
|
20 |
+
outputs = model.generate(
|
21 |
+
**inputs,
|
22 |
+
do_sample=False,
|
23 |
+
num_beams=5,
|
24 |
+
max_length=256,
|
25 |
+
min_length=1,
|
26 |
+
top_p=0.9,
|
27 |
+
repetition_penalty=1.5,
|
28 |
+
length_penalty=1.0,
|
29 |
+
temperature=1,
|
30 |
+
)
|
31 |
+
|
32 |
+
generated_text = processor.batch_decode(outputs, skip_special_tokens=True)[0].strip()
|
33 |
+
|
34 |
+
return generated_text
|
35 |
+
|
36 |
+
st.set_page_config(
|
37 |
+
page_title="Humour Detection",
|
38 |
+
initial_sidebar_state = 'auto'
|
39 |
+
)
|
40 |
+
|
41 |
+
file = st.file_uploader("", type=["jpg", "png"])
|
42 |
+
|
43 |
+
if file is None:
|
44 |
+
st.text("Please upload an image file")
|
45 |
+
else:
|
46 |
+
image = Image.open(file)
|
47 |
+
st.image(image)
|
48 |
+
|
49 |
+
caption = blip_generate()
|
50 |
+
|
51 |
+
st.text('Predicted output')
|
52 |
+
st.text(caption)
|
53 |
+
|