-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
140 lines (106 loc) · 3.38 KB
/
app.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import openai
from langchain.document_loaders import PyPDFLoader
import streamlit as st
# streamlit run app.py
from dotenv import load_dotenv
load_dotenv()
api_base = os.getenv("AZURE_OPENAI_BASE")
api_key = os.getenv("AZURE_OPENAI_API_KEY")
api_version = "2023-03-15-preview"
openai.api_type = 'azure'
openai.api_key = api_key
openai.api_version = api_version
openai.api_base = api_base
def get_response(messages):
return openai.ChatCompletion.create(
engine="Gpt35Turbo16k",
messages = messages,
temperature=0,
max_tokens=4000,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None,
request_timeout=3600,
timeout=3600
)
def load_file(file_name):
loader = PyPDFLoader(file_name)
pages = loader.load()
allpages = ""
for page in pages:
allpages += page.page_content
return allpages
def read_receipt(file_name):
actor = "I want you to act as my personal assistant and organize expenses."
prompt = """
The following is a receipt between '''
Give me the following information from the receipt:
1. Seller
2. Date
3. Category
4. Tax rates
5. Tax amounts
6. Total amount with tax
'''{context}'''
"""
# prompt = """
# The following is a receipt between '''
# Extract all relevant key information and give it to me in a JSON object.
# '''{context}'''
# """
pages = load_file(file_name)
# st.write(pages)
messages = [
{"role":"system","content":actor},
{"role":"user","content":prompt.format(context=pages)}
]
st.subheader(file_name)
response = get_response(messages)
st.markdown(response.choices[0].message.content)
# with st.expander('Document Response'):
# st.write(response)
st.divider()
def read_invoice(file_name):
actor = "I want you to act as my personal assistant and organize invoices."
prompt = """
The following is an invoice between '''
Give me the following information from the invoice:
1. Seller information
2. Invoice date, Due date
3. Shipping information
4. Invoice number
5. Order number
6. Payment information
7. Payment terms
8. Items
9. Sub total
10. Tax rate
11. Tax amount
12. Total
'''{context}'''
"""
# prompt = """
# The following is an invoice between '''
# Extract all relevant key information and give it to me in a JSON object.
# '''{context}'''
# """
pages = load_file(file_name)
#st.write(pages)
messages = [
{"role":"system","content":actor},
{"role":"user","content":prompt.format(context=pages)}
]
st.subheader(file_name)
response = get_response(messages)
st.markdown(response.choices[0].message.content.replace('$', '\$').replace('\n', ' \n'))
# with st.expander('Document Response'):
# st.write(response)
st.divider()
st.title('Personal accountant')
# read_receipt("receipt1.pdf")
# read_receipt("receipt2.pdf")
# read_invoice("invoice1.pdf")
# read_invoice("invoice2.pdf")
read_invoice("invoice3.pdf")