Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -333,32 +333,32 @@ Provide:
|
|
333 |
|
334 |
return "Key Recommendations:\n" + "\n".join(recommendations)
|
335 |
|
336 |
-
|
337 |
"""Main analysis function"""
|
338 |
-
|
339 |
# Validate input files
|
340 |
-
|
341 |
-
|
342 |
|
343 |
# Read files
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
|
349 |
# Process financial data
|
350 |
-
|
351 |
-
|
352 |
|
353 |
# Calculate metrics
|
354 |
-
|
355 |
-
|
356 |
|
357 |
# Generate analysis
|
358 |
-
|
359 |
|
360 |
# Prepare final results
|
361 |
-
|
362 |
"Financial Analysis": {
|
363 |
"Key Metrics": {
|
364 |
"Profitability": {
|
@@ -385,19 +385,19 @@ Provide:
|
|
385 |
}
|
386 |
}
|
387 |
|
388 |
-
|
389 |
|
390 |
-
|
391 |
-
|
392 |
|
393 |
-
|
394 |
"""Fine-tune the model with custom data"""
|
395 |
-
|
396 |
# Prepare dataset
|
397 |
-
|
398 |
|
399 |
# Training arguments
|
400 |
-
|
401 |
output_dir="./financial_model_tuned",
|
402 |
num_train_epochs=epochs,
|
403 |
per_device_train_batch_size=4,
|
@@ -411,22 +411,22 @@ Provide:
|
|
411 |
)
|
412 |
|
413 |
# Initialize trainer
|
414 |
-
|
415 |
model=self.llama_model,
|
416 |
args=training_args,
|
417 |
train_dataset=train_dataset
|
418 |
)
|
419 |
|
420 |
# Fine-tune the model
|
421 |
-
|
422 |
|
423 |
# Save the fine-tuned model
|
424 |
-
|
425 |
-
|
426 |
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
|
431 |
|
432 |
|
|
|
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": {
|
|
|
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,
|
|
|
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 |
|