Spaces:
Running
Running
File size: 3,021 Bytes
2ce5586 6e68bb5 8254d65 2ce5586 8254d65 252f0e0 4076444 2ce5586 4076444 fed3d8e 8254d65 fed3d8e 8254d65 2ce5586 252f0e0 8254d65 252f0e0 8254d65 252f0e0 8254d65 252f0e0 4536e71 8254d65 4536e71 8254d65 4536e71 8254d65 fed3d8e 8254d65 4536e71 2ce5586 8254d65 671ba32 8254d65 2ce5586 4536e71 8254d65 4536e71 4218e9b 8254d65 671ba32 4218e9b 8254d65 5497583 8254d65 148a81d 4536e71 6e68bb5 8254d65 2ce5586 8254d65 2ce5586 4536e71 fed3d8e 4076444 2ce5586 8254d65 2ce5586 8254d65 4218e9b 8254d65 4218e9b fed3d8e 4076444 fed3d8e 4218e9b 2ce5586 8254d65 2ce5586 8254d65 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | import requests
import json
import uuid
from fastapi import FastAPI, UploadFile, File, Query
from fastapi.responses import JSONResponse, Response
from fastapi.middleware.cors import CORSMiddleware
from rembg import remove
import uvicorn
# --------------------------
# Temp Memory Storage
# --------------------------
TEMP_FILES = {}
# --------------------------
# Pretty JSON Response
# --------------------------
class PrettyJSONResponse(JSONResponse):
def render(self, content) -> bytes:
return json.dumps(content, indent=4, ensure_ascii=False).encode("utf-8")
app = FastAPI(
title="AI ClearCut - HF UltraFast BG Remover",
default_response_class=PrettyJSONResponse
)
# --------------------------
# CORS
# --------------------------
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# --------------------------
# Root
# --------------------------
@app.get("/")
async def root():
return {
"message": "HF UltraFast Background Remover Running 🚀",
"endpoints": {
"POST /upload": "Upload image",
"GET /json?url=": "Image from URL",
"GET /file/{id}": "Get processed image"
},
"creator": "Jerrycoder"
}
# --------------------------
# Serve temp file
# --------------------------
@app.get("/file/{file_id:path}")
async def get_file(file_id: str):
file_data = TEMP_FILES.get(file_id)
if not file_data:
return {"status": "error", "message": "File expired"}
return Response(content=file_data, media_type="image/png")
# --------------------------
# Process image
# --------------------------
def process_image(img_bytes: bytes):
try:
# Remove background
output_bytes = remove(img_bytes)
# Generate unique ID
file_ext = "png" # default
file_id = f"{uuid.uuid4()}.{file_ext}"
file_url = f"https://jerrycoder-rembg-as.hf.space/file/{file_id}"
# Store temporarily
TEMP_FILES[file_id] = output_bytes
return {
"status": "success",
"url": file_url,
"full_url": file_url,
"creator": "Jerrycoder"
}
except Exception as e:
return {"status": "error", "message": str(e)}
# --------------------------
# POST upload
# --------------------------
@app.post("/upload")
async def upload_file(file: UploadFile = File(...)):
img_bytes = await file.read()
return process_image(img_bytes)
# --------------------------
# GET from URL
# --------------------------
@app.get("/json")
async def from_url(url: str = Query(...)):
try:
resp = requests.get(url, timeout=10)
resp.raise_for_status()
return process_image(resp.content)
except Exception as e:
return {"status": "error", "message": str(e)}
# --------------------------
# Run
# --------------------------
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) |