{"id":49687574,"url":"https://github.com/random-iceberg/model-backend","last_synced_at":"2026-05-07T10:45:01.361Z","repository":{"id":306148209,"uuid":"1024651624","full_name":"random-iceberg/model-backend","owner":"random-iceberg","description":null,"archived":false,"fork":false,"pushed_at":"2025-07-23T23:46:38.000Z","size":113,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-24T00:03:49.177Z","etag":null,"topics":["fastapi","scikit-learn"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/random-iceberg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-23T03:44:59.000Z","updated_at":"2025-07-23T23:46:39.000Z","dependencies_parsed_at":"2025-07-24T00:03:51.347Z","dependency_job_id":"a96b7735-3eec-40eb-a4cb-4d5dd0032835","html_url":"https://github.com/random-iceberg/model-backend","commit_stats":null,"previous_names":["random-iceberg/model-backend"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/random-iceberg/model-backend","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/random-iceberg%2Fmodel-backend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/random-iceberg%2Fmodel-backend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/random-iceberg%2Fmodel-backend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/random-iceberg%2Fmodel-backend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/random-iceberg","download_url":"https://codeload.github.com/random-iceberg/model-backend/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/random-iceberg%2Fmodel-backend/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32734388,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-07T02:14:30.463Z","status":"ssl_error","status_checked_at":"2026-05-07T02:14:29.405Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: 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":["fastapi","scikit-learn"],"created_at":"2026-05-07T10:45:00.468Z","updated_at":"2026-05-07T10:45:01.355Z","avatar_url":"https://github.com/random-iceberg.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Model Microservice\n\nMachine learning service for training and inference using scikit-learn models.\n\n## 🚀 Quick Start (Zero Configuration)\n\n```bash\n# From the project root directory\ndocker compose -f 'compose/compose.dev.yaml' up -d --build\n\n# Access Swagger UI\nopen http://localhost:8001/docs  # Development mode\n```\n\nNo setup needed! The service starts with pre-trained models ready for inference.\n\n## 📋 Features\n\n- **5 ML Algorithms**: Random Forest, SVM, Decision Tree, KNN, Logistic Regression\n- **Model Training**: Train models with configurable feature selection\n- **Model Persistence**: Automatic saving and loading of trained models\n- **RESTful API**: Full Swagger/OpenAPI documentation\n\n## 🏗️ API Documentation\n\n### Interactive API Explorer\nAccess the Swagger UI at: **http://localhost:8001/docs** (development mode)\n\n### Main Endpoints\n\n- `GET /health` - Service health check\n- `GET /models` - List all trained models with metadata\n- `POST /models/train` - Train a new model\n- `POST /models/{id}/predict` - Get prediction from specific model\n- `DELETE /models/{id}` - Delete a trained model\n\n## 🤖 Available Algorithms\n\n| Algorithm | ID | Configurable Parameters |\n|-----------|-----|------------------------|\n| Random Forest | `rf` | `n_estimators` |\n| Support Vector Machine | `svm` | - |\n| Decision Tree | `dt` | - |\n| K-Nearest Neighbors | `knn` | `n_neighbors` |\n| Logistic Regression | `lr` | - |\n\n## 📊 Available Features\n\nFeatures from the Titanic dataset (select which ones to use during training):\n- `pclass` - Passenger class (1, 2, 3)\n- `sex` - Gender (male/female)\n- `age` - Age in years\n- `fare` - Ticket fare\n- `embarked` - Port of embarkation\n- `title` - Extracted from name (Mr, Mrs, etc.)\n- `is_alone` - Traveling alone flag\n- `age_class` - Age × Class interaction\n\n## 🛠️ Development Workflow\n\n### Testing the API with Swagger\n\n1. Go to http://localhost:8001/docs\n2. Try `/models` to see pre-loaded models\n3. Test prediction with `/models/{model_id}/predict`:\n   ```json\n   {\n     \"pclass\": 1,\n     \"sex\": \"female\",\n     \"age\": 30,\n     \"fare\": 100,\n     \"travelled_alone\": false,\n     \"embarked\": \"cherbourg\",\n     \"title\": \"mrs\"\n   }\n   ```\n4. Train a custom model with `/models/train`\n\n## 🧪 Testing\n\n```bash\ncd model\n\n# Install dependencies (if not already done)\nuv sync --extra dev\n\n# Run tests\nuv run pytest\n\n# Linting and formatting check\nuv run ruff check\nuv run ruff format --check\n\n# Auto-fix formatting\nuv run ruff format\n```\n\n## 📁 Project Structure\n\n```\nmodel/\n├── main.py              # FastAPI application\n├── models_router.py     # Model management endpoints\n├── schemas.py           # Pydantic data models\n├── train.py            # Training script\n├── utils/\n│   ├── data.py         # Data preprocessing\n│   ├── models.py       # Model loading/saving\n│   └── model_factory.py # Algorithm factory\n├── data/               # Included dataset\n│   ├── train.csv\n│   ├── test.csv\n│   └── gender_submission.csv\n└── tests/              # Test suite\n```\n\n## 🔧 Model Training\n\n### Using Swagger UI\n\n1. Go to http://localhost:8001/docs\n2. Expand `POST /models/train`\n3. Click \"Try it out\"\n4. Use this example request:\n   ```json\n   {\n     \"algo\": {\n       \"name\": \"rf\",\n       \"n_estimators\": 150\n     },\n     \"features\": [\"pclass\", \"sex\", \"age\", \"fare\"],\n     \"random_state\": 42\n   }\n   ```\n5. Click \"Execute\"\n\n### Training Response\n```json\n{\n  \"id\": \"trained-abc123\",\n  \"params\": { ... },\n  \"info\": {\n    \"accuracy\": 0.85\n  }\n}\n```\n\n## 📈 Making Predictions\n\n### Using Swagger UI\n\n1. Get model ID from `/models` endpoint\n2. Use `POST /models/{model_id}/predict`\n3. Provide passenger data\n4. Receive survival prediction with probability\n\n### Prediction Request\n```json\n{\n  \"pclass\": 3,\n  \"sex\": \"male\",\n  \"age\": 25,\n  \"fare\": 15.5,\n  \"travelled_alone\": true,\n  \"embarked\": \"southampton\",\n  \"title\": \"mr\"\n}\n```\n\n### Prediction Response\n```json\n{\n  \"survived\": false,\n  \"probability\": 0.78\n}\n```\n\n## 💾 Data Management\n\n### Model Storage\n- Models automatically saved to `/data/models/`\n- Persisted across container restarts\n- Each model includes:\n  - `model.pkl` - Serialized scikit-learn model\n  - `params.json` - Training parameters\n  - `info.json` - Model metadata and accuracy\n\n### Pre-loaded Models\nOn startup, the service loads:\n- `rf` - Random Forest\n- `svm` - Support Vector Machine\n- `knn` - K-Nearest Neighbors\n- `lr` - Logistic Regression\n\n## 🐳 Production Deployment\n\nThe service is production-ready when deployed via:\n```bash\ndocker compose -f compose/compose.prod-local.yaml up\n```\n\n## 🔍 Troubleshooting\n\n### Model Not Found\n- Check model ID with `GET /models`\n- Verify model files in container: `docker compose exec model ls /data/models`\n\n### Slow Predictions\n- Models are loaded at container startup\n- Consider model complexity and input data size\n\n### Training Failures\n- Check feature names match schema\n- Verify algorithm parameters are valid\n- Review logs: `docker compose logs model`\n\n### Note on random_state\n- The `random_state` parameter only affects model training for reproducibility\n- It does not affect predictions\n\n## 📚 Additional Resources\n\n- [FastAPI Documentation](https://fastapi.tiangolo.com/)\n- [Scikit-learn User Guide](https://scikit-learn.org/stable/user_guide.html)\n- [Project Requirements](../../docs/Project-Requirements.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frandom-iceberg%2Fmodel-backend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frandom-iceberg%2Fmodel-backend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frandom-iceberg%2Fmodel-backend/lists"}