{"id":47423044,"url":"https://github.com/Sagargupta16/LeetCode_Rating_Predictor","last_synced_at":"2026-04-04T23:00:33.591Z","repository":{"id":213324605,"uuid":"733502981","full_name":"Sagargupta16/LeetCode_Rating_Predictor","owner":"Sagargupta16","description":"Full-stack LeetCode contest rating predictor. Dense neural network (15 features, 244K records, MAE ~14) with FastAPI backend, React glassmorphism frontend, and automated data pipeline via LeetCode GraphQL API.","archived":false,"fork":false,"pushed_at":"2026-04-03T01:39:58.000Z","size":23852,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-04-03T11:26:11.539Z","etag":null,"topics":["contest-rating","deep-learning","fastapi","graphql","leetcode","machine-learning","neural-network","prediction","python","react","tensorflow"],"latest_commit_sha":null,"homepage":"https://leetcode-rating-predictor.onrender.com/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sagargupta16.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":["Sagargupta16"]}},"created_at":"2023-12-19T13:26:00.000Z","updated_at":"2026-04-03T01:38:30.000Z","dependencies_parsed_at":"2023-12-28T02:42:39.710Z","dependency_job_id":"709a52c4-eaa8-45fa-87d5-3cebd3fcae6f","html_url":"https://github.com/Sagargupta16/LeetCode_Rating_Predictor","commit_stats":null,"previous_names":["sagargupta16/leetcode_rating_predictor"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Sagargupta16/LeetCode_Rating_Predictor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sagargupta16%2FLeetCode_Rating_Predictor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sagargupta16%2FLeetCode_Rating_Predictor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sagargupta16%2FLeetCode_Rating_Predictor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sagargupta16%2FLeetCode_Rating_Predictor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sagargupta16","download_url":"https://codeload.github.com/Sagargupta16/LeetCode_Rating_Predictor/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sagargupta16%2FLeetCode_Rating_Predictor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31418285,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-04T20:09:54.854Z","status":"ssl_error","status_checked_at":"2026-04-04T20:09:44.350Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["contest-rating","deep-learning","fastapi","graphql","leetcode","machine-learning","neural-network","prediction","python","react","tensorflow"],"created_at":"2026-03-21T18:00:31.269Z","updated_at":"2026-04-04T23:00:33.580Z","avatar_url":"https://github.com/Sagargupta16.png","language":"Python","funding_links":["https://github.com/sponsors/Sagargupta16"],"categories":["Open Source Projects"],"sub_categories":["New and Noteworthy"],"readme":"# LeetCode Contest Rating Predictor\n\n[![Python](https://img.shields.io/badge/Python-3.11+-blue.svg)](https://python.org)\n[![FastAPI](https://img.shields.io/badge/FastAPI-0.133.0-green.svg)](https://fastapi.tiangolo.com)\n[![React](https://img.shields.io/badge/React-19-blue.svg)](https://reactjs.org)\n[![TensorFlow](https://img.shields.io/badge/TensorFlow-2.20.0-orange.svg)](https://tensorflow.org)\n[![License](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)\n\nPredict your LeetCode contest rating changes using a Dense neural network trained on 121,000+ contest records. Enter your username, select a contest, and get a prediction.\n\n## Quick Start\n\n```bash\n# Clone and set up\ngit clone https://github.com/Sagargupta16/LeetCode_Rating_Predictor.git\ncd LeetCode_Rating_Predictor\n\n# Python environment\npython -m venv venv\nvenv\\Scripts\\activate       # Windows\n# source venv/bin/activate  # Linux/Mac\n\n# Install and run\npip install -r requirements.txt\npip install -r requirements-ml.txt   # For model loading (TensorFlow)\npython main.py\n# Open http://localhost:8000\n```\n\n## Architecture\n\n```\nReact Frontend (port 3000)  --\u003e  FastAPI Backend (port 8000)\n                                    |\n                                    +-- LeetCode GraphQL API\n                                    +-- Dense Neural Network (model.keras)\n```\n\nAll LeetCode data is fetched via **GraphQL** (the REST ranking API is blocked).\n\n## Project Structure\n\n```\nmain.py                          # FastAPI entry point\napp/                             # Backend package\n  config.py                      #   Environment variables, constants\n  schemas.py                     #   Pydantic request/response models\n  model_loader.py                #   Keras model loader (handles legacy HDF5)\n  services/\n    leetcode.py                  #   LeetCode GraphQL client\n    prediction.py                #   ML prediction logic\n  utils/\n    cache.py                     #   TTLCache / RedisCache\nscripts/\n  download_model.py              # Download model artifacts from URLs\n  update_data.py                 # Fetch training data from LeetCode\n  check.py                       # Smoke test the running API\nnotebooks/\n  LC_Contest_Rating_Predictor.ipynb  # Training notebook\ndata/                            # Training data (gitignored)\nmodels/                          # Model manifest\ntests/                           # 34 backend tests\nclient/                          # React frontend (11 tests)\n```\n\n## API\n\n### `POST /api/predict`\n\n```json\n{\n  \"username\": \"your_username\",\n  \"contests\": [\n    { \"name\": \"weekly-contest-490\", \"rank\": 1500 }\n  ]\n}\n```\n\n**Response:**\n\n```json\n[\n  {\n    \"contest_name\": \"weekly-contest-490\",\n    \"prediction\": 25.5,\n    \"rating_before_contest\": 1800,\n    \"rank\": 1500,\n    \"total_participants\": 42002,\n    \"rating_after_contest\": 1825.5,\n    \"attended_contests_count\": 45\n  }\n]\n```\n\n### `GET /api/contestData`\n\nReturns the latest contests (via GraphQL `topTwoContests`).\n\n### `GET /api/health`\n\nHealth check with model/scaler/client status.\n\n## ML Model\n\n### Architecture\n\nDense neural network (replaced LSTM since input is tabular, not sequential):\n\n```\nDense(64, relu) -\u003e Dropout(0.2) -\u003e Dense(32, relu) -\u003e Dropout(0.2) -\u003e Dense(16, relu) -\u003e Dense(1)\n```\n\n3,137 parameters. Trained with Adam (lr=0.001), MSE loss, early stopping (patience=10).\n\n### 7 Input Features\n\n| # | Feature | Correlation with output |\n|---|---------|------------------------|\n| 1 | Current rating | -0.148 |\n| 2 | Contest rank | -0.474 |\n| 3 | Total participants | -0.308 |\n| 4 | Rank percentage (rank*100/participants) | -0.495 |\n| 5 | Attended contests count | -0.115 |\n| 6 | log(1 + rank) | **-0.508** |\n| 7 | Rating * percentile | **-0.555** |\n\nFeatures 6 and 7 are engineered and provide the strongest signal.\n\n### Performance\n\n| Metric | Value |\n|--------|-------|\n| Test MAE | **7.84 rating points** |\n| Test RMSE | 12.26 |\n| Test MSE | 150.34 |\n| Training data | 121,241 records |\n| Early stopped at | Epoch 38/200 |\n\n## Updating Training Data\n\n```bash\npython scripts/update_data.py\n# Enter number of users when prompted (e.g., 5000)\n```\n\nThis fetches contest history via GraphQL and writes to `data/data.json`.\n\n## Model Retraining\n\n### Quick Retraining (CPU)\n\n```bash\npip install -r requirements-ml.txt\npip install jupyter\ncd notebooks\njupyter notebook LC_Contest_Rating_Predictor.ipynb\n# Run All Cells -\u003e model.keras and scaler.save saved to project root\n```\n\n### GPU Retraining (WSL2 + NVIDIA)\n\nTensorFlow on native Windows is CPU-only. For GPU, use WSL2:\n\n```bash\n# In Ubuntu (WSL2):\nsource ~/tf-gpu/bin/activate\ncd \"/mnt/c/Code/GitHub/My Repos/ml-ai/LeetCode_Rating_Predictor\"\njupyter notebook notebooks/LC_Contest_Rating_Predictor.ipynb\n```\n\nSetup WSL2 GPU (one-time):\n```bash\n# PowerShell (admin):\nwsl --install -d Ubuntu\n\n# Inside Ubuntu:\nsudo apt-get update \u0026\u0026 sudo apt-get install -y python3 python3-pip python3-venv\npython3 -m venv ~/tf-gpu\nsource ~/tf-gpu/bin/activate\npip install \"tensorflow[and-cuda]==2.20.0\" joblib scikit-learn numpy\n```\n\n### After Retraining\n\nRestart the server to pick up the new model:\n```bash\npython main.py\n```\n\n### Retraining Checklist\n\n- [ ] Run `python scripts/update_data.py` for fresh data\n- [ ] Run all notebook cells\n- [ ] Verify `model.keras` and `scaler.save` created at project root\n- [ ] Check test MAE \u003c 15 in notebook output\n- [ ] Restart API server\n- [ ] Test a prediction via the UI or `python scripts/check.py`\n\n### Troubleshooting\n\n| Issue | Fix |\n|-------|-----|\n| `Module not found` | `pip install -r requirements-ml.txt` |\n| GPU not detected (Windows) | Use WSL2 (see above) |\n| Out of memory | Reduce `batch_size` in notebook (default: 64) |\n| Poor performance | Fetch more data: `python scripts/update_data.py` with more users |\n\n## Development\n\n### Prerequisites\n\n- Python 3.11+\n- Node.js 20+\n\n### Backend\n\n```bash\npip install -r requirements.txt\npip install -r requirements-dev.txt\nuvicorn main:app --reload --host 0.0.0.0 --port 8000\n```\n\n### Frontend\n\n```bash\ncd client\nnpm ci\nnpm start    # Dev server on port 3000\n```\n\n### Testing\n\n```bash\n# Backend (34 tests)\npython -m pytest tests/\n\n# Frontend (11 tests)\ncd client\nnpx react-scripts test --watchAll=false\n```\n\n### Linting\n\n```bash\nblack .\nisort .\nruff check .\n# Or all at once:\npre-commit run --all-files\n```\n\n### Environment Variables\n\n| Variable | Default | Purpose |\n|----------|---------|---------|\n| `MODEL_PATH` | `./model.keras` | Path to model file |\n| `SCALER_PATH` | `./scaler.save` | Path to scaler file |\n| `API_HOST` | `0.0.0.0` | Server bind host |\n| `API_PORT` | `8000` | Server bind port |\n| `ALLOWED_ORIGINS` | `http://localhost:3000` | CORS origins (comma-separated) |\n| `REDIS_URL` | *(empty)* | Redis URL for caching (optional) |\n| `CACHE_TTL` | `300` | Cache TTL in seconds |\n| `REACT_APP_API_BASE_URL` | *(auto-detected)* | Frontend API endpoint |\n\n## Deployment\n\n### Docker\n\n```bash\n# With Redis caching\ndocker-compose up --build\n\n# Standalone with ML deps\ndocker build --build-arg INSTALL_ML=1 -t leetcode-predictor .\ndocker run -p 8000:8000 leetcode-predictor\n```\n\n### Production\n\n```bash\ncd client \u0026\u0026 npm run build \u0026\u0026 cd ..\nuvicorn main:app --host 0.0.0.0 --port 8000\n```\n\n### Model Artifacts\n\nDownload from a release or URL:\n```bash\nMODEL_URL=https://... SCALER_URL=https://... python scripts/download_model.py\n```\n\nGitHub release shorthand:\n```bash\nMODEL_URL=gh:owner/repo/releases/tag/v1/model.keras python scripts/download_model.py\n```\n\n## CI Pipeline\n\nGitHub Actions: **Lint** (Black, isort, Ruff) -\u003e **Python tests** (pytest) -\u003e **Frontend tests** (npm test, npm build) -\u003e **Integration** (manual, downloads model + full test suite).\n\n## Contributing\n\nSee [CONTRIBUTING.md](CONTRIBUTING.md).\n\n## License\n\nMIT - see [LICENSE](LICENSE).\n\n---\n\nMade by [Sagar Gupta](https://github.com/Sagargupta16)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSagargupta16%2FLeetCode_Rating_Predictor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSagargupta16%2FLeetCode_Rating_Predictor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSagargupta16%2FLeetCode_Rating_Predictor/lists"}