-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
187 lines (139 loc) · 5.66 KB
/
Copy pathmain.py
File metadata and controls
187 lines (139 loc) · 5.66 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import math
import streamlit as st
import pandas as pd
from io import StringIO
from parse import parse_file
from open_ai import ask_ai
from matplotlib import pyplot as plt
import numpy as np
from streamlit_extras.stylable_container import stylable_container
st.set_page_config(layout="wide")
def get_summary(items_dict, amounts, duplicates_set, total):
charges = items_dict.keys()
st.write("## Summary of bill")
with stylable_container(
key="container_with_border",
css_styles="""
{
border: 1px solid rgba(49, 51, 63, 0.2);
border-radius: 0.5rem;
padding: 10px;
background-color: #e591a3;
width: 950;
}
""",
):
st.write("**Here are your charges:**")
charge_list = []
for charge in charges:
charge_list.append(charge)
charge_bullet_list = "\n".join([f"- {charge}" for charge in charge_list])
st.markdown(charge_bullet_list)
#st.write(str(charge_list))
st.write("**Here are your duplicate charges (recommend double checking these charges to make sure you haven't been overcharged)**")
duplicates_bullet_list = "\n".join([f"- {duplicate}" for duplicate in duplicates_set])
st.markdown(duplicates_bullet_list)
st.write(f"**Here is your total: {total}**")
#st.write(str(duplicates_set))
#st.write("Here is your total: " + str(total))
def pie_chart(items_dict):
labels = list(items_dict.keys())
total, dict = calculator(items_dict)
amounts = list(dict.values())
#ex = amounts.sort()
fig1, ax1 = plt.subplots(figsize=(2, 3), facecolor = '#EBD4CB')
percents = np.array(amounts)
ax1.pie(percents, labels=labels, colors=['#DA9F93', '#B6465F', '#890620', '#e591a3'], autopct='%1.1f%%', pctdistance = .9,
shadow=False, startangle=45, textprops={'fontsize': 4})
ax1.axis('equal')
ax1.set_facecolor('#EBD4CB') # Equal aspect ratio ensures that pie is drawn as a circle.
st.pyplot(fig1)
return total
#dict/map charges -> amounts
def calculator(items_dict):
values = list(items_dict.values())
charges = list(items_dict.keys())
amounts = []
for value in values:
amounts.append(value[0])
#items = dict(zip(charges, amounts))
total = sum(amounts)
to_return = dict()
if total > 0:
for key, value in zip(charges, amounts):
decimal = value / total
percent = math.ceil(decimal * 100) / 100
to_return[key] = percent
else:
# If total is zero, return zeros or handle as needed
to_return = {key: 0 for key in charges}
return total, to_return
st.title("Bill Buddy : Your Medical Bill Assistant")
st.write("### Upload your itemized medical bill")
uploaded_file = st.file_uploader("",['png', 'jpg'])
if uploaded_file is not None:
print("Filename:" + uploaded_file.name)
print("Type" + uploaded_file.type)
with open(f"uploads/{uploaded_file.name}", "wb") as f:
f.write(uploaded_file.getbuffer())
items_dict = parse_file(uploaded_file.name)
amounts = []
duplicates_set = []
for key, value in items_dict.items():
amounts.append(value[0])
if value[1] > 1:
duplicates_set.append(key)
duplicates_set = set(duplicates_set)
print(duplicates_set)
df = pd.DataFrame({
"Charges": items_dict.keys(),
"Amount($)" :amounts
})
def highlight_duplicates(row):
if row['Charges'] in duplicates_set:
return['background-color: #b6465f'] * len(row)
else:
return[''] * len(row)
styled_df = df.style.apply(highlight_duplicates, axis=1)
col1, col2 = st.columns(2)
with col1:
st.write("**Here are your charges (highlighted = double charges)**")
st.dataframe(styled_df, key="styled_df", hide_index=True)
with col2:
total = pie_chart(items_dict)
def create_checkbox_columns(df):
checkboxes = []
total_items = len(df)
items_per_column = math.ceil(total_items / 2)
col1, col2 = st.columns(2)
for index, row in df.iterrows():
if index < items_per_column:
with col1:
is_checked = st.checkbox(f"{row['Charges']}", key=f"checkbox_{index}")
else:
with col2:
is_checked = st.checkbox(f"{row['Charges']}", key=f"checkbox_{index}")
if is_checked:
checkboxes.append(df.loc[index]['Charges'])
return checkboxes
st.write("## Which charges would you like to be explained to you?")
checkboxes = create_checkbox_columns(df)
ai_response = ask_ai(checkboxes)
with stylable_container(
key="container_with_border",
css_styles="""
{
border: 1px solid rgba(49, 51, 63, 0.2);
border-radius: 0.5rem;
padding: 10px;
background-color: #e591a3;
width: 950;
}
""",
):
for charge in ai_response['charges']:
st.write("Charge: " + charge['Procedure'])
st.write("Description: " + charge['Description'])
st.write("Validity: " + charge['Validity'])
if st.button("Get summary of bill"):
get_summary(items_dict, amounts, duplicates_set, total)