- Backend (FastAPI)
The FastAPI backend service is responsible for handling user requests, running recommendation logic, and returning enriched movie suggestions.
- Access URLs
-
FastAPI: http://localhost:8501
-
Swagger UI: http://localhost:8000/docs
- Features
-
/recommend_movies (POST):
Accepts a movie description and genre filters, and returns personalized movie suggestions. -
/ (root) (GET):
A health check route confirming the API is live.
- Recommendation Flow
- User Input: Accepts a query and genre selection.
- SBERT Encoding: Converts the query to an embedding using a SentenceTransformer.
- Hybrid Search: Combines cosine similarity and BM25 search in Elasticsearch.
- Genre Filtering: Filters results by genre using strict or relaxed mode.
- Metadata Enrichment: Fetches detailed metadata (title, poster, rating, actors) from BigQuery.
- Response: Returns a structured list of top movie matches.
- Docker Service
The backend is deployed as a container and connects with Elasticsearch and BigQuery:
- Dockerfile
FROM python:3.10-slim-bullseye
# Install system dependencies required for Python packages
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential libpq-dev libfreetype6-dev libpng-dev libjpeg-dev \
libblas-dev liblapack-dev gfortran libffi-dev libssl-dev curl \
&& rm -rf /var/lib/apt/lists/*
# Set work directory
WORKDIR /back
# Set env variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Install dependencies
COPY requirements.txt .
RUN pip install --upgrade pip setuptools wheel && pip install --no-cache-dir -r requirements.txt
# Copy project
COPY . .
# Expose the port FastAPI will run on
EXPOSE 8000
# Command to run the FastAPI app
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
get_bigquery_client()
Creates and returns a BigQuery client using service account credentials.
Returns:
| Type | Description |
|---|---|
Client
|
bigquery.Client: Authenticated BigQuery client configured with the service account file. |
Source code in app/back/config.py
47 48 49 50 51 52 53 54 55 56 57 58 | |
get_elasticsearch()
Creates and returns an Elasticsearch client instance.
Returns:
| Name | Type | Description |
|---|---|---|
Elasticsearch |
Elasticsearch
|
Configured Elasticsearch client with the specified URL and timeout. |
Source code in app/back/config.py
29 30 31 32 33 34 35 36 | |
get_sentence_transformer()
Loads and returns the sentence transformer model.
Returns:
| Name | Type | Description |
|---|---|---|
SentenceTransformer |
SentenceTransformer
|
Pre-trained sentence transformer model for generating embeddings. |
Source code in app/back/config.py
38 39 40 41 42 43 44 45 | |
MovieRequest
Bases: BaseModel
Represents the request body for the movie recommendation endpoint.
Attributes:
| Name | Type | Description |
|---|---|---|
query |
str
|
The search query for movie recommendations. |
use_genre_filter |
bool
|
Whether to filter results by genre. |
selected_genres |
Optional[List[str]]
|
List of genres to filter by. |
filtering_mode |
Optional[Literal['strict', 'relaxed']]
|
Mode for genre filtering. |
num_recs |
int
|
Number of recommendations to return. |
Source code in app/back/main.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
chunk_sort_key(chunk_id)
Generates a sorting key for chunk IDs based on their type and numerical order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_id |
str
|
The chunk ID to generate a sort key for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
A tuple containing the type order and numerical components of the chunk ID. |
Source code in app/back/main.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
recommend_movies(request)
Recommends movies based on a query and optional genre filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request |
MovieRequest
|
The request body containing query and filter options. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
A dictionary containing the recommended movies and their metadata. |
Source code in app/back/main.py
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
root()
Root endpoint to check if the API is running.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
A message indicating the API is running. |
Source code in app/back/main.py
39 40 41 42 43 44 45 46 47 | |