-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
64 lines (54 loc) · 1.98 KB
/
web.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
62
63
64
import streamlit as st
import requests
import numpy as np
from PIL import Image
from model.generate_caption import *
import datetime
import psycopg2
import pandas as pd
st.title('_Image_ Captioning Demo :sunglasses:')
# Connect to the database
@st.experimental_singleton
def init_connection():
return psycopg2.connect(**st.secrets["postgres"])
conn = init_connection()
# Uses st.experimental_memo to only rerun when the query changes or after 10 min.
@st.experimental_memo(ttl=2)
def run_query(query):
try:
with conn.cursor() as cur:
cur.execute(query)
return cur.fetchall()
except psycopg2.ProgrammingError:
st.success("Sucessfully Uploaded.")
return None
uploaded_files = st.file_uploader("Upload some file", accept_multiple_files=True)
for uploaded_file in uploaded_files:
bytes_data = uploaded_file.read()
st.write("filename:", uploaded_file.name)
print('Name of the file upload is: ',uploaded_file.name)
location = f'Upload/{uploaded_file.name}'
with open(os.path.join("Upload",uploaded_file.name),"wb") as f:
f.write(uploaded_file.getbuffer())
pred_caption = generate_caption(location)
query = f"INSERT INTO image_cap VALUES ('{uploaded_file.name}', '{location}', '{pred_caption}');"
data = run_query(query)
print(query)
print(type(bytes_data))
st.image(location)
st.write(pred_caption)
st.write('____________________________________________________________________________________________')
st.title('Uploaded')
rows = run_query("SELECT * from image_cap;")
df = pd.read_sql_query("SELECT * FROM image_cap", conn)
st.table(df)
st.title('Table of media')
for i in range(0,len(df['picture']),2):
cols = st.columns(2)
try:
cols[0].image(f'{df.picture[i]}', use_column_width=True)
cols[0].text(f'{df.prediction[i]}')
cols[1].image(f'{df.picture[i+1]}', use_column_width=True)
cols[1].text(f'{df.prediction[i+1]}')
except KeyError:
pass