Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,13 +9,12 @@ from transformers import (
|
|
9 |
import torch
|
10 |
import pandas as pd
|
11 |
import json
|
12 |
-
from huggingface_hub import login
|
13 |
|
14 |
class FinancialAnalyzer:
|
15 |
def __init__(self):
|
16 |
print("Loading models...")
|
17 |
try:
|
18 |
-
# Initialize TinyLlama
|
19 |
self.tiny_tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
20 |
self.tiny_model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
21 |
|
@@ -27,7 +26,7 @@ class FinancialAnalyzer:
|
|
27 |
self.t5_tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
28 |
self.t5_model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
29 |
|
30 |
-
self.device = "cpu"
|
31 |
self._move_models_to_device()
|
32 |
print("Models loaded successfully!")
|
33 |
except Exception as e:
|
@@ -39,70 +38,54 @@ class FinancialAnalyzer:
|
|
39 |
self.finbert_model.to(self.device)
|
40 |
self.t5_model.to(self.device)
|
41 |
|
42 |
-
def
|
|
|
|
|
|
|
|
|
43 |
try:
|
44 |
-
if
|
45 |
df = pd.read_csv(file.name)
|
46 |
return df.to_string()
|
47 |
-
elif
|
48 |
df = pd.read_excel(file.name)
|
49 |
return df.to_string()
|
50 |
-
elif
|
51 |
with open(file.name, 'r') as f:
|
52 |
return f.read()
|
|
|
|
|
53 |
except Exception as e:
|
54 |
return f"Error processing file: {str(e)}"
|
55 |
|
56 |
-
def
|
|
|
57 |
try:
|
58 |
-
#
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
61 |
|
62 |
-
# Format
|
63 |
-
prompt =
|
64 |
|
65 |
-
|
66 |
-
|
67 |
|
68 |
-
|
69 |
-
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
"Recommendations": recommendations
|
79 |
-
}
|
80 |
-
|
81 |
-
return json.dumps(analysis_results, indent=2)
|
82 |
-
|
83 |
-
except Exception as e:
|
84 |
-
return f"Error during analysis: {str(e)}"
|
85 |
-
|
86 |
-
def format_financial_prompt(self, balance_sheet, income_statement):
|
87 |
-
return f"""<human>Please analyze these financial statements and provide key insights:
|
88 |
-
|
89 |
-
Balance Sheet Summary:
|
90 |
-
{balance_sheet[:1000]}
|
91 |
-
|
92 |
-
Income Statement Summary:
|
93 |
-
{income_statement[:1000]}
|
94 |
-
|
95 |
-
Please provide:
|
96 |
-
1. Key financial metrics analysis
|
97 |
-
2. Growth trends
|
98 |
-
3. Risk factors
|
99 |
-
4. Areas of concern
|
100 |
-
5. Positive indicators</human>
|
101 |
-
|
102 |
-
<assistant>I'll analyze the financial statements and provide comprehensive insights:"""
|
103 |
|
104 |
-
|
105 |
-
try:
|
106 |
inputs = self.tiny_tokenizer(prompt, return_tensors="pt", max_length=1024, truncation=True)
|
107 |
outputs = self.tiny_model.generate(
|
108 |
inputs["input_ids"],
|
@@ -112,9 +95,21 @@ Please provide:
|
|
112 |
do_sample=True,
|
113 |
pad_token_id=self.tiny_tokenizer.eos_token_id
|
114 |
)
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
except Exception as e:
|
117 |
-
return f"Error
|
118 |
|
119 |
def analyze_sentiment(self, balance_sheet, income_statement):
|
120 |
try:
|
@@ -125,58 +120,36 @@ Please provide:
|
|
125 |
labels = ['negative', 'neutral', 'positive']
|
126 |
return {
|
127 |
'sentiment': labels[probs.argmax().item()],
|
128 |
-
'confidence': f"{probs.max().item():.2f}"
|
129 |
-
'detailed_scores': {
|
130 |
-
label: f"{prob:.2f}"
|
131 |
-
for label, prob in zip(labels, probs[0].tolist())
|
132 |
-
}
|
133 |
}
|
134 |
except Exception as e:
|
135 |
return f"Error in sentiment analysis: {str(e)}"
|
136 |
|
137 |
-
|
138 |
-
try:
|
139 |
-
prompt = f"summarize financial recommendations based on: {insights[:500]} Financial sentiment: {sentiment}"
|
140 |
-
inputs = self.t5_tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
141 |
-
outputs = self.t5_model.generate(
|
142 |
-
inputs["input_ids"],
|
143 |
-
max_length=200,
|
144 |
-
num_beams=4,
|
145 |
-
temperature=0.7,
|
146 |
-
top_p=0.95
|
147 |
-
)
|
148 |
-
return self.t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
149 |
-
except Exception as e:
|
150 |
-
return f"Error generating recommendations: {str(e)}"
|
151 |
-
|
152 |
-
def create_gradio_interface():
|
153 |
analyzer = FinancialAnalyzer()
|
154 |
|
155 |
-
def analyze_files(balance_sheet, income_statement, file_type):
|
156 |
-
return analyzer.analyze_financials(balance_sheet, income_statement, file_type)
|
157 |
-
|
158 |
iface = gr.Interface(
|
159 |
-
fn=
|
160 |
inputs=[
|
161 |
-
gr.File(
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
167 |
)
|
168 |
],
|
169 |
-
outputs=gr.Textbox(
|
|
|
|
|
|
|
170 |
title="Financial Statement Analyzer",
|
171 |
-
description="Upload your financial statements to get AI-powered insights and
|
172 |
-
examples=[
|
173 |
-
["balance_sheet.csv", "income_statement.csv", "csv"],
|
174 |
-
["balance_sheet.xlsx", "income_statement.xlsx", "excel"],
|
175 |
-
["balance_sheet.md", "income_statement.md", "markdown"]
|
176 |
-
]
|
177 |
)
|
|
|
178 |
return iface
|
179 |
|
180 |
if __name__ == "__main__":
|
181 |
-
iface =
|
182 |
iface.launch()
|
|
|
9 |
import torch
|
10 |
import pandas as pd
|
11 |
import json
|
|
|
12 |
|
13 |
class FinancialAnalyzer:
|
14 |
def __init__(self):
|
15 |
print("Loading models...")
|
16 |
try:
|
17 |
+
# Initialize TinyLlama
|
18 |
self.tiny_tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
19 |
self.tiny_model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
20 |
|
|
|
26 |
self.t5_tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
27 |
self.t5_model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
28 |
|
29 |
+
self.device = "cpu"
|
30 |
self._move_models_to_device()
|
31 |
print("Models loaded successfully!")
|
32 |
except Exception as e:
|
|
|
38 |
self.finbert_model.to(self.device)
|
39 |
self.t5_model.to(self.device)
|
40 |
|
41 |
+
def read_file_content(self, file):
|
42 |
+
"""Read and process uploaded file content"""
|
43 |
+
if file is None:
|
44 |
+
return "No file uploaded"
|
45 |
+
|
46 |
try:
|
47 |
+
if file.name.endswith('.csv'):
|
48 |
df = pd.read_csv(file.name)
|
49 |
return df.to_string()
|
50 |
+
elif file.name.endswith(('.xls', '.xlsx')):
|
51 |
df = pd.read_excel(file.name)
|
52 |
return df.to_string()
|
53 |
+
elif file.name.endswith('.md'):
|
54 |
with open(file.name, 'r') as f:
|
55 |
return f.read()
|
56 |
+
else:
|
57 |
+
return "Unsupported file format. Please upload CSV, Excel, or Markdown files."
|
58 |
except Exception as e:
|
59 |
return f"Error processing file: {str(e)}"
|
60 |
|
61 |
+
def analyze_financial_data(self, balance_sheet_file, income_statement_file):
|
62 |
+
"""Analyze uploaded financial statements"""
|
63 |
try:
|
64 |
+
# Read file contents
|
65 |
+
balance_sheet = self.read_file_content(balance_sheet_file)
|
66 |
+
income_statement = self.read_file_content(income_statement_file)
|
67 |
+
|
68 |
+
if "Error" in balance_sheet or "Error" in income_statement:
|
69 |
+
return "Error processing files. Please check the file format and content."
|
70 |
|
71 |
+
# Format prompt for analysis
|
72 |
+
prompt = f"""<human>Analyze these financial statements:
|
73 |
|
74 |
+
Balance Sheet:
|
75 |
+
{balance_sheet[:1000]}
|
76 |
|
77 |
+
Income Statement:
|
78 |
+
{income_statement[:1000]}
|
79 |
|
80 |
+
Provide:
|
81 |
+
1. Key financial metrics
|
82 |
+
2. Growth trends
|
83 |
+
3. Risk analysis
|
84 |
+
4. Recommendations</human>
|
85 |
+
|
86 |
+
<assistant>Here's my analysis:"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
+
# Generate analysis using TinyLlama
|
|
|
89 |
inputs = self.tiny_tokenizer(prompt, return_tensors="pt", max_length=1024, truncation=True)
|
90 |
outputs = self.tiny_model.generate(
|
91 |
inputs["input_ids"],
|
|
|
95 |
do_sample=True,
|
96 |
pad_token_id=self.tiny_tokenizer.eos_token_id
|
97 |
)
|
98 |
+
analysis = self.tiny_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
99 |
+
|
100 |
+
# Generate sentiment
|
101 |
+
sentiment = self.analyze_sentiment(balance_sheet, income_statement)
|
102 |
+
|
103 |
+
# Format results
|
104 |
+
results = {
|
105 |
+
"Analysis": analysis,
|
106 |
+
"Sentiment": sentiment
|
107 |
+
}
|
108 |
+
|
109 |
+
return json.dumps(results, indent=2)
|
110 |
+
|
111 |
except Exception as e:
|
112 |
+
return f"Error during analysis: {str(e)}"
|
113 |
|
114 |
def analyze_sentiment(self, balance_sheet, income_statement):
|
115 |
try:
|
|
|
120 |
labels = ['negative', 'neutral', 'positive']
|
121 |
return {
|
122 |
'sentiment': labels[probs.argmax().item()],
|
123 |
+
'confidence': f"{probs.max().item():.2f}"
|
|
|
|
|
|
|
|
|
124 |
}
|
125 |
except Exception as e:
|
126 |
return f"Error in sentiment analysis: {str(e)}"
|
127 |
|
128 |
+
def create_interface():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
analyzer = FinancialAnalyzer()
|
130 |
|
|
|
|
|
|
|
131 |
iface = gr.Interface(
|
132 |
+
fn=analyzer.analyze_financial_data,
|
133 |
inputs=[
|
134 |
+
gr.File(
|
135 |
+
label="Upload Balance Sheet (CSV, Excel, or Markdown)",
|
136 |
+
type="file"
|
137 |
+
),
|
138 |
+
gr.File(
|
139 |
+
label="Upload Income Statement (CSV, Excel, or Markdown)",
|
140 |
+
type="file"
|
141 |
)
|
142 |
],
|
143 |
+
outputs=gr.Textbox(
|
144 |
+
label="Analysis Results",
|
145 |
+
lines=20
|
146 |
+
),
|
147 |
title="Financial Statement Analyzer",
|
148 |
+
description="Upload your financial statements (Balance Sheet and Income Statement) to get AI-powered insights and analysis."
|
|
|
|
|
|
|
|
|
|
|
149 |
)
|
150 |
+
|
151 |
return iface
|
152 |
|
153 |
if __name__ == "__main__":
|
154 |
+
iface = create_interface()
|
155 |
iface.launch()
|