# arpitpatelsitapur/scholarlensai architecture

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

- Interactive diagram: https://gitdiagram.com/arpitpatelsitapur/scholarlensai
- Repository: https://github.com/arpitpatelsitapur/scholarlensai

## Overview

1. **Overall shape**
   ScholarLens AI is split into **two FastAPI-based services**: the main application in `app/` and a separate RAG backend in `rag_service/`. The boundary is intentional: the main app owns users, pages, and recommendations, while the RAG service owns paper ingestion, chunking, vector search, and answer generation.

2. **Main application boundary**
   `app/main.py` is the entrypoint for the user-facing service. It serves HTML pages, handles browser navigation, and exposes API routes under `app/api/`. The app is not a pure JSON API; it is a server-rendered web app built around Jinja2 templates and static assets.

3. **Web/UI subsystem**
   `app/templates/` contains the primary views: home, dashboard, profile, bookmarks, chat, interests form, about, and 404. `app/static/` provides CSS, JS, and images. This subsystem is the presentation layer and depends on the API layer plus session/user state to render personalized pages.

4. **Authentication and user flow**
   `app/api/auth.py` implements **Google OAuth** login. `app/api/users.py` and the template-driven pages rely on authenticated identity to personalize dashboards, interests, and bookmarks. The auth boundary is external-facing: user identity is established with Google and then persisted in the app’s own database/session state.

5. **Application data layer**
   `app/db/` contains the persistence layer: SQLAlchemy models, session setup, and SQL helpers. This is where user records, bookmarks, and preference/interests data are stored. The app is configured via `app/core/config.py`, and the README indicates SQLite-backed persistence with Docker volume support for durability.

6. **Recommendation subsystem**
   `app/utils/recommend.py` is the semantic discovery engine. It uses **SPECTER2 embeddings** and similarity-based ranking to recommend papers based on user interests and paper metadata. This subsystem is distinct from the RAG flow: it ranks papers for discovery, but does not answer questions from paper content.

7. **RAG service boundary**
   `rag_service/api.py` exposes the paper Q&A endpoints, and `rag_service/rag_engine.py` contains the core retrieval-augmented generation pipeline. The main app calls this service over HTTP using `httpx`, so the chat feature is a cross-service interaction rather than an in-process library call.

8. **RAG data flow**
   For each paper, the RAG service downloads or reads the PDF, extracts text, chunks it, embeds the chunks, and stores them in a **per-paper FAISS index** under `rag_store/<paper_id>/`. On a user question, it retrieves the top relevant chunks, builds a prompt, and sends it to **Groq-hosted Llama 3.1** to produce the answer. Cached FAISS indexes avoid rebuilding on repeat access.

9. **External services and model dependencies**
   The system depends on several external services that materially affect the architecture: **Google OAuth** for login, **Groq** for LLM inference, and **Hugging Face / SentenceTransformers** for embeddings. These dependencies are isolated mostly inside the RAG service and the auth layer to keep the main app lightweight.

10. **Deployment and runtime separation**
   The repo is designed for **Docker Compose** deployment with two containers and separate dependency sets. This separation is important because the ML stack in `rag_service/` has different package constraints from the web app. Persistent state is split as well: the app keeps its SQLite data, while the RAG service persists its FAISS store and cached paper artifacts.

11. **Request-level architecture**
   A typical user journey crosses multiple subsystems: the browser loads a template from the app, login establishes identity, the dashboard pulls personalized recommendations from the app database and recommender, and clicking “chat” sends the paper/question to the RAG service, which returns a generated answer to be rendered back in the app UI.

12. **What to show in an architecture diagram**
   The key boxes are: **Browser/UI**, **Main FastAPI app**, **SQLite/SQLAlchemy persistence**, **Recommendation engine**, **RAG FastAPI service**, **FAISS per-paper store**, and **external providers** (Google OAuth, Groq, Hugging Face). The main arrows are: browser ↔ app, app ↔ database, app ↔ RAG service over HTTP, and RAG service ↔ PDFs/embeddings/FAISS ↔ Groq.

Main entry points: [`app/main.py`](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/main.py) (FastAPI App), [`app/api/pages.py`](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/api/pages.py) (Page Routes), [`rag_service/rag_engine.py`](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/rag_service/rag_engine.py) (RAG Engine), [`app/api/users.py`](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/api/users.py) (Users API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui_app["Main App"]
  node_browser_ui(("Browser UI<br/>client"))
  node_main_app["FastAPI App<br/>web app<br/>[main.py]"]
  node_page_routes["Page Routes<br/>routing<br/>[pages.py]"]
  node_auth_routes["Auth<br/>[auth.py]"]
  node_user_routes["Users API<br/>user routes<br/>[users.py]"]
  node_templates["Templates"]
  node_static_assets["Static Assets<br/>frontend assets"]
  node_app_config["Config<br/>settings<br/>[config.py]"]
  node_db_models[("Data Models<br/>orm models<br/>[models.py]")]
  node_db_session[("DB Session<br/>persistence<br/>[session.py]")]
  node_db_helpers["SQL Helpers<br/>db helpers<br/>[sql_helpers.py]"]
  node_recommender{{"Recommender<br/>ranking engine<br/>[recommend.py]"}}
  node_rag_client["RAG Client<br/>http client<br/>[rag.py]"]
end

subgraph group_rag_service["RAG Service"]
  node_rag_api["RAG API<br/>api service<br/>[api.py]"]
  node_rag_engine{{"RAG Engine<br/>retrieval pipeline<br/>[rag_engine.py]"}}
  node_faiss_store[("FAISS Store<br/>vector index")]
end

subgraph group_external["External Systems"]
  node_google_oauth(("Google OAuth<br/>identity provider"))
  node_groq_llm(("Groq LLM<br/>model provider"))
  node_hf_embeddings(("Embeddings<br/>model provider"))
end

node_browser_ui -->|"HTTP"| node_main_app
node_main_app -->|"routes"| node_page_routes
node_main_app -->|"routes"| node_auth_routes
node_main_app -->|"routes"| node_user_routes
node_page_routes -->|"render"| node_templates
node_page_routes -->|"serve"| node_static_assets
node_auth_routes -->|"oauth"| node_google_oauth
node_user_routes -->|"read/write"| node_db_session
node_db_session -->|"orm"| node_db_models
node_db_helpers -->|"use"| node_db_session
node_main_app -->|"config"| node_app_config
node_user_routes -->|"rank"| node_recommender
node_recommender -->|"embed"| node_hf_embeddings
node_page_routes -->|"chat"| node_rag_client
node_rag_client -->|"http"| node_rag_api
node_rag_api -->|"delegate"| node_rag_engine
node_rag_engine -->|"retrieve"| node_faiss_store
node_rag_engine -->|"embed"| node_hf_embeddings
node_rag_engine -->|"generate"| node_groq_llm

click node_main_app "https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/main.py"
click node_page_routes "https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/api/pages.py"
click node_auth_routes "https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/api/auth.py"
click node_user_routes "https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/api/users.py"
click node_templates "https://github.com/arpitpatelsitapur/scholarlensai/tree/main/app/templates"
click node_static_assets "https://github.com/arpitpatelsitapur/scholarlensai/tree/main/app/static"
click node_app_config "https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/core/config.py"
click node_db_models "https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/db/models.py"
click node_db_session "https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/db/session.py"
click node_db_helpers "https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/db/sql_helpers.py"
click node_recommender "https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/utils/recommend.py"
click node_rag_client "https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/utils/rag.py"
click node_rag_api "https://github.com/arpitpatelsitapur/scholarlensai/blob/main/rag_service/api.py"
click node_rag_engine "https://github.com/arpitpatelsitapur/scholarlensai/blob/main/rag_service/rag_engine.py"

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_main_app,node_page_routes,node_auth_routes,node_user_routes,node_templates,node_static_assets,node_app_config,node_db_models,node_db_session,node_db_helpers,node_recommender,node_rag_client toneBlue
class node_rag_api,node_rag_engine,node_faiss_store toneAmber
class node_google_oauth,node_groq_llm,node_hf_embeddings toneMint
```

## Components

### Main App

Browser-facing FastAPI application with pages, auth, user data, and recommendations.

- **Browser UI**: client · User interaction layer that loads pages and sends chat/recommendation requests.
- [FastAPI App](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/main.py): web app · Entry point and request coordinator for the main ScholarLens UI and APIs. · `app/main.py`
- [Page Routes](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/api/pages.py): routing · Server-rendered page handlers for home, dashboard, profile, bookmarks, chat, and forms. · `app/api/pages.py`
- [Auth](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/api/auth.py): oauth routes · Google OAuth login and identity establishment. · `app/api/auth.py`
- [Users API](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/api/users.py): user routes · User-facing API for profile, bookmarks, interests, and personalization data. · `app/api/users.py`
- [Templates](https://github.com/arpitpatelsitapur/scholarlensai/tree/main/app/templates): ui templates · Jinja2 views for the rendered pages and error screens. · `app/templates`
- [Static Assets](https://github.com/arpitpatelsitapur/scholarlensai/tree/main/app/static): frontend assets · CSS, JavaScript, logo, and image assets for the web UI. · `app/static`
- [Config](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/core/config.py): settings · Application settings and environment configuration. · `app/core/config.py`
- [Data Models](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/db/models.py): orm models · SQLAlchemy models for users, bookmarks, interests, and related app state. · `app/db/models.py`
- [DB Session](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/db/session.py): persistence · Database connection and session setup for app data access. · `app/db/session.py`
- [SQL Helpers](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/db/sql_helpers.py): db helpers · Query and persistence helpers used by the application layer. · `app/db/sql_helpers.py`
- [Recommender](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/utils/recommend.py): ranking engine · SPECTER2-based semantic ranking for paper discovery and personalized recommendations. · `app/utils/recommend.py`
- [RAG Client](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/app/utils/rag.py): http client · HTTP bridge from the main app to the separate RAG backend. · `app/utils/rag.py`

### RAG Service

Separate FastAPI service for paper ingestion, retrieval, and answer generation.

- [RAG API](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/rag_service/api.py): api service · FastAPI endpoints for paper Q&A in the separate retrieval service. · `rag_service/api.py`
- [RAG Engine](https://github.com/arpitpatelsitapur/scholarlensai/blob/main/rag_service/rag_engine.py): retrieval pipeline · Ingestion, chunking, retrieval, prompt building, and answer generation pipeline. · `rag_service/rag_engine.py`
- **FAISS Store**: vector index · Per-paper vector indexes and cached artifacts under the RAG storage directory.

### External Systems

Third-party identity, model, and storage dependencies.

- **Google OAuth**: identity provider · External identity provider used for login.
- **Groq LLM**: model provider · Hosted Llama 3.1 inference used to generate chat answers.
- **Embeddings**: model provider · Hugging Face / SentenceTransformers embedding provider for retrieval and ranking.

## Connections

- Browser UI → FastAPI App: HTTP · Browser loads pages and submits actions to the FastAPI app.
- FastAPI App → Page Routes: routes · Main app mounts the server-rendered page handlers.
- FastAPI App → Auth: routes · Main app exposes login and callback endpoints.
- FastAPI App → Users API: routes · Main app exposes user/profile/bookmark APIs.
- Page Routes → Templates: render · Pages render Jinja2 templates for the UI.
- Page Routes → Static Assets: serve · Rendered pages depend on static CSS, JS, and images.
- Auth → Google OAuth: oauth · Authentication delegates to Google.
- Users API → DB Session: read/write · User APIs persist and fetch application state.
- DB Session → Data Models: orm · Sessions operate on SQLAlchemy models.
- SQL Helpers → DB Session: use · Helpers execute database operations through the session layer.
- FastAPI App → Config: config · Application startup reads configured settings.
- Users API → Recommender: rank · User-facing discovery uses the semantic recommender.
- Recommender → Embeddings: embed · Recommendation ranking depends on embedding models.
- Page Routes → RAG Client: chat · Chat page sends paper questions through the RAG client.
- RAG Client → RAG API: http · Main app calls the separate RAG service over HTTP.
- RAG API → RAG Engine: delegate · API endpoints forward work to the retrieval pipeline.
- RAG Engine → FAISS Store: retrieve · Engine reads and updates per-paper vector indexes.
- RAG Engine → Embeddings: embed · Chunk and query embeddings come from Hugging Face/SentenceTransformers.
- RAG Engine → Groq LLM: generate · Retrieved context is sent to Groq-hosted Llama for answers.
