# sagnik24-k/chatbot2 architecture

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

- Interactive diagram: https://gitdiagram.com/sagnik24-k/chatbot2
- Repository: https://github.com/sagnik24-k/chatbot2

## Overview

##### Project Type and Purpose
- Type: Full-stack, client-server web application.
- Purpose: A study-assistant chatbot built on Python/Flask and the Google Gemini AI model, with light front-end in HTML/CSS and integrations to Wikipedia.

##### File Structure
Top-level files & directories:
- Frontend: index.html (Flask template), CSS (Tailwind) embedded.
- Backend: Chat.py (Flask app entry point, called “app.py” in README), Main.py (auxiliary runner?), Chat_utils.py, wek.py.
- Data/config: responses.json (knowledge base), .env (Gemini API key), Api.txt (maybe API usage examples).
- __pycache__, .idea: ignore for architecture.

##### README for Insights
- Routes:
– “/” renders chat interface
– “/process_input” handles user submissions
- Modules:
– Chat_utils.py: input preprocessing, fallback logic, local knowledge_base via responses.json
– wek.py: fetches & sanitizes Wikipedia book summaries
– External: Google Gemini API via google.generativeai
- Data flow: browser → Flask → choose Gemini vs. local vs. Wikipedia → store session → render page

##### Main Components & Interactions
a. Components
– Client (Browser): Renders index.html, sends user input via POST
– Web Server (Flask in Chat.py): Receives requests, manages sessions
- Chat_utils module
- wek module
- Session store (in-memory or Flask sessions)
– Data Store: responses.json (intents & canned replies)
– External Services:
- Google Gemini AI API (requires .env key)
- Wikipedia API (via wek.py)
b. Relationships
1. User types question → Browser POST to /process_input
2. Flask route dispatches to Chat_utils or wek
3. Chat_utils either:
- Loads fallback from responses.json
- Or calls Google Gemini API
4. If “Book review” intent detected → wek.py calls Wikipedia API
5. Response is assembled, stored in session, and returned as rendered HTML
c. Architectural Patterns
– Client-Server (thin client, server-side rendering)
– Modular backend: separation of concerns (routing vs. AI logic vs. external-API logic)
– Config management: .env for secrets
d. Technologies
– Python 3.x, Flask
– HTML5, Tailwind CSS (embedded in index.html)
– google.generativeai library
– wikipedia Python library
– JSON for knowledge base

##### Diagramming Conventions
- Blue rectangles for frontend components
- Green for backend modules
- Orange clouds for external services
– Group related modules inside a bigger “Flask Server” container.

##### Detailed Component Breakdown
- Browser
– index.html (Tailwind layout, chat UI, sidebar)
- Flask Server
– Chat.py (routing, session mgmt)
– Chat_utils.py (intent classification, fallback logic)
– wek.py (Wikipedia integration)
– responses.json (knowledge base)
– .env (Gemini API key)
- External Services
– Google Gemini AI API
– Wikipedia API

Putting It All Together:

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Layers and components

    %% Frontend Layer
    subgraph "Presentation Layer"
        direction TB
        Browser["Client Browser\n(index.html)"]:::frontend
    end

    %% Application Layer
    subgraph "Flask Server" 
        direction TB
        FlaskApp["Flask App\n(Chat.py)"]:::backend
        ChatUtils["Intent Classification & Fallback Logic\n(Chat_utils.py)"]:::backend
        Wek["Wikipedia Integration Module\n(wek.py)"]:::backend
        Responses["Local Knowledge Base\n(responses.json)"]:::data
        Env["Config Store for API Keys\n(.env)"]:::data
        MainRunner["Auxiliary Runner\n(Main.py)"]:::backend
    end

    %% External Services
    subgraph "External Services"
        direction TB
        Gemini["Google Gemini AI API"]:::external
        WikiAPI["Wikipedia API"]:::external
    end

    %% Documentation
    subgraph "Documentation & Examples"
        direction TB
        Readme["Project Documentation\n(README.md)"]:::data
        ApiTxt["API Usage Examples\n(Api.txt)"]:::data
    end

    %% Connections
    Browser -->|POST /process_input| FlaskApp
    FlaskApp -->|"invokes"| ChatUtils
    FlaskApp -->|"manages session"| Responses
    FlaskApp -->|"loads config"| Env
    FlaskApp -->|"started by"| MainRunner

    ChatUtils -->|"fallback lookup"| Responses
    ChatUtils -->|"calls Gemini API"| Gemini
    ChatUtils -->|"detects book intent"| Wek

    Wek -->|"sends summary request"| WikiAPI

    %% Click Events
    click Browser "https://github.com/sagnik24-k/chatbot2/blob/master/index.html"
    click FlaskApp "https://github.com/sagnik24-k/chatbot2/blob/master/Chat.py"
    click ChatUtils "https://github.com/sagnik24-k/chatbot2/blob/master/Chat_utils.py"
    click Wek "https://github.com/sagnik24-k/chatbot2/blob/master/wek.py"
    click Responses "https://github.com/sagnik24-k/chatbot2/blob/master/responses.json"
    click Env "https://github.com/sagnik24-k/chatbot2/blob/master/.env"
    click MainRunner "https://github.com/sagnik24-k/chatbot2/blob/master/Main.py"
    click ApiTxt "https://github.com/sagnik24-k/chatbot2/blob/master/Api.txt"
    click Readme "https://github.com/sagnik24-k/chatbot2/blob/master/README.md"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085,color:#004085;
    classDef backend fill:#d4edda,stroke:#155724,color:#155724;
    classDef data fill:#e2e3e5,stroke:#6c757d,color:#6c757d;
    classDef external fill:#fff3cd,stroke:#856404,color:#856404;
```
