# retajmelhem/imdb-sentiment-api architecture

> Architecture diagram of the GitHub repository retajmelhem/imdb-sentiment-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/retajmelhem/imdb-sentiment-api
- Repository: https://github.com/retajmelhem/imdb-sentiment-api

## Overview

This repository serves fine-tuned IMDB sentiment predictions through a FastAPI application. The main workflow is client request → API validation and safeguards → shared predictor → normalization/tokenization and batched model inference → typed prediction result. Separate branches cover notebook-based model research, artifact export/parity verification, and the optional Gradio interface. The application, inference modules, export tool, and parity checker are excerpted; the notebook and demo are included as documented capabilities, but their internal behavior is not sampled.

Main entry points: [`app/main.py`](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/app/main.py) (FastAPI app), [`src/sentiment/predictor.py`](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/predictor.py) (Sentiment predictor), [`src/sentiment/config.py`](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/config.py) (Settings and metadata), [`scripts/verify_parity.py`](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/scripts/verify_parity.py) (Inference parity check)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_research["Model research"]
  node_notebook["Training and evaluation"]
end

subgraph group_artifacts["Model artifacts"]
  node_model[("Fine-tuned model and tokenizer")]
  node_metadata_file["Inference metadata"]
  node_export["Checkpoint export<br/>[export_model.py]"]
  node_parity["Inference parity check<br/>[verify_parity.py]"]
end

subgraph group_serving["Inference service"]
  node_schemas["Request and result schemas<br/>[schemas.py]"]
  node_middleware["Body-size safeguard<br/>[middleware.py]"]
  node_config["Settings and metadata<br/>[config.py]"]
  node_logging["Logging setup<br/>[logging_config.py]"]
  node_errors["Inference errors<br/>[exceptions.py]"]
  node_predictor["Sentiment predictor<br/>[predictor.py]"]
  node_preprocessing["Normalize and encode<br/>[preprocessing.py]"]
end

subgraph group_interfaces["Client interfaces"]
  node_api["FastAPI app<br/>[main.py]"]
  node_gradio["Optional Gradio UI<br/>[app.py]"]
end

node_client(("Client"))
node_model_source[("Local model or Hub")]

node_client -->|"sends request"| node_api
node_api -->|"applies safeguard"| node_middleware
node_api -->|"validates and returns"| node_schemas
node_api -->|"loads settings"| node_config
node_api -->|"configures"| node_logging
node_api -->|"creates and calls"| node_predictor
node_api -->|"handles"| node_errors
node_predictor -->|"uses settings"| node_config
node_predictor -->|"raises"| node_errors
node_predictor -->|"encodes texts"| node_preprocessing
node_predictor -->|"builds results"| node_schemas
node_predictor -->|"loads artifacts"| node_model_source
node_config -->|"loads metadata"| node_metadata_file
node_notebook -->|"provides checkpoint"| node_export
node_export -->|"writes artifacts"| node_model
node_parity -->|"checks parity"| node_predictor
node_parity -->|"verifies outputs"| node_model
node_gradio -.->|"uses service"| node_api

click node_api "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/app/main.py"
click node_schemas "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/schemas.py"
click node_middleware "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/app/middleware.py"
click node_config "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/config.py"
click node_logging "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/logging_config.py"
click node_errors "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/exceptions.py"
click node_predictor "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/predictor.py"
click node_preprocessing "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/preprocessing.py"
click node_metadata_file "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/model_metadata.json"
click node_notebook "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/notebooks/model_training.ipynb"
click node_export "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/scripts/export_model.py"
click node_parity "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/scripts/verify_parity.py"
click node_gradio "https://github.com/retajmelhem/imdb-sentiment-api/blob/main/demo/app.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_notebook,node_client toneBlue
class node_model,node_metadata_file,node_export,node_parity,node_model_source toneAmber
class node_schemas,node_middleware,node_config,node_logging,node_errors,node_predictor,node_preprocessing toneMint
class node_api,node_gradio toneRose
```

## Components

### Model research

- [Training and evaluation](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/notebooks/model_training.ipynb): `notebooks/model_training.ipynb`

### Model artifacts

- **Fine-tuned model and tokenizer**
- [Inference metadata](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/model_metadata.json): `model_metadata.json`
- [Checkpoint export](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/scripts/export_model.py): `scripts/export_model.py`
- [Inference parity check](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/scripts/verify_parity.py): `scripts/verify_parity.py`

### Inference service

- [Request and result schemas](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/schemas.py): `src/sentiment/schemas.py`
- [Body-size safeguard](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/app/middleware.py): `app/middleware.py`
- [Settings and metadata](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/config.py): `src/sentiment/config.py`
- [Logging setup](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/logging_config.py): `src/sentiment/logging_config.py`
- [Inference errors](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/exceptions.py): `src/sentiment/exceptions.py`
- [Sentiment predictor](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/predictor.py): `src/sentiment/predictor.py`
- [Normalize and encode](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/src/sentiment/preprocessing.py): `src/sentiment/preprocessing.py`

### Client interfaces

- [FastAPI app](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/app/main.py): `app/main.py`
- [Optional Gradio UI](https://github.com/retajmelhem/imdb-sentiment-api/blob/main/demo/app.py): `demo/app.py`

### Other components

- **Client**
- **Local model or Hub**

## Connections

- Client → FastAPI app: sends request (evidence: `README.md`)
- FastAPI app → Body-size safeguard: applies safeguard (evidence: `app/main.py`)
- FastAPI app → Request and result schemas: validates and returns (evidence: `app/main.py`)
- FastAPI app → Settings and metadata: loads settings (evidence: `app/main.py`)
- FastAPI app → Logging setup: configures (evidence: `app/main.py`)
- FastAPI app → Sentiment predictor: creates and calls (evidence: `app/main.py`)
- FastAPI app → Inference errors: handles (evidence: `app/main.py`)
- Sentiment predictor → Settings and metadata: uses settings (evidence: `src/sentiment/predictor.py`)
- Sentiment predictor → Inference errors: raises (evidence: `src/sentiment/predictor.py`)
- Sentiment predictor → Normalize and encode: encodes texts (evidence: `src/sentiment/predictor.py`)
- Sentiment predictor → Request and result schemas: builds results (evidence: `src/sentiment/predictor.py`)
- Sentiment predictor → Local model or Hub: loads artifacts (evidence: `README.md`)
- Settings and metadata → Inference metadata: loads metadata (evidence: `README.md`)
- Training and evaluation → Checkpoint export: provides checkpoint (evidence: `README.md`)
- Checkpoint export → Fine-tuned model and tokenizer: writes artifacts (evidence: `scripts/export_model.py`)
- Inference parity check → Sentiment predictor: checks parity (evidence: `scripts/verify_parity.py`)
- Inference parity check → Fine-tuned model and tokenizer: verifies outputs (evidence: `README.md`)
- Optional Gradio UI → FastAPI app: uses service (evidence: `README.md`)
