Spaces:
Sleeping
Sleeping
File size: 7,213 Bytes
864f28a a1ef945 0ff54a0 a1ef945 35acd3c 0ff54a0 ceb9625 6e9bd28 0ff54a0 6e9bd28 35acd3c ceb9625 57061b5 ceb9625 57061b5 ceb9625 0ff54a0 35acd3c 37a163f 35acd3c 0ff54a0 37a163f 0ff54a0 35acd3c 0ff54a0 35acd3c 0ff54a0 35acd3c 0ff54a0 35acd3c 0ff54a0 35acd3c 0ff54a0 35acd3c a1ef945 37a163f 35acd3c a1ef945 0ff54a0 a1ef945 35acd3c a1ef945 0ff54a0 a1ef945 0ff54a0 35acd3c 37a163f 0ff54a0 35acd3c 0ff54a0 35acd3c 37a163f a1ef945 35acd3c 37a163f 35acd3c a1ef945 37a163f 0ff54a0 35acd3c a1ef945 37a163f a1ef945 35acd3c 0ff54a0 a1ef945 35acd3c a1ef945 37a163f a1ef945 35acd3c |
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
import os
import gradio as gr
import pandas as pd
from transformers import pipeline
import torch
import sys
import logging
import io
from huggingface_hub import login
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Get token securely from environment variable
hf_token = os.getenv('HUGGINGFACE_TOKEN')
# Check if the token is available
if hf_token:
# Log in to Hugging Face Hub
login(token=hf_token)
print("Successfully logged in to Hugging Face Hub.")
else:
print("HF_TOKEN environment variable not found. Please set it in the Space settings.")
class FinancialAnalyzer:
def __init__(self):
"""Initialize models with error handling"""
try:
# 1. Llama 2 for strategic analysis
self.strategic_analyzer = pipeline(
"text-generation",
model="meta-llama/Llama-2-7b-chat-hf",
device_map="auto"
)
logger.info("Llama 2 initialized successfully")
# 2. FinBERT for financial sentiment
self.financial_analyzer = pipeline(
"text-classification",
model="ProsusAI/finbert",
return_all_scores=True
)
logger.info("FinBERT initialized successfully")
# 3. Falcon for recommendations
self.recommendation_generator = pipeline(
"text-generation",
model="tiiuae/falcon-7b-instruct",
device_map="auto"
)
logger.info("Falcon initialized successfully")
except Exception as e:
logger.error(f"Error initializing models: {str(e)}")
raise
def read_csv_file(self, file_obj):
"""Safely read CSV file"""
try:
if file_obj is None:
raise ValueError("No file provided")
return pd.read_csv(file_obj)
except Exception as e:
logger.error(f"Error reading CSV file: {str(e)}")
raise
def generate_strategic_analysis(self, financial_data):
"""Generate strategic analysis using Llama 2"""
try:
prompt = f"""[INST] As a senior financial analyst, analyze these financial statements:
Financial Data:
{financial_data}
Provide:
1. Business Health Assessment
2. Key Strategic Insights
3. Market Position Analysis
4. Growth Opportunities
5. Risk Factors [/INST]"""
response = self.strategic_analyzer(
prompt,
max_length=1000,
temperature=0.7
)
return response[0]['generated_text']
except Exception as e:
logger.error(f"Error in strategic analysis: {str(e)}")
return "Error generating strategic analysis"
def analyze_sentiment(self, text):
"""Analyze financial sentiment using FinBERT"""
try:
return self.financial_analyzer(text)
except Exception as e:
logger.error(f"Error in sentiment analysis: {str(e)}")
return [{"label": "error", "score": 1.0}]
def generate_recommendations(self, analysis):
"""Generate recommendations using Falcon"""
try:
prompt = f"""Based on this financial analysis:
{analysis}
Provide specific, actionable recommendations covering:
1. Strategic Initiatives
2. Operational Improvements
3. Financial Management
4. Risk Mitigation
5. Growth Strategy"""
response = self.recommendation_generator(
prompt,
max_length=800,
temperature=0.6
)
return response[0]['generated_text']
except Exception as e:
logger.error(f"Error generating recommendations: {str(e)}")
return "Error generating recommendations"
def analyze_financial_statements(income_statement, balance_sheet):
"""Main analysis function with error handling"""
try:
# Initialize analyzer
analyzer = FinancialAnalyzer()
# Read CSV files safely
logger.info("Reading input files...")
income_df = analyzer.read_csv_file(income_statement)
balance_df = analyzer.read_csv_file(balance_sheet)
# Prepare financial data
financial_data = f"""
Income Statement Summary:
{income_df.to_string()}
Balance Sheet Summary:
{balance_df.to_string()}
"""
# Generate analyses
logger.info("Generating analysis...")
strategic_analysis = analyzer.generate_strategic_analysis(financial_data)
sentiment = analyzer.analyze_sentiment(strategic_analysis)
recommendations = analyzer.generate_recommendations(strategic_analysis)
# Format output
logger.info("Formatting results...")
return format_results(strategic_analysis, sentiment, recommendations)
except Exception as e:
logger.error(f"Error in analysis: {str(e)}")
return f"""Error analyzing files: {str(e)}
Please check:
1. Files are in correct CSV format
2. Files are not corrupted
3. Files contain the expected data
If the problem persists, try uploading the files again."""
def format_results(analysis, sentiment, recommendations):
"""Format analysis results"""
try:
output = "# Financial Analysis Report\n\n"
# Strategic Analysis
output += "## Strategic Analysis\n\n"
output += analysis + "\n\n"
# Sentiment Analysis
output += "## Market Sentiment\n\n"
for score in sentiment[0]:
output += f"- {score['label']}: {score['score']:.2%}\n"
output += "\n"
# Recommendations
output += "## Strategic Recommendations\n\n"
output += recommendations
return output
except Exception as e:
logger.error(f"Error formatting results: {str(e)}")
return "Error formatting analysis results"
# Create Gradio interface
iface = gr.Interface(
fn=analyze_financial_statements,
inputs=[
gr.File(label="Income Statement (CSV)"),
gr.File(label="Balance Sheet (CSV)")
],
outputs=gr.Markdown(),
title="AI-Powered Financial Statement Analysis",
description="""Upload your financial statements for comprehensive analysis using:
- Llama 2: Strategic Analysis
- FinBERT: Financial Sentiment Analysis
- Falcon: Strategic Recommendations""",
examples=[
[
"OFINTECH-Income Statement-template.csv",
"OFINTECH Balance Sheet template.csv"
]
]
)
# Launch the interface
if __name__ == "__main__":
try:
iface.launch()
except Exception as e:
logger.error(f"Error launching application: {str(e)}")
sys.exit(1) |