Thinh11062005/Carafe_Yolov12
0
Drill Bit Defect Detection - Hugging Face Spaces Deployment
This project deploys a drill bit defect detection model using YOLOv12 with CARAFE to Hugging Face Spaces with FastAPI backend.
๐ Deployment Instructions
Prerequisites
- Hugging Face account with access token (write permissions)
- Git installed
- HF CLI (optional, but recommended)
Method 1: Using Git (Recommended)
- Clone the Space
git clone https://huggingface.co/spaces/Thinh11062005/Carafe_Yolov12
cd Carafe_Yolov12- Copy all files
# Copy all files from huggingface_space directory
cp -r /Users/mac/Detect_Drill_Bit/huggingface_space/* ./- Commit and push
git add .
git commit -m "Deploy drill bit detection app with FastAPI backend"
git push -u origin mainWhen prompted for password, use your Hugging Face access token.
Method 2: Using HF CLI
- Install HF CLI
curl -LsSf https://hf.co/cli/install.sh | bash- Download the Space
hf download Thinh11062005/Carafe_Yolov12 --repo-type=space
cd Carafe_Yolov12- Copy and deploy
# Copy all files
cp -r /Users/mac/Detect_Drill_Bit/huggingface_space/* ./
# Commit and push
git add .
git commit -m "Deploy drill bit detection app with FastAPI backend"
git push -u origin mainMethod 3: Using Web UI
- Go to your Space: https://huggingface.co/spaces/Thinh11062005/Carafe_Yolov12
- Click on "Files" tab
- Upload all files:
app.pyindex.htmlrequirements.txtmodels/best_carafe.pt- Click "Commit changes to main"
- Add a commit message and click "Commit"
๐ File Structure
Carafe_Yolov12/
โโโ app.py # FastAPI backend server
โโโ index.html # Frontend web application
โโโ requirements.txt # Python dependencies
โโโ README.md # This file
โโโ models/ # Model weights directory
โโโ best_carafe.pt # YOLOv12 + CARAFE model๐จ Features
The web application includes:
- FastAPI backend for real-time inference
- Drag & drop image upload
- Multiple model selection:
- YOLOv12n + CARAFE (Recommended)
- YOLOv12n + DySample
- YOLOv12n (After/Before Augmentation)
- YOLOv12n + HQNN Fusion
- Baseline YOLO models (v8n, v9n, v10n, v11n, v12n, v26n)
- Real-time defect detection with bounding boxes
- Confidence scores for each detection
- Responsive design for mobile and desktop
๐ง Defect Classes
The model can detect 5 types of drill bit defects:
- Broken - Drill bit is broken or fractured
- Chipped - Small pieces chipped off the edge
- Scratched - Surface scratches or marks
- Severe_Rust - Heavy rust or corrosion
- Tip_Wear - Wear at the drill bit tip
๐ API Endpoints
POST /predict
Run inference on uploaded image.
Parameters:
file: Image file (JPG, PNG)model: Model selection (carafe, dysample, after_aug, etc.)conf_threshold: Confidence threshold (default: 0.25)
Response:
{
"success": true,
"detections": [
{
"class": "Scratched",
"confidence": 0.92,
"bbox": {"x": 50, "y": 80, "width": 200, "height": 150}
}
],
"model_used": "carafe",
"image_size": {"width": 640, "height": 480}
}GET /health
Health check endpoint.
GET /models
List available models.
๐ Access the Deployed App
After deployment, your app will be available at:
https://huggingface.co/spaces/Thinh11062005/Carafe_Yolov12๐ Adding More Models
To add more models:
- Copy model weights to
models/directory:
cp /path/to/model.pt ./models/best_[model_name].pt- Update
app.pyto map model selection to file path (already configured for common models)
- Commit and push changes
๐ Useful Links
๐งช Testing Locally
To test the app locally before deploying:
- Navigate to the huggingface_space directory
cd /Users/mac/Detect_Drill_Bit/huggingface_space- Install dependencies
pip install -r requirements.txt- Run the FastAPI server
python app.pyNote: Use python app.py instead of fastapi dev to avoid multiprocessing issues with custom modules (CARAFE, DySample).
- Open browser The app will be available at:
http://localhost:7860
- Test the API
- Upload an image
- Select a model
- Click "Detect Defects"
- View bounding boxes and detections
๏ฟฝ Deploying to Hugging Face Spaces
Method 1: Using Git (Recommended)
- Create a new Space on Hugging Face
- Go to https://huggingface.co/spaces
- Click "Create new Space"
- Select "Docker" as SDK
- Name your space (e.g.,
drill-bit-detection) - Choose "Public" or "Private"
- Clone the Space
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
cd YOUR_SPACE_NAME- Copy all files from huggingface_space directory
cp -r /Users/mac/Detect_Drill_Bit/huggingface_space/* ./- Create Dockerfile (if not exists)
cat > Dockerfile << 'EOF'
FROM python:3.11-slim
WORKDIR /app
# Install system dependencies
RUN apt-get update && apt-get install -y \
libgl1-mesa-glx \
libglib2.0-0 \
&& rm -rf /var/lib/apt/lists/*
# Copy requirements and install
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy application files
COPY . .
# Expose port
EXPOSE 7860
# Run the application
CMD ["python", "app.py"]
EOF- Commit and push
git add .
git commit -m "Deploy drill bit detection with CARAFE"
git pushMethod 2: Using Hugging Face CLI
- Install HF CLI
pip install huggingface_hub- Login to Hugging Face
huggingface-cli login- Upload files
cd /Users/mac/Detect_Drill_Bit/huggingface_space
huggingface-cli upload YOUR_USERNAME/YOUR_SPACE_NAME . --repo-type=spaceMethod 3: Using Web UI
- Go to your Space on Hugging Face
- Click on "Files" tab
- Upload all files:
app.pyindex.htmlrequirements.txtcustom_modules.pyDockerfilemodels/best_carafe.pt- Click "Commit changes to main"
Important Notes for Hugging Face Deployment
- Dockerfile is required for custom modules (CARAFE) to work properly
- Model weights must be uploaded to
models/directory - Custom modules (
custom_modules.py) must be included - Port 7860 is the standard port for Hugging Face Spaces
- GPU Space is recommended for faster inference (select "GPU" when creating Space)
GPU Space Setup (Recommended)
When creating your Space:
- Select "GPU" as hardware (T4 or A10G)
- This will significantly speed up inference
- GPU Space has a monthly free tier limit
Troubleshooting
If you encounter issues on Hugging Face:
- Check the "Logs" tab in your Space
- Ensure
custom_modules.pyis uploaded - Verify model weights are in
models/directory - Make sure
Dockerfileis present and correct
๏ฟฝ๏ฟฝ Support
For issues or questions, contact the project maintainer.
