Ochank commited on
Commit
569d4a7
·
verified ·
1 Parent(s): a8192de

Upload main.py

Browse files

from 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")

Files changed (1) hide show
  1. main.py +24 -0
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
+