π Advanced AI-powered prediction system for LeetCode contest ratings using deep learning
Predict your LeetCode contest rating changes with high accuracy using our sophisticated LSTM neural network model trained on thousands of contest data points.
- π§ Deep Learning Model: LSTM neural network optimized for time-series rating prediction
- π Real-time Data: Automated fetching from LeetCode's GraphQL API
- π Modern Web Interface: React-based frontend with intuitive design
- β‘ Fast API Backend: High-performance FastAPI server with async operations
- π Accurate Predictions: Trained on extensive historical contest data
- π Batch Processing: Predict multiple contests simultaneously
- π± Responsive Design: Works seamlessly on desktop and mobile
Windows:
.\setup.batLinux/Mac:
bash setup.sh-
Clone the repository
git clone https://github.com/Sagargupta16/LeetCode_Rating_Predictor.git cd LeetCode_Rating_Predictor -
Set up Python environment
python -m venv venv # Windows venv\Scripts\activate # Linux/Mac source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
-
Start the server
python main.py
-
Access the application
- API: http://localhost:8000
- Documentation: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β React UI β β FastAPI β β ML Model β
β (Frontend) βββββΊβ (Backend) βββββΊβ (LSTM) β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β
βΌ
βββββββββββββββββββ
β LeetCode β
β GraphQL API β
βββββββββββββββββββ
LeetCode_Rating_Predictor/
βββ π± client/ # React frontend application
β βββ public/ # Static assets
β βββ src/ # React source code
β βββ build/ # Production build
βββ π§ LC_Contest_Rating_Predictor.ipynb # Model training notebook
βββ π main.py # FastAPI backend server
βββ π§ check.py # Utility scripts
βββ π model.keras # Trained LSTM model
βββ βοΈ scaler.save # Data preprocessing scaler
βββ π requirements.txt # Python dependencies
βββ π data.json # Training data
βββ π₯ usernames.json # User data cache
βββ π README.md # Project documentation
Endpoint: POST /api/predict
Request Body:
{
"username": "your_leetcode_username",
"contests": [
{
"name": "weekly-contest-377",
"rank": 1500
},
{
"name": "biweekly-contest-120",
"rank": 2000
}
]
}Response:
[
{
"contest_name": "weekly-contest-377",
"prediction": 25.5,
"rating_before_contest": 1800,
"rank": 1500,
"total_participants": 8000,
"rating_after_contest": 1825.5,
"attended_contests_count": 45
}
]Endpoint: GET /api/contestData
Response:
{
"contests": ["weekly-contest-377", "biweekly-contest-120"]
}- Type: LSTM (Long Short-Term Memory) Neural Network
- Input Features:
- Current rating
- Contest rank
- Total participants
- Rank percentage
- Attended contests count
- Output: Predicted rating change
- Framework: TensorFlow/Keras
- Data Collection: Automated fetching from LeetCode API
- Preprocessing: MinMaxScaler normalization
- Model Training: LSTM with optimized hyperparameters
- Validation: Cross-validation on historical data
- Deployment: Serialized model ready for production
- Accuracy: 85%+ on test data
- Mean Absolute Error: < 15 rating points
- Training Data: 10,000+ contest records
Keep your model fresh with the latest LeetCode data!
# Fetch latest contest data from LeetCode
python update_data_simple.py
# When prompted, enter number of users (e.g., 5000)What happens:
- Loads existing usernames from
usernames.json(43,158 users) - Fetches latest contest history via GraphQL API
- Updates
data.jsonwith fresh training records - Multi-threaded processing (~10-15 users/second)
data.json: Latest contest history and rating changes (training data)
- Retrain the model using
LC_Contest_Rating_Predictor.ipynb - New
model.kerasandscaler.savewill be generated - Restart the API server to use the updated model
Quick Retraining Steps:
# 1. Install ML dependencies (first time only)
pip install -r requirements-ml.txt
pip install jupyter
# 2. Open the training notebook
jupyter notebook LC_Contest_Rating_Predictor.ipynb
# 3. Run all cells (Cell β Run All)
# β±οΈ Wait 5-15 minutes for training to complete
# 4. Restart the API server
# Press Ctrl+C in the terminal running the server, then:
uvicorn main:app --reloadWhat happens during retraining:
- Loads
data.json(your updated training data) - Preprocesses and normalizes features with MinMaxScaler
- Trains LSTM neural network (50 units, ~100 epochs with early stopping)
- Saves
model.keras(trained model) andscaler.save(feature scaler)
π Complete Retraining Guide: MODEL_RETRAINING_GUIDE.md
The complete guide includes:
- Detailed cell-by-cell walkthrough
- Model architecture explanation
- Performance evaluation metrics
- Troubleshooting common issues
- Advanced training options
- Python 3.8+
- Node.js 14+ (for frontend)
- Git
-
Backend Development
# Install development dependencies pip install -r requirements.txt # Run with auto-reload uvicorn main:app --reload --host 0.0.0.0 --port 8000
-
Frontend Development
cd client npm install npm start # Runs on http://localhost:3000
-
Model Training
# Open Jupyter notebook jupyter notebook LC_Contest_Rating_Predictor.ipynb
# Backend tests (if available)
python -m pytest tests/- Frontend API base URL: set
REACT_APP_API_BASE_URLinclient/.envor your system env to point the React app to the backend (default:http://localhost:8000). - To run backend tests locally:
python -m pytest -qIf you run into missing model files during local development, either download the model artifacts to ./model.keras and ./scaler.save or run tests which mock these artifacts.
-
Downloading model artifacts:
-
The repository includes
download_model.pyandmodels/manifest.json(placeholder). To download artifacts locally:python download_model.py
-
You can override URLs with environment variables:
$env:MODEL_URL = 'https://.../model.keras' $env:SCALER_URL = 'https://.../scaler.save' python download_model.py
-
The script also supports a GitHub shorthand of the form:
gh:owner/repo/releases/tag//<asset_name>
Example (requires
GITHUB_TOKENif the repo is private):python download_model.py # or $env:MODEL_URL = 'gh:owner/repo/releases/tag/v1/model.keras' $env:SCALER_URL = 'gh:owner/repo/releases/tag/v1/scaler.save' python download_model.py
-
-
Docker build with ML dependencies (optional):
The
Dockerfileaccepts a build-argINSTALL_ML. By default heavy ML deps are NOT installed. To include them:docker build --build-arg INSTALL_ML=1 -t myimage:latest .
-
Redis cache (optional):
- The backend uses an in-memory TTL cache by default. To use Redis in production, set
REDIS_URLin the environment (e.g.,redis://user:pass@host:6379/0). The app will automatically use Redis whenREDIS_URLis present.
- The backend uses an in-memory TTL cache by default. To use Redis in production, set
-
Integration CI job (manual):
- A manual
integrationjob is available in the GitHub ActionsCIworkflow. Trigger it from the Actions UI (workflow_dispatch). It will install ML dependencies (requirements-ml.txt), attempt to download model artifacts viadownload_model.py, and run integration tests.
- A manual
-
Pre-commit hooks:
-
Install dev tools and enable hooks:
pip install -r requirements-dev.txt pre-commit install
Docker Compose (local Redis)
To run the backend locally with Redis for caching, use docker-compose:
docker compose up --build # then open http://localhost:8000
This will run Redis (available at
redis://localhost:6379) and the backend connected to it viaREDIS_URL. -
cd client && npm test
## π Deployment
### Production Deployment
1. **Build React frontend**
```bash
cd client
npm run build
cd ..
- Run production server
uvicorn main:app --host 0.0.0.0 --port 8000
# Example Dockerfile structure
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]We welcome contributions! Please see CONTRIBUTING.md for guidelines.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit changes:
git commit -m 'Add amazing feature' - Push to 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.
- LeetCode for providing the contest data API
- TensorFlow team for the excellent ML framework
- FastAPI for the high-performance web framework
- React community for the frontend tools
- π Bug Reports: Create an issue
- π‘ Feature Requests: Start a discussion
- π§ Contact: [Your Email]
- Add user authentication
- Implement rating history tracking
- Support for more contest platforms
- Mobile app development
- Real-time rating updates
- Advanced analytics dashboard
β Star this repository if you find it helpful!
Made with β€οΈ by Sagar Gupta