# tanujkumar24/e-commerce-swot-analysis architecture

> Architecture diagram of the GitHub repository tanujkumar24/e-commerce-swot-analysis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tanujkumar24/e-commerce-swot-analysis
- Repository: https://github.com/tanujkumar24/e-commerce-swot-analysis

## Overview

##### Project Type and Purpose
- This is a backend-focused, API-driven SWOT Analysis tool written in Python.
- It exposes a Flask `/analyze` endpoint, integrates with external NLP and search APIs, classifies sentiment into SWOT categories, and generates PDF reports.
- It also provides FastMCP integration so LLM agents can invoke the tool in an MCP-compatible environment.

##### Top-Level Components
a. Client Layer
– REST client (could be curl, Postman, or any HTTP client)
– FastMCP inspector CLI (via `npx @modelcontextprotocol/inspector python main.py`)
b. API Layer
– app.py (Flask application exposing `/analyze`)
– test_swot.py (automated tests invoking the endpoint)
c. Business Logic Layer
– Review scraper & fetcher (Google Custom Search API module)
– NLP analyzer (Hugging Face Transformers using DistilBERT)
– Data processor (Pandas for data frames, Matplotlib for charts)
– SWOT classifier (aggregates sentiment into Strengths, Weaknesses, Opportunities, Threats)
d. Persistence & Reporting
– analysis_results/* (JSON snapshots of raw and processed data)
– pdf_reports/* (generated PDF reports via FPDF)
e. External Services
– Google Custom Search API (for product reviews)
– Hugging Face inference endpoint (for sentiment analysis)
– FastMCP Inspector (npm package that wraps your tool for LLM agents)

##### Relationships & Data Flow
1. Client (or FastMCP) sends POST /analyze → Flask (`app.py`)
2. Flask handler extracts `product_name` → calls Review Scraper
3. Scraper invokes Google API → returns raw reviews
4. Raw reviews → NLP Analyzer → produces sentiment scores
5. Sentiment data → Data Processor → generates charts, aggregates metrics
6. Aggregated metrics → SWOT Classifier → constructs strengths/weaknesses/etc.
7. JSON result →
- Stored in analysis_results/
- Returned synchronously in HTTP response
8. Post-processing:
- PDF Generator reads classification + charts → writes to pdf_reports/
9. test_swot.py runs as a separate process → hits the same `/analyze` endpoint → validates JSON schema

##### Architectural Patterns & Design Principles
– Layered pattern separating API, business logic, and persistence
– Modular components for scraper, analyzer, classifier, PDF generator
– Single responsibility per module for easier testing and replacement
– Externalization of credentials (Google API key, Search Engine ID)
– FastMCP wrapper exposes identical `/analyze` functionality to LLM-based clients

##### Essential Elements
– Clear labels for: Client, FastMCP Inspector, Flask API, Scraper, NLP Analyzer, Classifier, PDF Generator, Storage folders, Test script

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        REST["REST Client"]:::external
        FastMCP["FastMCP Inspector"]:::external
    end

    %% Test Layer
    subgraph "Test Layer"
        TestHarness["Test Harness"]:::external
    end

    %% API Layer
    subgraph "API Layer"
        Flask["Flask API"]:::internal
    end

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        Scraper["Review Scraper"]:::internal
        Analyzer["NLP Analyzer"]:::internal
        Processor["Data Processor"]:::internal
        Classifier["SWOT Classifier"]:::internal
        PDFGen["PDF Generator"]:::internal
    end

    %% Persistence & Reporting
    subgraph "Persistence & Reporting"
        Results["analysis_results"]:::storage
        Reports["pdf_reports"]:::storage
    end

    %% External Services
    subgraph "External Services"
        Google["Google Custom Search API"]:::external
        HF["Hugging Face Inference"]:::external
    end

    %% Artifacts
    subgraph "Artifacts"
        Requirements["requirements.txt"]:::artifact
        SampleResp["api_result.json"]:::artifact
        Docs["README.md"]:::artifact
        Screens["screenshots/"]:::artifact
    end

    %% Connections
    REST -->|"POST /analyze"| Flask
    FastMCP -->|"Invoke /analyze"| Flask
    TestHarness -->|"POST /analyze"| Flask

    Flask -->|"product_name"| Scraper
    Scraper -->|"reviews request"| Google
    Google -->|"JSON reviews"| Scraper
    Scraper -->|"reviews data"| Analyzer
    Analyzer -->|"sentiment scores"| Processor
    Processor -->|"metrics & charts"| Classifier
    Classifier -->|"SWOT JSON"| Results
    Classifier -->|"SWOT JSON"| Flask

    Flask -->|"JSON response"| REST
    Flask -->|"JSON response"| FastMCP
    Flask -->|"JSON response"| TestHarness

    Processor -->|"charts data"| PDFGen
    PDFGen -->|"PDF report"| Reports

    %% Click Events
    click Flask "https://github.com/tanujkumar24/e-commerce-swot-analysis/blob/main/swot_server/app.py"
    click TestHarness "https://github.com/tanujkumar24/e-commerce-swot-analysis/blob/main/swot_server/test_swot.py"
    click Results "https://github.com/tanujkumar24/e-commerce-swot-analysis/tree/main/swot_server/analysis_results/"
    click Reports "https://github.com/tanujkumar24/e-commerce-swot-analysis/tree/main/swot_server/pdf_reports/"
    click Requirements "https://github.com/tanujkumar24/e-commerce-swot-analysis/blob/main/swot_server/requirements.txt"
    click SampleResp "https://github.com/tanujkumar24/e-commerce-swot-analysis/blob/main/swot_server/api_result.json"
    click Docs "https://github.com/tanujkumar24/e-commerce-swot-analysis/blob/main/README.md"
    click Screens "https://github.com/tanujkumar24/e-commerce-swot-analysis/tree/main/swot_server/screenshots/"

    %% Styles
    classDef internal fill:#cce5ff,stroke:#000
    classDef external fill:#d4edda,stroke:#000
    classDef storage fill:#e2e3e5,stroke:#000
    classDef artifact fill:#fff3cd,stroke:#000
```
