Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app/main.py +3 -0
- app/routers/image/bgremover.py +46 -0
- requirements.txt +3 -1
app/main.py
CHANGED
|
@@ -10,6 +10,7 @@ from app.routers.pdf import pdf_tools
|
|
| 10 |
# π Image Module
|
| 11 |
# ==========================
|
| 12 |
from app.routers.image import jpgcompressor
|
|
|
|
| 13 |
|
| 14 |
# ==========================
|
| 15 |
# π Security Module
|
|
@@ -52,6 +53,7 @@ app.include_router(calculator.router) # Mini calculator service
|
|
| 52 |
app.include_router(auth_system.router) # <-- NEW AUTH SYSTEM
|
| 53 |
app.include_router(storage.router) # new cloud drive support
|
| 54 |
app.include_router(server_status.router) # function to check the system
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
|
|
@@ -63,4 +65,5 @@ def greet_json():
|
|
| 63 |
return """
|
| 64 |
<h2>Go to the Swagger docs π</h2>
|
| 65 |
<a href="/docs">Click here for API Docs</a>
|
|
|
|
| 66 |
"""
|
|
|
|
| 10 |
# π Image Module
|
| 11 |
# ==========================
|
| 12 |
from app.routers.image import jpgcompressor
|
| 13 |
+
from app.routers.image import bgremover
|
| 14 |
|
| 15 |
# ==========================
|
| 16 |
# π Security Module
|
|
|
|
| 53 |
app.include_router(auth_system.router) # <-- NEW AUTH SYSTEM
|
| 54 |
app.include_router(storage.router) # new cloud drive support
|
| 55 |
app.include_router(server_status.router) # function to check the system
|
| 56 |
+
app.include_router(bgremover.router) # Background Remover
|
| 57 |
|
| 58 |
|
| 59 |
|
|
|
|
| 65 |
return """
|
| 66 |
<h2>Go to the Swagger docs π</h2>
|
| 67 |
<a href="/docs">Click here for API Docs</a>
|
| 68 |
+
<h3>created by sameer banchhor</h3>
|
| 69 |
"""
|
app/routers/image/bgremover.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, UploadFile, File, Depends, HTTPException
|
| 2 |
+
from fastapi.responses import Response
|
| 3 |
+
from rembg import remove
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
# Import the auth dependency from your existing system
|
| 8 |
+
from app.routers.auth.system import get_current_user
|
| 9 |
+
|
| 10 |
+
router = APIRouter(
|
| 11 |
+
prefix="/image",
|
| 12 |
+
tags=["Image Processing"]
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# ==========================================
|
| 16 |
+
# π Auth Toggle
|
| 17 |
+
# ==========================================
|
| 18 |
+
AUTH_ENABLED = False # <<< set False to disable authentication
|
| 19 |
+
|
| 20 |
+
# Logic to conditionally apply authentication
|
| 21 |
+
# If True, the endpoint will require a valid token.
|
| 22 |
+
# If False, the dependency list is empty.
|
| 23 |
+
security_dependencies = [Depends(get_current_user)] if AUTH_ENABLED else []
|
| 24 |
+
|
| 25 |
+
@router.post("/remove-bg", dependencies=security_dependencies)
|
| 26 |
+
async def remove_background(file: UploadFile = File(...)):
|
| 27 |
+
"""
|
| 28 |
+
Upload an image and receive it back with the background removed (PNG).
|
| 29 |
+
"""
|
| 30 |
+
# 1. Validate file type
|
| 31 |
+
if not file.content_type.startswith("image/"):
|
| 32 |
+
raise HTTPException(status_code=400, detail="Invalid file type. Please upload an image.")
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# 2. Read image data
|
| 36 |
+
image_data = await file.read()
|
| 37 |
+
|
| 38 |
+
# 3. Process image using rembg
|
| 39 |
+
# We pass the raw bytes to remove() and it returns bytes (PNG)
|
| 40 |
+
output_data = remove(image_data)
|
| 41 |
+
|
| 42 |
+
# 4. Return the result as a raw image response
|
| 43 |
+
return Response(content=output_data, media_type="image/png")
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
raise HTTPException(status_code=500, detail=f"Image processing failed: {str(e)}")
|
requirements.txt
CHANGED
|
@@ -45,6 +45,8 @@ httpx # Async HTTP client
|
|
| 45 |
# πΌ IMAGE PROCESSING
|
| 46 |
# =========================================
|
| 47 |
Pillow # Image compression/manipulation
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# =========================================
|
| 50 |
# π PDF HANDLING
|
|
@@ -62,7 +64,7 @@ aiofiles # Async file write/read
|
|
| 62 |
boto3 # AWS S3 storage
|
| 63 |
minio # Local S3-compatible storage
|
| 64 |
google-cloud-storage # Google Cloud storage support
|
| 65 |
-
hf #huggingface-cli
|
| 66 |
|
| 67 |
# =========================================
|
| 68 |
# β‘ CACHING / QUEUES / BACKGROUND JOBS
|
|
|
|
| 45 |
# πΌ IMAGE PROCESSING
|
| 46 |
# =========================================
|
| 47 |
Pillow # Image compression/manipulation
|
| 48 |
+
rembg # AI Background removal tools
|
| 49 |
+
onnxruntime # Required runtime for rembg (or onnxruntime-gpu)
|
| 50 |
|
| 51 |
# =========================================
|
| 52 |
# π PDF HANDLING
|
|
|
|
| 64 |
boto3 # AWS S3 storage
|
| 65 |
minio # Local S3-compatible storage
|
| 66 |
google-cloud-storage # Google Cloud storage support
|
| 67 |
+
hf # OPTIONAL huggingface-cli for deployment purposes
|
| 68 |
|
| 69 |
# =========================================
|
| 70 |
# β‘ CACHING / QUEUES / BACKGROUND JOBS
|