teganmosi commited on
Commit
e3b66c8
·
1 Parent(s): 80a2e84

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +87 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.chat_models import ChatOpenAI
10
+ from io import BytesIO
11
+
12
+ load_dotenv()
13
+
14
+ processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
15
+ model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
16
+
17
+ llm = ChatOpenAI(temperature=0.2)
18
+ prompt = PromptTemplate(
19
+ input_variables=["question", "elements"],
20
+ template="""You are a helpful assistant that can answer question related to an image. You have the ability to see the image and answer questions about it.
21
+ I will give you a question and element about the image and you will answer the question.
22
+ \n\n
23
+ #Question: {question}
24
+ #Elements: {elements}
25
+ \n\n
26
+ Your structured response:""",
27
+ )
28
+
29
+ def process_query(image, query):
30
+ encoding = processor(image, query, return_tensors="pt")
31
+ outputs = model(**encoding)
32
+ logits = outputs.logits
33
+ idx = logits.argmax(-1).item()
34
+ chain = LLMChain(llm=llm, prompt=prompt)
35
+ response = chain.run(question=query, elements=model.config.id2label[idx])
36
+ return response
37
+
38
+ def convert_png_to_jpg(image):
39
+ rgb_image = image.convert('RGB')
40
+ byte_arr = BytesIO()
41
+ rgb_image.save(byte_arr, format='JPEG')
42
+ byte_arr.seek(0)
43
+ return Image.open(byte_arr)
44
+
45
+ # Sidebar contents
46
+ with st.sidebar:
47
+ st.title('🤗💬 LLM Chat App')
48
+ st.markdown('''
49
+ ## About
50
+ This app is an LLM-powered chatbot built using:
51
+ - [Streamlit](https://streamlit.io/)
52
+ - [LangChain](https://python.langchain.com/)
53
+ - [OpenAI](https://platform.openai.com/docs/models) LLM model
54
+ - [ViLT](https://huggingface.co/dandelin/vilt-b32-finetuned-vqa)
55
+ ''')
56
+ add_vertical_space(5)
57
+ st.write('Made by [Nicolas tch](https://twitter.com/nicolas_tch)')
58
+
59
+ load_dotenv()
60
+
61
+ def main():
62
+ st.title("Chat with your IMAGE 💬")
63
+
64
+ uploaded_file = st.file_uploader('Upload your IMAGE', type=['png', 'jpeg', 'jpg'])
65
+
66
+ if uploaded_file is not None:
67
+ image = Image.open(uploaded_file)
68
+
69
+ # ViLT model only supports JPG images
70
+ if image.format == 'PNG':
71
+ image = convert_png_to_jpg(image)
72
+
73
+ st.image(image, caption='Uploaded Image.', width=300)
74
+
75
+ cancel_button = st.button('Cancel')
76
+ query = st.text_input('Ask a question to the IMAGE')
77
+
78
+ if query:
79
+ with st.spinner('Processing...'):
80
+ answer = process_query(image, query)
81
+ st.write(answer)
82
+
83
+ if cancel_button:
84
+ st.stop()
85
+
86
+ if __name__ == "__main__":
87
+ 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