Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,18 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
import
|
4 |
-
from datetime import datetime
|
5 |
from transformers import AutoTokenizer, AutoModelForTableQuestionAnswering
|
|
|
6 |
|
7 |
class InterestCalculatorApp:
|
8 |
def __init__(self):
|
9 |
-
self.invoices_df =
|
10 |
-
self.base_rates_df =
|
11 |
self.late_payment_interest = st.sidebar.slider("Late Payment Interest Rate (%)", min_value=0.0, max_value=10.0, value=4.0)
|
12 |
self.compounding_method = st.sidebar.selectbox("Compounding Method", ["x% above Base Rate (daily)",
|
13 |
"x% above Base Rate (annually)",
|
14 |
"Quarterly compounding (25 Mar, 24 Jun, 29 Sep, 25 Dec)",
|
15 |
"Quarterly compounding (1 Mar, 1 Jun, 1 Sep, 1 Dec)"])
|
16 |
-
|
17 |
-
# Load TAPAS model
|
18 |
self.tokenizer = AutoTokenizer.from_pretrained("google/tapas-large-finetuned-wtq")
|
19 |
self.model = AutoModelForTableQuestionAnswering.from_pretrained("google/tapas-large-finetuned-wtq")
|
20 |
|
@@ -26,9 +24,9 @@ class InterestCalculatorApp:
|
|
26 |
st.error(f"Failed to load invoices: {e}")
|
27 |
|
28 |
def calculate_interest(self):
|
29 |
-
if self.invoices_df
|
30 |
st.write("Calculating interest...")
|
31 |
-
today = datetime.today()
|
32 |
interests = []
|
33 |
for index, invoice in self.invoices_df.iterrows():
|
34 |
due_date = invoice['Due Date']
|
@@ -73,7 +71,7 @@ class InterestCalculatorApp:
|
|
73 |
interest = 0
|
74 |
compounded_amount = amount
|
75 |
for month, day in quarterly_dates:
|
76 |
-
compounding_date = datetime(today.year, month, day)
|
77 |
if compounding_date > today:
|
78 |
break
|
79 |
if compounding_date > due_date:
|
@@ -105,14 +103,12 @@ class InterestCalculatorApp:
|
|
105 |
st.error(f"Failed to download rates: {e}")
|
106 |
|
107 |
def ask_tapas(self, query, table):
|
108 |
-
if isinstance(table, pd.DataFrame):
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
else:
|
115 |
-
raise TypeError("Table must be of type pd.DataFrame")
|
116 |
|
117 |
def main():
|
118 |
st.title("Interest Calculation App")
|
@@ -126,13 +122,9 @@ def main():
|
|
126 |
|
127 |
query = st.text_input("Enter your query:")
|
128 |
if query:
|
129 |
-
# Assuming you have a DataFrame named 'invoices_df' containing the invoice data
|
130 |
if not app.invoices_df.empty:
|
131 |
-
# Display the invoice data
|
132 |
st.write("Invoice Data:")
|
133 |
st.write(app.invoices_df)
|
134 |
-
|
135 |
-
# Call TAPAS model to answer user's query
|
136 |
answer = app.ask_tapas(query, app.invoices_df)
|
137 |
st.write("Answer:", answer)
|
138 |
else:
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
import datetime
|
|
|
4 |
from transformers import AutoTokenizer, AutoModelForTableQuestionAnswering
|
5 |
+
import requests
|
6 |
|
7 |
class InterestCalculatorApp:
|
8 |
def __init__(self):
|
9 |
+
self.invoices_df = pd.DataFrame() # Initialize as empty DataFrame
|
10 |
+
self.base_rates_df = pd.DataFrame() # Initialize as empty DataFrame
|
11 |
self.late_payment_interest = st.sidebar.slider("Late Payment Interest Rate (%)", min_value=0.0, max_value=10.0, value=4.0)
|
12 |
self.compounding_method = st.sidebar.selectbox("Compounding Method", ["x% above Base Rate (daily)",
|
13 |
"x% above Base Rate (annually)",
|
14 |
"Quarterly compounding (25 Mar, 24 Jun, 29 Sep, 25 Dec)",
|
15 |
"Quarterly compounding (1 Mar, 1 Jun, 1 Sep, 1 Dec)"])
|
|
|
|
|
16 |
self.tokenizer = AutoTokenizer.from_pretrained("google/tapas-large-finetuned-wtq")
|
17 |
self.model = AutoModelForTableQuestionAnswering.from_pretrained("google/tapas-large-finetuned-wtq")
|
18 |
|
|
|
24 |
st.error(f"Failed to load invoices: {e}")
|
25 |
|
26 |
def calculate_interest(self):
|
27 |
+
if not self.invoices_df.empty and not self.base_rates_df.empty:
|
28 |
st.write("Calculating interest...")
|
29 |
+
today = datetime.datetime.today()
|
30 |
interests = []
|
31 |
for index, invoice in self.invoices_df.iterrows():
|
32 |
due_date = invoice['Due Date']
|
|
|
71 |
interest = 0
|
72 |
compounded_amount = amount
|
73 |
for month, day in quarterly_dates:
|
74 |
+
compounding_date = datetime.datetime(today.year, month, day)
|
75 |
if compounding_date > today:
|
76 |
break
|
77 |
if compounding_date > due_date:
|
|
|
103 |
st.error(f"Failed to download rates: {e}")
|
104 |
|
105 |
def ask_tapas(self, query, table):
|
106 |
+
if not isinstance(table, pd.DataFrame):
|
107 |
+
raise TypeError("Expected the table to be a pd.DataFrame, got {}".format(type(table).__name__))
|
108 |
+
inputs = self.tokenizer(table=table, queries=[query], return_tensors="pt", padding=True)
|
109 |
+
outputs = self.model(**inputs)
|
110 |
+
predicted_answer = self.tokenizer.decode(outputs.logits.argmax(dim=-1))
|
111 |
+
return predicted_answer
|
|
|
|
|
112 |
|
113 |
def main():
|
114 |
st.title("Interest Calculation App")
|
|
|
122 |
|
123 |
query = st.text_input("Enter your query:")
|
124 |
if query:
|
|
|
125 |
if not app.invoices_df.empty:
|
|
|
126 |
st.write("Invoice Data:")
|
127 |
st.write(app.invoices_df)
|
|
|
|
|
128 |
answer = app.ask_tapas(query, app.invoices_df)
|
129 |
st.write("Answer:", answer)
|
130 |
else:
|