File size: 3,709 Bytes
435e28e
 
 
 
 
 
 
 
 
 
b5739f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Movie Recommender
emoji: 🤖
colorFrom: blue
colorTo: green
sdk: docker
sdk_version: "1.0"
app_file: app.main
pinned: false
---
# 🎬 Movie Recommender System (FastAPI + LangGraph + FAISS)

This project is an AI-powered **movie recommender system**.  
It uses **FAISS vector search**, **local embeddings**, and **LLMs (via OpenRouter)** to recommend movies in **any language**.  

The pipeline:
1. Detects the language of the user query.
2. Translates the query into English.
3. Retrieves similar movies using embeddings + FAISS.
4. Generates natural language explanations with an LLM.
5. Translates the explanations back into the user’s language.

---

## ✨ Features
- Multilingual support (query in any language 🌍).
- Fast similarity search with **FAISS**.
- Local embeddings with [MiniLM](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2).
- Explanations powered by **OpenRouter LLMs**.
- Modular pipeline built with **LangGraph**.

---

## 🛠️ Tech Stack
- **Backend**: FastAPI
- **Vector DB**: FAISS
- **Embeddings**: `sentence-transformers/all-MiniLM-L6-v2` (local)
- **Orchestration**: LangChain + LangGraph
- **LLM**: OpenRouter (Meta LLaMA Scout free by default)
- **Deployment**: Docker / Hugging Face Spaces

---

## 📂 Project Structure
```
.
├── app/
│   ├── main.py           # FastAPI entry point
│   ├── recommender.py    # Core recommender logic
│   ├── graph.py          # LangGraph workflow
│   └── utils.py          # Helper functions
├── data/                 # Movies dataset
├── faiss_index/          # Prebuilt FAISS index + metadata
├── prepare_data.py       # Script to build FAISS index
├── requirements.txt
├── .env                  # API keys (not committed)
├── .gitignore
└── README.md
```

---

## 🚀 Getting Started

### 1. Clone & Setup
```bash
git clone https://github.com/your-username/movie-recommender.git
cd movie-recommender

# Create virtual environment
python -m venv .venv
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt
```

### 2. Environment Variables
Create a `.env` file in the project root:
```ini
OPENROUTER=your_openrouter_api_key
```

### 3. Prepare FAISS Index
If not already included:
```bash
python prepare_data.py
```

This builds:
- `faiss_index/movies_index.faiss`
- `faiss_index/movies.pkl`

### 4. Run FastAPI App
```bash
uvicorn app.main:app --reload
```

Backend will start at:  
👉 http://127.0.0.1:8000

Interactive API docs at:  
👉 http://127.0.0.1:8000/docs

---

## 📌 Example Usage

### Request
```bash
curl -X POST http://127.0.0.1:8000/recommend -H "Content-Type: application/json" -d '{"query": "لطفا یک فیلم فانتزی هیجان انگیز شاد بهم معرفی کن", "k": 5}'
```

### Response
```json
[
  {
    "title": "The Incredibles",
    "genres": "Action|Animation|Adventure",
    "overview": "A family of superheroes...",
    "explanation": "این فیلم یک ماجراجویی شاد و هیجان‌انگیز است که با درخواست شما مطابقت دارد."
  },
  ...
]
```

---

## 🐳 Deployment with Docker
Build and run locally:
```bash
docker build -t movie-recommender .
docker run -p 8000:8000 movie-recommender
```

For Hugging Face Spaces:  
- Only `/tmp` is writable at runtime.  
- Pre-download embeddings + FAISS index during build.  

---

## 🧩 Next Steps
- Add **user profiles** for personalized recommendations.
- Support **hybrid search** (metadata + embeddings).
- Add **Next.js frontend** for a full-stack app.
- Deploy to **Hugging Face Spaces**.

---

## 📜 License
MIT License. Free to use & modify.