{"id":26830619,"url":"https://github.com/kingabzpro/redis-ml-project","last_synced_at":"2025-04-30T08:13:48.493Z","repository":{"id":285084557,"uuid":"949960088","full_name":"kingabzpro/Redis-ml-project","owner":"kingabzpro","description":"Model serving with FastAPI and Redis caching to enable fast and efficient inference by reducing latency for repeated predictions.","archived":false,"fork":false,"pushed_at":"2025-03-29T11:19:10.000Z","size":20731,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-30T08:13:40.494Z","etag":null,"topics":["async","fastapi","machine-learning","phishing-classcification","redis","redis-cache","sklearn","text-classification"],"latest_commit_sha":null,"homepage":"https://www.kdnuggets.com/accelerate-machine-learning-model-serving-with-fastapi-and-redis-caching","language":"Python","has_issues":true,"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/kingabzpro.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}},"created_at":"2025-03-17T12:12:50.000Z","updated_at":"2025-04-29T03:37:35.000Z","dependencies_parsed_at":"2025-03-29T12:34:31.741Z","dependency_job_id":null,"html_url":"https://github.com/kingabzpro/Redis-ml-project","commit_stats":null,"previous_names":["kingabzpro/redis-ml-project"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingabzpro%2FRedis-ml-project","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingabzpro%2FRedis-ml-project/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingabzpro%2FRedis-ml-project/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingabzpro%2FRedis-ml-project/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kingabzpro","download_url":"https://codeload.github.com/kingabzpro/Redis-ml-project/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251666361,"owners_count":21624298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["async","fastapi","machine-learning","phishing-classcification","redis","redis-cache","sklearn","text-classification"],"created_at":"2025-03-30T14:16:41.644Z","updated_at":"2025-04-30T08:13:48.472Z","avatar_url":"https://github.com/kingabzpro.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Phishing Email Classification App with Redis\n\nThis project demonstrates a phishing email classification application using machine learning. It includes four main components:\n\n- **Model Training (`train.py`)**: Processes the dataset, trains a model using TF-IDF vectorization and logistic regression, and saves the trained model.\n- **Model Validation (`validate.py`)**: Evaluates the saved model on a test dataset and outputs performance metrics such as accuracy, precision, and recall.\n- **Model Serving (`serve.py`)**: Implements a FastAPI-based service to provide real-time phishing email predictions. Redis caching is used to optimize performance by speeding up repeated requests.\n- **Redis Check (`check_redis.py`)**: A utility script to inspect the Redis database, displaying the number of cached entries and sample predictions.\n\n---\n## Application Workflow\nThe following diagram illustrates how the application works, including the interaction between the machine learning model, FastAPI, and Redis caching:\n\n![Redis ML applicaiton diagram](image/redisml.svg)\n\n\n---\n\n## Project Structure\n\n```\n.\n├── data/\n│   └── phishing_emails.csv         # Kaggle dataset \n├── phishing_model.pkl              # Saved model file\n├── train.py                        # Script for training the model\n├── validate.py                     # Script for validating the model\n├── serve.py                        # FastAPI app for model serving with Redis caching\n└── check_redis.py                  # Script to inspect Redis cache entries\n```\n\n---\n\n## Prerequisites\n\n- Python 3.7+\n- Pip\n- Redis (see instructions below for running Redis on Windows)\n\nInstall the required Python packages with:\n```bash\npip install -r requirements.txt\n```\n\n---\n\n## Dataset\n\nThe dataset is sourced from [Kaggle](https://www.kaggle.com/datasets/subhajournal/phishingemails). Download the dataset and place it in the `data/` directory.\n\n---\n\n## Usage\n\n### 1. Training the Model\nRun the training script to process the dataset and generate the model file:\n```bash\npython train.py\n```\n\n### 2. Validating the Model\nRun the validation script to evaluate the model's performance:\n```bash\npython validate.py\n```\n\n### 3. Serving Predictions\nEnsure Redis is running (see instructions below for Windows), then start the FastAPI server:\n```bash\npython serve.py\n```\nAccess the API at `http://localhost:8000` and send POST requests to the `/predict` endpoint with JSON payloads:\n```json\n{\n  \"text\": \"Your email content here...\"\n}\n```\n\n### 4. Checking Redis\nUse the `check_redis.py` script to inspect the Redis cache. This script displays the number of cached entries and the first five email texts along with their predictions:\n```bash\npython check_redis.py\n```\n\n---\n\n## Running Redis on Windows\n\n1. **Enable WSL**: Follow [Microsoft's guide](https://docs.microsoft.com/en-us/windows/wsl/install) to enable WSL and install a Linux distribution (e.g., Ubuntu) from the Microsoft Store.\n2. **Install Redis**: Open your WSL terminal and run:\n   ```bash\n   sudo apt update\n   sudo apt install redis-server\n   ```\n3. **Start Redis**:\n   ```bash\n   sudo service redis-server start\n   ```\n4. **Access Redis from Windows**: With Redis running in WSL (default on `localhost:6379`), the Python Redis client in your FastAPI app can connect without extra configuration.\n\n---\n\n## API Usage\n\nSend a POST request to `http://localhost:8000/predict` with the following JSON payload:\n```bash\ncurl -X 'POST' \\\n  'http://localhost:8000/predict' \\\n  -H 'accept: application/json' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\n  \"text\": \"todays floor meeting you may get a few pointed questions about today article about lays potential severance of $ 80 mm\"\n}'\n```\n\nThe API responds with a JSON object containing:\n- **`prediction`**: The predicted class name (e.g., \"Phishing Email\" or \"Safe Email\").\n- **`probability`**: The confidence score of the prediction.\n\nExample response:\n```json\n{\n  \"prediction\": \"Safe Email\",\n  \"probability\": 0.7791625553383463\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkingabzpro%2Fredis-ml-project","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkingabzpro%2Fredis-ml-project","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkingabzpro%2Fredis-ml-project/lists"}