walaa2022 commited on
Commit
bc8be1d
·
verified ·
1 Parent(s): 30c4182

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -62,11 +62,14 @@ class FinancialAnalyzer:
62
  logger.error(f"Error loading models: {str(e)}")
63
  raise
64
 
65
- def analyze_financials(self, csv_data: str) -> str:
66
  """Generate financial analysis using Tiny Llama and analyze sentiment using FinBERT"""
67
  try:
 
 
 
68
  # Generate status and insights using Tiny Llama
69
- status_prompt = f"Please analyze the following financial data and provide status, insights, and metrics:\n\n{csv_data}"
70
  response = self.analysis_model(
71
  status_prompt,
72
  max_length=1500,
@@ -94,12 +97,9 @@ class FinancialAnalyzer:
94
 
95
  # Return a comprehensive report
96
  result = f"""# Financial Analysis Report
97
-
98
  ### Sentiment Analysis: {sentiment_label} ({sentiment_score:.1%})
99
-
100
  ### Financial Status and Insights:
101
  {insights_result}
102
-
103
  ### Recommendations and Roadmap:
104
  {roadmap_result}
105
  """
@@ -109,11 +109,10 @@ class FinancialAnalyzer:
109
  logger.error(f"Analysis error: {str(e)}")
110
  return f"Analysis Error: {str(e)}"
111
 
112
- # Function to read CSV and convert to text format
113
- def csv_to_text(file_path: str) -> str:
114
- """Convert CSV to raw text format for model input"""
115
- df = pd.read_csv(file_path)
116
- return df.to_string(index=False) # Convert DataFrame to string without index
117
 
118
  def analyze_statements(income_statement, balance_sheet):
119
  """Main function to analyze financial statements"""
@@ -121,16 +120,13 @@ def analyze_statements(income_statement, balance_sheet):
121
  if not income_statement or not balance_sheet:
122
  return "Please upload both Income Statement and Balance Sheet CSV files."
123
 
124
- # Read files as raw text (no need to clean manually)
125
- income_data = csv_to_text(income_statement.name)
126
- balance_data = csv_to_text(balance_sheet.name)
127
 
128
- # Combine the data for AI to process (can adjust prompt as needed)
129
- combined_data = f"Income Statement Data:\n{income_data}\n\nBalance Sheet Data:\n{balance_data}"
130
-
131
  # Create analyzer and process data
132
  analyzer = FinancialAnalyzer()
133
- result = analyzer.analyze_financials(combined_data)
134
 
135
  # Clear memory
136
  clear_gpu_memory()
@@ -167,4 +163,4 @@ if __name__ == "__main__":
167
  iface.launch(server_name="0.0.0.0", server_port=7860)
168
  except Exception as e:
169
  logger.error(f"Launch error: {str(e)}")
170
- sys.exit(1)
 
62
  logger.error(f"Error loading models: {str(e)}")
63
  raise
64
 
65
+ def analyze_financials(self, income_data: pd.DataFrame, balance_data: pd.DataFrame) -> str:
66
  """Generate financial analysis using Tiny Llama and analyze sentiment using FinBERT"""
67
  try:
68
+ # Combine the data for AI to process (can adjust prompt as needed)
69
+ combined_data = f"Income Statement Data:\n{income_data.to_string()}\n\nBalance Sheet Data:\n{balance_data.to_string()}"
70
+
71
  # Generate status and insights using Tiny Llama
72
+ status_prompt = f"Please analyze the following financial data and provide status, insights, and metrics:\n\n{combined_data}"
73
  response = self.analysis_model(
74
  status_prompt,
75
  max_length=1500,
 
97
 
98
  # Return a comprehensive report
99
  result = f"""# Financial Analysis Report
 
100
  ### Sentiment Analysis: {sentiment_label} ({sentiment_score:.1%})
 
101
  ### Financial Status and Insights:
102
  {insights_result}
 
103
  ### Recommendations and Roadmap:
104
  {roadmap_result}
105
  """
 
109
  logger.error(f"Analysis error: {str(e)}")
110
  return f"Analysis Error: {str(e)}"
111
 
112
+ # Function to read CSV and convert to DataFrame
113
+ def read_csv(file_path: str) -> pd.DataFrame:
114
+ """Read CSV and return a DataFrame"""
115
+ return pd.read_csv(file_path)
 
116
 
117
  def analyze_statements(income_statement, balance_sheet):
118
  """Main function to analyze financial statements"""
 
120
  if not income_statement or not balance_sheet:
121
  return "Please upload both Income Statement and Balance Sheet CSV files."
122
 
123
+ # Read files as DataFrames (no need to clean manually)
124
+ income_data = read_csv(income_statement.name)
125
+ balance_data = read_csv(balance_sheet.name)
126
 
 
 
 
127
  # Create analyzer and process data
128
  analyzer = FinancialAnalyzer()
129
+ result = analyzer.analyze_financials(income_data, balance_data)
130
 
131
  # Clear memory
132
  clear_gpu_memory()
 
163
  iface.launch(server_name="0.0.0.0", server_port=7860)
164
  except Exception as e:
165
  logger.error(f"Launch error: {str(e)}")
166
+ sys.exit(1)