albhu commited on
Commit
73327e8
·
verified ·
1 Parent(s): 4aa29f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -66
app.py CHANGED
@@ -1,7 +1,76 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import openai
4
- from dateutil import parser
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  # Function to calculate late interest
7
  def calculate_late_interest(data, late_interest_rate):
@@ -25,70 +94,5 @@ def analyze_excel(df):
25
 
26
  return due_dates, payment_dates, amounts
27
 
28
- # Streamlit App
29
- def main():
30
- st.title("Invoice Interest Calculator and Conversation")
31
-
32
- # Allow user to upload Excel sheet
33
- uploaded_file = st.file_uploader("Upload Excel file", type=["xlsx", "xls"])
34
-
35
- if uploaded_file is not None:
36
- df = pd.read_excel(uploaded_file)
37
-
38
- # Display uploaded data
39
- st.write("Uploaded Data:")
40
- st.write(df)
41
-
42
- # Analyze Excel sheet
43
- due_dates, payment_dates, amounts = analyze_excel(df)
44
-
45
- # Allow user to specify late interest rate
46
- late_interest_rate = st.number_input("Enter Late Interest Rate (%):", min_value=0.0, max_value=100.0, step=0.1)
47
-
48
- # Calculate late interest if due dates and payment dates are available
49
- if due_dates and payment_dates:
50
- # Create DataFrame with extracted due dates, payment dates, and placeholder amount
51
- df_calculate = pd.DataFrame({
52
- 'due_date': due_dates,
53
- 'payment_date': payment_dates,
54
- 'amount': [0] * len(due_dates) # Placeholder amount for calculation
55
- })
56
-
57
- # Calculate late interest
58
- df_with_interest = calculate_late_interest(df_calculate, late_interest_rate)
59
-
60
- # Display calculated late interest
61
- st.write("Calculated Late Interest:")
62
- st.write(df_with_interest['late_interest'].sum())
63
-
64
- # Generate conversation prompt
65
- prompt = "I have analyzed the provided Excel sheet. "
66
- if due_dates:
67
- prompt += f"The due dates in the sheet are: {', '.join(str(date) for date in due_dates)}. "
68
- if payment_dates:
69
- prompt += f"The payment dates in the sheet are: {', '.join(str(date) for date in payment_dates)}. "
70
- if amounts:
71
- prompt += f"The amounts in the sheet are: {', '.join(str(amount) for amount in amounts)}. "
72
- prompt += "Based on this information, what would you like to discuss?"
73
-
74
- # Allow user to engage in conversation
75
- user_input = st.text_input("Start a conversation:")
76
- if st.button("Send"):
77
- if 'api_key' not in st.session_state:
78
- st.session_state.api_key = st.text_input("Enter your OpenAI API key:")
79
- openai.api_key = st.session_state.api_key # Set OpenAI API key
80
-
81
- completion = openai.ChatCompletion.create(
82
- model="gpt-3.5-turbo",
83
- messages=[
84
- {"role": "system", "content": prompt},
85
- {"role": "user", "content": user_input}
86
- ],
87
- max_tokens=800 # Adjust this value to allow longer responses
88
- )
89
- response = completion.choices[0].message['content']
90
- st.write("AI's Response:")
91
- st.write(response)
92
-
93
  if __name__ == "__main__":
94
  main()
 
1
  import streamlit as st
2
  import pandas as pd
3
  import openai
4
+
5
+ # Streamlit App
6
+ def main():
7
+ st.title("Invoice Interest Calculator and Conversation")
8
+
9
+ # Prompt user for OpenAI API key
10
+ api_key = st.text_input("Enter your OpenAI API key:")
11
+
12
+ if api_key:
13
+ # Allow user to upload Excel sheet
14
+ uploaded_file = st.file_uploader("Upload Excel file", type=["xlsx", "xls"])
15
+
16
+ if uploaded_file is not None:
17
+ df = pd.read_excel(uploaded_file)
18
+
19
+ # Display uploaded data
20
+ st.write("Uploaded Data:")
21
+ st.write(df)
22
+
23
+ # Analyze Excel sheet
24
+ due_dates, payment_dates, amounts = analyze_excel(df)
25
+
26
+ # Allow user to specify late interest rate
27
+ late_interest_rate = st.number_input("Enter Late Interest Rate (%):", min_value=0.0, max_value=100.0, step=0.1)
28
+
29
+ # Calculate late interest if due dates and payment dates are available
30
+ if due_dates and payment_dates:
31
+ # Create DataFrame with extracted due dates, payment dates, and placeholder amount
32
+ df_calculate = pd.DataFrame({
33
+ 'due_date': due_dates,
34
+ 'payment_date': payment_dates,
35
+ 'amount': amounts
36
+ })
37
+
38
+ # Calculate late interest
39
+ df_with_interest = calculate_late_interest(df_calculate, late_interest_rate)
40
+
41
+ # Display calculated late interest
42
+ total_late_interest = df_with_interest['late_interest'].sum()
43
+ st.write("Calculated Late Interest:")
44
+ st.write(total_late_interest)
45
+
46
+ # Generate conversation prompt
47
+ prompt = "I have analyzed the provided Excel sheet. "
48
+ if due_dates:
49
+ prompt += f"The due dates in the sheet are: {', '.join(str(date) for date in due_dates)}. "
50
+ if payment_dates:
51
+ prompt += f"The payment dates in the sheet are: {', '.join(str(date) for date in payment_dates)}. "
52
+ if amounts:
53
+ prompt += f"The amounts in the sheet are: {', '.join(str(amount) for amount in amounts)}. "
54
+ prompt += "Based on this information, what would you like to discuss?"
55
+
56
+ # Allow user to engage in conversation
57
+ user_input = st.text_input("Start a conversation:")
58
+ if st.button("Send"):
59
+ openai.api_key = api_key # Set user-provided OpenAI API key
60
+
61
+ completion = openai.ChatCompletion.create(
62
+ model="gpt-3.5-turbo",
63
+ messages=[
64
+ {"role": "system", "content": prompt},
65
+ {"role": "user", "content": user_input}
66
+ ],
67
+ max_tokens=1800 # Adjust this value to allow longer responses
68
+ )
69
+ response = completion.choices[0].message['content']
70
+ st.write("AI's Response:")
71
+ st.write(response)
72
+ else:
73
+ st.warning("Please enter your OpenAI API key.")
74
 
75
  # Function to calculate late interest
76
  def calculate_late_interest(data, late_interest_rate):
 
94
 
95
  return due_dates, payment_dates, amounts
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  if __name__ == "__main__":
98
  main()