# nexpectarpit/stock_market_and_investment_analyser architecture

> Architecture diagram of the GitHub repository nexpectarpit/stock_market_and_investment_analyser, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nexpectarpit/stock_market_and_investment_analyser
- Repository: https://github.com/nexpectarpit/stock_market_and_investment_analyser

## Overview

1. This repo is a small Retrieval-Augmented Generation app focused on a single domain: answering stock market / investment questions from an uploaded PDF textbook.

2. The architecture is split into two clear runtime boundaries:
   - `backend/`: Python service that ingests the PDF, creates embeddings, stores/retrieves chunk data, and generates answers.
   - `frontend/`: static browser UI served separately with `python -m http.server`.

3. `backend/app.py` is the main backend entry point. It is the orchestration layer that connects PDF ingestion, retrieval, and response generation. From the README, it is started directly with `python app.py`, so it likely exposes the HTTP endpoints used by the frontend.

4. `backend/pdf_processor.py` is the ingestion subsystem. Its job is to extract text from the uploaded PDF, split it into chunks, and prepare those chunks for indexing. This is the first step in the RAG pipeline.

5. `backend/embeddings.py` is the vectorization subsystem. It likely takes the processed chunks and turns them into embeddings so the system can do semantic retrieval later. This is the key bridge between raw document text and question answering.

6. The data flow is:
   - user uploads the PDF in the UI
   - backend extracts and chunks the document
   - chunks are embedded
   - chunk text + embeddings are stored in a database
   - later questions retrieve the most relevant chunks
   - retrieved context is sent to the LLM to generate the answer

7. `backend/show_db.py` is an inspection/debug utility. The README explicitly mentions viewing generated chunks and embeddings in the database, so this script is for verifying what the indexing pipeline stored.

8. The generated answers are powered by an external LLM service, Groq. The backend reads `GROQ_API_KEY` from `backend/.env`, so the model call is an external dependency and a clear architecture boundary.

9. The frontend is intentionally simple:
   - `frontend/index.html` provides the page structure
   - `frontend/app.js` drives interaction with the backend
   It appears to support uploading the PDF and submitting the five required questions.

10. The repository is optimized for a local demo workflow rather than production deployment:
   - backend runs as a Python app
   - frontend is served as static files
   - the document is a local PDF in the repo
   - database contents are inspectable for assignment verification

11. The main architectural pattern is therefore: document ingestion + embedding index + retrieval + LLM generation, with a lightweight static UI on top.

12. For an architecture diagram, the most important boxes are:
   - Browser UI
   - Python backend API
   - PDF processing/chunking
   - Embedding generation
   - Database/vector storage
   - Groq LLM API
   - DB inspection utility

Main entry points: [`backend/app.py`](https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/backend/app.py) (Backend API), [`backend/pdf_processor.py`](https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/backend/pdf_processor.py) (PDF Processor), [`frontend/index.html`](https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/frontend/index.html) (Browser UI), [`frontend/app.js`](https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/frontend/app.js) (UI Logic)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_browser_ui["Browser UI<br/>client<br/>[index.html]"]
  node_ui_script["UI Logic<br/>script<br/>[app.js]"]
  node_static_server["Static Server"]
end

subgraph group_backend["Backend"]
  node_backend_api["Backend API<br/>orchestrator<br/>[app.py]"]
  node_pdf_processor["PDF Processor<br/>ingest<br/>[pdf_processor.py]"]
  node_embeddings["Embeddings<br/>vectorize<br/>[embeddings.py]"]
  node_db_inspector["DB Viewer<br/>inspect<br/>[show_db.py]"]
  node_chunk_store[("Chunk Store<br/>storage")]
  node_retriever["Retriever<br/>search"]
  node_answerer["Answer Generator<br/>llm orchestration"]
  node_question_flow["Q&A Flow<br/>request pipeline"]
  node_ingestion_flow["Ingest Flow<br/>pipeline"]
  node_index_flow["Index Build<br/>pipeline"]
  node_query_flow["Query Path<br/>pipeline"]
end

node_pdf_textbook["Textbook PDF<br/>source document<br/>[rag assigment.pdf]"]
node_groq_api{{"Groq API<br/>external llm"}}

node_static_server -->|"serves"| node_browser_ui
node_browser_ui -->|"runs"| node_ui_script
node_ui_script -->|"requests"| node_backend_api
node_backend_api -->|"starts"| node_ingestion_flow
node_ingestion_flow -->|"extracts"| node_pdf_processor
node_pdf_textbook -->|"input"| node_pdf_processor
node_pdf_processor -->|"chunks"| node_index_flow
node_index_flow -->|"vectorizes"| node_embeddings
node_embeddings -->|"stores"| node_chunk_store
node_backend_api -->|"handles"| node_question_flow
node_question_flow -->|"retrieves"| node_retriever
node_retriever -->|"reads"| node_chunk_store
node_retriever -->|"passes context"| node_query_flow
node_query_flow -->|"builds"| node_answerer
node_answerer -->|"calls"| node_groq_api
node_backend_api -.->|"supports"| node_db_inspector
node_db_inspector -->|"reads"| node_chunk_store

click node_browser_ui "https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/frontend/index.html"
click node_ui_script "https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/frontend/app.js"
click node_backend_api "https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/backend/app.py"
click node_pdf_processor "https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/backend/pdf_processor.py"
click node_embeddings "https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/backend/embeddings.py"
click node_db_inspector "https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/backend/show_db.py"
click node_pdf_textbook "https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/rag assigment.pdf"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser_ui,node_ui_script,node_static_server toneBlue
class node_backend_api,node_pdf_processor,node_embeddings,node_db_inspector,node_chunk_store,node_retriever,node_answerer,node_question_flow,node_ingestion_flow,node_index_flow,node_query_flow toneAmber
class node_pdf_textbook,node_groq_api toneNeutral
```

## Components

### Frontend

Static browser UI served separately for the demo.

- [Browser UI](https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/frontend/index.html): client · User-facing page for upload and Q&A. · `frontend/index.html`
- [UI Logic](https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/frontend/app.js): script · Handles user interaction and backend calls. · `frontend/app.js`
- **Static Server**: serve · Simple local file server for the frontend.

### Backend

Python runtime that ingests the PDF, retrieves context, and generates answers.

- [Backend API](https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/backend/app.py): orchestrator · Main entry point that coordinates ingestion, retrieval, and generation. · `backend/app.py`
- [PDF Processor](https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/backend/pdf_processor.py): ingest · Extracts text from the uploaded PDF and splits it into chunks. · `backend/pdf_processor.py`
- [Embeddings](https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/backend/embeddings.py): vectorize · Turns chunks into embeddings for semantic retrieval. · `backend/embeddings.py`
- [DB Viewer](https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/main/backend/show_db.py): inspect · Debug utility for viewing stored chunks and embeddings. · `backend/show_db.py`
- **Chunk Store**: storage · Persists chunk text and embedding data for later retrieval.
- **Retriever**: search · Fetches the most relevant chunks for a question.
- **Answer Generator**: llm orchestration · Builds the final response from retrieved context.
- **Q&A Flow**: request pipeline · Routes user questions from the UI into the backend response path.
- **Ingest Flow**: pipeline · Runs upload, extraction, chunking, and indexing.
- **Index Build**: pipeline · Converts processed text into searchable vector data.
- **Query Path**: pipeline · Retrieves context and asks the LLM for an answer.

### Other components

- [Textbook PDF](https://github.com/nexpectarpit/stock_market_and_investment_analyser/blob/HEAD/rag%20assigment.pdf): source document · Domain document uploaded into the RAG pipeline. · `rag assigment.pdf`
- **Groq API**: external llm · External model provider used to generate answers.

## Connections

- Static Server → Browser UI: serves · The local server hosts the frontend files.
- Browser UI → UI Logic: runs · The page loads the client-side logic.
- UI Logic → Backend API: requests · Sends upload and question requests to the backend.
- Backend API → Ingest Flow: starts · Coordinates the document indexing workflow.
- Ingest Flow → PDF Processor: extracts · The backend hands the uploaded PDF to the processor.
- Textbook PDF → PDF Processor: input · The uploaded textbook is the ingestion source.
- PDF Processor → Index Build: chunks · Processed text is prepared for embedding.
- Index Build → Embeddings: vectorizes · Chunks are converted into embeddings.
- Embeddings → Chunk Store: stores · Embedding data and chunk text are persisted.
- Backend API → Q&A Flow: handles · Answers incoming user questions.
- Q&A Flow → Retriever: retrieves · Finds the most relevant stored chunks.
- Retriever → Chunk Store: reads · Looks up candidate chunks and vectors.
- Retriever → Query Path: passes context · Provides retrieved context to the answer path.
- Query Path → Answer Generator: builds · Prepares the prompt and response request.
- Answer Generator → Groq API: calls · Uses the external LLM service for generation.
- Backend API → DB Viewer: supports · The backend can be inspected through the DB viewer utility.
- DB Viewer → Chunk Store: reads · Shows stored chunks and embeddings for verification.
