rairo commited on
Commit
588e3c6
·
verified ·
1 Parent(s): 998dd70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -2
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import re
2
  import json
3
  import os
 
4
  from datetime import datetime
5
  from io import BytesIO
6
 
@@ -19,6 +20,7 @@ def configure_gemini(api_key):
19
 
20
  # Read PDF content from a file-like object (from Streamlit uploader)
21
  def read_pdf(file_obj):
 
22
  text_content = []
23
  pdf_reader = pypdf.PdfReader(file_obj)
24
  for page in pdf_reader.pages:
@@ -51,6 +53,7 @@ Return ONLY valid JSON with this structure:
51
  ]
52
  }"""
53
  response = model.generate_content([prompt, text])
 
54
  return response.text
55
 
56
  # Generate financial report from aggregated JSON transactions and chosen sections
@@ -62,6 +65,7 @@ Generate a detailed financial report that includes the following sections: {', '
62
  Ensure that each section is clearly formatted with headings and includes insights and summaries.
63
  Return the complete report as plain text."""
64
  response = model.generate_content([prompt])
 
65
  return response.text
66
 
67
  # Create a PDF file from the report text
@@ -80,11 +84,16 @@ def create_pdf_report(report_text):
80
  return pdf_buffer
81
 
82
  def main():
83
- st.title("Quantitlytix AI ")
84
  st.markdown("*Bank Statement Parser & Financial Report Generator*")
 
85
  # Allow multiple PDF uploads
86
  uploaded_files = st.file_uploader("Upload PDF bank statements", type="pdf", accept_multiple_files=True)
87
 
 
 
 
 
88
  if uploaded_files:
89
  try:
90
  # Initialize the Gemini model
@@ -94,10 +103,19 @@ def main():
94
  for uploaded_file in uploaded_files:
95
  # Read PDF text directly from the uploaded file
96
  pdf_text = read_pdf(uploaded_file)
 
 
 
 
97
  with st.spinner(f"Processing {uploaded_file.name}..."):
98
  json_response = process_with_gemini(model, pdf_text)
99
  # Extract valid JSON from the response
100
- json_str = json_response[json_response.find('{'):json_response.rfind('}')+1]
 
 
 
 
 
101
  json_str = json_str.replace('```json', '').replace('```', '')
102
  data = json.loads(json_str)
103
  transactions = data.get('transactions', [])
 
1
  import re
2
  import json
3
  import os
4
+ import time
5
  from datetime import datetime
6
  from io import BytesIO
7
 
 
20
 
21
  # Read PDF content from a file-like object (from Streamlit uploader)
22
  def read_pdf(file_obj):
23
+ file_obj.seek(0) # Ensure the file pointer is at the start
24
  text_content = []
25
  pdf_reader = pypdf.PdfReader(file_obj)
26
  for page in pdf_reader.pages:
 
53
  ]
54
  }"""
55
  response = model.generate_content([prompt, text])
56
+ time.sleep(7) # Sleep for 7 seconds to work around rate limit
57
  return response.text
58
 
59
  # Generate financial report from aggregated JSON transactions and chosen sections
 
65
  Ensure that each section is clearly formatted with headings and includes insights and summaries.
66
  Return the complete report as plain text."""
67
  response = model.generate_content([prompt])
68
+ time.sleep(7) # Sleep for 7 seconds to work around rate limit
69
  return response.text
70
 
71
  # Create a PDF file from the report text
 
84
  return pdf_buffer
85
 
86
  def main():
87
+ st.title("Quantitlytix AI")
88
  st.markdown("*Bank Statement Parser & Financial Report Generator*")
89
+
90
  # Allow multiple PDF uploads
91
  uploaded_files = st.file_uploader("Upload PDF bank statements", type="pdf", accept_multiple_files=True)
92
 
93
+ # Debug: Show number of files uploaded
94
+ if uploaded_files:
95
+ st.write(f"{len(uploaded_files)} file(s) uploaded.")
96
+
97
  if uploaded_files:
98
  try:
99
  # Initialize the Gemini model
 
103
  for uploaded_file in uploaded_files:
104
  # Read PDF text directly from the uploaded file
105
  pdf_text = read_pdf(uploaded_file)
106
+ if not pdf_text:
107
+ st.warning(f"No text found in {uploaded_file.name}.")
108
+ continue
109
+
110
  with st.spinner(f"Processing {uploaded_file.name}..."):
111
  json_response = process_with_gemini(model, pdf_text)
112
  # Extract valid JSON from the response
113
+ start_idx = json_response.find('{')
114
+ end_idx = json_response.rfind('}') + 1
115
+ if start_idx == -1 or end_idx == -1:
116
+ st.warning(f"Invalid JSON response for {uploaded_file.name}.")
117
+ continue
118
+ json_str = json_response[start_idx:end_idx]
119
  json_str = json_str.replace('```json', '').replace('```', '')
120
  data = json.loads(json_str)
121
  transactions = data.get('transactions', [])