This FastAPI application recommends movies or books based on genre similarity using semantic text similarity.
-
Install Python 3.12 (if not already installed):
brew install python@3.12
-
Set up a virtual environment:
# Navigate to the project directory cd recommender_api # Create a virtual environment python3.12 -m venv venv # Activate the virtual environment source venv/bin/activate
-
Install the package in development mode:
# While in the recommender_api directory with activated environment pip install -e .
# Install Hatch if you don't have it
brew install hatch
# Navigate to the project directory
cd recommender_api
# Install dependencies with Hatch
hatch env createYou can run the API in one of two ways:
# Navigate to the app directory
cd recommender_api/app
# Run the FastAPI application
uvicorn main:app --reload --port 8081# From the project root
cd recommender_api
python -m uvicorn app.main:app --reload --port 8081To use a different port:
uvicorn app.main:app --reload --port <YOUR_PORT>The project includes comprehensive unit and integration tests for the API. To run the tests:
Install the testing dependencies:
pip install pytest pytest-cov httpxNote: httpx is required for FastAPI's TestClient.
From the project root directory:
# Run all tests
pytest
# Run tests with coverage report
pytest --cov=app tests/
# Run specific test categories
pytest tests/unit/
pytest tests/integration/For convenience, you can use the provided test runner script:
# Make sure the script is executable
chmod +x run_tests.sh
# Run all tests
./run_tests.sh
# Run with coverage
./run_tests.sh --cov
# Generate HTML coverage report
./run_tests.sh --cov --html
# Run only unit tests
./run_tests.sh --unit
# Run only integration tests
./run_tests.sh --integration
# Run a specific test file
./run_tests.sh tests/unit/test_api.pyFor detailed information about the test suite, see tests/README.md.
After starting the server, access the documentation at:
- Interactive API docs: http://127.0.0.1:8081/docs
- ReDoc documentation: http://127.0.0.1:8081/redoc
Request body:
{
"type": "movies", // or "books"
"genres": ["Action", "Adventure"]
}Example response:
{
"name": "The Lord of the Rings",
"description": "Epic fantasy adventure about a quest to destroy a powerful ring",
"genres": ["Fantasy", "Adventure", "Action"],
"similarity_score": 1.85
}