shreyasiv commited on
Commit
16f50b1
1 Parent(s): 96432c9

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +81 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ import os
3
+ import streamlit as st
4
+ from PIL import Image
5
+ from transformers import ViltProcessor, ViltForQuestionAnswering
6
+ from langchain.prompts import PromptTemplate
7
+ from langchain.chains import LLMChain
8
+ from streamlit_extras.add_vertical_space import add_vertical_space
9
+ from langchain.llms import OpenAI
10
+
11
+ load_dotenv()
12
+
13
+ processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
14
+ model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
15
+
16
+ llm = OpenAI(temperature=0.2)
17
+ prompt = PromptTemplate(
18
+ input_variables=["question", "elements"],
19
+ template="""Please generate a structured response using the following information:
20
+ \n\n
21
+ #Question: {question}
22
+ #Response: {elements}
23
+ \n\n
24
+ Your structured response:""",
25
+ )
26
+
27
+ def process_query(image, query):
28
+ encoding = processor(image, query, return_tensors="pt")
29
+ outputs = model(**encoding)
30
+ logits = outputs.logits
31
+ idx = logits.argmax(-1).item()
32
+ chain = LLMChain(llm=llm, prompt=prompt)
33
+ response = chain.run(question=query, elements=model.config.id2label[idx])
34
+ return response
35
+
36
+ st.set_page_config(page_title="Insightly")
37
+ # Sidebar contents
38
+ with st.sidebar:
39
+ st.sidebar.image("/home/oem/Downloads/insightly_wbg.png", use_column_width=True)
40
+
41
+
42
+
43
+
44
+ load_dotenv()
45
+
46
+
47
+ def main():
48
+ st.title("Chat With Images 🖼️💬")
49
+
50
+ uploaded_file = st.file_uploader('Upload your Image', type=['png', 'jpeg', 'jpg'])
51
+
52
+ if uploaded_file is not None:
53
+
54
+ image = Image.open(uploaded_file)
55
+ st.image(image, caption='Uploaded Image.', width=300)
56
+
57
+ cancel_button = st.button('Remove this image')
58
+ if cancel_button:
59
+ st.markdown(
60
+ """<style>
61
+ .css-2trqyj:focus,
62
+ .css-2trqyj:active,
63
+ .css-2trqyj:hover {
64
+ outline: 2px solid red;
65
+ outline-offset: 2px;
66
+ }
67
+ </style>""",
68
+ unsafe_allow_html=True
69
+ )
70
+ query = st.text_input('Type your question here')
71
+
72
+ if query:
73
+ with st.spinner('Processing...'):
74
+ answer = process_query(image, query)
75
+ st.write(answer)
76
+
77
+ if cancel_button:
78
+ st.stop()
79
+
80
+ if __name__ == "__main__":
81
+ main()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ python-dotenv
3
+ Pillow
4
+ transformers
5
+ streamlit-extras
6
+ langchain
7
+ torch
8
+ openai
9
+ tokenizers