Spaces:
Sleeping
Sleeping
Deploy personalization engine
Browse files- .dockerignore +16 -0
- .gitignore +18 -0
- .python-version +1 -0
- Dockerfile +71 -0
- README.md +149 -4
- pyproject.toml +23 -0
- requirements.txt +8 -0
- scripts/1b_generate_semantic_data.py +109 -0
- scripts/download_artifacts.py +54 -0
- scripts/download_model.py +13 -0
- scripts/evaluate_quality.py +94 -0
- scripts/evaluate_system.py +149 -0
- scripts/inspect_data.py +33 -0
- scripts/optimize_index.py +47 -0
- scripts/visualize_users.py +113 -0
- src/personalization/__init__.py +0 -0
- src/personalization/api/__init__.py +0 -0
- src/personalization/api/main.py +186 -0
- src/personalization/config.py +12 -0
- test_api.py +46 -0
- uv.lock +0 -0
.dockerignore
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
+
*.pyc
|
| 3 |
+
*.pyo
|
| 4 |
+
*.pyd
|
| 5 |
+
.Python
|
| 6 |
+
env/
|
| 7 |
+
venv/
|
| 8 |
+
.venv/
|
| 9 |
+
*.log
|
| 10 |
+
.git
|
| 11 |
+
.mypy_cache
|
| 12 |
+
.pytest_cache
|
| 13 |
+
|
| 14 |
+
# Ignore raw data if any, but keep catalog/index/embeddings
|
| 15 |
+
data/raw
|
| 16 |
+
data/synthetic
|
.gitignore
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
.venv/
|
| 6 |
+
venv/
|
| 7 |
+
.env
|
| 8 |
+
|
| 9 |
+
# Data & Models (Too large for git)
|
| 10 |
+
data/
|
| 11 |
+
*.pt
|
| 12 |
+
*.pth
|
| 13 |
+
*.parquet
|
| 14 |
+
*.csv
|
| 15 |
+
|
| 16 |
+
# IDE
|
| 17 |
+
.vscode/
|
| 18 |
+
.idea/
|
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.12
|
Dockerfile
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Build Stage
|
| 2 |
+
FROM python:3.10-slim AS builder
|
| 3 |
+
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 7 |
+
ENV PYTHONUNBUFFERED=1
|
| 8 |
+
|
| 9 |
+
# Install build dependencies
|
| 10 |
+
RUN apt-get update && apt-get install -y \
|
| 11 |
+
gcc \
|
| 12 |
+
python3-dev \
|
| 13 |
+
curl \
|
| 14 |
+
git \
|
| 15 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
+
|
| 17 |
+
# Install uv
|
| 18 |
+
RUN pip install --no-cache-dir uv
|
| 19 |
+
|
| 20 |
+
COPY requirements.txt .
|
| 21 |
+
|
| 22 |
+
# Create virtual environment and install dependencies
|
| 23 |
+
ENV UV_HTTP_TIMEOUT=300
|
| 24 |
+
RUN uv venv .venv && \
|
| 25 |
+
uv pip install --no-cache -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu
|
| 26 |
+
|
| 27 |
+
# --- Runtime Stage ---
|
| 28 |
+
FROM python:3.10-slim
|
| 29 |
+
|
| 30 |
+
WORKDIR /app
|
| 31 |
+
|
| 32 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 33 |
+
ENV PYTHONUNBUFFERED=1
|
| 34 |
+
ENV PATH="/app/.venv:$PATH"
|
| 35 |
+
ENV LANG=C.UTF-8
|
| 36 |
+
ENV LC_ALL=C.UTF-8
|
| 37 |
+
|
| 38 |
+
# Install runtime dependencies
|
| 39 |
+
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
|
| 40 |
+
|
| 41 |
+
# Copy virtual environment from builder
|
| 42 |
+
COPY --from=builder /app/.venv /app/.venv
|
| 43 |
+
|
| 44 |
+
# Copy Scripts
|
| 45 |
+
COPY scripts/ ./scripts/
|
| 46 |
+
|
| 47 |
+
# Data & Model Baking
|
| 48 |
+
ENV HF_HOME=/app/data/model_cache
|
| 49 |
+
|
| 50 |
+
# Download Model
|
| 51 |
+
RUN /app/.venv/bin/python scripts/download_model.py
|
| 52 |
+
|
| 53 |
+
# Download Data
|
| 54 |
+
# Ensure data directory exists
|
| 55 |
+
RUN mkdir -p data/catalog data/index
|
| 56 |
+
RUN /app/.venv/bin/python scripts/download_artifacts.py
|
| 57 |
+
|
| 58 |
+
# Copy Code (Last to maximize layer caching)
|
| 59 |
+
COPY src/ ./src/
|
| 60 |
+
|
| 61 |
+
# Create directories and permissions
|
| 62 |
+
# RUN addgroup --system app && adduser --system --group app && \
|
| 63 |
+
# chown -R app:app /app
|
| 64 |
+
|
| 65 |
+
# USER app
|
| 66 |
+
|
| 67 |
+
# Expose port
|
| 68 |
+
EXPOSE 7860
|
| 69 |
+
|
| 70 |
+
# Run Command
|
| 71 |
+
CMD ["uvicorn", "src.personalization.api.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,11 +1,156 @@
|
|
| 1 |
-
---
|
| 2 |
title: Personalisation Engine
|
| 3 |
emoji: 😻
|
| 4 |
colorFrom: gray
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
-
license: mit
|
| 9 |
-
---
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
title: Personalisation Engine
|
| 2 |
emoji: 😻
|
| 3 |
colorFrom: gray
|
| 4 |
colorTo: indigo
|
| 5 |
sdk: docker
|
| 6 |
pinned: false
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Semantic Book Personalization Engine
|
| 9 |
+
|
| 10 |
+
A high-performance, standalone recommendation service that uses **Semantic Search** to provide personalized book suggestions.
|
| 11 |
+
|
| 12 |
+
Unlike traditional recommenders that rely on collaborative filtering (which fails without massive user data), this engine uses **Sentence Transformers** to understand the *content* of books (Title + Author + Genre + Description), allowing it to work effectively from Day 1 ("Cold Start").
|
| 13 |
+
|
| 14 |
+
## 🚀 Key Features
|
| 15 |
+
|
| 16 |
+
* **Semantic Understanding:** Connects "The Haunted School" to "Ghost Beach" based on plot descriptions, not just title keywords.
|
| 17 |
+
* **Hybrid Scoring:** Combines **Semantic Similarity** (85%) with **Book Ratings** (15%) to recommend high-quality matches.
|
| 18 |
+
* **Smart Optimization:** Uses **Product Quantization (IVF-PQ)** to compress the search index by **48x** (146MB -> 3MB) with minimal accuracy loss.
|
| 19 |
+
* **Time-Decay Memory:** Prioritizes a user's *recent* reads over ancient history.
|
| 20 |
+
* **Evaluation:** Achieves **40% Exact Hit Rate @ 10** on held-out author tests.
|
| 21 |
+
* **Standalone API:** Runs as a separate microservice (FastAPI) on Port 8001.
|
| 22 |
+
|
| 23 |
+
## 🏗️ Architecture
|
| 24 |
+
|
| 25 |
+
This project uses a **retrieval-based** approach:
|
| 26 |
+
|
| 27 |
+
1. **The Brain:** A pre-trained `all-MiniLM-L6-v2` model encodes all book metadata (Title, Author, Genre, Description) into 384-dimensional vectors.
|
| 28 |
+
2. **The Index:** A highly optimized FAISS `IndexIVFPQ` (Inverted File + Product Quantization) index for millisecond retrieval.
|
| 29 |
+
3. **The Engine:**
|
| 30 |
+
* User history is converted to vectors.
|
| 31 |
+
* Vectors are aggregated using **Time-Decay Averaging**.
|
| 32 |
+
* The engine searches the FAISS index for the nearest neighbors.
|
| 33 |
+
* Results are re-ranked using the book's rating.
|
| 34 |
+
|
| 35 |
+
## 📦 Installation & Setup
|
| 36 |
+
|
| 37 |
+
### Prerequisites
|
| 38 |
+
* Python 3.10+ (or Docker)
|
| 39 |
+
* `uv` (recommended for fast package management) or `pip`
|
| 40 |
+
|
| 41 |
+
### 1. Clone the Repository
|
| 42 |
+
```bash
|
| 43 |
+
git clone <your-repo-url>
|
| 44 |
+
cd personalise
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
### 2. Setup Environment
|
| 48 |
+
```bash
|
| 49 |
+
# Using uv (Recommended)
|
| 50 |
+
uv venv
|
| 51 |
+
# Windows:
|
| 52 |
+
.venv\Scripts\activate
|
| 53 |
+
# Linux/Mac:
|
| 54 |
+
source .venv/bin/activate
|
| 55 |
+
|
| 56 |
+
uv pip install -r requirements.txt
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
### 3. Data Preparation (Crucial Step)
|
| 60 |
+
The system needs the "Brain" (Embeddings) and "Index" to function.
|
| 61 |
+
|
| 62 |
+
**Option A: Download Pre-computed Artifacts (Fast)**
|
| 63 |
+
```bash
|
| 64 |
+
# Make sure you are in the root 'personalise' folder
|
| 65 |
+
python scripts/download_artifacts.py
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
**Option B: Generate from Scratch (Slow - ~1.5 hours)**
|
| 69 |
+
```bash
|
| 70 |
+
# 1. Generate Embeddings
|
| 71 |
+
python scripts/1b_generate_semantic_data.py
|
| 72 |
+
|
| 73 |
+
# 2. Optimize Index
|
| 74 |
+
python scripts/optimize_index.py
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
## 🏃 Run the Application
|
| 78 |
+
|
| 79 |
+
### Option A: Run Locally
|
| 80 |
+
```bash
|
| 81 |
+
uvicorn src.personalization.api.main:app --reload --port 8001
|
| 82 |
+
```
|
| 83 |
+
API will be available at `http://localhost:8001`.
|
| 84 |
+
|
| 85 |
+
### Option B: Run with Docker
|
| 86 |
+
The Dockerfile is optimized to cache the model and data layers.
|
| 87 |
+
|
| 88 |
+
```bash
|
| 89 |
+
# 1. Build the image
|
| 90 |
+
docker build -t personalise .
|
| 91 |
+
|
| 92 |
+
# 2. Run the container
|
| 93 |
+
docker run -p 8001:8001 personalise
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
## 🧪 Evaluation & Demo
|
| 97 |
+
We have included a synthetic dataset of 10,000 users to validate the model.
|
| 98 |
+
|
| 99 |
+
**Run the Offline Evaluation:**
|
| 100 |
+
This script uses a "Leave-One-Out" strategy to see if the model can predict the next book a user reads.
|
| 101 |
+
```bash
|
| 102 |
+
python scripts/evaluate_system.py
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
**Visualize User Clusters:**
|
| 106 |
+
Generate a 2D t-SNE plot showing how the model groups users by interest (requires `matplotlib` & `seaborn`).
|
| 107 |
+
```bash
|
| 108 |
+
# First install viz deps
|
| 109 |
+
uv pip install matplotlib seaborn
|
| 110 |
+
|
| 111 |
+
# Run visualization
|
| 112 |
+
python scripts/visualize_users.py
|
| 113 |
+
```
|
| 114 |
+
*Output saved to `docs/user_clusters_tsne.png`*
|
| 115 |
+
|
| 116 |
+
**Inspect Synthetic Data:**
|
| 117 |
+
```bash
|
| 118 |
+
python scripts/inspect_data.py
|
| 119 |
+
```
|
| 120 |
+
|
| 121 |
+
## 📡 API Usage
|
| 122 |
+
|
| 123 |
+
#### POST `/personalize/recommend`
|
| 124 |
+
Get personalized books based on reading history.
|
| 125 |
+
```json
|
| 126 |
+
{
|
| 127 |
+
"user_history": ["The Haunted School", "It Came from Beneath the Sink!"],
|
| 128 |
+
"top_k": 5
|
| 129 |
+
}
|
| 130 |
+
```
|
| 131 |
+
|
| 132 |
+
#### POST `/search`
|
| 133 |
+
Semantic search by plot or vibe.
|
| 134 |
+
```json
|
| 135 |
+
{
|
| 136 |
+
"query": "detective in space solving crimes",
|
| 137 |
+
"top_k": 5
|
| 138 |
+
}
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
## 📊 Performance Stats
|
| 142 |
+
|
| 143 |
+
| Metric | Brute Force (Flat) | Optimized (IVF-PQ) |
|
| 144 |
+
| :--- | :--- | :--- |
|
| 145 |
+
| **Memory** | ~150 MB | **~3 MB** |
|
| 146 |
+
| **Recall @ 10** | 100% | ~95% |
|
| 147 |
+
| **Speed** | ~10ms | ~2ms |
|
| 148 |
+
| **Hit Rate @ 10** | N/A | **40.0%** |
|
| 149 |
+
|
| 150 |
+
## 🗺️ Roadmap & Future Improvements
|
| 151 |
+
* **Model Compression (ONNX):** Replace the heavy PyTorch dependency with **ONNX Runtime**. This would reduce the Docker image size from ~3GB to ~500MB and improve CPU inference latency by 2-3x.
|
| 152 |
+
* **Real-Time Learning:** Implement a "Session-Based" Recommender (using RNNs or Transformers) to adapt to user intent within a single session, rather than just long-term history.
|
| 153 |
+
* **A/B Testing Framework:** Add infrastructure to serve different model versions to different user segments to scientifically measure engagement.
|
| 154 |
+
|
| 155 |
+
## 📄 License
|
| 156 |
+
MIT
|
pyproject.toml
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "personalise"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.12"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"faiss-cpu>=1.13.0",
|
| 9 |
+
"fastapi>=0.123.0",
|
| 10 |
+
"huggingface-hub>=0.36.0",
|
| 11 |
+
"matplotlib>=3.10.7",
|
| 12 |
+
"numpy>=2.3.5",
|
| 13 |
+
"pandas>=2.3.3",
|
| 14 |
+
"prometheus-fastapi-instrumentator>=7.1.0",
|
| 15 |
+
"pyarrow>=22.0.0",
|
| 16 |
+
"requests>=2.32.5",
|
| 17 |
+
"scikit-learn>=1.7.2",
|
| 18 |
+
"seaborn>=0.13.2",
|
| 19 |
+
"sentence-transformers>=5.1.2",
|
| 20 |
+
"torch>=2.9.1",
|
| 21 |
+
"tqdm>=4.67.1",
|
| 22 |
+
"uvicorn>=0.38.0",
|
| 23 |
+
]
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
numpy
|
| 4 |
+
pandas
|
| 5 |
+
faiss-cpu
|
| 6 |
+
sentence-transformers
|
| 7 |
+
requests
|
| 8 |
+
huggingface_hub
|
scripts/1b_generate_semantic_data.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from tqdm import tqdm
|
| 5 |
+
import json
|
| 6 |
+
import torch
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
import random
|
| 9 |
+
import faiss
|
| 10 |
+
|
| 11 |
+
NUM_USERS = 10000
|
| 12 |
+
MIN_SEQUENCE_LENGTH = 5
|
| 13 |
+
MAX_SEQUENCE_LENGTH = 50
|
| 14 |
+
DATA_DIR = Path("data")
|
| 15 |
+
CATALOG_PATH = DATA_DIR / "catalog" / "books_catalog.csv"
|
| 16 |
+
OUTPUT_DIR = DATA_DIR / "synthetic"
|
| 17 |
+
MODEL_NAME = "all-MiniLM-L6-v2"
|
| 18 |
+
|
| 19 |
+
def main():
|
| 20 |
+
print("Loading catalog...")
|
| 21 |
+
df = pd.read_csv(CATALOG_PATH)
|
| 22 |
+
|
| 23 |
+
df['rich_content'] = (
|
| 24 |
+
"Title: " + df['title'].fillna("") +
|
| 25 |
+
"; Author: " + df['authors'].fillna("Unknown") +
|
| 26 |
+
"; Genres: " + df['genres'].fillna("") +
|
| 27 |
+
"; Description: " + df['description'].fillna("").astype(str).str.slice(0, 300)
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
titles = df['title'].tolist()
|
| 31 |
+
content_to_encode = df['rich_content'].tolist()
|
| 32 |
+
|
| 33 |
+
EMBEDDINGS_CACHE = DATA_DIR / "embeddings_cache.npy"
|
| 34 |
+
|
| 35 |
+
if EMBEDDINGS_CACHE.exists():
|
| 36 |
+
print(f"Loading cached embeddings from {EMBEDDINGS_CACHE}...")
|
| 37 |
+
emb_np = np.load(EMBEDDINGS_CACHE)
|
| 38 |
+
print("Embeddings loaded.")
|
| 39 |
+
else:
|
| 40 |
+
print(f"Loading Teacher Model ({MODEL_NAME})...")
|
| 41 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 42 |
+
model = SentenceTransformer(MODEL_NAME, device=device)
|
| 43 |
+
|
| 44 |
+
print("Encoding books (Title + Author + Genre + Desc)...")
|
| 45 |
+
embeddings = model.encode(content_to_encode, show_progress_bar=True, convert_to_tensor=True)
|
| 46 |
+
emb_np = embeddings.cpu().numpy()
|
| 47 |
+
|
| 48 |
+
print(f"Saving embeddings to {EMBEDDINGS_CACHE}...")
|
| 49 |
+
np.save(EMBEDDINGS_CACHE, emb_np)
|
| 50 |
+
|
| 51 |
+
print(f"Generating {NUM_USERS} semantic user journeys...")
|
| 52 |
+
|
| 53 |
+
cpu_index = faiss.IndexFlatIP(emb_np.shape[1])
|
| 54 |
+
faiss.normalize_L2(emb_np)
|
| 55 |
+
cpu_index.add(emb_np)
|
| 56 |
+
|
| 57 |
+
users = []
|
| 58 |
+
|
| 59 |
+
for user_id in tqdm(range(NUM_USERS)):
|
| 60 |
+
sequence = []
|
| 61 |
+
|
| 62 |
+
num_interests = random.choice([1, 1, 2, 3])
|
| 63 |
+
|
| 64 |
+
for _ in range(num_interests):
|
| 65 |
+
anchor_idx = random.randint(0, len(titles) - 1)
|
| 66 |
+
|
| 67 |
+
k_neighbors = 50
|
| 68 |
+
q = emb_np[anchor_idx].reshape(1, -1)
|
| 69 |
+
_, indices = cpu_index.search(q, k_neighbors)
|
| 70 |
+
neighbors_indices = indices[0]
|
| 71 |
+
|
| 72 |
+
num_to_read = random.randint(5, 15)
|
| 73 |
+
|
| 74 |
+
read_indices = np.random.choice(neighbors_indices, size=min(len(neighbors_indices), num_to_read), replace=False)
|
| 75 |
+
|
| 76 |
+
for idx in read_indices:
|
| 77 |
+
sequence.append(titles[idx])
|
| 78 |
+
|
| 79 |
+
if len(sequence) > MAX_SEQUENCE_LENGTH:
|
| 80 |
+
sequence = sequence[:MAX_SEQUENCE_LENGTH]
|
| 81 |
+
|
| 82 |
+
if len(sequence) >= MIN_SEQUENCE_LENGTH:
|
| 83 |
+
users.append({
|
| 84 |
+
'user_id': user_id,
|
| 85 |
+
'book_sequence': sequence,
|
| 86 |
+
'sequence_length': len(sequence),
|
| 87 |
+
'persona': 'semantic_explorer',
|
| 88 |
+
'metadata': {'generated': True}
|
| 89 |
+
})
|
| 90 |
+
|
| 91 |
+
users_df = pd.DataFrame(users)
|
| 92 |
+
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 93 |
+
output_path = OUTPUT_DIR / "user_sequences.parquet"
|
| 94 |
+
users_df.to_parquet(output_path, index=False)
|
| 95 |
+
|
| 96 |
+
stats = {
|
| 97 |
+
'num_users': len(users_df),
|
| 98 |
+
'avg_sequence_length': float(users_df['sequence_length'].mean()),
|
| 99 |
+
'generated_via': "semantic_clustering"
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
with open(OUTPUT_DIR / "user_metadata.json", 'w') as f:
|
| 103 |
+
json.dump(stats, f, indent=2)
|
| 104 |
+
|
| 105 |
+
print(f"\n Generated {len(users_df)} semantic users")
|
| 106 |
+
print(f" Output: {output_path}")
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
main()
|
scripts/download_artifacts.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import shutil
|
| 5 |
+
|
| 6 |
+
HF_REPO_ID = "nice-bill/book-recommender-artifacts"
|
| 7 |
+
REPO_TYPE = "dataset"
|
| 8 |
+
|
| 9 |
+
# Local Paths
|
| 10 |
+
DATA_DIR = Path("data")
|
| 11 |
+
CATALOG_DIR = DATA_DIR / "catalog"
|
| 12 |
+
INDEX_DIR = DATA_DIR / "index"
|
| 13 |
+
|
| 14 |
+
FILES_TO_DOWNLOAD = {
|
| 15 |
+
"books_catalog.csv": CATALOG_DIR,
|
| 16 |
+
"embeddings_cache.npy": DATA_DIR,
|
| 17 |
+
"optimized.index": INDEX_DIR
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
def main():
|
| 21 |
+
print(f"--- Checking artifacts from {HF_REPO_ID} ---")
|
| 22 |
+
|
| 23 |
+
# Ensure directories exist
|
| 24 |
+
for dir_path in [DATA_DIR, CATALOG_DIR, INDEX_DIR]:
|
| 25 |
+
dir_path.mkdir(parents=True, exist_ok=True)
|
| 26 |
+
|
| 27 |
+
for filename, dest_dir in FILES_TO_DOWNLOAD.items():
|
| 28 |
+
dest_path = dest_dir / filename
|
| 29 |
+
|
| 30 |
+
if dest_path.exists():
|
| 31 |
+
print(f"Found {filename}")
|
| 32 |
+
continue
|
| 33 |
+
|
| 34 |
+
print(f"Downloading {filename}...")
|
| 35 |
+
try:
|
| 36 |
+
# Download to local cache
|
| 37 |
+
cached_path = hf_hub_download(
|
| 38 |
+
repo_id=HF_REPO_ID,
|
| 39 |
+
filename=filename,
|
| 40 |
+
repo_type=REPO_TYPE
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Copy from cache to our project structure
|
| 44 |
+
shutil.copy(cached_path, dest_path)
|
| 45 |
+
print(f" Saved to {dest_path}")
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"Failed to download {filename}: {e}")
|
| 49 |
+
print(" (Did you create the HF repo and upload the files?)")
|
| 50 |
+
|
| 51 |
+
print("\nArtifact setup complete.")
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
main()
|
scripts/download_model.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
MODEL_NAME = "all-MiniLM-L6-v2"
|
| 5 |
+
|
| 6 |
+
def download():
|
| 7 |
+
print(f"Downloading {MODEL_NAME}...")
|
| 8 |
+
# This will download to HF_HOME (set in Dockerfile)
|
| 9 |
+
SentenceTransformer(MODEL_NAME)
|
| 10 |
+
print("Done.")
|
| 11 |
+
|
| 12 |
+
if __name__ == "__main__":
|
| 13 |
+
download()
|
scripts/evaluate_quality.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import requests
|
| 3 |
+
import random
|
| 4 |
+
import argparse
|
| 5 |
+
import sys
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
# Add src to path
|
| 10 |
+
sys.path.append(str(Path(__file__).parent.parent))
|
| 11 |
+
from src.personalization.config import settings
|
| 12 |
+
|
| 13 |
+
# Config
|
| 14 |
+
CATALOG_PATH = Path("data/catalog/books_catalog.csv")
|
| 15 |
+
NUM_SAMPLES = 100
|
| 16 |
+
|
| 17 |
+
def main():
|
| 18 |
+
parser = argparse.ArgumentParser()
|
| 19 |
+
parser.add_argument("--host", type=str, default=settings.HOST)
|
| 20 |
+
parser.add_argument("--port", type=int, default=settings.PORT)
|
| 21 |
+
parser.add_argument("--samples", type=int, default=100, help="Number of evaluation queries")
|
| 22 |
+
args = parser.parse_args()
|
| 23 |
+
|
| 24 |
+
api_url = f"http://{args.host}:{args.port}/personalize/recommend"
|
| 25 |
+
|
| 26 |
+
print("Loading catalog for ground truth...")
|
| 27 |
+
if not CATALOG_PATH.exists():
|
| 28 |
+
print("Catalog not found!")
|
| 29 |
+
return
|
| 30 |
+
|
| 31 |
+
df = pd.read_csv(CATALOG_PATH)
|
| 32 |
+
|
| 33 |
+
# Filter authors with at least 5 books
|
| 34 |
+
author_counts = df['authors'].value_counts()
|
| 35 |
+
valid_authors = author_counts[author_counts >= 5].index.tolist()
|
| 36 |
+
|
| 37 |
+
print(f"Found {len(valid_authors)} authors with 5+ books.")
|
| 38 |
+
|
| 39 |
+
hits = 0
|
| 40 |
+
genre_matches = 0
|
| 41 |
+
total_recs = 0
|
| 42 |
+
|
| 43 |
+
print(f"Running {args.samples} evaluation queries against {api_url}...")
|
| 44 |
+
|
| 45 |
+
for _ in tqdm(range(args.samples)):
|
| 46 |
+
# 1. Pick a random author
|
| 47 |
+
author = random.choice(valid_authors)
|
| 48 |
+
books = df[df['authors'] == author]
|
| 49 |
+
|
| 50 |
+
if len(books) < 5:
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
+
# 2. Split: History (3 books) -> Target (1 book)
|
| 54 |
+
sample = books.sample(n=4, replace=False)
|
| 55 |
+
history = sample.iloc[:3]['title'].tolist()
|
| 56 |
+
target_book = sample.iloc[3]
|
| 57 |
+
target_title = target_book['title']
|
| 58 |
+
|
| 59 |
+
# 3. Call API
|
| 60 |
+
try:
|
| 61 |
+
payload = {"user_history": history, "top_k": 10}
|
| 62 |
+
resp = requests.post(api_url, json=payload)
|
| 63 |
+
|
| 64 |
+
if resp.status_code != 200:
|
| 65 |
+
continue
|
| 66 |
+
|
| 67 |
+
recs = resp.json()
|
| 68 |
+
rec_titles = [r['title'] for r in recs]
|
| 69 |
+
|
| 70 |
+
# Metrics
|
| 71 |
+
if target_title in rec_titles:
|
| 72 |
+
hits += 1
|
| 73 |
+
|
| 74 |
+
# Author Match
|
| 75 |
+
rec_authors = df[df['title'].isin(rec_titles)]['authors'].tolist()
|
| 76 |
+
if author in rec_authors:
|
| 77 |
+
matches = rec_authors.count(author)
|
| 78 |
+
genre_matches += matches
|
| 79 |
+
|
| 80 |
+
total_recs += len(recs)
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"Connection Error: {e}")
|
| 84 |
+
break
|
| 85 |
+
|
| 86 |
+
if total_recs > 0:
|
| 87 |
+
print("\n--- Evaluation Results ---")
|
| 88 |
+
print(f"Exact Target Hit Rate @ 10: {hits / args.samples:.2%}")
|
| 89 |
+
print(f"Same Author Relevance: {genre_matches / total_recs:.2%} (Approx)")
|
| 90 |
+
else:
|
| 91 |
+
print("No results obtained. Check API connection.")
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
main()
|
scripts/evaluate_system.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import faiss
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import logging
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
from tqdm import tqdm
|
| 8 |
+
|
| 9 |
+
# Setup
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
logger = logging.getLogger("Evaluator")
|
| 12 |
+
|
| 13 |
+
# Paths
|
| 14 |
+
DATA_DIR = Path("data")
|
| 15 |
+
SYNTHETIC_DATA_PATH = DATA_DIR / "synthetic" / "user_sequences.parquet"
|
| 16 |
+
CATALOG_PATH = DATA_DIR / "catalog" / "books_catalog.csv"
|
| 17 |
+
EMBEDDINGS_PATH = DATA_DIR / "embeddings_cache.npy"
|
| 18 |
+
INDEX_PATH = DATA_DIR / "index" / "optimized.index"
|
| 19 |
+
|
| 20 |
+
def evaluate_hit_rate(top_k=10, sample_size=1000):
|
| 21 |
+
"""
|
| 22 |
+
Evaluates the recommender using a Leave-One-Out strategy.
|
| 23 |
+
metric: Hit Rate @ k
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
# 1. Load Resources
|
| 27 |
+
logger.info("Loading Catalog and Embeddings...")
|
| 28 |
+
if not CATALOG_PATH.exists() or not EMBEDDINGS_PATH.exists():
|
| 29 |
+
logger.error("Missing Data! Run download scripts first.")
|
| 30 |
+
return
|
| 31 |
+
|
| 32 |
+
# Load Titles for mapping
|
| 33 |
+
df_catalog = pd.read_csv(CATALOG_PATH)
|
| 34 |
+
titles = df_catalog['title'].tolist()
|
| 35 |
+
# Create Title -> Index map (normalized)
|
| 36 |
+
title_to_idx = {t.lower().strip(): i for i, t in enumerate(titles)}
|
| 37 |
+
|
| 38 |
+
# Load Embeddings
|
| 39 |
+
embeddings = np.load(EMBEDDINGS_PATH)
|
| 40 |
+
|
| 41 |
+
# Load Index
|
| 42 |
+
logger.info("Loading FAISS Index...")
|
| 43 |
+
if INDEX_PATH.exists():
|
| 44 |
+
index = faiss.read_index(str(INDEX_PATH))
|
| 45 |
+
index.nprobe = 10
|
| 46 |
+
else:
|
| 47 |
+
logger.info("Optimized index not found, building flat index on the fly...")
|
| 48 |
+
d = embeddings.shape[1]
|
| 49 |
+
index = faiss.IndexFlatIP(d)
|
| 50 |
+
faiss.normalize_L2(embeddings)
|
| 51 |
+
index.add(embeddings)
|
| 52 |
+
|
| 53 |
+
# 2. Load Synthetic Users
|
| 54 |
+
logger.info(f"Loading Synthetic Data from {SYNTHETIC_DATA_PATH}...")
|
| 55 |
+
df_users = pd.read_parquet(SYNTHETIC_DATA_PATH)
|
| 56 |
+
|
| 57 |
+
# Sample users if dataset is too large
|
| 58 |
+
if len(df_users) > sample_size:
|
| 59 |
+
df_users = df_users.sample(sample_size, random_state=42)
|
| 60 |
+
|
| 61 |
+
logger.info(f"Evaluating on {len(df_users)} users...")
|
| 62 |
+
|
| 63 |
+
hits = 0
|
| 64 |
+
processed_users = 0
|
| 65 |
+
|
| 66 |
+
for _, row in tqdm(df_users.iterrows(), total=len(df_users)):
|
| 67 |
+
history = row['book_sequence']
|
| 68 |
+
|
| 69 |
+
# Need at least 2 books (1 for history, 1 for test)
|
| 70 |
+
if len(history) < 2:
|
| 71 |
+
continue
|
| 72 |
+
|
| 73 |
+
# Leave-One-Out Split
|
| 74 |
+
target_book = history[-1]
|
| 75 |
+
context_books = history[:-1]
|
| 76 |
+
|
| 77 |
+
# 3. Convert Context to Vector
|
| 78 |
+
valid_indices = []
|
| 79 |
+
for book in context_books:
|
| 80 |
+
norm_title = book.lower().strip()
|
| 81 |
+
if norm_title in title_to_idx:
|
| 82 |
+
valid_indices.append(title_to_idx[norm_title])
|
| 83 |
+
|
| 84 |
+
if not valid_indices:
|
| 85 |
+
continue
|
| 86 |
+
|
| 87 |
+
# Get vectors and average (Time Decay Simulation)
|
| 88 |
+
context_vectors = embeddings[valid_indices]
|
| 89 |
+
|
| 90 |
+
# Simple Time Decay
|
| 91 |
+
n = len(valid_indices)
|
| 92 |
+
decay_factor = 0.9
|
| 93 |
+
weights = np.array([decay_factor ** (n - 1 - i) for i in range(n)])
|
| 94 |
+
weights = weights / weights.sum()
|
| 95 |
+
|
| 96 |
+
user_vector = np.average(context_vectors, axis=0, weights=weights).reshape(1, -1).astype(np.float32)
|
| 97 |
+
faiss.normalize_L2(user_vector)
|
| 98 |
+
|
| 99 |
+
# 4. Search
|
| 100 |
+
# We search for top_k + len(context) because the model might return books the user already read
|
| 101 |
+
search_k = top_k + len(valid_indices) + 5
|
| 102 |
+
scores, indices = index.search(user_vector, search_k)
|
| 103 |
+
|
| 104 |
+
# Filter results
|
| 105 |
+
recommended_titles = []
|
| 106 |
+
seen_indices = set(valid_indices) # Don't recommend what they just read
|
| 107 |
+
|
| 108 |
+
for idx in indices[0]:
|
| 109 |
+
if idx in seen_indices:
|
| 110 |
+
continue
|
| 111 |
+
|
| 112 |
+
rec_title = titles[idx]
|
| 113 |
+
recommended_titles.append(rec_title)
|
| 114 |
+
|
| 115 |
+
if len(recommended_titles) >= top_k:
|
| 116 |
+
break
|
| 117 |
+
|
| 118 |
+
# 5. Check Hit
|
| 119 |
+
# We check if the TARGET book title is in the recommended list
|
| 120 |
+
# Using loose matching (substring or exact) can be generous, but strict is better for ML metrics
|
| 121 |
+
# We'll stick to exact string match (normalized)
|
| 122 |
+
|
| 123 |
+
target_norm = target_book.lower().strip()
|
| 124 |
+
rec_norm = [t.lower().strip() for t in recommended_titles]
|
| 125 |
+
|
| 126 |
+
if target_norm in rec_norm:
|
| 127 |
+
hits += 1
|
| 128 |
+
|
| 129 |
+
processed_users += 1
|
| 130 |
+
|
| 131 |
+
# 6. Report
|
| 132 |
+
if processed_users == 0:
|
| 133 |
+
print("No valid users found for evaluation.")
|
| 134 |
+
return
|
| 135 |
+
|
| 136 |
+
hit_rate = hits / processed_users
|
| 137 |
+
print("\n" + "="*40)
|
| 138 |
+
print(f"EVALUATION REPORT (Sample: {processed_users} users)")
|
| 139 |
+
print("="*40)
|
| 140 |
+
print(f"Metric: Hit Rate @ {top_k}")
|
| 141 |
+
print(f"Score: {hit_rate:.4f} ({hit_rate*100:.2f}%)")
|
| 142 |
+
print("-" * 40)
|
| 143 |
+
print("Interpretation:")
|
| 144 |
+
print(f"In {hit_rate*100:.1f}% of cases, the model successfully predicted")
|
| 145 |
+
print("the exact next book the user would read.")
|
| 146 |
+
print("="*40)
|
| 147 |
+
|
| 148 |
+
if __name__ == "__main__":
|
| 149 |
+
evaluate_hit_rate()
|
scripts/inspect_data.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
# Config
|
| 5 |
+
DATA_PATH = Path("data/synthetic/user_sequences.parquet")
|
| 6 |
+
|
| 7 |
+
def inspect():
|
| 8 |
+
if not DATA_PATH.exists():
|
| 9 |
+
print(f"Error: File not found at {DATA_PATH}")
|
| 10 |
+
return
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
print(f"Reading {DATA_PATH}...")
|
| 14 |
+
df = pd.read_parquet(DATA_PATH)
|
| 15 |
+
|
| 16 |
+
print("\n--- Schema ---")
|
| 17 |
+
print(df.info())
|
| 18 |
+
|
| 19 |
+
print("\n--- First 5 Rows ---")
|
| 20 |
+
print(df.head().to_string())
|
| 21 |
+
|
| 22 |
+
print("\n--- Sample User History ---")
|
| 23 |
+
# Show the full history of the first user
|
| 24 |
+
first_user = df.iloc[0]
|
| 25 |
+
print(f"User ID: {first_user.get('user_id', 'N/A')}")
|
| 26 |
+
print(f"History: {first_user.get('book_history', 'N/A')}")
|
| 27 |
+
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Failed to read parquet: {e}")
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
inspect()
|
| 33 |
+
|
scripts/optimize_index.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import faiss
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import time
|
| 5 |
+
import sys
|
| 6 |
+
|
| 7 |
+
# Config
|
| 8 |
+
DATA_DIR = Path("data")
|
| 9 |
+
EMBEDDINGS_PATH = DATA_DIR / "embeddings_cache.npy"
|
| 10 |
+
OUTPUT_PATH = DATA_DIR / "index" / "optimized.index"
|
| 11 |
+
|
| 12 |
+
def main():
|
| 13 |
+
if not EMBEDDINGS_PATH.exists():
|
| 14 |
+
print("No embeddings found. Run scripts/1b... first.")
|
| 15 |
+
sys.exit(1)
|
| 16 |
+
|
| 17 |
+
print(f"Loading embeddings from {EMBEDDINGS_PATH}...")
|
| 18 |
+
embeddings = np.load(EMBEDDINGS_PATH).astype(np.float32)
|
| 19 |
+
d = embeddings.shape[1]
|
| 20 |
+
nb = embeddings.shape[0]
|
| 21 |
+
|
| 22 |
+
print(f"Dataset: {nb} items, {d} dimensions.")
|
| 23 |
+
|
| 24 |
+
nlist = 100
|
| 25 |
+
m = 32
|
| 26 |
+
nbits = 8
|
| 27 |
+
|
| 28 |
+
print(f"Training IVF{nlist}, PQ{m} index...")
|
| 29 |
+
quantizer = faiss.IndexFlatL2(d)
|
| 30 |
+
index = faiss.IndexIVFPQ(quantizer, d, nlist, m, nbits)
|
| 31 |
+
|
| 32 |
+
start_t = time.time()
|
| 33 |
+
index.train(embeddings)
|
| 34 |
+
print(f"Training time: {time.time() - start_t:.2f}s")
|
| 35 |
+
|
| 36 |
+
print("Adding vectors to index...")
|
| 37 |
+
index.add(embeddings)
|
| 38 |
+
|
| 39 |
+
OUTPUT_PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 40 |
+
faiss.write_index(index, str(OUTPUT_PATH))
|
| 41 |
+
|
| 42 |
+
print(f"Optimized index saved to {OUTPUT_PATH}")
|
| 43 |
+
print(f"Original Size: {nb * d * 4 / 1024 / 1024:.2f} MB")
|
| 44 |
+
print(f"Optimized Size: {nb * m / 1024 / 1024:.2f} MB (Approx)")
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
main()
|
scripts/visualize_users.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import logging
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from tqdm import tqdm
|
| 6 |
+
|
| 7 |
+
# Setup Logging
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
+
logger = logging.getLogger("Visualizer")
|
| 10 |
+
|
| 11 |
+
# Paths
|
| 12 |
+
DATA_DIR = Path("data")
|
| 13 |
+
SYNTHETIC_DATA_PATH = DATA_DIR / "synthetic" / "user_sequences.parquet"
|
| 14 |
+
CATALOG_PATH = DATA_DIR / "catalog" / "books_catalog.csv"
|
| 15 |
+
EMBEDDINGS_PATH = DATA_DIR / "embeddings_cache.npy"
|
| 16 |
+
OUTPUT_DIR = Path("docs")
|
| 17 |
+
OUTPUT_IMAGE = OUTPUT_DIR / "user_clusters_tsne.png"
|
| 18 |
+
|
| 19 |
+
def visualize_clusters(sample_size=2000):
|
| 20 |
+
"""
|
| 21 |
+
Generates a 2D t-SNE projection of user vectors, colored by Persona.
|
| 22 |
+
"""
|
| 23 |
+
try:
|
| 24 |
+
import matplotlib.pyplot as plt
|
| 25 |
+
import seaborn as sns
|
| 26 |
+
from sklearn.manifold import TSNE
|
| 27 |
+
except ImportError as e:
|
| 28 |
+
logger.error("Missing visualization libraries!")
|
| 29 |
+
logger.error("Please run: uv pip install matplotlib seaborn")
|
| 30 |
+
return
|
| 31 |
+
|
| 32 |
+
# 1. Load Resources
|
| 33 |
+
logger.info("Loading Data...")
|
| 34 |
+
if not CATALOG_PATH.exists() or not EMBEDDINGS_PATH.exists():
|
| 35 |
+
logger.error("Missing Data! Run download scripts first.")
|
| 36 |
+
return
|
| 37 |
+
|
| 38 |
+
# Load Titles for mapping
|
| 39 |
+
df_catalog = pd.read_csv(CATALOG_PATH)
|
| 40 |
+
titles = df_catalog['title'].tolist()
|
| 41 |
+
title_to_idx = {t.lower().strip(): i for i, t in enumerate(titles)}
|
| 42 |
+
|
| 43 |
+
# Load Embeddings
|
| 44 |
+
embeddings = np.load(EMBEDDINGS_PATH)
|
| 45 |
+
|
| 46 |
+
# Load Users
|
| 47 |
+
df_users = pd.read_parquet(SYNTHETIC_DATA_PATH)
|
| 48 |
+
|
| 49 |
+
# Sample
|
| 50 |
+
if len(df_users) > sample_size:
|
| 51 |
+
df_users = df_users.sample(sample_size, random_state=42)
|
| 52 |
+
|
| 53 |
+
logger.info(f"Processing {len(df_users)} users...")
|
| 54 |
+
|
| 55 |
+
user_vectors = []
|
| 56 |
+
user_personas = []
|
| 57 |
+
|
| 58 |
+
# 2. Calculate User Vectors
|
| 59 |
+
valid_users = 0
|
| 60 |
+
for _, row in tqdm(df_users.iterrows(), total=len(df_users)):
|
| 61 |
+
history = row['book_sequence']
|
| 62 |
+
persona = row['persona']
|
| 63 |
+
|
| 64 |
+
valid_indices = []
|
| 65 |
+
for book in history:
|
| 66 |
+
norm_title = book.lower().strip()
|
| 67 |
+
if norm_title in title_to_idx:
|
| 68 |
+
valid_indices.append(title_to_idx[norm_title])
|
| 69 |
+
|
| 70 |
+
if not valid_indices:
|
| 71 |
+
continue
|
| 72 |
+
|
| 73 |
+
# Average Embeddings
|
| 74 |
+
vectors = embeddings[valid_indices]
|
| 75 |
+
user_vec = np.mean(vectors, axis=0)
|
| 76 |
+
|
| 77 |
+
user_vectors.append(user_vec)
|
| 78 |
+
user_personas.append(persona)
|
| 79 |
+
valid_users += 1
|
| 80 |
+
|
| 81 |
+
X = np.array(user_vectors)
|
| 82 |
+
|
| 83 |
+
# 3. t-SNE Reduction
|
| 84 |
+
logger.info("Running t-SNE (this might take a moment)...")
|
| 85 |
+
tsne = TSNE(n_components=2, random_state=42, perplexity=30)
|
| 86 |
+
X_embedded = tsne.fit_transform(X)
|
| 87 |
+
|
| 88 |
+
# 4. Plotting
|
| 89 |
+
logger.info("Generating Plot...")
|
| 90 |
+
OUTPUT_DIR.mkdir(exist_ok=True)
|
| 91 |
+
|
| 92 |
+
plt.figure(figsize=(12, 8))
|
| 93 |
+
sns.scatterplot(
|
| 94 |
+
x=X_embedded[:, 0],
|
| 95 |
+
y=X_embedded[:, 1],
|
| 96 |
+
hue=user_personas,
|
| 97 |
+
palette="viridis",
|
| 98 |
+
alpha=0.7,
|
| 99 |
+
s=60
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
plt.title(f"Semantic User Clusters (t-SNE Projection of {valid_users} Users)", fontsize=16)
|
| 103 |
+
plt.xlabel("Dimension 1")
|
| 104 |
+
plt.ylabel("Dimension 2")
|
| 105 |
+
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', title="Persona")
|
| 106 |
+
plt.tight_layout()
|
| 107 |
+
|
| 108 |
+
plt.savefig(OUTPUT_IMAGE, dpi=300)
|
| 109 |
+
logger.info(f"✅ Visualization saved to {OUTPUT_IMAGE}")
|
| 110 |
+
print(f"Success! Check {OUTPUT_IMAGE} to see your user clusters.")
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
visualize_clusters()
|
src/personalization/__init__.py
ADDED
|
File without changes
|
src/personalization/api/__init__.py
ADDED
|
File without changes
|
src/personalization/api/main.py
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from contextlib import asynccontextmanager
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import logging
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
from prometheus_fastapi_instrumentator import Instrumentator
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
from typing import List
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import faiss
|
| 11 |
+
import numpy as np
|
| 12 |
+
import time
|
| 13 |
+
|
| 14 |
+
# Setup Logging
|
| 15 |
+
logging.basicConfig(level=logging.INFO)
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
+
|
| 18 |
+
# Config
|
| 19 |
+
DATA_DIR = Path("data")
|
| 20 |
+
CATALOG_PATH = DATA_DIR / "catalog" / "books_catalog.csv"
|
| 21 |
+
EMBEDDINGS_PATH = DATA_DIR / "embeddings_cache.npy"
|
| 22 |
+
MODEL_NAME = "all-MiniLM-L6-v2"
|
| 23 |
+
|
| 24 |
+
# Global State
|
| 25 |
+
state = {
|
| 26 |
+
"titles": [],
|
| 27 |
+
"title_to_idx": {},
|
| 28 |
+
"index": None,
|
| 29 |
+
"embeddings": None,
|
| 30 |
+
"ratings": [],
|
| 31 |
+
"genres": [],
|
| 32 |
+
"model": None,
|
| 33 |
+
"popular_indices": []
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
@asynccontextmanager
|
| 37 |
+
async def lifespan(app: FastAPI):
|
| 38 |
+
logger.info("Loading resources...")
|
| 39 |
+
start_time = time.time()
|
| 40 |
+
|
| 41 |
+
if not CATALOG_PATH.exists() or not EMBEDDINGS_PATH.exists():
|
| 42 |
+
logger.error("Missing catalog or embeddings! Run scripts/1b... first.")
|
| 43 |
+
# We continue but service might be degraded
|
| 44 |
+
else:
|
| 45 |
+
try:
|
| 46 |
+
df = pd.read_csv(CATALOG_PATH)
|
| 47 |
+
state["titles"] = df['title'].tolist()
|
| 48 |
+
state["genres"] = df['genres'].fillna("").tolist()
|
| 49 |
+
|
| 50 |
+
raw_ratings = pd.to_numeric(df['rating'], errors='coerce').fillna(3.0)
|
| 51 |
+
max_rating = raw_ratings.max()
|
| 52 |
+
state["ratings"] = (raw_ratings / max_rating).tolist() if max_rating > 0 else [0.5] * len(df)
|
| 53 |
+
|
| 54 |
+
# Use normalized keys for robust lookup
|
| 55 |
+
state["title_to_idx"] = {t.lower().strip(): i for i, t in enumerate(state["titles"])}
|
| 56 |
+
|
| 57 |
+
state["popular_indices"] = np.argsort(raw_ratings)[::-1][:50].tolist()
|
| 58 |
+
|
| 59 |
+
logger.info("Loading embeddings...")
|
| 60 |
+
embeddings = np.load(EMBEDDINGS_PATH)
|
| 61 |
+
state["embeddings"] = embeddings
|
| 62 |
+
|
| 63 |
+
OPTIMIZED_INDEX_PATH = DATA_DIR / "index" / "optimized.index"
|
| 64 |
+
|
| 65 |
+
if OPTIMIZED_INDEX_PATH.exists():
|
| 66 |
+
logger.info("Loading OPTIMIZED FAISS index (IVF-PQ)...")
|
| 67 |
+
state["index"] = faiss.read_index(str(OPTIMIZED_INDEX_PATH))
|
| 68 |
+
state["index"].nprobe = 10
|
| 69 |
+
else:
|
| 70 |
+
logger.info("Building Standard FAISS index (Flat)...")
|
| 71 |
+
d = embeddings.shape[1]
|
| 72 |
+
index = faiss.IndexFlatIP(d)
|
| 73 |
+
faiss.normalize_L2(embeddings)
|
| 74 |
+
index.add(embeddings)
|
| 75 |
+
state["index"] = index
|
| 76 |
+
|
| 77 |
+
logger.info(f"Loading Semantic Model ({MODEL_NAME})...")
|
| 78 |
+
state["model"] = SentenceTransformer(MODEL_NAME)
|
| 79 |
+
|
| 80 |
+
logger.info(f"Ready! Loaded {len(state['titles'])} books in {time.time() - start_time:.2f}s")
|
| 81 |
+
except Exception as e:
|
| 82 |
+
logger.error(f"Failed to load resources: {e}")
|
| 83 |
+
# Consider raising if critical
|
| 84 |
+
|
| 85 |
+
yield
|
| 86 |
+
|
| 87 |
+
logger.info("Shutting down...")
|
| 88 |
+
# Clean up resources if needed
|
| 89 |
+
|
| 90 |
+
app = FastAPI(title="Semantic Book Discovery Engine", lifespan=lifespan)
|
| 91 |
+
|
| 92 |
+
# Add Prometheus Instrumentation
|
| 93 |
+
Instrumentator().instrument(app).expose(app)
|
| 94 |
+
|
| 95 |
+
class RecommendationRequest(BaseModel):
|
| 96 |
+
user_history: List[str]
|
| 97 |
+
top_k: int = 10
|
| 98 |
+
|
| 99 |
+
class SearchRequest(BaseModel):
|
| 100 |
+
query: str
|
| 101 |
+
top_k: int = 10
|
| 102 |
+
|
| 103 |
+
class BookResponse(BaseModel):
|
| 104 |
+
title: str
|
| 105 |
+
score: float
|
| 106 |
+
genres: str
|
| 107 |
+
|
| 108 |
+
@app.post("/search", response_model=List[BookResponse])
|
| 109 |
+
async def search(request: SearchRequest):
|
| 110 |
+
if state["model"] is None or state["index"] is None:
|
| 111 |
+
raise HTTPException(status_code=503, detail="Service loading...")
|
| 112 |
+
|
| 113 |
+
query_vector = state["model"].encode([request.query], convert_to_numpy=True)
|
| 114 |
+
faiss.normalize_L2(query_vector)
|
| 115 |
+
|
| 116 |
+
scores, indices = state["index"].search(query_vector, request.top_k)
|
| 117 |
+
|
| 118 |
+
results = []
|
| 119 |
+
for score, idx in zip(scores[0], indices[0]):
|
| 120 |
+
results.append(BookResponse(
|
| 121 |
+
title=state["titles"][idx],
|
| 122 |
+
score=float(score),
|
| 123 |
+
genres=str(state["genres"][idx])
|
| 124 |
+
))
|
| 125 |
+
|
| 126 |
+
return results
|
| 127 |
+
|
| 128 |
+
@app.post("/personalize/recommend", response_model=List[BookResponse])
|
| 129 |
+
async def recommend(request: RecommendationRequest):
|
| 130 |
+
if state["index"] is None:
|
| 131 |
+
raise HTTPException(status_code=503, detail="Service not ready")
|
| 132 |
+
|
| 133 |
+
valid_indices = []
|
| 134 |
+
for title in request.user_history:
|
| 135 |
+
normalized_title = title.lower().strip()
|
| 136 |
+
if normalized_title in state["title_to_idx"]:
|
| 137 |
+
valid_indices.append(state["title_to_idx"][normalized_title])
|
| 138 |
+
|
| 139 |
+
if not valid_indices:
|
| 140 |
+
logger.info("Cold start user: returning popular books")
|
| 141 |
+
results = []
|
| 142 |
+
for idx in state["popular_indices"][:request.top_k]:
|
| 143 |
+
results.append(BookResponse(
|
| 144 |
+
title=state["titles"][idx],
|
| 145 |
+
score=state["ratings"][idx],
|
| 146 |
+
genres=str(state["genres"][idx])
|
| 147 |
+
))
|
| 148 |
+
return results
|
| 149 |
+
|
| 150 |
+
history_vectors = state["embeddings"][valid_indices]
|
| 151 |
+
|
| 152 |
+
n = len(valid_indices)
|
| 153 |
+
decay_factor = 0.9
|
| 154 |
+
weights = np.array([decay_factor ** (n - 1 - i) for i in range(n)])
|
| 155 |
+
weights = weights / weights.sum()
|
| 156 |
+
|
| 157 |
+
user_vector = np.average(history_vectors, axis=0, weights=weights).reshape(1, -1).astype(np.float32)
|
| 158 |
+
faiss.normalize_L2(user_vector)
|
| 159 |
+
|
| 160 |
+
search_k = (request.top_k * 3) + len(valid_indices)
|
| 161 |
+
scores, indices = state["index"].search(user_vector, search_k)
|
| 162 |
+
|
| 163 |
+
results = []
|
| 164 |
+
seen_indices = set(valid_indices)
|
| 165 |
+
seen_titles = set()
|
| 166 |
+
|
| 167 |
+
for score, idx in zip(scores[0], indices[0]):
|
| 168 |
+
if idx in seen_indices: continue
|
| 169 |
+
title = state["titles"][idx]
|
| 170 |
+
if title in seen_titles: continue
|
| 171 |
+
seen_titles.add(title)
|
| 172 |
+
|
| 173 |
+
final_score = float(score) + (state["ratings"][idx] * 0.1)
|
| 174 |
+
|
| 175 |
+
results.append(BookResponse(
|
| 176 |
+
title=title,
|
| 177 |
+
score=final_score,
|
| 178 |
+
genres=str(state["genres"][idx])
|
| 179 |
+
))
|
| 180 |
+
|
| 181 |
+
if len(results) >= request.top_k:
|
| 182 |
+
break
|
| 183 |
+
|
| 184 |
+
results.sort(key=lambda x: x.score, reverse=True)
|
| 185 |
+
|
| 186 |
+
return results
|
src/personalization/config.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
class Settings:
|
| 4 |
+
# Default to 8001, but allow Env Override
|
| 5 |
+
HOST = os.getenv("API_HOST", "localhost")
|
| 6 |
+
PORT = int(os.getenv("API_PORT", 8001))
|
| 7 |
+
|
| 8 |
+
@property
|
| 9 |
+
def BASE_URL(self):
|
| 10 |
+
return f"http://{self.HOST}:{self.PORT}"
|
| 11 |
+
|
| 12 |
+
settings = Settings()
|
test_api.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import argparse
|
| 3 |
+
import sys
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
# Add src to path to import config
|
| 7 |
+
sys.path.append(str(Path(__file__).parent))
|
| 8 |
+
from src.personalization.config import settings
|
| 9 |
+
|
| 10 |
+
def main():
|
| 11 |
+
parser = argparse.ArgumentParser(description="Test the Recommendation API")
|
| 12 |
+
parser.add_argument("--host", type=str, default=settings.HOST, help="API Host")
|
| 13 |
+
parser.add_argument("--port", type=int, default=settings.PORT, help="API Port")
|
| 14 |
+
args = parser.parse_args()
|
| 15 |
+
|
| 16 |
+
base_url = f"http://{args.host}:{args.port}"
|
| 17 |
+
url = f"{base_url}/personalize/recommend"
|
| 18 |
+
|
| 19 |
+
payload = {
|
| 20 |
+
"user_history": [
|
| 21 |
+
"The Haunted School",
|
| 22 |
+
"It Came from Beneath the Sink!"
|
| 23 |
+
"Legion"
|
| 24 |
+
],
|
| 25 |
+
"top_k": 5
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
print(f"Sending request to {url}...")
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
response = requests.post(url, json=payload)
|
| 32 |
+
|
| 33 |
+
if response.status_code == 200:
|
| 34 |
+
results = response.json()
|
| 35 |
+
print("\u2714 Recommendations:")
|
| 36 |
+
for i, book in enumerate(results, 1):
|
| 37 |
+
print(f"{i}. {book['title']} (Score: {book['score']:.4f})")
|
| 38 |
+
else:
|
| 39 |
+
print(f"\u2714 Error {response.status_code}: {response.text}")
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"\u2714 Failed to connect: {e}")
|
| 43 |
+
print("Make sure the uvicorn server is running on port 8001!")
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
main()
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|