File size: 6,309 Bytes
ea4b18b
b2501de
74f7ba2
b2501de
c6b42a6
ab0bea5
 
74f7ba2
ab0bea5
ea4b18b
ab0bea5
 
 
 
 
 
 
74f7ba2
ab0bea5
 
 
 
c6b42a6
 
 
 
ab0bea5
c6b42a6
 
 
 
74f7ba2
c6b42a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74f7ba2
 
c6b42a6
9bdd84e
c6b42a6
ab0bea5
 
 
 
 
c6b42a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab0bea5
 
c6b42a6
9bdd84e
ab0bea5
c6b42a6
74f7ba2
9bdd84e
c6b42a6
 
 
 
 
 
 
 
 
 
 
 
9bdd84e
5462ac3
ab0bea5
ea4b18b
 
74f7ba2
ea4b18b
c6b42a6
 
ea4b18b
ab0bea5
 
c6b42a6
ea4b18b
5462ac3
ea4b18b
 
 
5462ac3
ea4b18b
1
2
3
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import gradio as gr
import pandas as pd
import numpy as np
import json
import re
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSequenceClassification
import torch

class FinancialAnalyzer:
    def __init__(self):
        print("Initializing Financial Analyzer...")
        self.initialize_models()

    def initialize_models(self):
        print("Loading models...")
        self.tiny_tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
        self.tiny_model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
        
        self.finbert_tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
        self.finbert_model = AutoModelForSequenceClassification.from_pretrained("ProsusAI/finbert")
        print("Models loaded successfully!")

    def parse_markdown_table(self, markdown_content):
        """Parse markdown table into pandas DataFrame"""
        # Split content into lines
        lines = markdown_content.strip().split('\n')
        
        # Find table start (line with |)
        table_lines = []
        headers = None
        current_table = []
        
        for line in lines:
            if '|' in line:
                # Skip separator lines (contains ---)
                if '-|-' in line:
                    continue
                # Clean and split the line
                row = [cell.strip() for cell in line.split('|')[1:-1]]
                if headers is None:
                    headers = row
                else:
                    current_table.append(row)
                    
        # Create DataFrame
        df = pd.DataFrame(current_table, columns=headers)
        return df

    def extract_financial_data(self, markdown_content):
        """Convert markdown content to a structured text format"""
        # Remove markdown formatting
        clean_text = markdown_content.replace('#', '').replace('*', '')
        
        # Extract tables
        tables = {}
        current_section = "General"
        
        for line in clean_text.split('\n'):
            if line.strip() and not line.startswith('|'):
                current_section = line.strip()
            elif '|' in line:
                if current_section not in tables:
                    tables[current_section] = []
                tables[current_section].append(line)
                
        # Convert to text format
        structured_text = []
        for section, content in tables.items():
            structured_text.append(f"\n{section}:")
            if content:
                df = self.parse_markdown_table('\n'.join(content))
                structured_text.append(df.to_string())
                
        return '\n'.join(structured_text)

    def analyze_financials(self, balance_sheet_file, income_stmt_file):
        """Main analysis function"""
        try:
            # Read markdown files
            with open(balance_sheet_file, 'r') as f:
                balance_sheet_content = f.read()
            with open(income_stmt_file, 'r') as f:
                income_stmt_content = f.read()

            # Convert to structured text
            structured_balance = self.extract_financial_data(balance_sheet_content)
            structured_income = self.extract_financial_data(income_stmt_content)

            # Create analysis prompt
            prompt = f"""<human>Please analyze these financial statements and provide detailed insights:

Financial Statements Analysis (2021-2025)

Balance Sheet Summary:
{structured_balance}

Income Statement Summary:
{structured_income}

Please provide a detailed analysis including:
1. Financial Health Assessment
   - Liquidity position
   - Capital structure
   - Asset efficiency

2. Profitability Analysis
   - Revenue trends
   - Cost management
   - Profit margins

3. Growth Analysis
   - Year-over-year growth rates
   - Market position
   - Future growth potential

4. Risk Assessment
   - Operating risks
   - Financial risks
   - Strategic risks

5. Recommendations
   - Short-term actions
   - Medium-term strategy
   - Long-term planning

6. Future Outlook
   - Market conditions
   - Company positioning
   - Growth opportunities</human>"""

            # Generate AI analysis
            inputs = self.tiny_tokenizer(prompt, return_tensors="pt", truncation=True)
            outputs = self.tiny_model.generate(
                inputs["input_ids"],
                max_length=2048,
                temperature=0.7,
                top_p=0.95,
                do_sample=True
            )
            analysis = self.tiny_tokenizer.decode(outputs[0], skip_special_tokens=True)

            # Generate sentiment
            sentiment = self.analyze_sentiment(structured_balance + structured_income)

            # Compile results
            results = {
                "Financial Analysis": analysis,
                "Sentiment Analysis": sentiment,
                "Analysis Period": "2021-2025",
                "Note": "All values in millions ($M)"
            }

            return json.dumps(results, indent=2)

        except Exception as e:
            return f"Error in analysis: {str(e)}\n\nDetails: {type(e).__name__}"

    def analyze_sentiment(self, text):
        inputs = self.finbert_tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
        outputs = self.finbert_model(**inputs)
        probs = torch.nn.functional.softmax(outputs.logits, dim=1)
        sentiment_labels = ['negative', 'neutral', 'positive']
        
        return {
            'sentiment': sentiment_labels[probs.argmax().item()],
            'confidence': f"{probs.max().item():.2f}"
        }

def create_interface():
    analyzer = FinancialAnalyzer()
    
    iface = gr.Interface(
        fn=analyzer.analyze_financials,
        inputs=[
            gr.File(label="Balance Sheet (Markdown)", type="filepath"),
            gr.File(label="Income Statement (Markdown)", type="filepath")
        ],
        outputs=gr.Textbox(label="Analysis Results", lines=25),
        title="Financial Statement Analyzer",
        description="Upload financial statements in Markdown format for comprehensive AI-powered analysis."
    )
    
    return iface

if __name__ == "__main__":
    iface = create_interface()
    iface.launch()