File size: 2,683 Bytes
b54df69
 
771a15e
e554cac
3cddf46
b54df69
 
 
e554cac
 
b54df69
e554cac
 
 
 
 
 
 
 
d6e934d
 
b54df69
 
 
e554cac
 
 
b54df69
 
 
 
2532ae2
3cddf46
 
e554cac
 
3cddf46
 
 
 
bbfaf4f
c99ae18
e554cac
 
 
 
 
 
 
 
 
 
 
 
 
 
771a15e
d6e934d
e554cac
c99ae18
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
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