albhu commited on
Commit
43ecdc4
·
verified ·
1 Parent(s): 7b8b9ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -27
app.py CHANGED
@@ -48,7 +48,7 @@ def main():
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:")
@@ -66,36 +66,48 @@ def main():
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
 
 
79
  def calculate_late_interest(data, late_interest_rate, boe_rates_df):
80
  # Convert due_date column to Timestamp objects
81
  data['due_date'] = pd.to_datetime(data['due_date'])
 
82
 
83
  # Calculate late days and late interest
84
  data['late_days'] = (data['payment_date'] - data['due_date']).dt.days.clip(lower=0)
85
  data['late_interest'] = data['late_days'] * data['amount'] * (late_interest_rate / 100)
86
 
87
  # Consider additional factors like Bank of England base rate
88
- data['boe_base_rate'] = data['due_date'].map(lambda x: get_boe_base_rate(x, boe_rates_df))
89
- data['late_interest'] += data['amount'] * (data['boe_base_rate'] / 100)
 
90
 
91
  return data
92
 
93
-
94
- # Function to get Bank of England base rate for a given date
95
- def get_boe_base_rate(date, boe_rates_df):
96
- closest_date = boe_rates_df['Date Changed'].iloc[(boe_rates_df['Date Changed']-date).abs().argsort()[0]]
97
- return boe_rates_df[boe_rates_df['Date Changed'] == closest_date]['Base Rate'].values[0]
98
-
99
  # Function to analyze Excel sheet and extract relevant information
100
  def analyze_excel(df):
101
  # Extract due dates and payment dates
@@ -132,18 +144,10 @@ def download_boe_rates():
132
  st.error(f"Failed to download rates: {e}")
133
  return None
134
 
135
- # Function to generate conversation prompt
136
- def generate_conversation_prompt(df):
137
- prompt = "I have analyzed the provided Excel sheet. "
138
- due_dates, payment_dates, amounts = analyze_excel(df)
139
- if due_dates:
140
- prompt += f"The due dates in the sheet are: {', '.join(str(date) for date in due_dates)}. "
141
- if payment_dates:
142
- prompt += f"The payment dates in the sheet are: {', '.join(str(date) for date in payment_dates)}. "
143
- if amounts:
144
- prompt += f"The amounts in the sheet are: {', '.join(str(amount) for amount in amounts)}. "
145
- prompt += "Based on this information, what would you like to discuss?"
146
- return prompt
147
 
148
  if __name__ == "__main__":
149
  main()
 
48
  st.write(total_late_interest)
49
 
50
  # Generate conversation prompt
51
+ prompt = generate_conversation_prompt(df, boe_rates_df)
52
 
53
  # Allow user to engage in conversation
54
  user_input = st.text_input("Start a conversation:")
 
66
  response = completion.choices[0].message['content']
67
  st.write("AI's Response:")
68
  st.write(response)
 
 
 
 
 
69
  else:
70
  st.warning("Please enter your OpenAI API key.")
71
 
72
+ # Function to generate conversation prompt
73
+ def generate_conversation_prompt(df, boe_rates_df):
74
+ prompt = "I have analyzed the provided Excel sheet. "
75
+
76
+ # Include due dates, payment dates, and amounts from the Excel sheet
77
+ due_dates = df['due_date'].tolist()
78
+ payment_dates = df['payment_date'].tolist()
79
+ amounts = df['amount'].tolist()
80
+ prompt += f"The due dates in the sheet are: {', '.join(str(date) for date in due_dates)}. "
81
+ prompt += f"The payment dates in the sheet are: {', '.join(str(date) for date in payment_dates)}. "
82
+ prompt += f"The amounts in the sheet are: {', '.join(str(amount) for amount in amounts)}. "
83
+
84
+ # Include Bank of England base rates
85
+ if boe_rates_df is not None:
86
+ prompt += "The Bank of England base rates are as follows: \n"
87
+ for index, row in boe_rates_df.iterrows():
88
+ prompt += f"On {row['Date Changed']}, the base rate was {row['Current Bank Rate']}. \n"
89
+
90
+ prompt += "Based on this information, what would you like to discuss?"
91
+
92
+ return prompt
93
 
94
+ # Function to calculate late interest
95
  def calculate_late_interest(data, late_interest_rate, boe_rates_df):
96
  # Convert due_date column to Timestamp objects
97
  data['due_date'] = pd.to_datetime(data['due_date'])
98
+ data['payment_date'] = pd.to_datetime(data['payment_date'])
99
 
100
  # Calculate late days and late interest
101
  data['late_days'] = (data['payment_date'] - data['due_date']).dt.days.clip(lower=0)
102
  data['late_interest'] = data['late_days'] * data['amount'] * (late_interest_rate / 100)
103
 
104
  # Consider additional factors like Bank of England base rate
105
+ if boe_rates_df is not None:
106
+ data['boe_base_rate'] = data['due_date'].map(lambda x: get_boe_base_rate(x, boe_rates_df))
107
+ data['late_interest'] += data['amount'] * (data['boe_base_rate'] / 100)
108
 
109
  return data
110
 
 
 
 
 
 
 
111
  # Function to analyze Excel sheet and extract relevant information
112
  def analyze_excel(df):
113
  # Extract due dates and payment dates
 
144
  st.error(f"Failed to download rates: {e}")
145
  return None
146
 
147
+ # Function to get Bank of England base rate for a given date
148
+ def get_boe_base_rate(date, boe_rates_df):
149
+ closest_date = boe_rates_df['Date Changed'].iloc[(boe_rates_df['Date Changed'] - date).abs().argsort()[0]]
150
+ return boe_rates_df.loc[boe_rates_df['Date Changed'] == closest_date, 'Current Bank Rate'].values[0]
 
 
 
 
 
 
 
 
151
 
152
  if __name__ == "__main__":
153
  main()