Spaces:
Sleeping
Sleeping
File size: 5,073 Bytes
4b5c25d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | ---
title: LITVISION Recommendation API
emoji: π
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
license: mit
---
# LITVISION Book Recommendation API
A production-ready FastAPI service for the LITVISION Book Recommendation Feature. This API provides personalized book recommendations using zero-shot genre classification, SentenceTransformer embeddings, FAISS similarity search, and an event-weighted ranking pipeline.
Fully configured for deployment on Hugging Face Spaces with Docker SDK.
## Features
- **Zero-Shot Genre Classification** using `joeddav/xlm-roberta-large-xnli`
- **SentenceTransformer Embeddings** using `paraphrase-multilingual-MiniLM-L12-v2`
- **FAISS Similarity Search** with `IndexFlatIP` for cosine similarity on normalized vectors
- **Event-Weighted User Profiling** with configurable view/like weights
- **Genre-Balanced Feed Allocation** for diverse recommendations
- **Cosine-Similarity Ranking Pipeline** for personalized ordering
- **GPU/CPU Fallback** with FP16 optimization on CUDA
- **Async Processing** via `asyncio.to_thread` for non-blocking inference
- **Production Error Handling** including CUDA OOM recovery
## API Endpoints
### GET /
Returns basic API information.
```json
{
"api": "LITVISION Book Recommendation API",
"status": "online",
"version": "1.0.0",
"endpoints": ["/health", "/recommend"]
}
```
### GET /health
Returns health status and model readiness.
```json
{
"status": "healthy",
"models_loaded": true,
"device": "cuda",
"total_books": 200,
"faiss_index_size": 200
}
```
### POST /recommend
Generates personalized book recommendations for a user.
**Request Body:**
```json
{
"user_id": 1,
"interactions": [
{
"book_id": 5,
"event_type": "like",
"timestamp": "2025-01-01T00:00:00"
},
{
"book_id": 12,
"event_type": "view",
"timestamp": "2025-01-02T00:00:00"
}
],
"favorite_genres": ["Fantasy", "Science Fiction"],
"viewed_books": [1, 2, 3],
"feed_size": 20
}
```
**Parameters:**
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| user_id | int | Yes | β | Unique user identifier (> 0) |
| interactions | list | No | null | Explicit user-book interaction events |
| favorite_genres | list | No | null | Preferred genres for boosting |
| viewed_books | list | No | null | Book IDs already viewed by the user |
| feed_size | int | No | 20 | Number of recommendations (1-100) |
**Valid Genres:**
Fantasy, Romance, Mystery, Science Fiction, Self-Help, History, Business, Children, Horror, Poetry
**Response:**
```json
{
"success": true,
"user_id": 1,
"recommendations": [
{
"book_id": 42,
"title": "Fantasy Book 42",
"author": "Author 7",
"genre": "Fantasy",
"score": 0.9234
}
],
"genre_distribution": {
"Fantasy": 8,
"Romance": 4,
"Mystery": 3,
"Science Fiction": 2,
"Self-Help": 1,
"History": 1,
"Children": 1
},
"total_recommendations": 20,
"processing_time_seconds": 1.234
}
```
## Folder Structure
```text
.
βββ app.py # FastAPI endpoints and lifespan events
βββ recommender.py # Full recommendation pipeline engine
βββ utils.py # Logging, device helpers, and cleanup
βββ requirements.txt # Python dependencies
βββ Dockerfile # Container configuration for HF Spaces
βββ .dockerignore # Docker build exclusions
βββ .gitignore # Git exclusions
βββ .gitattributes # Line ending configuration
βββ README.md # This file
βββ sample_data/
βββ books.csv # Sample book dataset (200 books)
```
## Local Development
### 1. Install Python Dependencies
```bash
pip install -r requirements.txt
```
### 2. Run the Server
```bash
uvicorn app:app --host 0.0.0.0 --port 7860 --reload
```
### 3. Test with cURL
```bash
curl -X POST http://localhost:7860/recommend \
-H "Content-Type: application/json" \
-d '{"user_id": 1, "feed_size": 10}'
```
## Docker Build and Run
### Build the Image
```bash
docker build -t litvision-recommender .
```
### Run the Container
```bash
docker run -p 7860:7860 litvision-recommender
```
With GPU support:
```bash
docker run -p 7860:7860 --gpus all litvision-recommender
```
## Deployment to Hugging Face Spaces
1. Go to [Hugging Face](https://huggingface.co) and create a new Space.
2. Select **Docker** as the Space SDK.
3. Upload all the files in this directory to the repository.
4. The Space will automatically build the container and start the Uvicorn server on port 7860.
## Troubleshooting
- **Models loading slowly:** The first startup downloads `xlm-roberta-large-xnli` (~2.2 GB) and `paraphrase-multilingual-MiniLM-L12-v2`. Subsequent starts use the cached models.
- **CUDA OOM:** The API automatically clears CUDA cache and returns HTTP 503. Retry the request or reduce `feed_size`.
- **503 on first request:** Models may still be loading. Check `/health` endpoint for status.
|