rate_calc / app.py
albhu's picture
Update app.py
c99ae18 verified
raw
history blame
2.68 kB
import streamlit as st
import pandas as pd
from transformers import AutoTokenizer, AutoModelForTableQuestionAnswering
import datetime
import requests
class InterestCalculatorApp:
def __init__(self):
self.invoices_df = pd.DataFrame()
self.base_rates_df = pd.DataFrame()
self.late_payment_interest = st.sidebar.slider("Late Payment Interest Rate (%)", min_value=0.0, max_value=10.0, value=4.0)
self.compounding_method = st.sidebar.selectbox(
"Compounding Method", [
"x% above Base Rate (daily)",
"x% above Base Rate (annually)",
"Quarterly compounding (25 Mar, 24 Jun, 29 Sep, 25 Dec)",
"Quarterly compounding (1 Mar, 1 Jun, 1 Sep, 1 Dec)"
]
)
self.tokenizer = AutoTokenizer.from_pretrained("google/tapas-large-finetuned-wtq")
self.model = AutoModelForTableQuestionAnswering.from_pretrained("google/tapas-large-finetuned-wtq")
def load_invoices(self, file_path):
try:
self.invoices_df = pd.read_excel(file_path, parse_dates=True)
self.invoices_df = self.invoices_df.fillna('N/A') # Handle missing values
self.invoices_df = self.invoices_df.applymap(lambda x: str(x).strip() if isinstance(x, str) else str(x))
st.success("Invoices loaded successfully.")
except Exception as e:
st.error(f"Failed to load invoices: {e}")
def ask_tapas(self, query, table):
if not isinstance(table, pd.DataFrame):
raise TypeError("Expected the table to be a pd.DataFrame, got {}".format(type(table).__name__))
table = table.applymap(lambda x: str(x) if not pd.isnull(x) else "N/A")
inputs = self.tokenizer(table=table, queries=[query], return_tensors="pt", padding=True)
outputs = self.model(**inputs)
predicted_answer = self.tokenizer.decode(outputs.logits.argmax(dim=-1))
return predicted_answer
def main(self):
st.title("Interest Calculation App")
file_path = st.file_uploader("Upload Invoices File", type=["xlsx"])
if file_path is not None:
self.load_invoices(file_path)
query = st.text_input("Enter your query:")
if query:
if not self.invoices_df.empty:
st.write("Invoice Data:")
st.dataframe(self.invoices_df)
answer = self.ask_tapas(query, self.invoices_df)
st.write("Answer:", answer)
else:
st.warning("Please upload the invoices file first.")
if __name__ == "__main__":
app = InterestCalculatorApp()
app.main() # Corrected call