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)