Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import (
|
4 |
+
AutoModelForCausalLM,
|
5 |
+
AutoTokenizer,
|
6 |
+
AutoModelForSequenceClassification,
|
7 |
+
T5ForConditionalGeneration,
|
8 |
+
T5Tokenizer
|
9 |
+
)
|
10 |
+
import pandas as pd
|
11 |
+
import numpy as np
|
12 |
+
import io
|
13 |
+
import json
|
14 |
+
|
15 |
+
class FinancialAnalyzer:
|
16 |
+
def __init__(self):
|
17 |
+
# Initialize models and tokenizers
|
18 |
+
print("Loading models...")
|
19 |
+
self.tiny_tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat")
|
20 |
+
self.tiny_model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat")
|
21 |
+
|
22 |
+
self.finbert_tokenizer = AutoTokenizer.from_pretrained("yiyanghkust/finbert-tone")
|
23 |
+
self.finbert_model = AutoModelForSequenceClassification.from_pretrained("yiyanghkust/finbert-tone")
|
24 |
+
|
25 |
+
self.t5_tokenizer = T5Tokenizer.from_pretrained("t5-base")
|
26 |
+
self.t5_model = T5ForConditionalGeneration.from_pretrained("t5-base")
|
27 |
+
|
28 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
29 |
+
self._move_models_to_device()
|
30 |
+
print("Models loaded successfully!")
|
31 |
+
|
32 |
+
def _move_models_to_device(self):
|
33 |
+
self.tiny_model.to(self.device)
|
34 |
+
self.finbert_model.to(self.device)
|
35 |
+
self.t5_model.to(self.device)
|
36 |
+
|
37 |
+
def process_file(self, file, file_type):
|
38 |
+
"""Process uploaded file based on its type"""
|
39 |
+
if file_type == "csv":
|
40 |
+
df = pd.read_csv(file)
|
41 |
+
return df.to_string()
|
42 |
+
elif file_type == "excel":
|
43 |
+
df = pd.read_excel(file)
|
44 |
+
return df.to_string()
|
45 |
+
elif file_type == "markdown":
|
46 |
+
return file.read().decode('utf-8')
|
47 |
+
else:
|
48 |
+
raise ValueError(f"Unsupported file type: {file_type}")
|
49 |
+
|
50 |
+
def analyze_financials(self, balance_sheet_file, income_statement_file, file_type="csv"):
|
51 |
+
"""Main analysis function for Gradio interface"""
|
52 |
+
try:
|
53 |
+
# Process uploaded files
|
54 |
+
balance_sheet_data = self.process_file(balance_sheet_file, file_type)
|
55 |
+
income_statement_data = self.process_file(income_statement_file, file_type)
|
56 |
+
|
57 |
+
# Generate insights using TinyLlama
|
58 |
+
insights = self.generate_insights(balance_sheet_data, income_statement_data)
|
59 |
+
|
60 |
+
# Generate sentiment analysis using FinBERT
|
61 |
+
sentiment = self.analyze_sentiment(balance_sheet_data, income_statement_data)
|
62 |
+
|
63 |
+
# Generate recommendations using T5
|
64 |
+
recommendations = self.generate_recommendations(balance_sheet_data, income_statement_data)
|
65 |
+
|
66 |
+
# Generate roadmap
|
67 |
+
roadmap = self.generate_roadmap(insights, sentiment, recommendations)
|
68 |
+
|
69 |
+
# Combine results
|
70 |
+
analysis_results = {
|
71 |
+
"Financial Insights": insights,
|
72 |
+
"Sentiment Analysis": sentiment,
|
73 |
+
"Recommendations": recommendations,
|
74 |
+
"Strategic Roadmap": roadmap
|
75 |
+
}
|
76 |
+
|
77 |
+
return json.dumps(analysis_results, indent=2)
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
return f"Error during analysis: {str(e)}"
|
81 |
+
|
82 |
+
def generate_insights(self, balance_sheet, income_statement):
|
83 |
+
prompt = f"""Analyze these financial statements and provide key insights:
|
84 |
+
Balance Sheet:
|
85 |
+
{balance_sheet[:1000]}
|
86 |
+
|
87 |
+
Income Statement:
|
88 |
+
{income_statement[:1000]}
|
89 |
+
"""
|
90 |
+
|
91 |
+
inputs = self.tiny_tokenizer(prompt, return_tensors="pt").to(self.device)
|
92 |
+
outputs = self.tiny_model.generate(
|
93 |
+
inputs["input_ids"],
|
94 |
+
max_length=500,
|
95 |
+
temperature=0.7
|
96 |
+
)
|
97 |
+
return self.tiny_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
98 |
+
|
99 |
+
def analyze_sentiment(self, balance_sheet, income_statement):
|
100 |
+
financial_text = f"{balance_sheet[:500]}\n{income_statement[:500]}"
|
101 |
+
inputs = self.finbert_tokenizer(financial_text, return_tensors="pt").to(self.device)
|
102 |
+
outputs = self.finbert_model(**inputs)
|
103 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=1)
|
104 |
+
sentiment_labels = ['negative', 'neutral', 'positive']
|
105 |
+
|
106 |
+
return {
|
107 |
+
'sentiment': sentiment_labels[probabilities.argmax().item()],
|
108 |
+
'confidence': f"{probabilities.max().item():.2f}"
|
109 |
+
}
|
110 |
+
|
111 |
+
def generate_recommendations(self, balance_sheet, income_statement):
|
112 |
+
prompt = f"generate financial recommendations based on: {balance_sheet[:200]} {income_statement[:200]}"
|
113 |
+
inputs = self.t5_tokenizer(prompt, return_tensors="pt").to(self.device)
|
114 |
+
outputs = self.t5_model.generate(inputs["input_ids"], max_length=200)
|
115 |
+
return self.t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
116 |
+
|
117 |
+
def generate_roadmap(self, insights, sentiment, recommendations):
|
118 |
+
return {
|
119 |
+
"Short-term Actions (0-12 months)": self._generate_short_term_actions(insights, sentiment),
|
120 |
+
"Medium-term Strategy (1-2 years)": self._generate_medium_term_strategy(recommendations),
|
121 |
+
"Long-term Vision (3-5 years)": self._generate_long_term_vision(insights, recommendations)
|
122 |
+
}
|
123 |
+
|
124 |
+
def _generate_short_term_actions(self, insights, sentiment):
|
125 |
+
prompt = f"Generate short-term actions based on: {insights[:100]} Sentiment: {sentiment}"
|
126 |
+
inputs = self.t5_tokenizer(prompt, return_tensors="pt").to(self.device)
|
127 |
+
outputs = self.t5_model.generate(inputs["input_ids"], max_length=100)
|
128 |
+
return self.t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
129 |
+
|
130 |
+
def _generate_medium_term_strategy(self, recommendations):
|
131 |
+
prompt = f"Generate medium-term strategy based on: {recommendations}"
|
132 |
+
inputs = self.t5_tokenizer(prompt, return_tensors="pt").to(self.device)
|
133 |
+
outputs = self.t5_model.generate(inputs["input_ids"], max_length=100)
|
134 |
+
return self.t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
135 |
+
|
136 |
+
def _generate_long_term_vision(self, insights, recommendations):
|
137 |
+
prompt = f"Generate long-term vision based on: {insights[:100]} {recommendations[:100]}"
|
138 |
+
inputs = self.t5_tokenizer(prompt, return_tensors="pt").to(self.device)
|
139 |
+
outputs = self.t5_model.generate(inputs["input_ids"], max_length=100)
|
140 |
+
return self.t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
141 |
+
|
142 |
+
# Create Gradio interface
|
143 |
+
def create_gradio_interface():
|
144 |
+
analyzer = FinancialAnalyzer()
|
145 |
+
|
146 |
+
def analyze_files(balance_sheet, income_statement, file_type):
|
147 |
+
return analyzer.analyze_financials(balance_sheet, income_statement, file_type)
|
148 |
+
|
149 |
+
iface = gr.Interface(
|
150 |
+
fn=analyze_files,
|
151 |
+
inputs=[
|
152 |
+
gr.File(label="Upload Balance Sheet"),
|
153 |
+
gr.File(label="Upload Income Statement"),
|
154 |
+
gr.Radio(
|
155 |
+
choices=["csv", "excel", "markdown"],
|
156 |
+
label="File Type",
|
157 |
+
value="csv"
|
158 |
+
)
|
159 |
+
],
|
160 |
+
outputs=gr.Textbox(label="Analysis Results", lines=20),
|
161 |
+
title="Financial Statement Analyzer",
|
162 |
+
description="Upload your financial statements (Balance Sheet and Income Statement) to get AI-powered insights, recommendations, and strategic roadmap.",
|
163 |
+
examples=[
|
164 |
+
["balance_sheet.csv", "income_statement.csv", "csv"],
|
165 |
+
["balance_sheet.xlsx", "income_statement.xlsx", "excel"],
|
166 |
+
["balance_sheet.md", "income_statement.md", "markdown"]
|
167 |
+
]
|
168 |
+
)
|
169 |
+
|
170 |
+
return iface
|
171 |
+
|
172 |
+
if __name__ == "__main__":
|
173 |
+
iface = create_gradio_interface()
|
174 |
+
iface.launch()
|