Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,37 @@
|
|
| 1 |
-
import io
|
| 2 |
import gradio as gr
|
| 3 |
import spaces
|
| 4 |
-
from fastapi import FastAPI, File, UploadFile
|
| 5 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
-
from fastapi.responses import Response
|
| 7 |
from transparent_background import Remover
|
| 8 |
from PIL import Image
|
|
|
|
| 9 |
|
| 10 |
-
# Inisialisasi
|
| 11 |
remover = Remover(mode='base')
|
| 12 |
|
| 13 |
-
@spaces.GPU(duration=
|
| 14 |
-
def remove_bg(img
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# -
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
demo = gr.Interface(
|
| 20 |
fn=remove_bg,
|
| 21 |
inputs=gr.Image(type="pil", label="Upload Gambar"),
|
| 22 |
outputs=gr.Image(type="pil", label="Hasil (PNG Transparan)"),
|
| 23 |
title="Remove Background API",
|
| 24 |
-
description="Backend
|
| 25 |
-
|
| 26 |
-
cache_examples=False,
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
# ---------- 2) REST API FASTAPI (untuk frontend Vercel) ----------
|
| 30 |
-
app = FastAPI()
|
| 31 |
-
|
| 32 |
-
app.add_middleware(
|
| 33 |
-
CORSMiddleware,
|
| 34 |
-
allow_origins=["*"],
|
| 35 |
-
allow_methods=["*"],
|
| 36 |
-
allow_headers=["*"],
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
@app.post("/remove-bg")
|
| 44 |
-
async def api_remove_bg(file: UploadFile = File(...)):
|
| 45 |
-
data = await file.read()
|
| 46 |
-
input_img = Image.open(io.BytesIO(data)).convert("RGB")
|
| 47 |
-
output_img = remove_bg(input_img)
|
| 48 |
-
buf = io.BytesIO()
|
| 49 |
-
output_img.save(buf, format="PNG")
|
| 50 |
-
return Response(content=buf.getvalue(), media_type="image/png")
|
| 51 |
-
|
| 52 |
-
# Gabungkan Gradio UI + FastAPI
|
| 53 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
|
|
|
|
|
|
|
|
|
| 3 |
from transparent_background import Remover
|
| 4 |
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
+
# Inisialisasi model saat startup (akan otomatis pakai GPU di ZeroGPU)
|
| 8 |
remover = Remover(mode='base')
|
| 9 |
|
| 10 |
+
@spaces.GPU(duration=60) # Alokasi GPU selama 60 detik per request
|
| 11 |
+
def remove_bg(img):
|
| 12 |
+
if img is None:
|
| 13 |
+
return None
|
| 14 |
+
|
| 15 |
+
# transparent-background butuh format RGB
|
| 16 |
+
if isinstance(img, np.ndarray):
|
| 17 |
+
img = Image.fromarray(img)
|
| 18 |
+
|
| 19 |
+
img_rgb = img.convert("RGB")
|
| 20 |
+
|
| 21 |
+
# Proses hapus background (kembalikan PNG transparan)
|
| 22 |
+
output = remover.process(img_rgb, type='rgba')
|
| 23 |
+
return output
|
| 24 |
+
|
| 25 |
+
# Buat Interface Gradio Murni
|
| 26 |
demo = gr.Interface(
|
| 27 |
fn=remove_bg,
|
| 28 |
inputs=gr.Image(type="pil", label="Upload Gambar"),
|
| 29 |
outputs=gr.Image(type="pil", label="Hasil (PNG Transparan)"),
|
| 30 |
title="Remove Background API",
|
| 31 |
+
description="Backend untuk Vercel. Endpoint API: /api/remove_bg",
|
| 32 |
+
api_name="remove_bg" # PENTING: Ini membuat endpoint API otomatis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
# Jangan pakai FastAPI, biarkan Gradio berjalan native
|
| 37 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|