Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,157 +1,153 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import pandas as pd
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
"
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
def format_results(
|
111 |
-
"""Format analysis results"""
|
112 |
-
output = "# Financial Analysis Report\n\n"
|
113 |
-
|
114 |
-
#
|
115 |
-
output += "##
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
output +=
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
)
|
154 |
-
|
155 |
-
# Launch the interface
|
156 |
-
if __name__ == "__main__":
|
157 |
iface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from transformers import pipeline
|
4 |
+
import torch
|
5 |
+
|
6 |
+
class FinancialAnalyzer:
|
7 |
+
def __init__(self):
|
8 |
+
"""Initialize models"""
|
9 |
+
# 1. Llama 2 for strategic analysis
|
10 |
+
self.strategic_analyzer = pipeline(
|
11 |
+
"text-generation",
|
12 |
+
model="meta-llama/Llama-2-7b-chat-hf",
|
13 |
+
device_map="auto"
|
14 |
+
)
|
15 |
+
|
16 |
+
# 2. FinBERT for financial sentiment
|
17 |
+
self.financial_analyzer = pipeline(
|
18 |
+
"text-classification",
|
19 |
+
model="ProsusAI/finbert",
|
20 |
+
return_all_scores=True
|
21 |
+
)
|
22 |
+
|
23 |
+
# 3. Falcon for recommendations
|
24 |
+
self.recommendation_generator = pipeline(
|
25 |
+
"text-generation",
|
26 |
+
model="tiiuae/falcon-7b-instruct",
|
27 |
+
device_map="auto"
|
28 |
+
)
|
29 |
+
|
30 |
+
def generate_strategic_analysis(self, financial_data):
|
31 |
+
"""Generate strategic analysis using Llama 2"""
|
32 |
+
prompt = f"""[INST] As a senior financial analyst, analyze these financial statements:
|
33 |
+
|
34 |
+
Financial Data:
|
35 |
+
{financial_data}
|
36 |
+
|
37 |
+
Provide:
|
38 |
+
1. Business Health Assessment
|
39 |
+
2. Key Strategic Insights
|
40 |
+
3. Market Position Analysis
|
41 |
+
4. Growth Opportunities
|
42 |
+
5. Risk Factors [/INST]"""
|
43 |
+
|
44 |
+
response = self.strategic_analyzer(
|
45 |
+
prompt,
|
46 |
+
max_length=1000,
|
47 |
+
temperature=0.7
|
48 |
+
)
|
49 |
+
return response[0]['generated_text']
|
50 |
+
|
51 |
+
def analyze_sentiment(self, text):
|
52 |
+
"""Analyze financial sentiment using FinBERT"""
|
53 |
+
return self.financial_analyzer(text)
|
54 |
+
|
55 |
+
def generate_recommendations(self, analysis):
|
56 |
+
"""Generate recommendations using Falcon"""
|
57 |
+
prompt = f"""Based on this financial analysis:
|
58 |
+
{analysis}
|
59 |
+
|
60 |
+
Provide specific, actionable recommendations covering:
|
61 |
+
1. Strategic Initiatives
|
62 |
+
2. Operational Improvements
|
63 |
+
3. Financial Management
|
64 |
+
4. Risk Mitigation
|
65 |
+
5. Growth Strategy"""
|
66 |
+
|
67 |
+
response = self.recommendation_generator(
|
68 |
+
prompt,
|
69 |
+
max_length=800,
|
70 |
+
temperature=0.6
|
71 |
+
)
|
72 |
+
return response[0]['generated_text']
|
73 |
+
|
74 |
+
def analyze_financial_statements(income_statement, balance_sheet):
|
75 |
+
"""Main analysis function"""
|
76 |
+
try:
|
77 |
+
# Read files
|
78 |
+
income_df = pd.read_csv(income_statement.name)
|
79 |
+
balance_df = pd.read_csv(balance_sheet.name)
|
80 |
+
|
81 |
+
# Initialize analyzer
|
82 |
+
analyzer = FinancialAnalyzer()
|
83 |
+
|
84 |
+
# Prepare financial data
|
85 |
+
financial_data = f"""
|
86 |
+
Income Statement Summary:
|
87 |
+
{income_df.to_string()}
|
88 |
+
|
89 |
+
Balance Sheet Summary:
|
90 |
+
{balance_df.to_string()}
|
91 |
+
"""
|
92 |
+
|
93 |
+
# Generate strategic analysis
|
94 |
+
strategic_analysis = analyzer.generate_strategic_analysis(financial_data)
|
95 |
+
|
96 |
+
# Analyze sentiment
|
97 |
+
sentiment = analyzer.analyze_sentiment(strategic_analysis)
|
98 |
+
|
99 |
+
# Generate recommendations
|
100 |
+
recommendations = analyzer.generate_recommendations(strategic_analysis)
|
101 |
+
|
102 |
+
# Format output
|
103 |
+
output = format_results(strategic_analysis, sentiment, recommendations)
|
104 |
+
|
105 |
+
return output
|
106 |
+
|
107 |
+
except Exception as e:
|
108 |
+
return f"Error analyzing files: {str(e)}"
|
109 |
+
|
110 |
+
def format_results(analysis, sentiment, recommendations):
|
111 |
+
"""Format analysis results"""
|
112 |
+
output = "# Financial Analysis Report\n\n"
|
113 |
+
|
114 |
+
# Strategic Analysis
|
115 |
+
output += "## Strategic Analysis\n\n"
|
116 |
+
output += analysis + "\n\n"
|
117 |
+
|
118 |
+
# Sentiment Analysis
|
119 |
+
output += "## Market Sentiment\n\n"
|
120 |
+
for score in sentiment[0]:
|
121 |
+
output += f"- {score['label']}: {score['score']:.2%}\n"
|
122 |
+
output += "\n"
|
123 |
+
|
124 |
+
# Recommendations
|
125 |
+
output += "## Strategic Recommendations\n\n"
|
126 |
+
output += recommendations
|
127 |
+
|
128 |
+
return output
|
129 |
+
|
130 |
+
# Create Gradio interface
|
131 |
+
iface = gr.Interface(
|
132 |
+
fn=analyze_financial_statements,
|
133 |
+
inputs=[
|
134 |
+
gr.File(label="Income Statement (CSV)"),
|
135 |
+
gr.File(label="Balance Sheet (CSV)")
|
136 |
+
],
|
137 |
+
outputs=gr.Markdown(),
|
138 |
+
title="AI-Powered Financial Statement Analysis",
|
139 |
+
description="""Upload your financial statements for comprehensive analysis using:
|
140 |
+
- Llama 2: Strategic Analysis
|
141 |
+
- FinBERT: Financial Sentiment Analysis
|
142 |
+
- Falcon: Strategic Recommendations""",
|
143 |
+
examples=[
|
144 |
+
[
|
145 |
+
"OFINTECH-Income Statement-template.csv",
|
146 |
+
"OFINTECH Balance Sheet template.csv"
|
147 |
+
]
|
148 |
+
]
|
149 |
+
)
|
150 |
+
|
151 |
+
# Launch the interface
|
152 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
153 |
iface.launch()
|