Andi Syahkty commited on
Commit
2c12ddb
·
2 Parent(s): 7efb1f6 e435887

Merge pull request #1 from syahkty/codex/explain-codebase-structure-and-essentials

Browse files
Files changed (3) hide show
  1. README.md +13 -0
  2. app.py +12 -24
  3. requirements.txt +2 -0
README.md CHANGED
@@ -7,4 +7,17 @@ sdk: docker
7
  pinned: false
8
  ---
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
7
  pinned: false
8
  ---
9
 
10
+ Service ini menjalankan FastAPI yang memanfaatkan model Hugging Face untuk
11
+ menghapus background gambar.
12
+
13
+ ## Endpoint
14
+
15
+ `POST /removebg` dengan field multipart `image` akan mengembalikan hasil dalam
16
+ format `image/png` dengan background transparan.
17
+
18
+ ## Konfigurasi
19
+
20
+ - `MODEL_ID` (opsional): ID model di Hub, default `briaai/RMBG-1.4`
21
+ - `HF_TOKEN` (opsional): token akses jika model memerlukan autentikasi
22
+
23
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,12 +1,12 @@
 
1
  import os
2
  from fastapi import FastAPI, UploadFile, File, Response
3
- import httpx
4
 
5
- # Endpoint Space tujuan (bisa dioverride via Space variable TARGET_URL)
6
- TARGET_URL = os.getenv(
7
- "TARGET_URL",
8
- "https://not-lain-background-removal.hf.space/image"
9
- )
10
 
11
  app = FastAPI(title="Background Removal Proxy")
12
 
@@ -15,7 +15,7 @@ async def root():
15
  return {
16
  "ok": True,
17
  "message": "POST /removebg with multipart field 'image' → returns image/png",
18
- "target": TARGET_URL,
19
  }
20
 
21
  @app.get("/healthz")
@@ -24,20 +24,8 @@ async def health():
24
 
25
  @app.post("/removebg")
26
  async def removebg(image: UploadFile = File(...)):
27
- timeout = httpx.Timeout(60.0, connect=20.0)
28
- async with httpx.AsyncClient(timeout=timeout, follow_redirects=True) as client:
29
- files = {
30
- # Space target menerima field bernama 'image'
31
- "image": (image.filename, await image.read(), image.content_type or "image/jpeg")
32
- }
33
-
34
- # retry ringan untuk cold start/gateway
35
- for attempt in range(2):
36
- r = await client.post(TARGET_URL, files=files)
37
- if r.status_code == 200:
38
- return Response(content=r.content, media_type="image/png")
39
- if r.status_code in (502, 503, 504) and attempt == 0:
40
- continue
41
- return Response(content=r.text, media_type="application/json", status_code=r.status_code)
42
-
43
- return Response(content='{"detail":"unknown error"}', media_type="application/json", status_code=500)
 
1
+ import io
2
  import os
3
  from fastapi import FastAPI, UploadFile, File, Response
4
+ from huggingface_hub import AsyncInferenceClient
5
 
6
+ # Model Hugging Face untuk penghapusan background
7
+ MODEL_ID = os.getenv("MODEL_ID", "briaai/RMBG-1.4")
8
+ HF_TOKEN = os.getenv("HF_TOKEN")
9
+ client = AsyncInferenceClient(model=MODEL_ID, token=HF_TOKEN)
 
10
 
11
  app = FastAPI(title="Background Removal Proxy")
12
 
 
15
  return {
16
  "ok": True,
17
  "message": "POST /removebg with multipart field 'image' → returns image/png",
18
+ "model": MODEL_ID,
19
  }
20
 
21
  @app.get("/healthz")
 
24
 
25
  @app.post("/removebg")
26
  async def removebg(image: UploadFile = File(...)):
27
+ img_bytes = await image.read()
28
+ result = await client.image_to_image(img_bytes, model=MODEL_ID)
29
+ buf = io.BytesIO()
30
+ result.save(buf, format="PNG")
31
+ return Response(content=buf.getvalue(), media_type="image/png")
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -2,3 +2,5 @@ fastapi==0.110.3
2
  uvicorn==0.30.1
3
  httpx==0.27.2
4
  python-multipart==0.0.9
 
 
 
2
  uvicorn==0.30.1
3
  httpx==0.27.2
4
  python-multipart==0.0.9
5
+ huggingface-hub==0.34.4
6
+ Pillow==11.3.0