{"id":15129834,"url":"https://github.com/raptorblingx/fastapi_ml_task","last_synced_at":"2026-01-18T19:09:09.884Z","repository":{"id":257289081,"uuid":"857822789","full_name":"RaptorBlingx/fastapi_ml_task","owner":"RaptorBlingx","description":"This project demonstrates a complete pipeline for building a machine learning model using XGBoost on sustainable energy data, integrated with a FastAPI-based REST API. The application allows users to upload data, train the model, and make predictions via API endpoints. Docker and PostgreSQL are used for containerization and data management.","archived":false,"fork":false,"pushed_at":"2024-09-15T22:08:50.000Z","size":886,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-11T16:41:46.652Z","etag":null,"topics":["api-development","data-pipeline","data-science","docker","docker-compose","fastapi","jupyter-notebook","machine-learning","model-training","postgresql","python","rest-api","sustainable-energy","xgboost"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","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/RaptorBlingx.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":"2024-09-15T17:42:26.000Z","updated_at":"2024-09-16T09:27:36.000Z","dependencies_parsed_at":"2024-09-15T19:27:16.366Z","dependency_job_id":"183d93bb-c99f-4f7b-be5c-df3d5e2a657e","html_url":"https://github.com/RaptorBlingx/fastapi_ml_task","commit_stats":null,"previous_names":["raptorblingx/fastapi_ml_task"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RaptorBlingx%2Ffastapi_ml_task","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RaptorBlingx%2Ffastapi_ml_task/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RaptorBlingx%2Ffastapi_ml_task/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RaptorBlingx%2Ffastapi_ml_task/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RaptorBlingx","download_url":"https://codeload.github.com/RaptorBlingx/fastapi_ml_task/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247386410,"owners_count":20930636,"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":["api-development","data-pipeline","data-science","docker","docker-compose","fastapi","jupyter-notebook","machine-learning","model-training","postgresql","python","rest-api","sustainable-energy","xgboost"],"created_at":"2024-09-26T02:21:13.008Z","updated_at":"2026-01-18T19:09:09.855Z","avatar_url":"https://github.com/RaptorBlingx.png","language":"Jupyter Notebook","readme":"# FastAPI Machine Learning Task\n\nThis repository contains a project to develop a machine learning model based on sustainable energy data and integrate it with a FastAPI-based REST API.\n\n## Setup Instructions\n\n1. Clone the repository:\n   ```\n   git clone https://github.com/yourusername/fastapi_ml_task.git\n   ```\n\n2. Install the dependencies:\n   ```\n   pip install -r requirements.txt\n   ```\n\n3. Run the Jupyter notebook to train the model:\n   - Navigate to `models/model_training.ipynb`.\n   - Ensure the dataset is in the `data/` folder.\n   - Run the notebook step by step.\n\n# FastAPI Machine Learning Task\n\nThis repository contains a project to develop a machine learning model based on sustainable energy data and integrate it with a FastAPI-based REST API.\n\n## Project Structure\n```\nFastAPI_ML_Task/\n│\n├── data/                          # Folder to store the dataset\n│   └── global-data-on-sustainable-energy.csv\n├── models/                        # Folder to store model files and notebooks\n│   ├── model_training.ipynb       # Jupyter notebook for the ML workflow\n│   ├── trained_model.pkl          # Initial trained model\n│   ├── tuned_model.pkl            # Tuned XGBoost model\n│   └── model_utils.py             # Utility functions for model training\n├── app/                           # Folder for FastAPI application\n│   ├── main.py                    # Main FastAPI application with endpoints\n│   ├── database_setup.py          # Database setup and connection management\n├── Dockerfile                     # Dockerfile for building the FastAPI container\n├── docker-compose.yml             # Docker Compose file to orchestrate FastAPI and PostgreSQL services\n├── requirements.txt               # List of Python dependencies\n├── README.md                      # Project documentation\n└── .gitignore                     # Git ignore file to avoid committing sensitive files (like .env)\n```\n\n## Setup Instructions\n\n1. Clone the repository:\n   ```\n   git clone https://github.com/yourusername/fastapi_ml_task.git\n   ```\n\n2. Install the dependencies:\n   ```\n   pip install -r requirements.txt\n   ```\n\n3. Run the Jupyter notebook to train the model:\n   - Navigate to `models/model_training.ipynb`.\n   - Ensure the dataset is in the `data/` folder.\n   - Run the notebook step by step.\n\n## Database Setup (PostgreSQL)\n\nTo set up the PostgreSQL database, follow these steps:\n\n1. **Install PostgreSQL and pgAdmin4**: \n   Ensure that PostgreSQL and pgAdmin4 are installed and running on your machine.\n\n2. **Configure the Database URL**:\n   In the `app/database_setup.py` file, update the `DATABASE_URL` with your PostgreSQL credentials.\n\n   Example:\n   ```\n   DATABASE_URL = \"postgresql://your_username:your_password@localhost/your_dbname\"\n    ```\n\n3. **Initialize the Database**:\n   Run the following command to initialize the database and create the tables:\n\n   ```\n   python app/main.py\n   ```\n\n   This will create the following tables in your PostgreSQL database:\n   - **energy_data**: For storing energy data records.\n   - **predictions**: For storing prediction results.\n\n5. **Verify the Database**:\n   - You can open **pgAdmin4** and verify that the tables have been successfully created.\n  \n---\n\n## Setting Up the Environment Variables\n\nYou will need to create a `.env` file in the root directory of this project.\n\n### Steps to Set Up `.env`:\n\n1. **Create a `.env` file**:\n   - In the root of the project (where the `docker-compose.yml` file is located), create a new file named `.env`.\n\n2. **Add the following content** to the `.env` file:\n   ```\n   POSTGRES_USER=your_postgres_username\n   POSTGRES_PASSWORD=your_postgres_password\n   POSTGRES_DB=your_database_name\n   ```\n\n   - Replace `your_postgres_username`, `your_postgres_password`, and `your_database_name` with your actual PostgreSQL credentials. For example:\n\n   ```\n   POSTGRES_USER=postgres\n   POSTGRES_PASSWORD=your_secure_password\n   POSTGRES_DB=ml_api\n   ```\n\n3. **Database URL**:\n   - The environment variables in the `.env` file will be used to automatically configure the database connection inside the **`docker-compose.yml`** and **`app/database_setup.py`**.\n   - The database connection string will look like this:\n     ```\n     postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}\n     ```\n\n---\n\n\n### Run the FastAPI Application\nStart the FastAPI server by running:\n\n```\nuvicorn app.main:app --reload\n```\n\nYou can access the API documentation and test the endpoints at **`http://127.0.0.1:8000/docs`**.\n\n## API Endpoints\n\n### 1. `/upload-data` (POST)\nUploads a CSV file containing energy data and stores it in the PostgreSQL database.\n\n- **Request**: Upload a CSV file with columns `Entity`, `Year`, `Primary energy consumption per capita (kWh/person)`, and `Renewable energy share in the total final energy consumption (%)`.\n\n### 2. `/get-data/{country}` (GET)\nRetrieves energy data for a specific country from the PostgreSQL database.\n\n- **Parameters**: `country` - The name of the country (e.g., \"Afghanistan\").\n\n### 3. `/train-model` (POST)\nTrains an XGBoost model using the uploaded energy data and saves the trained model.\n\n- **Response**: Confirms that the model was successfully trained and saved.\n\n### 4. `/predict` (POST)\nMakes predictions using the trained model based on the energy production provided.\n\n- **Parameters**: \n  - `energy_production`: The amount of energy production (numeric value e.g., \"5000\").\n  \n- **Response**: Returns the predicted value for the renewable energy share.\n\n## Testing\n\nYou can test the API using the Swagger UI at **`http://127.0.0.1:8000/docs`**, or with tools like **Postman**.\n=======\n\n\n## Running the Project with Docker\n\nTo run the FastAPI application and PostgreSQL database using Docker, follow these steps:\n\n### Prerequisites\n\n- Ensure Docker and Docker Compose are installed on your machine.\n\n### Steps:\n\n1. **Build the Docker containers**:\n   ```\n   docker-compose build\n   ```\n\n2. **Start the services** (FastAPI and PostgreSQL):\n   ```\n   docker-compose up\n   ```\n\n   This will start both the FastAPI application and PostgreSQL database.\n\n3. **Access the API**:\n   - The FastAPI application will be available at **`http://localhost:8000`**.\n   - The API documentation (Swagger UI) will be available at **`http://localhost:8000/docs`**.\n\n4. **Interacting with the API**:\n   - **Upload Data**: Use the `/upload-data` endpoint to upload the dataset (CSV file).\n   - **Train the Model**: Call the `/train-model` endpoint to train the XGBoost model.\n   - **Make Predictions**: Use the `/predict` endpoint to make predictions based on the trained model.\n   - **Retrieve Data**: Fetch energy data for a specific country via the `/get-data/{country}` endpoint.\n\n5. **Stopping the Docker services**:\n   ```\n   docker-compose down\n   ```\n\n   This will stop and remove the containers.\n\n### Setting Up the Environment Variables\n\n- The environment variables (like PostgreSQL credentials) are configured in the `.env` file. Each user should create a `.env` file in the root directory with the following content:\n\n```\nPOSTGRES_USER=your_postgres_username\nPOSTGRES_PASSWORD=your_postgres_password\nPOSTGRES_DB=ml_api\n```\n\n- Replace `your_postgres_username` and `your_postgres_password` with your actual PostgreSQL credentials.\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraptorblingx%2Ffastapi_ml_task","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fraptorblingx%2Ffastapi_ml_task","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraptorblingx%2Ffastapi_ml_task/lists"}