A sophisticated, full-stack movie recommendation application built with FastAPI and Streamlit that leverages collaborative filtering and machine learning to provide personalized movie recommendations. The system features automatic model retraining, real-time recommendations, and an intuitive web interface.
- Intelligent Recommendations: Advanced collaborative filtering using cosine similarity
- Real-time Rating System: Users can rate movies to improve recommendation accuracy
- Auto-Retraining: ML model automatically retrains when new data is added
- Movie Management: Complete CRUD operations for movies
- Interactive Web UI: Beautiful Streamlit frontend for easy interaction
- Async FastAPI Backend: High-performance REST API with automatic documentation
- SQLAlchemy ORM: Robust database management with async support
- Scikit-learn ML: Professional-grade machine learning algorithms
- Model Persistence: Efficient model caching with joblib
- Database Flexibility: Supports both PostgreSQL and SQLite
Movie-Recommendation-Application/
โโโ app/ # FastAPI backend
โ โโโ main.py # API endpoints and business logic
โ โโโ models.py # Database models (Movie, Rating)
โ โโโ schemas.py # Pydantic schemas for validation
โ โโโ db.py # Database configuration
โ โโโ create_tables.py # Database initialization
โโโ frontend/ # Streamlit web interface
โ โโโ app.py # Interactive UI for recommendations and ratings
โโโ recommender/ # Machine learning module
โ โโโ train_model.py # Collaborative filtering implementation
โโโ images/ # Application screenshots
โโโ requirements.txt # Python dependencies
The recommendation interface allows users to select a movie and get personalized suggestions based on collaborative filtering
Users can rate movies on a scale of 0-5 stars, which automatically retrains the ML model for better recommendations
| Component | Technology | Purpose |
|---|---|---|
| Backend API | FastAPI | High-performance async REST API |
| Frontend | Streamlit | Interactive web interface |
| Database | SQLAlchemy + PostgreSQL/SQLite | Data persistence and ORM |
| Machine Learning | Scikit-learn | Collaborative filtering algorithms |
| Model Storage | Joblib | Efficient model serialization |
| Validation | Pydantic | Request/response validation |
- Python 3.8 or higher
- pip package manager
- PostgreSQL (optional - SQLite works too)
# Clone the repository
git clone <repository-url>
cd Movie-Recommendation-Application
# Create virtual environment (recommended)
python -m venv movie_rec_env
source movie_rec_env/bin/activate # On Windows: movie_rec_env\Scripts\activate
# Install dependencies
pip install -r requirements.txt# Initialize the database with sample data
python init_db.py# Run the API server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000The API will be available at http://localhost:8000
- Interactive API Docs:
http://localhost:8000/docs(Swagger UI) - Alternative Docs:
http://localhost:8000/redoc
# In a new terminal, start the web interface
streamlit run frontend/app.pyThe web application will open automatically at http://localhost:8501
| Method | Endpoint | Description |
|---|---|---|
GET |
/movies/ |
Retrieve all movies |
GET |
/movies/{movie_id} |
Get specific movie by ID |
POST |
/movies/ |
Add a new movie |
DELETE |
/movies/{movie_id} |
Delete a movie |
| Method | Endpoint | Description |
|---|---|---|
POST |
/rate/ |
Submit a movie rating (triggers model retraining) |
| Method | Endpoint | Description |
|---|---|---|
GET |
/recommend/{movie}?top_n={number} |
Get movie recommendations |
# Get recommendations for "Toy Story"
curl "http://localhost:8000/recommend/Toy%20Story?top_n=5"
# Add a rating
curl -X POST "http://localhost:8000/rate/" \
-H "Content-Type: application/json" \
-d '{"user_id": 1, "movie_id": 1, "rating": 4.5}'- User ratings are stored in a relational database
- Each rating links a user, movie, and score (0-5 stars)
- Creates a user-movie matrix from rating data
- Uses cosine similarity to find movies with similar rating patterns
- Handles sparse data with zero-filling for unrated movies
- Calculates similarity scores between movies
- Returns top-N most similar movies to the input
- Excludes the input movie from recommendations
- Model retrains whenever new movies or ratings are added
- Ensures recommendations stay current with user preferences
- Uses joblib for efficient model persistence
- Start both servers (FastAPI + Streamlit)
- Open
http://localhost:8501in your browser - Get Recommendations: Select a movie and click "Get Recommendations"
- Rate Movies: Choose a movie, set your rating, and submit
- Visit
http://localhost:8000/docsfor interactive API documentation - Test endpoints directly in the Swagger UI
- Integrate with your own applications using the REST API
Edit app/db.py to configure your database:
# For PostgreSQL
DATABASE_URL = "postgresql+asyncpg://user:password@localhost/moviedb"
# For SQLite (default)
DATABASE_URL = "sqlite+aiosqlite:///./movies.db"Adjust recommendation parameters in app/main.py:
# Change default number of recommendations
@app.get("/recommend/{movie}")
def recommend(movie: str, top_n: int = 5): # Default changed to 5"Model not found" error
# Retrain the model manually
python -c "import asyncio; from recommender.train_model import train_model; asyncio.run(train_model())"Database connection issues
# Reinitialize the database
python init_db.pyPort already in use
# Use different ports
uvicorn app.main:app --port 8001
streamlit run frontend/app.py --server.port 8502- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- FastAPI for the excellent async web framework
- Streamlit for making beautiful web apps simple
- Scikit-learn for robust machine learning algorithms
- SQLAlchemy for powerful database ORM capabilities
Built with โค๏ธ for movie enthusiasts and ML practitioners