Spaces:
Runtime error
Runtime error
Commit
·
dd567a8
1
Parent(s):
4abebd2
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import os
|
3 |
+
import streamlit as st
|
4 |
+
import requests
|
5 |
+
from PIL import Image
|
6 |
+
from model import get_caption_model, generate_caption
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
|
10 |
+
@st.cache(allow_output_mutation=True)
|
11 |
+
def get_model():
|
12 |
+
return get_caption_model()
|
13 |
+
|
14 |
+
caption_model = get_model()
|
15 |
+
|
16 |
+
|
17 |
+
def predict():
|
18 |
+
captions = []
|
19 |
+
pred_caption = generate_caption('tmp.jpg', caption_model)
|
20 |
+
|
21 |
+
|
22 |
+
captions.append(pred_caption)
|
23 |
+
|
24 |
+
for _ in range(4):
|
25 |
+
pred_caption = generate_caption('tmp.jpg', caption_model, add_noise=True)
|
26 |
+
if pred_caption not in captions:
|
27 |
+
captions.append(pred_caption)
|
28 |
+
|
29 |
+
#finalc = ' '.join([str(elem) for elem in captions])
|
30 |
+
return captions;
|
31 |
+
def launch(inputs):
|
32 |
+
img = Image.open(requests.get(inputs, stream=True).raw)
|
33 |
+
img = img.convert('RGB')
|
34 |
+
st.image(img)
|
35 |
+
img.save('tmp.jpg')
|
36 |
+
o=predict()
|
37 |
+
str1=""
|
38 |
+
for ele in o:
|
39 |
+
str1 += "\n"+ele
|
40 |
+
os.remove('tmp.jpg')
|
41 |
+
return str1
|
42 |
+
iface = gr.Interface(launch, inputs="text", outputs="text")
|
43 |
+
iface.launch(debug=True)
|