Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ def main():
|
|
12 |
|
13 |
if api_key:
|
14 |
# Download BOE rates
|
15 |
-
download_boe_rates()
|
16 |
|
17 |
# Allow user to upload Excel sheet
|
18 |
uploaded_file = st.file_uploader("Upload Excel file", type=["xlsx", "xls"])
|
@@ -40,7 +40,7 @@ def main():
|
|
40 |
})
|
41 |
|
42 |
# Calculate late interest
|
43 |
-
df_with_interest = calculate_late_interest(df_calculate, late_interest_rate)
|
44 |
|
45 |
# Display calculated late interest
|
46 |
total_late_interest = df_with_interest['late_interest'].sum()
|
@@ -48,14 +48,7 @@ def main():
|
|
48 |
st.write(total_late_interest)
|
49 |
|
50 |
# Generate conversation prompt
|
51 |
-
prompt =
|
52 |
-
if due_dates:
|
53 |
-
prompt += f"The due dates in the sheet are: {', '.join(str(date) for date in due_dates)}. "
|
54 |
-
if payment_dates:
|
55 |
-
prompt += f"The payment dates in the sheet are: {', '.join(str(date) for date in payment_dates)}. "
|
56 |
-
if amounts:
|
57 |
-
prompt += f"The amounts in the sheet are: {', '.join(str(amount) for amount in amounts)}. "
|
58 |
-
prompt += "Based on this information, what would you like to discuss?"
|
59 |
|
60 |
# Allow user to engage in conversation
|
61 |
user_input = st.text_input("Start a conversation:")
|
@@ -68,21 +61,36 @@ def main():
|
|
68 |
{"role": "system", "content": prompt},
|
69 |
{"role": "user", "content": user_input}
|
70 |
],
|
71 |
-
max_tokens=1800
|
72 |
)
|
73 |
response = completion.choices[0].message['content']
|
74 |
st.write("AI's Response:")
|
75 |
st.write(response)
|
|
|
|
|
|
|
|
|
|
|
76 |
else:
|
77 |
st.warning("Please enter your OpenAI API key.")
|
78 |
|
79 |
# Function to calculate late interest
|
80 |
-
def calculate_late_interest(data, late_interest_rate):
|
81 |
# Calculate late days and late interest
|
82 |
data['late_days'] = (data['payment_date'] - data['due_date']).dt.days.clip(lower=0)
|
83 |
data['late_interest'] = data['late_days'] * data['amount'] * (late_interest_rate / 100)
|
|
|
|
|
|
|
|
|
|
|
84 |
return data
|
85 |
|
|
|
|
|
|
|
|
|
|
|
86 |
# Function to analyze Excel sheet and extract relevant information
|
87 |
def analyze_excel(df):
|
88 |
# Extract due dates and payment dates
|
@@ -111,10 +119,26 @@ def download_boe_rates():
|
|
111 |
df = pd.read_html(response.text)[0]
|
112 |
df.to_csv('boe_rates.csv', index=False)
|
113 |
st.success("Bank of England rates downloaded successfully.")
|
|
|
114 |
else:
|
115 |
st.error("Failed to retrieve data from the Bank of England website.")
|
|
|
116 |
except requests.RequestException as e:
|
117 |
st.error(f"Failed to download rates: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
if __name__ == "__main__":
|
120 |
main()
|
|
|
12 |
|
13 |
if api_key:
|
14 |
# Download BOE rates
|
15 |
+
boe_rates_df = download_boe_rates()
|
16 |
|
17 |
# Allow user to upload Excel sheet
|
18 |
uploaded_file = st.file_uploader("Upload Excel file", type=["xlsx", "xls"])
|
|
|
40 |
})
|
41 |
|
42 |
# Calculate late interest
|
43 |
+
df_with_interest = calculate_late_interest(df_calculate, late_interest_rate, boe_rates_df)
|
44 |
|
45 |
# Display calculated late interest
|
46 |
total_late_interest = df_with_interest['late_interest'].sum()
|
|
|
48 |
st.write(total_late_interest)
|
49 |
|
50 |
# Generate conversation prompt
|
51 |
+
prompt = generate_conversation_prompt(df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# Allow user to engage in conversation
|
54 |
user_input = st.text_input("Start a conversation:")
|
|
|
61 |
{"role": "system", "content": prompt},
|
62 |
{"role": "user", "content": user_input}
|
63 |
],
|
64 |
+
max_tokens=1800 # Adjust this value to allow longer responses
|
65 |
)
|
66 |
response = completion.choices[0].message['content']
|
67 |
st.write("AI's Response:")
|
68 |
st.write(response)
|
69 |
+
|
70 |
+
# Button to clear cache
|
71 |
+
if st.button("Clear Cache"):
|
72 |
+
st.cache.clear()
|
73 |
+
|
74 |
else:
|
75 |
st.warning("Please enter your OpenAI API key.")
|
76 |
|
77 |
# Function to calculate late interest
|
78 |
+
def calculate_late_interest(data, late_interest_rate, boe_rates_df):
|
79 |
# Calculate late days and late interest
|
80 |
data['late_days'] = (data['payment_date'] - data['due_date']).dt.days.clip(lower=0)
|
81 |
data['late_interest'] = data['late_days'] * data['amount'] * (late_interest_rate / 100)
|
82 |
+
|
83 |
+
# Consider additional factors like Bank of England base rate
|
84 |
+
data['boe_base_rate'] = data['due_date'].map(lambda x: get_boe_base_rate(x, boe_rates_df))
|
85 |
+
data['late_interest'] += data['amount'] * (data['boe_base_rate'] / 100)
|
86 |
+
|
87 |
return data
|
88 |
|
89 |
+
# Function to get Bank of England base rate for a given date
|
90 |
+
def get_boe_base_rate(date, boe_rates_df):
|
91 |
+
closest_date = boe_rates_df['Date Changed'].iloc[(boe_rates_df['Date Changed']-date).abs().argsort()[0]]
|
92 |
+
return boe_rates_df[boe_rates_df['Date Changed'] == closest_date]['Base Rate'].values[0]
|
93 |
+
|
94 |
# Function to analyze Excel sheet and extract relevant information
|
95 |
def analyze_excel(df):
|
96 |
# Extract due dates and payment dates
|
|
|
119 |
df = pd.read_html(response.text)[0]
|
120 |
df.to_csv('boe_rates.csv', index=False)
|
121 |
st.success("Bank of England rates downloaded successfully.")
|
122 |
+
return df
|
123 |
else:
|
124 |
st.error("Failed to retrieve data from the Bank of England website.")
|
125 |
+
return None
|
126 |
except requests.RequestException as e:
|
127 |
st.error(f"Failed to download rates: {e}")
|
128 |
+
return None
|
129 |
+
|
130 |
+
# Function to generate conversation prompt
|
131 |
+
def generate_conversation_prompt(df):
|
132 |
+
prompt = "I have analyzed the provided Excel sheet. "
|
133 |
+
due_dates, payment_dates, amounts = analyze_excel(df)
|
134 |
+
if due_dates:
|
135 |
+
prompt += f"The due dates in the sheet are: {', '.join(str(date) for date in due_dates)}. "
|
136 |
+
if payment_dates:
|
137 |
+
prompt += f"The payment dates in the sheet are: {', '.join(str(date) for date in payment_dates)}. "
|
138 |
+
if amounts:
|
139 |
+
prompt += f"The amounts in the sheet are: {', '.join(str(amount) for amount in amounts)}. "
|
140 |
+
prompt += "Based on this information, what would you like to discuss?"
|
141 |
+
return prompt
|
142 |
|
143 |
if __name__ == "__main__":
|
144 |
main()
|