forked from SimranAnand1/LLMtutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (46 loc) · 1.69 KB
/
Copy pathmain.py
File metadata and controls
57 lines (46 loc) · 1.69 KB
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
import streamlit as st
import base64
from langchain.schema.messages import HumanMessage,AIMessage
from langchain_openai import ChatOpenAI
from PIL import Image
chain=ChatOpenAI(model="gpt-4-vision-preview",max_tokens=1024)
def encode_image(upload_file):
image_bytes=upload_file.getvalue()
base64_image=base64.b64encode(image_bytes).decode("utf-8")
return base64_image
def get_response(b64image,qsn):
msg=chain.invoke(
[
AIMessage(
content="you are a useful and intelligent bot who is very good at ocr related task , such getting insights from images of invoices"
),
HumanMessage(
content=[
{"type":"text","text":qsn},
{
"type": "image_url",
"image_url":f"data:image/jpg;base64,{b64image}"
}
]
)
]
)
return msg.content
# if "conversation" not in st.session_state:
# st.session_state.conversation=[]
def main():
st.title("CONVA INVOICE ANALYSIS SYSTEM")
upload_file=st.file_uploader("upload the invoice image..",type=["jpg"])
if upload_file is not None:
image=Image.open(upload_file)
st.image(image,caption="your uploaded invoice",use_column_width=True)
st.write("invoice image uploaded successfully")
b64_image=encode_image(upload_file)
st.success("image converted successfully")
user_question=st.text_input("ask anything related to the invoice")
submit_button=st.button("Submit")
if submit_button and user_question:
response=get_response(b64_image,user_question)
st.write(response)
if __name__=="__main__":
main()