Skip to content

Trishit-Pal/BCGX-Forage_GenAI

Repository files navigation

Financial Analysis and Smart Chatbot (Forage BCGX GenAI)

Project Overview

This project provides comprehensive financial analysis and AI-powered Q&A for Apple, Microsoft, and Tesla (FY2022-2024). It includes:

  1. Financial Analysis Notebook - Data processing, metrics calculation, and visualizations
  2. Rule-Based Chatbot - Keyword-matching Q&A system
  3. Smart AI Chatbot - LLM-powered natural language understanding
  4. Web Interface - Dark mode Streamlit UI for easy interaction

Built for the Forage BCGX GenAI program.


Repository Files

Core Components

File Description
analysis.ipynb Jupyter notebook for data cleaning, metrics engineering, and visualization
chatbot_5.py Rule-based command-line chatbot with predefined queries
smart_chatbot.py LLM-powered chatbot engine using Google Gemini API
chatbot_ui.py Streamlit web interface with dark mode theme

Data Files

File Description
financial_data.csv Raw financial statements (revenue, income, assets, liabilities)
financial_data_summaryreport.csv Processed dataset with calculated metrics and growth rates

Configuration

File Description
requirements.txt Python dependencies (pandas, streamlit, httpx, etc.)
.env Environment variables (API keys) - not tracked in git
.env.example Template for environment variables
.gitignore Files excluded from version control

Documentation

File Description
README.md Project overview (this file)
ChatBot_Documentation.md Detailed chatbot usage guide

Quick Start

1. Install Dependencies

pip install -r requirements.txt

2. Get Free API Key

Visit https://aistudio.google.com/apikey and create a Gemini API key (no credit card required).

3. Configure Environment

Copy .env.example to .env and add your key:

GEMINI_API_KEY=your_actual_key_here

4. Run the Chatbot

Web Interface (Recommended):

streamlit run chatbot_ui.py

Command Line:

python chatbot_5.py          # Rule-based
python smart_chatbot.py      # LLM-powered (needs setup)

Financial Analysis Workflow

The analysis.ipynb notebook performs comprehensive financial analysis:

Step 1: Data Loading

  • Reads financial_data.csv containing raw financial statements
  • Sorts by Company and Fiscal Year
  • Cleans numeric columns and handles formatting

Step 2: Metrics Calculation

Growth Metrics (Year-over-Year):

  • Revenue Growth (%)
  • Net Income Growth (%)
  • Assets Growth (%)
  • Liabilities Growth (%)
  • Cash Flow Growth (%)

Quality/Risk Indicators:

  • Profit Margin (%) = Net Income / Total Revenue
  • Debt to Asset Ratio (%) = Total Liabilities / Total Assets
  • Cash Flow Margin (%) = Operating Cash Flow / Total Revenue

Step 3: Aggregation & Comparison

  • Company-level averages across all fiscal years
  • Cross-company performance rankings
  • Trend analysis over time

Step 4: Visualization

  • Line charts: Revenue, net income, assets, liabilities over time
  • Bar charts: Profitability metrics comparison
  • Bar charts: Growth rates by company

Step 5: Export

Generates financial_data_summaryreport.csv with all calculated metrics for chatbot use.


Key Financial Findings

Analysis of FY2022-2024 data reveals:

Revenue Performance

  • Microsoft: 11.28% average growth (leader)
  • Tesla: 9.87% average growth
  • Apple: -0.39% (flat/declining)

Profitability

  • Microsoft: 35.60% profit margin (highest)
  • Apple: 24.86% profit margin
  • Tesla: 12.75% profit margin (lowest)

Financial Health

  • Apple: 84.14% debt-to-asset ratio (most leveraged)
  • Microsoft: 50.62% debt-to-asset ratio
  • Tesla: 41.41% debt-to-asset ratio (least leveraged)

Cash Generation

  • Microsoft: 44.87% cash flow margin (best)
  • Apple: 30.02% cash flow margin
  • Tesla: 15.68% cash flow margin

Summary

  • Microsoft = Strongest performer in growth quality and profitability
  • Apple = Stable but limited growth, high leverage
  • Tesla = High asset growth but volatile earnings (net income dropped 52% in FY2024)

Chatbot Features

Option 1: Rule-Based Chatbot (chatbot_5.py)

Type: Keyword-matching Q&A
Setup: None required
Run: python chatbot_5.py

Supported Queries:

  1. Total revenue for each company
  2. Net income year-on-year change
  3. Which company has highest profit margin
  4. Debt-to-asset ratio by company
  5. Operating cash flow trends
  6. Company-specific revenue growth
  7. Company-specific asset growth
  8. Company-specific net income trend

Example Questions:

What is Apple's total revenue in 2023?
How has Tesla's net income changed?
3
menu

Option 2: Smart AI Chatbot (smart_chatbot.py)

Type: LLM-powered (Google Gemini)
Setup: Requires API key (free)
Run: python smart_chatbot.py (command-line) or via chatbot_ui.py (web interface)

Features:

  • Natural language understanding (ask in your own words)
  • Concise 2-4 sentence answers with exact numbers
  • Only answers from available data (no hallucinations)
  • Interprets complex comparative questions

How It Works:

  1. Loads entire financial dataset as context
  2. Sends your question + data to Gemini API
  3. Returns concise, data-backed response

Example Questions:

What is Apple's revenue in 2024?
Which company has the highest profit margin?
How has Tesla's net income changed over the years?
Compare Microsoft and Apple debt ratios
What are the key financial findings?
Which company performed best in profitability?

Option 3: Web Interface (chatbot_ui.py)

Type: Streamlit web UI powered by Smart AI Chatbot
Setup: Requires API key (free)
Run: streamlit run chatbot_ui.py

Features:

  • Dark mode theme (#0f0f0f background, #2563eb accents)
  • Chat interface with message history
  • 6 example question buttons for quick access
  • Real-time AI responses with "Thinking..." indicator
  • Clear chat button to reset conversation

Interface Elements:

  • Title: "Financial Analysis Chatbot"
  • Companies: Apple | Microsoft | Tesla -- FY2022-2024
  • Example buttons in 3-column grid
  • Chat input at bottom
  • Responses formatted in markdown

Technology Stack

Data Processing

  • pandas - CSV loading, data manipulation, aggregations
  • numpy - Numerical computations

Visualization (Notebook)

  • matplotlib - Line and bar charts
  • seaborn - Statistical visualizations
  • plotly - Interactive charts

Chatbot Engine

  • httpx - HTTP requests to Gemini API
  • python-dotenv - Environment variable management
  • Google Gemini 2.0 Flash - LLM for natural language understanding

Web Interface

  • streamlit - Web framework and UI components
  • Custom CSS - Dark mode styling

Development

  • Python 3.8+ - Core language
  • Jupyter - Notebook environment

Project Structure

BCGX-Forage_GenAI-main/
│
├── analysis.ipynb                      # Data analysis notebook
├── financial_data.csv                  # Raw financial data
├── financial_data_summaryreport.csv    # Processed data with metrics
│
├── chatbot_5.py                        # Rule-based chatbot
├── smart_chatbot.py                    # LLM chatbot engine
├── chatbot_ui.py                       # Streamlit web interface
│
├── requirements.txt                    # Python dependencies
├── .env                                # API keys (create this)
├── .env.example                        # API key template
├── .gitignore                          # Git exclusions
│
├── README.md                           # Project overview (this file)
└── ChatBot_Documentation.md            # Detailed chatbot guide

Installation

Prerequisites

  • Python 3.8 or higher
  • pip package manager
  • Google Gemini API key (free)

Step-by-Step Setup

  1. Clone or download the repository

  2. Navigate to project directory

    cd BCGX-Forage_GenAI-main
  3. Install dependencies

    pip install -r requirements.txt
  4. Get API key

  5. Configure environment

    # Copy example file
    cp .env.example .env
    
    # Edit .env and add your key
    GEMINI_API_KEY=your_actual_key_here
  6. Run the application

    streamlit run chatbot_ui.py

Usage Examples

Analyze Financial Data

jupyter notebook analysis.ipynb

Run all cells to:

  • Load and clean raw data
  • Calculate growth and profitability metrics
  • Generate visualizations
  • Export processed dataset

Use Rule-Based Chatbot

python chatbot_5.py

Type questions or numbers (1-8) from menu.

Use AI Chatbot (Web)

streamlit run chatbot_ui.py

Browser opens automatically. Click example buttons or type questions.

Use AI Chatbot (Terminal)

python smart_chatbot.py

Type questions directly in terminal.


Sample Questions

Revenue & Growth

  • What is Apple's revenue in 2024?
  • Which company grew revenue the fastest?
  • How does Microsoft's revenue compare to Tesla?

Profitability

  • Which company has the highest profit margin?
  • Compare profit margins across all three companies
  • How profitable is Apple compared to Microsoft?

Financial Health

  • What is Tesla's debt-to-asset ratio?
  • Which company is most leveraged?
  • Compare Microsoft and Apple debt levels

Performance Trends

  • How has Tesla's net income changed over the years?
  • What is the trend in Apple's cash flow?
  • Which company shows the strongest growth trajectory?

Overall Analysis

  • What are the key financial findings?
  • Which company performed best overall?
  • Summarize the financial health of all companies

Troubleshooting

"LLM not available" Error

  • Verify .env file exists in project folder
  • Check GEMINI_API_KEY= is set correctly (no spaces)
  • Ensure API key is valid (test at https://aistudio.google.com)

"Data file not found" Error

  • Confirm you're in the correct directory
  • Verify financial_data_summaryreport.csv exists
  • Run analysis.ipynb to regenerate if missing

Streamlit Not Starting

  • Check Python version: python --version (need 3.8+)
  • Try: python -m streamlit run chatbot_ui.py
  • Check port 8501 is available

API Quota Exceeded

  • Gemini free tier has rate limits
  • Wait a few minutes before retrying
  • Consider creating a new API key if needed

Import Errors

  • Run: pip install -r requirements.txt
  • Verify all packages installed: pip list
  • Use virtual environment if conflicts occur

Important Notes

Data Scope

  • Companies: Apple, Microsoft, Tesla only
  • Period: Fiscal years 2022, 2023, 2024
  • Metrics: From historical financial statements

Chatbot Limitations

  • Only answers questions about available data
  • Does not access real-time information
  • Cannot make predictions or provide investment advice
  • No internet access or external data sources

API Usage

  • Google Gemini free tier included
  • Fair use limits apply
  • Responses typically take 1-3 seconds
  • Context size: ~3000 tokens per request

Project Objectives Achieved

✅ Transform raw financial data into analysis-ready format
✅ Calculate growth, profitability, and risk indicators
✅ Generate clear visual insights for comparison
✅ Export enriched dataset for downstream use
✅ Build rule-based financial Q&A chatbot
✅ Build smart LLM-powered chatbot with natural language understanding
✅ Deploy interactive web interface with dark mode design


Credits

Program: Forage BCGX GenAI
Data Source: Public financial statements (Apple, Microsoft, Tesla)
LLM Provider: Google Gemini API (free tier)
UI Framework: Streamlit


License

This project is for educational purposes as part of the Forage BCGX GenAI program.


Support

For detailed chatbot usage instructions, see ChatBot_Documentation.md

For issues or questions about the financial data, refer to the analysis notebook analysis.ipynb

About

Solution of BCGX's Gen AI Virtual Job Simulation hosted on Forage.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors