Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,15 +10,6 @@ import io
|
|
10 |
logging.basicConfig(level=logging.INFO)
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
-
# Try importing PyPDF2
|
14 |
-
try:
|
15 |
-
import PyPDF2
|
16 |
-
logger.info("PyPDF2 imported successfully")
|
17 |
-
except ImportError as e:
|
18 |
-
logger.error(f"Error importing PyPDF2: {str(e)}")
|
19 |
-
logger.info("Falling back to text-only mode")
|
20 |
-
PyPDF2 = None
|
21 |
-
|
22 |
class FinancialAnalyzer:
|
23 |
def __init__(self):
|
24 |
"""Initialize models with error handling"""
|
@@ -50,65 +41,24 @@ class FinancialAnalyzer:
|
|
50 |
except Exception as e:
|
51 |
logger.error(f"Error initializing models: {str(e)}")
|
52 |
raise
|
53 |
-
|
54 |
-
def read_file(self, file_obj):
|
55 |
-
"""Safely read file content"""
|
56 |
-
try:
|
57 |
-
# If file_obj is a string (file path)
|
58 |
-
if isinstance(file_obj, str):
|
59 |
-
return open(file_obj, 'rb')
|
60 |
-
# If file_obj is bytes
|
61 |
-
elif isinstance(file_obj, bytes):
|
62 |
-
return io.BytesIO(file_obj)
|
63 |
-
# If file_obj is already a file object
|
64 |
-
elif hasattr(file_obj, 'read'):
|
65 |
-
return file_obj
|
66 |
-
else:
|
67 |
-
raise ValueError(f"Unsupported file object type: {type(file_obj)}")
|
68 |
-
except Exception as e:
|
69 |
-
logger.error(f"Error reading file: {str(e)}")
|
70 |
-
raise
|
71 |
-
|
72 |
-
def extract_text_from_pdf(self, pdf_file):
|
73 |
-
"""Extract text from PDF file with fallback"""
|
74 |
-
if PyPDF2 is None:
|
75 |
-
logger.warning("PyPDF2 not available, skipping PDF processing")
|
76 |
-
return "PDF processing not available"
|
77 |
-
|
78 |
-
try:
|
79 |
-
# Handle the file object properly
|
80 |
-
pdf_file_obj = self.read_file(pdf_file)
|
81 |
-
pdf_reader = PyPDF2.PdfReader(pdf_file_obj)
|
82 |
-
text = ""
|
83 |
-
for page in pdf_reader.pages:
|
84 |
-
text += page.extract_text() + "\n"
|
85 |
-
return text
|
86 |
-
except Exception as e:
|
87 |
-
logger.error(f"Error extracting PDF text: {str(e)}")
|
88 |
-
return "Error processing PDF"
|
89 |
|
90 |
def read_csv_file(self, file_obj):
|
91 |
"""Safely read CSV file"""
|
92 |
try:
|
93 |
-
|
94 |
-
|
95 |
-
return pd.read_csv(self.read_file(file_obj))
|
96 |
return pd.read_csv(file_obj)
|
97 |
except Exception as e:
|
98 |
logger.error(f"Error reading CSV file: {str(e)}")
|
99 |
raise
|
100 |
|
101 |
-
def generate_strategic_analysis(self, financial_data
|
102 |
"""Generate strategic analysis using Llama 2"""
|
103 |
try:
|
104 |
-
# Include KPI data if available
|
105 |
-
kpi_section = f"\nKPI Information:\n{kpi_data}" if kpi_data else ""
|
106 |
-
|
107 |
prompt = f"""[INST] As a senior financial analyst, analyze these financial statements:
|
108 |
|
109 |
Financial Data:
|
110 |
{financial_data}
|
111 |
-
{kpi_section}
|
112 |
|
113 |
Provide:
|
114 |
1. Business Health Assessment
|
@@ -158,7 +108,7 @@ class FinancialAnalyzer:
|
|
158 |
logger.error(f"Error generating recommendations: {str(e)}")
|
159 |
return "Error generating recommendations"
|
160 |
|
161 |
-
def analyze_financial_statements(income_statement, balance_sheet
|
162 |
"""Main analysis function with error handling"""
|
163 |
try:
|
164 |
# Initialize analyzer
|
@@ -178,34 +128,28 @@ def analyze_financial_statements(income_statement, balance_sheet, kpi_pdf=None):
|
|
178 |
{balance_df.to_string()}
|
179 |
"""
|
180 |
|
181 |
-
# Process KPI PDF if provided
|
182 |
-
kpi_data = None
|
183 |
-
if kpi_pdf is not None:
|
184 |
-
logger.info("Processing KPI PDF...")
|
185 |
-
kpi_data = analyzer.extract_text_from_pdf(kpi_pdf)
|
186 |
-
|
187 |
# Generate analyses
|
188 |
logger.info("Generating analysis...")
|
189 |
-
strategic_analysis = analyzer.generate_strategic_analysis(financial_data
|
190 |
sentiment = analyzer.analyze_sentiment(strategic_analysis)
|
191 |
recommendations = analyzer.generate_recommendations(strategic_analysis)
|
192 |
|
193 |
# Format output
|
194 |
logger.info("Formatting results...")
|
195 |
-
return format_results(strategic_analysis, sentiment, recommendations
|
196 |
|
197 |
except Exception as e:
|
198 |
logger.error(f"Error in analysis: {str(e)}")
|
199 |
return f"""Error analyzing files: {str(e)}
|
200 |
|
201 |
Please check:
|
202 |
-
1. Files are in correct
|
203 |
2. Files are not corrupted
|
204 |
3. Files contain the expected data
|
205 |
|
206 |
If the problem persists, try uploading the files again."""
|
207 |
|
208 |
-
def format_results(analysis, sentiment, recommendations
|
209 |
"""Format analysis results"""
|
210 |
try:
|
211 |
output = "# Financial Analysis Report\n\n"
|
@@ -224,11 +168,6 @@ def format_results(analysis, sentiment, recommendations, kpi_data=None):
|
|
224 |
output += "## Strategic Recommendations\n\n"
|
225 |
output += recommendations
|
226 |
|
227 |
-
# KPI Analysis (if available)
|
228 |
-
if kpi_data:
|
229 |
-
output += "\n\n## KPI Analysis\n\n"
|
230 |
-
output += "KPI data was included in the analysis.\n"
|
231 |
-
|
232 |
return output
|
233 |
except Exception as e:
|
234 |
logger.error(f"Error formatting results: {str(e)}")
|
@@ -239,10 +178,7 @@ iface = gr.Interface(
|
|
239 |
fn=analyze_financial_statements,
|
240 |
inputs=[
|
241 |
gr.File(label="Income Statement (CSV)"),
|
242 |
-
gr.File(label="Balance Sheet (CSV)")
|
243 |
-
gr.File(label="KPI Documentation (PDF, Optional)",
|
244 |
-
file_types=[".pdf"],
|
245 |
-
optional=True)
|
246 |
],
|
247 |
outputs=gr.Markdown(),
|
248 |
title="AI-Powered Financial Statement Analysis",
|
@@ -253,8 +189,7 @@ iface = gr.Interface(
|
|
253 |
examples=[
|
254 |
[
|
255 |
"OFINTECH-Income Statement-template.csv",
|
256 |
-
"OFINTECH Balance Sheet template.csv"
|
257 |
-
None
|
258 |
]
|
259 |
]
|
260 |
)
|
|
|
10 |
logging.basicConfig(level=logging.INFO)
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
class FinancialAnalyzer:
|
14 |
def __init__(self):
|
15 |
"""Initialize models with error handling"""
|
|
|
41 |
except Exception as e:
|
42 |
logger.error(f"Error initializing models: {str(e)}")
|
43 |
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
def read_csv_file(self, file_obj):
|
46 |
"""Safely read CSV file"""
|
47 |
try:
|
48 |
+
if file_obj is None:
|
49 |
+
raise ValueError("No file provided")
|
|
|
50 |
return pd.read_csv(file_obj)
|
51 |
except Exception as e:
|
52 |
logger.error(f"Error reading CSV file: {str(e)}")
|
53 |
raise
|
54 |
|
55 |
+
def generate_strategic_analysis(self, financial_data):
|
56 |
"""Generate strategic analysis using Llama 2"""
|
57 |
try:
|
|
|
|
|
|
|
58 |
prompt = f"""[INST] As a senior financial analyst, analyze these financial statements:
|
59 |
|
60 |
Financial Data:
|
61 |
{financial_data}
|
|
|
62 |
|
63 |
Provide:
|
64 |
1. Business Health Assessment
|
|
|
108 |
logger.error(f"Error generating recommendations: {str(e)}")
|
109 |
return "Error generating recommendations"
|
110 |
|
111 |
+
def analyze_financial_statements(income_statement, balance_sheet):
|
112 |
"""Main analysis function with error handling"""
|
113 |
try:
|
114 |
# Initialize analyzer
|
|
|
128 |
{balance_df.to_string()}
|
129 |
"""
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
# Generate analyses
|
132 |
logger.info("Generating analysis...")
|
133 |
+
strategic_analysis = analyzer.generate_strategic_analysis(financial_data)
|
134 |
sentiment = analyzer.analyze_sentiment(strategic_analysis)
|
135 |
recommendations = analyzer.generate_recommendations(strategic_analysis)
|
136 |
|
137 |
# Format output
|
138 |
logger.info("Formatting results...")
|
139 |
+
return format_results(strategic_analysis, sentiment, recommendations)
|
140 |
|
141 |
except Exception as e:
|
142 |
logger.error(f"Error in analysis: {str(e)}")
|
143 |
return f"""Error analyzing files: {str(e)}
|
144 |
|
145 |
Please check:
|
146 |
+
1. Files are in correct CSV format
|
147 |
2. Files are not corrupted
|
148 |
3. Files contain the expected data
|
149 |
|
150 |
If the problem persists, try uploading the files again."""
|
151 |
|
152 |
+
def format_results(analysis, sentiment, recommendations):
|
153 |
"""Format analysis results"""
|
154 |
try:
|
155 |
output = "# Financial Analysis Report\n\n"
|
|
|
168 |
output += "## Strategic Recommendations\n\n"
|
169 |
output += recommendations
|
170 |
|
|
|
|
|
|
|
|
|
|
|
171 |
return output
|
172 |
except Exception as e:
|
173 |
logger.error(f"Error formatting results: {str(e)}")
|
|
|
178 |
fn=analyze_financial_statements,
|
179 |
inputs=[
|
180 |
gr.File(label="Income Statement (CSV)"),
|
181 |
+
gr.File(label="Balance Sheet (CSV)")
|
|
|
|
|
|
|
182 |
],
|
183 |
outputs=gr.Markdown(),
|
184 |
title="AI-Powered Financial Statement Analysis",
|
|
|
189 |
examples=[
|
190 |
[
|
191 |
"OFINTECH-Income Statement-template.csv",
|
192 |
+
"OFINTECH Balance Sheet template.csv"
|
|
|
193 |
]
|
194 |
]
|
195 |
)
|