Spaces:
Sleeping
Sleeping
Upload main.py
Browse filesfrom fastapi import FastAPI, UploadFile, File
from rembg import remove
from io import BytesIO
from starlette.responses import Response
app = FastAPI()
@app
.get("/")
def home():
return {"message": "Halo! API di Hugging Face siap digunakan."}
@app
.post("/hapus-background")
async def hapus_background(file: UploadFile = File(...)):
gambar_asli = await file.read()
gambar_bersih = remove(gambar_asli)
return Response(content=gambar_bersih, media_type="image/png")
main.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from rembg import remove
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from starlette.responses import Response
|
| 5 |
+
|
| 6 |
+
# Membuat aplikasi
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
@app.get("/")
|
| 10 |
+
def home():
|
| 11 |
+
return {"message": "Halo! API Penghapus Background siap digunakan."}
|
| 12 |
+
|
| 13 |
+
# Ini fitur utamanya
|
| 14 |
+
@app.post("/hapus-background")
|
| 15 |
+
async def hapus_background(file: UploadFile = File(...)):
|
| 16 |
+
# 1. Baca gambar yang diupload user
|
| 17 |
+
gambar_asli = await file.read()
|
| 18 |
+
|
| 19 |
+
# 2. Proses hapus background pakai rembg
|
| 20 |
+
gambar_bersih = remove(gambar_asli)
|
| 21 |
+
|
| 22 |
+
# 3. Kembalikan hasilnya sebagai gambar PNG
|
| 23 |
+
return Response(content=gambar_bersih, media_type="image/png")
|
| 24 |
+
|