forked from jaykshirsagar05/captionify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
61 lines (45 loc) · 1.78 KB
/
ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import streamlit as st
from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests
from PIL import Image
import io
st.title('Caption Generator')
# fastapi endpoint
url = 'http://fastapi:8000'
endpoint = '/segmentation'
endpoint1 = '/caption'
st.write('''Generate brief caption of the selected image.
This streamlit example uses a FastAPI service as backend.
Visit this URL at `:8000/docs` for FastAPI documentation.''') # description and instructions
image = st.file_uploader('insert image') # image upload widget
def process(image, server_url: str):
m = MultipartEncoder(
fields={'file': ('filename', image, 'image/jpeg')}
)
r = requests.post(server_url,
data=m,
headers={'Content-Type': m.content_type},
timeout=8000)
return r
def process1(image, server_url: str):
m = MultipartEncoder(
fields={'file': ('filename', image, 'image/jpeg')}
)
r = requests.post(server_url,data=m,headers={'Content-Type': m.content_type},
timeout=800000)
return r
if st.button('Get segmentation'):
if image is None:
st.write("Insert an image!") # handle case with no image
else:
segments = process(image, url+endpoint)
segmented_image = Image.open(io.BytesIO(segments.content)).convert('RGB')
st.image([image, segmented_image], width=300) # output dyptich
if st.button('Get caption'):
if image is None:
st.write("Insert an image!") # handle case with no image
else:
segments = process1(image, url+endpoint1)
# segmented_image = Image.open(io.BytesIO(segments.content)).convert('RGB')
st.image([image], width=300) # output dyptich
st.write(segments)