Spaces:
Running
title: Pest Detection API
emoji: πΎ
colorFrom: green
colorTo: gray
sdk: docker
app_port: 7860
pinned: false
πΎ Pest Detection API & Mobile App
A complete mobile application and Dockerized FastAPI backend system utilizing YOLOv8 to scan, identify, and recommend treatment plans for agricultural crop pests. It supports a 10-class Cereal Pests model (Rice & Maize) and can dynamically fall back or load the full 102-class IP102 dataset model.
π Deployment Guide
To make the app usable anywhere, you can deploy the backend to cloud platforms like Hugging Face Spaces or Render.
Option A: Deploy to Hugging Face Spaces (Recommended & Free)
Hugging Face Spaces provides a free Docker hosting platform that runs 24/7.
Step 1: Create a Space on Hugging Face
- Log in to your account at Hugging Face.
- Click on New Space (or go to
huggingface.co/new-space). - Set the Space Name (e.g.,
pest-detection-api). - Select Docker as the SDK.
- Choose Blank (or any Docker template) as the template.
- Set the Space visibility (Public is required for your app to connect easily).
- Click Create Space.
Step 2: Push your Code to the Space
You can deploy your code using Git:
# 1. Initialize git if you haven't (or use your existing repository)
git init
# 2. Add the Hugging Face Space repository as a remote
# Replace username and space-name with your HF details
git remote add hf https://huggingface.co/spaces/<your-username>/<space-name>
# 3. Add files and commit
git add .
git commit -m "Configure deployment files for Hugging Face"
# 4. Push to Hugging Face (usually to the main branch)
git push -f hf main
Hugging Face will automatically read the root
Dockerfileand build/deploy your API. Once deployed, your URL will look like:https://<your-username>-<space-name>.hf.space
Option B: Deploy to Render
Render is a robust, developer-friendly cloud hosting platform.
Step 1: Create a Web Service on Render
- Sign up/log in to Render.
- Click New + and select Web Service.
- Connect your GitHub repository containing this project.
- Set the following configuration details:
- Name:
pest-detection-api - Language:
Docker(Render will automatically detect the rootDockerfile) - Branch:
main - Region: Choose the closest region to your users
- Instance Type: Free (or Starter for faster performance/no sleep times)
- Name:
- Click Create Web Service.
Step 2: Render Environmental Variables
No extra variables are strictly required, but you can customize the execution port if needed. By default, Render overrides the PORT environment variable and binds it automatically.
The Free Tier of Render will "spin down" (go to sleep) if it receives no traffic for 15 minutes. The next request will take 30-50 seconds to wake the server up.
π± Updating the Mobile App to Connect to the Cloud
Once your backend is deployed on either platform, you need to point your Android app to the new production URL.
Open your Android project files in Android Studio.
Locate the ApiConstants.java file:
app/src/main/java/com/example/pestdetection/ApiConstants.javaUpdate the
DEFAULT_API_BASE_URLconstant with your production URL:// Example for Hugging Face Spaces: public static final String DEFAULT_API_BASE_URL = "https://<your-username>-<space-name>.hf.space"; // Example for Render: // public static final String DEFAULT_API_BASE_URL = "https://pest-detection-api.onrender.com";Build and run your Android App! You can now test scanning agricultural pests from any device anywhere in the country.
π οΈ Local Development & Testing
If you want to run the backend API locally for development:
- Navigate to the
backend/directory:cd backend - Install Python dependencies:
pip install -r requirements.txt - Run the development server:
python app.py - Access the API locally at
http://localhost:5000or check the health page athttp://localhost:5000/health.