Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -329,6 +329,106 @@ Provide:
|
|
329 |
recommendations.append("- Focus on improving operational efficiency to enhance returns")
|
330 |
if metrics.get('revenue_growth', 0) < 5:
|
331 |
recommendations.append("- Develop strategies to accelerate revenue growth")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
|
333 |
def create_interface():
|
334 |
analyzer = FinancialAnalyzer()
|
|
|
329 |
recommendations.append("- Focus on improving operational efficiency to enhance returns")
|
330 |
if metrics.get('revenue_growth', 0) < 5:
|
331 |
recommendations.append("- Develop strategies to accelerate revenue growth")
|
332 |
+
recommendations.append("- Consider strategic acquisitions or new market entry")
|
333 |
+
|
334 |
+
return "Key Recommendations:\n" + "\n".join(recommendations)
|
335 |
+
|
336 |
+
def analyze_financials(self, balance_sheet_file, income_stmt_file):
|
337 |
+
"""Main analysis function"""
|
338 |
+
try:
|
339 |
+
# Validate input files
|
340 |
+
if not (self.is_valid_markdown(balance_sheet_file) and self.is_valid_markdown(income_stmt_file)):
|
341 |
+
return "Error: One or both files are invalid or not in Markdown format."
|
342 |
+
|
343 |
+
# Read files
|
344 |
+
with open(balance_sheet_file, 'r') as f:
|
345 |
+
balance_sheet = f.read()
|
346 |
+
with open(income_stmt_file, 'r') as f:
|
347 |
+
income_stmt = f.read()
|
348 |
+
|
349 |
+
# Process financial data
|
350 |
+
income_data = self.parse_financial_data(income_stmt)
|
351 |
+
balance_data = self.parse_financial_data(balance_sheet)
|
352 |
+
|
353 |
+
# Calculate metrics
|
354 |
+
metrics = self.calculate_metrics(income_data, balance_data)
|
355 |
+
self.last_metrics = metrics
|
356 |
+
|
357 |
+
# Generate analysis
|
358 |
+
analysis = self.generate_analysis(metrics)
|
359 |
+
|
360 |
+
# Prepare final results
|
361 |
+
results = {
|
362 |
+
"Financial Analysis": {
|
363 |
+
"Key Metrics": {
|
364 |
+
"Profitability": {
|
365 |
+
"Gross Profit Margin": f"{metrics['gross_profit_margin']:.2f}%",
|
366 |
+
"Net Profit Margin": f"{metrics['net_profit_margin']:.2f}%",
|
367 |
+
"Return on Equity": f"{metrics['roe']:.2f}%"
|
368 |
+
},
|
369 |
+
"Liquidity": {
|
370 |
+
"Current Ratio": f"{metrics['current_ratio']:.2f}",
|
371 |
+
"Accounts Receivable Turnover": f"{metrics['ar_turnover']:.2f}"
|
372 |
+
},
|
373 |
+
"Solvency": {
|
374 |
+
"Debt Ratio": f"{metrics['debt_ratio']:.2f}%",
|
375 |
+
"Retained Earnings Ratio": f"{metrics['retained_earnings_ratio']:.2f}%"
|
376 |
+
},
|
377 |
+
"Growth": {
|
378 |
+
"Sustainable Growth Rate": f"{metrics['sgr']:.2f}%",
|
379 |
+
"Revenue Growth (YoY)": f"{metrics['revenue_growth']:.2f}%"
|
380 |
+
}
|
381 |
+
},
|
382 |
+
"Analysis": analysis,
|
383 |
+
"Analysis Period": "2021-2025",
|
384 |
+
"Note": "Analysis based on CFI standards"
|
385 |
+
}
|
386 |
+
}
|
387 |
+
|
388 |
+
return json.dumps(results, indent=2)
|
389 |
+
|
390 |
+
except Exception as e:
|
391 |
+
return f"Error in analysis: {str(e)}\n\nDetails: {type(e).__name__}"
|
392 |
+
|
393 |
+
def fine_tune_models(self, train_texts, train_labels, epochs=3):
|
394 |
+
"""Fine-tune the model with custom data"""
|
395 |
+
try:
|
396 |
+
# Prepare dataset
|
397 |
+
train_dataset = FinancialDataset(train_texts, train_labels, self.llama_tokenizer)
|
398 |
+
|
399 |
+
# Training arguments
|
400 |
+
training_args = TrainingArguments(
|
401 |
+
output_dir="./financial_model_tuned",
|
402 |
+
num_train_epochs=epochs,
|
403 |
+
per_device_train_batch_size=4,
|
404 |
+
logging_dir="./logs",
|
405 |
+
logging_steps=10,
|
406 |
+
save_steps=50,
|
407 |
+
eval_steps=50,
|
408 |
+
learning_rate=2e-5,
|
409 |
+
weight_decay=0.01,
|
410 |
+
warmup_steps=500
|
411 |
+
)
|
412 |
+
|
413 |
+
# Initialize trainer
|
414 |
+
trainer = Trainer(
|
415 |
+
model=self.llama_model,
|
416 |
+
args=training_args,
|
417 |
+
train_dataset=train_dataset
|
418 |
+
)
|
419 |
+
|
420 |
+
# Fine-tune the model
|
421 |
+
trainer.train()
|
422 |
+
|
423 |
+
# Save the fine-tuned model
|
424 |
+
self.llama_model.save_pretrained("./financial_model_tuned")
|
425 |
+
self.llama_tokenizer.save_pretrained("./financial_model_tuned")
|
426 |
+
|
427 |
+
print("Fine-tuning completed successfully!")
|
428 |
+
except Exception as e:
|
429 |
+
print(f"Error in fine-tuning: {str(e)}")
|
430 |
+
|
431 |
+
|
432 |
|
433 |
def create_interface():
|
434 |
analyzer = FinancialAnalyzer()
|