ilhamdev commited on
Commit
5ee72d2
·
1 Parent(s): 926bbda

feat: add Next.js frontend for Vercel + enable Gradio API on HuggingFace

Browse files
app.py CHANGED
@@ -1,6 +1,10 @@
 
1
  import gradio as gr
 
2
  import spaces
 
3
  import numpy as np
 
4
  from PIL import Image
5
  from rembg import remove, new_session
6
 
@@ -59,4 +63,7 @@ demo = gr.Interface(
59
  )
60
 
61
  if __name__ == "__main__":
62
- demo.launch()
 
 
 
 
1
+ # pyrefly: ignore [missing-import]
2
  import gradio as gr
3
+ # pyrefly: ignore [missing-import]
4
  import spaces
5
+ # pyrefly: ignore [missing-import]
6
  import numpy as np
7
+ # pyrefly: ignore [missing-import]
8
  from PIL import Image
9
  from rembg import remove, new_session
10
 
 
63
  )
64
 
65
  if __name__ == "__main__":
66
+ demo.launch(
67
+ server_name="0.0.0.0",
68
+ show_api=True,
69
+ )
frontend/.gitignore ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dependencies
2
+ node_modules/
3
+ /.pnp
4
+ .pnp.js
5
+
6
+ # Production
7
+ .next/
8
+ out/
9
+ build/
10
+
11
+ # Environment
12
+ .env
13
+ .env.local
14
+ .env.development.local
15
+ .env.test.local
16
+ .env.production.local
17
+
18
+ # Misc
19
+ .DS_Store
20
+ *.pem
21
+ npm-debug.log*
22
+ yarn-debug.log*
23
+ yarn-error.log*
24
+ .vercel
frontend/next.config.js ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /** @type {import('next').NextConfig} */
2
+ const nextConfig = {
3
+ // Izinkan gambar dari HuggingFace (jika nanti perlu next/image)
4
+ images: {
5
+ remotePatterns: [
6
+ {
7
+ protocol: 'https',
8
+ hostname: 'ilhamdev-rembg.hf.space',
9
+ },
10
+ ],
11
+ },
12
+ }
13
+
14
+ module.exports = nextConfig
frontend/package.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "rembg-frontend",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "scripts": {
6
+ "dev": "next dev",
7
+ "build": "next build",
8
+ "start": "next start"
9
+ },
10
+ "dependencies": {
11
+ "next": "14.2.5",
12
+ "react": "^18",
13
+ "react-dom": "^18"
14
+ }
15
+ }
frontend/src/app/api/remove-bg/route.js ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * API Route: /api/remove-bg
3
+ * Proxy ke HuggingFace Gradio API untuk menghindari CORS issue dari browser.
4
+ * Frontend mengirim form-data berisi image, lalu route ini meneruskan ke HF.
5
+ */
6
+
7
+ const HF_SPACE_URL = process.env.NEXT_PUBLIC_HF_SPACE_URL || 'https://ilhamdev-rembg.hf.space'
8
+
9
+ /**
10
+ * Konversi ArrayBuffer image ke base64 string untuk Gradio API
11
+ */
12
+ function bufferToBase64(buffer) {
13
+ return Buffer.from(buffer).toString('base64')
14
+ }
15
+
16
+ export async function POST(request) {
17
+ try {
18
+ const formData = await request.formData()
19
+ const imageFile = formData.get('image')
20
+ const modelName = formData.get('model_name') || 'birefnet-portrait'
21
+ const alphaMatting = formData.get('alpha_matting') === 'true'
22
+
23
+ if (!imageFile) {
24
+ return Response.json({ error: 'No image provided' }, { status: 400 })
25
+ }
26
+
27
+ // Konversi File ke ArrayBuffer → base64
28
+ const arrayBuffer = await imageFile.arrayBuffer()
29
+ const base64 = bufferToBase64(arrayBuffer)
30
+ const mimeType = imageFile.type || 'image/png'
31
+ const dataUrl = `data:${mimeType};base64,${base64}`
32
+
33
+ // Panggil Gradio API - format sesuai Gradio v4+ (/call/ SSE atau /api/ langsung)
34
+ // Gunakan /api/predict untuk single call (lebih simpel dari SSE)
35
+ const gradioPayload = {
36
+ data: [
37
+ { path: dataUrl, mime_type: mimeType, orig_name: imageFile.name || 'image.png' },
38
+ modelName,
39
+ alphaMatting,
40
+ ]
41
+ }
42
+
43
+ // Langkah 1: POST ke /call/remove_bg untuk dapat event_id
44
+ const callRes = await fetch(`${HF_SPACE_URL}/call/remove_bg`, {
45
+ method: 'POST',
46
+ headers: { 'Content-Type': 'application/json' },
47
+ body: JSON.stringify(gradioPayload),
48
+ })
49
+
50
+ if (!callRes.ok) {
51
+ const errText = await callRes.text()
52
+ console.error('[HF API Error]', callRes.status, errText)
53
+ return Response.json({ error: `HuggingFace API error: ${callRes.status}` }, { status: 502 })
54
+ }
55
+
56
+ const { event_id } = await callRes.json()
57
+
58
+ // Langkah 2: GET /call/remove_bg/{event_id} untuk ambil hasil (SSE stream)
59
+ const resultRes = await fetch(`${HF_SPACE_URL}/call/remove_bg/${event_id}`, {
60
+ headers: { Accept: 'text/event-stream' },
61
+ })
62
+
63
+ if (!resultRes.ok) {
64
+ return Response.json({ error: 'Failed to get result from HuggingFace' }, { status: 502 })
65
+ }
66
+
67
+ // Parse SSE response
68
+ const resultText = await resultRes.text()
69
+ const lines = resultText.split('\n')
70
+ let resultData = null
71
+
72
+ for (const line of lines) {
73
+ if (line.startsWith('data: ')) {
74
+ try {
75
+ const parsed = JSON.parse(line.slice(6))
76
+ if (Array.isArray(parsed) && parsed.length > 0) {
77
+ resultData = parsed[0]
78
+ }
79
+ } catch {
80
+ // skip non-JSON lines
81
+ }
82
+ }
83
+ }
84
+
85
+ if (!resultData) {
86
+ return Response.json({ error: 'No result data from HuggingFace' }, { status: 502 })
87
+ }
88
+
89
+ // resultData bisa berupa { path, url } atau string base64
90
+ // Kembalikan URL atau base64 ke frontend
91
+ return Response.json({ success: true, result: resultData })
92
+
93
+ } catch (error) {
94
+ console.error('[API Error]', error)
95
+ return Response.json({ error: error.message }, { status: 500 })
96
+ }
97
+ }
frontend/src/app/globals.css ADDED
@@ -0,0 +1,673 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* ===== GLOBAL CSS — RemBG Frontend ===== */
2
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap');
3
+
4
+ :root {
5
+ /* Color palette — deep dark with violet accent */
6
+ --bg-base: #09090b;
7
+ --bg-surface: #111113;
8
+ --bg-card: #18181b;
9
+ --bg-card-hover: #1f1f23;
10
+ --border: rgba(255, 255, 255, 0.08);
11
+ --border-accent: rgba(139, 92, 246, 0.4);
12
+
13
+ --text-primary: #fafafa;
14
+ --text-secondary: #a1a1aa;
15
+ --text-muted: #52525b;
16
+
17
+ --accent: #8b5cf6;
18
+ --accent-light: #a78bfa;
19
+ --accent-dark: #7c3aed;
20
+ --accent-glow: rgba(139, 92, 246, 0.25);
21
+
22
+ --success: #22c55e;
23
+ --error: #ef4444;
24
+ --warning: #f59e0b;
25
+
26
+ --radius-sm: 8px;
27
+ --radius-md: 12px;
28
+ --radius-lg: 16px;
29
+ --radius-xl: 24px;
30
+
31
+ --shadow-card: 0 4px 24px rgba(0, 0, 0, 0.4);
32
+ --shadow-glow: 0 0 40px rgba(139, 92, 246, 0.15);
33
+ }
34
+
35
+ *, *::before, *::after {
36
+ box-sizing: border-box;
37
+ margin: 0;
38
+ padding: 0;
39
+ }
40
+
41
+ html {
42
+ scroll-behavior: smooth;
43
+ }
44
+
45
+ body {
46
+ font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
47
+ background-color: var(--bg-base);
48
+ color: var(--text-primary);
49
+ min-height: 100vh;
50
+ line-height: 1.6;
51
+ -webkit-font-smoothing: antialiased;
52
+ }
53
+
54
+ /* ===== LAYOUT ===== */
55
+ .app-wrapper {
56
+ min-height: 100vh;
57
+ background:
58
+ radial-gradient(ellipse 80% 60% at 50% -20%, rgba(139, 92, 246, 0.12) 0%, transparent 70%),
59
+ radial-gradient(ellipse 60% 40% at 80% 80%, rgba(59, 130, 246, 0.06) 0%, transparent 60%),
60
+ var(--bg-base);
61
+ }
62
+
63
+ .container {
64
+ max-width: 1100px;
65
+ margin: 0 auto;
66
+ padding: 0 24px;
67
+ }
68
+
69
+ /* ===== HEADER ===== */
70
+ .header {
71
+ padding: 24px 0;
72
+ border-bottom: 1px solid var(--border);
73
+ backdrop-filter: blur(12px);
74
+ background: rgba(9, 9, 11, 0.8);
75
+ position: sticky;
76
+ top: 0;
77
+ z-index: 100;
78
+ }
79
+
80
+ .header-inner {
81
+ display: flex;
82
+ align-items: center;
83
+ justify-content: space-between;
84
+ }
85
+
86
+ .logo {
87
+ display: flex;
88
+ align-items: center;
89
+ gap: 10px;
90
+ text-decoration: none;
91
+ }
92
+
93
+ .logo-icon {
94
+ width: 36px;
95
+ height: 36px;
96
+ background: linear-gradient(135deg, var(--accent), #3b82f6);
97
+ border-radius: var(--radius-sm);
98
+ display: flex;
99
+ align-items: center;
100
+ justify-content: center;
101
+ font-size: 18px;
102
+ box-shadow: 0 0 20px var(--accent-glow);
103
+ }
104
+
105
+ .logo-text {
106
+ font-size: 20px;
107
+ font-weight: 700;
108
+ background: linear-gradient(135deg, var(--text-primary), var(--accent-light));
109
+ -webkit-background-clip: text;
110
+ -webkit-text-fill-color: transparent;
111
+ background-clip: text;
112
+ }
113
+
114
+ .header-badge {
115
+ display: flex;
116
+ align-items: center;
117
+ gap: 6px;
118
+ padding: 6px 12px;
119
+ background: rgba(139, 92, 246, 0.1);
120
+ border: 1px solid var(--border-accent);
121
+ border-radius: 100px;
122
+ font-size: 12px;
123
+ color: var(--accent-light);
124
+ font-weight: 500;
125
+ }
126
+
127
+ .badge-dot {
128
+ width: 6px;
129
+ height: 6px;
130
+ background: var(--success);
131
+ border-radius: 50%;
132
+ animation: pulse 2s infinite;
133
+ }
134
+
135
+ @keyframes pulse {
136
+ 0%, 100% { opacity: 1; transform: scale(1); }
137
+ 50% { opacity: 0.5; transform: scale(0.8); }
138
+ }
139
+
140
+ /* ===== HERO ===== */
141
+ .hero {
142
+ padding: 72px 0 48px;
143
+ text-align: center;
144
+ }
145
+
146
+ .hero-label {
147
+ display: inline-flex;
148
+ align-items: center;
149
+ gap: 6px;
150
+ padding: 6px 14px;
151
+ background: rgba(139, 92, 246, 0.08);
152
+ border: 1px solid var(--border-accent);
153
+ border-radius: 100px;
154
+ font-size: 12px;
155
+ color: var(--accent-light);
156
+ font-weight: 500;
157
+ margin-bottom: 24px;
158
+ }
159
+
160
+ .hero h1 {
161
+ font-size: clamp(36px, 6vw, 64px);
162
+ font-weight: 800;
163
+ line-height: 1.1;
164
+ letter-spacing: -0.03em;
165
+ margin-bottom: 20px;
166
+ }
167
+
168
+ .hero h1 span {
169
+ background: linear-gradient(135deg, var(--accent-light) 0%, #60a5fa 100%);
170
+ -webkit-background-clip: text;
171
+ -webkit-text-fill-color: transparent;
172
+ background-clip: text;
173
+ }
174
+
175
+ .hero-desc {
176
+ font-size: 18px;
177
+ color: var(--text-secondary);
178
+ max-width: 560px;
179
+ margin: 0 auto 40px;
180
+ line-height: 1.7;
181
+ }
182
+
183
+ /* ===== MAIN CARD ===== */
184
+ .main-card {
185
+ background: var(--bg-card);
186
+ border: 1px solid var(--border);
187
+ border-radius: var(--radius-xl);
188
+ padding: 32px;
189
+ margin-bottom: 24px;
190
+ box-shadow: var(--shadow-card);
191
+ transition: border-color 0.3s ease;
192
+ }
193
+
194
+ .main-card:hover {
195
+ border-color: rgba(139, 92, 246, 0.2);
196
+ }
197
+
198
+ /* ===== SETTINGS ROW ===== */
199
+ .settings-row {
200
+ display: grid;
201
+ grid-template-columns: 1fr auto;
202
+ gap: 16px;
203
+ align-items: center;
204
+ margin-bottom: 24px;
205
+ }
206
+
207
+ .field-label {
208
+ font-size: 13px;
209
+ font-weight: 500;
210
+ color: var(--text-secondary);
211
+ margin-bottom: 8px;
212
+ text-transform: uppercase;
213
+ letter-spacing: 0.05em;
214
+ }
215
+
216
+ .select-wrapper {
217
+ position: relative;
218
+ }
219
+
220
+ .select-wrapper select {
221
+ width: 100%;
222
+ padding: 10px 36px 10px 14px;
223
+ background: var(--bg-surface);
224
+ border: 1px solid var(--border);
225
+ border-radius: var(--radius-sm);
226
+ color: var(--text-primary);
227
+ font-size: 14px;
228
+ font-family: inherit;
229
+ appearance: none;
230
+ cursor: pointer;
231
+ transition: border-color 0.2s, box-shadow 0.2s;
232
+ outline: none;
233
+ }
234
+
235
+ .select-wrapper select:focus {
236
+ border-color: var(--accent);
237
+ box-shadow: 0 0 0 3px var(--accent-glow);
238
+ }
239
+
240
+ .select-arrow {
241
+ position: absolute;
242
+ right: 10px;
243
+ top: 50%;
244
+ transform: translateY(-50%);
245
+ color: var(--text-muted);
246
+ pointer-events: none;
247
+ }
248
+
249
+ .toggle-group {
250
+ display: flex;
251
+ align-items: center;
252
+ gap: 10px;
253
+ }
254
+
255
+ .toggle-label {
256
+ font-size: 14px;
257
+ color: var(--text-secondary);
258
+ white-space: nowrap;
259
+ }
260
+
261
+ /* Toggle switch */
262
+ .toggle {
263
+ position: relative;
264
+ width: 44px;
265
+ height: 24px;
266
+ cursor: pointer;
267
+ }
268
+
269
+ .toggle input {
270
+ opacity: 0;
271
+ width: 0;
272
+ height: 0;
273
+ position: absolute;
274
+ }
275
+
276
+ .toggle-track {
277
+ position: absolute;
278
+ inset: 0;
279
+ background: var(--bg-surface);
280
+ border: 1px solid var(--border);
281
+ border-radius: 100px;
282
+ transition: background 0.3s, border-color 0.3s;
283
+ }
284
+
285
+ .toggle input:checked + .toggle-track {
286
+ background: var(--accent);
287
+ border-color: var(--accent);
288
+ }
289
+
290
+ .toggle-thumb {
291
+ position: absolute;
292
+ top: 3px;
293
+ left: 3px;
294
+ width: 16px;
295
+ height: 16px;
296
+ background: white;
297
+ border-radius: 50%;
298
+ transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
299
+ box-shadow: 0 1px 4px rgba(0,0,0,0.4);
300
+ }
301
+
302
+ .toggle input:checked ~ .toggle-thumb {
303
+ transform: translateX(20px);
304
+ }
305
+
306
+ /* ===== UPLOAD ZONE ===== */
307
+ .upload-zone {
308
+ border: 2px dashed var(--border);
309
+ border-radius: var(--radius-lg);
310
+ padding: 48px 24px;
311
+ text-align: center;
312
+ cursor: pointer;
313
+ transition: all 0.3s ease;
314
+ position: relative;
315
+ background: var(--bg-surface);
316
+ overflow: hidden;
317
+ }
318
+
319
+ .upload-zone::before {
320
+ content: '';
321
+ position: absolute;
322
+ inset: 0;
323
+ background: radial-gradient(ellipse at center, rgba(139, 92, 246, 0.05) 0%, transparent 70%);
324
+ opacity: 0;
325
+ transition: opacity 0.3s;
326
+ }
327
+
328
+ .upload-zone:hover::before,
329
+ .upload-zone.dragover::before {
330
+ opacity: 1;
331
+ }
332
+
333
+ .upload-zone:hover,
334
+ .upload-zone.dragover {
335
+ border-color: var(--accent);
336
+ background: rgba(139, 92, 246, 0.04);
337
+ transform: scale(1.005);
338
+ }
339
+
340
+ .upload-zone.has-image {
341
+ border-style: solid;
342
+ border-color: var(--border-accent);
343
+ padding: 0;
344
+ }
345
+
346
+ .upload-icon {
347
+ font-size: 48px;
348
+ margin-bottom: 16px;
349
+ display: block;
350
+ filter: drop-shadow(0 0 20px rgba(139, 92, 246, 0.4));
351
+ }
352
+
353
+ .upload-title {
354
+ font-size: 18px;
355
+ font-weight: 600;
356
+ color: var(--text-primary);
357
+ margin-bottom: 8px;
358
+ }
359
+
360
+ .upload-sub {
361
+ font-size: 14px;
362
+ color: var(--text-muted);
363
+ }
364
+
365
+ .upload-sub span {
366
+ color: var(--accent-light);
367
+ }
368
+
369
+ #file-input {
370
+ display: none;
371
+ }
372
+
373
+ /* Preview image */
374
+ .preview-image {
375
+ width: 100%;
376
+ max-height: 400px;
377
+ object-fit: contain;
378
+ border-radius: calc(var(--radius-lg) - 2px);
379
+ display: block;
380
+ }
381
+
382
+ /* ===== REMOVE BUTTON ===== */
383
+ .btn-remove {
384
+ width: 100%;
385
+ padding: 16px 32px;
386
+ background: linear-gradient(135deg, var(--accent), var(--accent-dark));
387
+ border: none;
388
+ border-radius: var(--radius-md);
389
+ color: white;
390
+ font-size: 16px;
391
+ font-weight: 600;
392
+ font-family: inherit;
393
+ cursor: pointer;
394
+ display: flex;
395
+ align-items: center;
396
+ justify-content: center;
397
+ gap: 10px;
398
+ transition: all 0.3s ease;
399
+ position: relative;
400
+ overflow: hidden;
401
+ box-shadow: 0 4px 24px rgba(139, 92, 246, 0.3);
402
+ }
403
+
404
+ .btn-remove::before {
405
+ content: '';
406
+ position: absolute;
407
+ inset: 0;
408
+ background: linear-gradient(135deg, rgba(255,255,255,0.1), transparent);
409
+ opacity: 0;
410
+ transition: opacity 0.3s;
411
+ }
412
+
413
+ .btn-remove:hover:not(:disabled)::before {
414
+ opacity: 1;
415
+ }
416
+
417
+ .btn-remove:hover:not(:disabled) {
418
+ transform: translateY(-2px);
419
+ box-shadow: 0 8px 32px rgba(139, 92, 246, 0.45);
420
+ }
421
+
422
+ .btn-remove:active:not(:disabled) {
423
+ transform: translateY(0);
424
+ }
425
+
426
+ .btn-remove:disabled {
427
+ opacity: 0.5;
428
+ cursor: not-allowed;
429
+ }
430
+
431
+ /* Loading spinner */
432
+ .spinner {
433
+ width: 20px;
434
+ height: 20px;
435
+ border: 2px solid rgba(255,255,255,0.3);
436
+ border-top-color: white;
437
+ border-radius: 50%;
438
+ animation: spin 0.8s linear infinite;
439
+ }
440
+
441
+ @keyframes spin {
442
+ to { transform: rotate(360deg); }
443
+ }
444
+
445
+ /* ===== RESULT SECTION ===== */
446
+ .result-section {
447
+ margin-top: 24px;
448
+ }
449
+
450
+ .section-title {
451
+ font-size: 14px;
452
+ font-weight: 600;
453
+ color: var(--text-secondary);
454
+ text-transform: uppercase;
455
+ letter-spacing: 0.05em;
456
+ margin-bottom: 16px;
457
+ display: flex;
458
+ align-items: center;
459
+ gap: 8px;
460
+ }
461
+
462
+ .result-card {
463
+ background: var(--bg-card);
464
+ border: 1px solid var(--border);
465
+ border-radius: var(--radius-xl);
466
+ overflow: hidden;
467
+ box-shadow: var(--shadow-card);
468
+ }
469
+
470
+ /* Checkerboard background untuk transparan */
471
+ .result-canvas {
472
+ background-image:
473
+ linear-gradient(45deg, #2a2a2e 25%, transparent 25%),
474
+ linear-gradient(-45deg, #2a2a2e 25%, transparent 25%),
475
+ linear-gradient(45deg, transparent 75%, #2a2a2e 75%),
476
+ linear-gradient(-45deg, transparent 75%, #2a2a2e 75%);
477
+ background-size: 20px 20px;
478
+ background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
479
+ background-color: #1e1e22;
480
+ padding: 16px;
481
+ display: flex;
482
+ align-items: center;
483
+ justify-content: center;
484
+ min-height: 300px;
485
+ }
486
+
487
+ .result-img {
488
+ max-width: 100%;
489
+ max-height: 500px;
490
+ object-fit: contain;
491
+ border-radius: var(--radius-md);
492
+ animation: fadeIn 0.5s ease;
493
+ }
494
+
495
+ @keyframes fadeIn {
496
+ from { opacity: 0; transform: scale(0.97); }
497
+ to { opacity: 1; transform: scale(1); }
498
+ }
499
+
500
+ .result-actions {
501
+ padding: 16px 20px;
502
+ border-top: 1px solid var(--border);
503
+ display: flex;
504
+ gap: 12px;
505
+ background: var(--bg-surface);
506
+ }
507
+
508
+ .btn-download {
509
+ flex: 1;
510
+ padding: 12px;
511
+ background: linear-gradient(135deg, var(--accent), var(--accent-dark));
512
+ border: none;
513
+ border-radius: var(--radius-sm);
514
+ color: white;
515
+ font-size: 14px;
516
+ font-weight: 600;
517
+ font-family: inherit;
518
+ cursor: pointer;
519
+ display: flex;
520
+ align-items: center;
521
+ justify-content: center;
522
+ gap: 8px;
523
+ text-decoration: none;
524
+ transition: all 0.2s ease;
525
+ box-shadow: 0 4px 16px rgba(139, 92, 246, 0.3);
526
+ }
527
+
528
+ .btn-download:hover {
529
+ transform: translateY(-1px);
530
+ box-shadow: 0 6px 24px rgba(139, 92, 246, 0.4);
531
+ }
532
+
533
+ .btn-reset {
534
+ padding: 12px 20px;
535
+ background: transparent;
536
+ border: 1px solid var(--border);
537
+ border-radius: var(--radius-sm);
538
+ color: var(--text-secondary);
539
+ font-size: 14px;
540
+ font-weight: 500;
541
+ font-family: inherit;
542
+ cursor: pointer;
543
+ display: flex;
544
+ align-items: center;
545
+ gap: 8px;
546
+ transition: all 0.2s ease;
547
+ }
548
+
549
+ .btn-reset:hover {
550
+ border-color: var(--border-accent);
551
+ color: var(--accent-light);
552
+ background: rgba(139, 92, 246, 0.05);
553
+ }
554
+
555
+ /* ===== ERROR MESSAGE ===== */
556
+ .error-msg {
557
+ margin-top: 12px;
558
+ padding: 12px 16px;
559
+ background: rgba(239, 68, 68, 0.08);
560
+ border: 1px solid rgba(239, 68, 68, 0.25);
561
+ border-radius: var(--radius-sm);
562
+ color: #fca5a5;
563
+ font-size: 14px;
564
+ display: flex;
565
+ align-items: flex-start;
566
+ gap: 8px;
567
+ animation: fadeIn 0.3s ease;
568
+ }
569
+
570
+ /* ===== FEATURES GRID ===== */
571
+ .features {
572
+ padding: 64px 0;
573
+ }
574
+
575
+ .features-grid {
576
+ display: grid;
577
+ grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
578
+ gap: 16px;
579
+ }
580
+
581
+ .feature-card {
582
+ padding: 24px;
583
+ background: var(--bg-card);
584
+ border: 1px solid var(--border);
585
+ border-radius: var(--radius-lg);
586
+ transition: all 0.3s ease;
587
+ }
588
+
589
+ .feature-card:hover {
590
+ border-color: var(--border-accent);
591
+ transform: translateY(-2px);
592
+ box-shadow: 0 8px 32px rgba(0,0,0,0.3), var(--shadow-glow);
593
+ }
594
+
595
+ .feature-icon {
596
+ font-size: 28px;
597
+ margin-bottom: 16px;
598
+ display: block;
599
+ }
600
+
601
+ .feature-title {
602
+ font-size: 16px;
603
+ font-weight: 600;
604
+ margin-bottom: 8px;
605
+ color: var(--text-primary);
606
+ }
607
+
608
+ .feature-desc {
609
+ font-size: 14px;
610
+ color: var(--text-secondary);
611
+ line-height: 1.6;
612
+ }
613
+
614
+ /* ===== FOOTER ===== */
615
+ .footer {
616
+ padding: 32px 0;
617
+ border-top: 1px solid var(--border);
618
+ text-align: center;
619
+ }
620
+
621
+ .footer-text {
622
+ font-size: 14px;
623
+ color: var(--text-muted);
624
+ }
625
+
626
+ .footer-text a {
627
+ color: var(--accent-light);
628
+ text-decoration: none;
629
+ transition: color 0.2s;
630
+ }
631
+
632
+ .footer-text a:hover {
633
+ color: var(--accent);
634
+ }
635
+
636
+ /* ===== PROGRESS BAR ===== */
637
+ .progress-bar {
638
+ height: 3px;
639
+ background: var(--bg-surface);
640
+ border-radius: 100px;
641
+ margin-top: 16px;
642
+ overflow: hidden;
643
+ }
644
+
645
+ .progress-fill {
646
+ height: 100%;
647
+ background: linear-gradient(90deg, var(--accent), #60a5fa);
648
+ border-radius: 100px;
649
+ width: 0%;
650
+ transition: width 0.5s ease;
651
+ animation: shimmer 1.5s infinite;
652
+ }
653
+
654
+ @keyframes shimmer {
655
+ 0% { background-position: -200px 0; }
656
+ 100% { background-position: 200px 0; }
657
+ }
658
+
659
+ /* ===== RESPONSIVE ===== */
660
+ @media (max-width: 640px) {
661
+ .settings-row {
662
+ grid-template-columns: 1fr;
663
+ }
664
+ .hero {
665
+ padding: 48px 0 32px;
666
+ }
667
+ .main-card {
668
+ padding: 20px;
669
+ }
670
+ .result-actions {
671
+ flex-direction: column;
672
+ }
673
+ }
frontend/src/app/layout.js ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Inter } from 'next/font/google'
2
+ import './globals.css'
3
+
4
+ const inter = Inter({ subsets: ['latin'] })
5
+
6
+ export const metadata = {
7
+ title: 'RemBG — AI Background Remover',
8
+ description: 'Hapus background foto secara otomatis menggunakan AI. Powered by HuggingFace ZeroGPU & rembg.',
9
+ keywords: 'remove background, hapus background, AI, rembg, transparan',
10
+ openGraph: {
11
+ title: 'RemBG — AI Background Remover',
12
+ description: 'Hapus background foto otomatis dengan AI. Gratis, cepat, presisi tinggi.',
13
+ type: 'website',
14
+ },
15
+ }
16
+
17
+ export default function RootLayout({ children }) {
18
+ return (
19
+ <html lang="id">
20
+ <body className={inter.className}>{children}</body>
21
+ </html>
22
+ )
23
+ }
frontend/src/app/page.js ADDED
@@ -0,0 +1,358 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 'use client'
2
+
3
+ import { useState, useRef, useCallback } from 'react'
4
+ import './globals.css'
5
+
6
+ const MODELS = [
7
+ { value: 'birefnet-portrait', label: '👤 BiRefNet Portrait — Terbaik untuk orang' },
8
+ { value: 'birefnet-general', label: '📦 BiRefNet General — Terbaik untuk objek/produk' },
9
+ { value: 'isnet-general-use', label: '⚡ ISNet General — Seimbang: cepat & bagus' },
10
+ { value: 'u2net_human_seg', label: '🚀 U2Net Human — Ringan untuk orang' },
11
+ ]
12
+
13
+ export default function Home() {
14
+ const [previewUrl, setPreviewUrl] = useState(null)
15
+ const [resultUrl, setResultUrl] = useState(null)
16
+ const [imageFile, setImageFile] = useState(null)
17
+ const [model, setModel] = useState('birefnet-portrait')
18
+ const [alphaMatting, setAlphaMatting] = useState(false)
19
+ const [loading, setLoading] = useState(false)
20
+ const [error, setError] = useState(null)
21
+ const [progress, setProgress] = useState(0)
22
+ const [dragover, setDragover] = useState(false)
23
+
24
+ const fileInputRef = useRef(null)
25
+
26
+ const handleFile = useCallback((file) => {
27
+ if (!file || !file.type.startsWith('image/')) {
28
+ setError('File harus berupa gambar (JPG, PNG, WEBP)')
29
+ return
30
+ }
31
+ if (file.size > 20 * 1024 * 1024) {
32
+ setError('Ukuran file maksimal 20MB')
33
+ return
34
+ }
35
+ setError(null)
36
+ setResultUrl(null)
37
+ setImageFile(file)
38
+ const url = URL.createObjectURL(file)
39
+ setPreviewUrl(url)
40
+ }, [])
41
+
42
+ const handleDrop = useCallback((e) => {
43
+ e.preventDefault()
44
+ setDragover(false)
45
+ const file = e.dataTransfer.files[0]
46
+ handleFile(file)
47
+ }, [handleFile])
48
+
49
+ const handleDragOver = (e) => {
50
+ e.preventDefault()
51
+ setDragover(true)
52
+ }
53
+
54
+ const handleDragLeave = () => setDragover(false)
55
+
56
+ const handleFileInput = (e) => {
57
+ handleFile(e.target.files[0])
58
+ }
59
+
60
+ const handleRemoveBg = async () => {
61
+ if (!imageFile) return
62
+
63
+ setLoading(true)
64
+ setError(null)
65
+ setResultUrl(null)
66
+ setProgress(0)
67
+
68
+ // Simulasi progress
69
+ const progressInterval = setInterval(() => {
70
+ setProgress(prev => Math.min(prev + Math.random() * 15, 85))
71
+ }, 800)
72
+
73
+ try {
74
+ const formData = new FormData()
75
+ formData.append('image', imageFile)
76
+ formData.append('model_name', model)
77
+ formData.append('alpha_matting', alphaMatting.toString())
78
+
79
+ const res = await fetch('/api/remove-bg', {
80
+ method: 'POST',
81
+ body: formData,
82
+ })
83
+
84
+ clearInterval(progressInterval)
85
+ setProgress(95)
86
+
87
+ const data = await res.json()
88
+
89
+ if (!res.ok || !data.success) {
90
+ throw new Error(data.error || 'Terjadi kesalahan pada server')
91
+ }
92
+
93
+ // result bisa berupa { url } atau { path } atau string
94
+ let imgUrl
95
+ if (typeof data.result === 'string') {
96
+ imgUrl = data.result
97
+ } else if (data.result?.url) {
98
+ imgUrl = data.result.url
99
+ } else if (data.result?.path) {
100
+ // HF returns relative path, construct full URL
101
+ const hfBase = process.env.NEXT_PUBLIC_HF_SPACE_URL || 'https://ilhamdev-rembg.hf.space'
102
+ imgUrl = `${hfBase}/file=${data.result.path}`
103
+ }
104
+
105
+ if (!imgUrl) throw new Error('Tidak ada URL gambar hasil')
106
+
107
+ // Fetch gambar sebagai blob untuk download lokal
108
+ const imgRes = await fetch(imgUrl)
109
+ const blob = await imgRes.blob()
110
+ const localUrl = URL.createObjectURL(blob)
111
+
112
+ setProgress(100)
113
+ setResultUrl(localUrl)
114
+ } catch (err) {
115
+ clearInterval(progressInterval)
116
+ setError(err.message)
117
+ } finally {
118
+ setLoading(false)
119
+ setTimeout(() => setProgress(0), 1000)
120
+ }
121
+ }
122
+
123
+ const handleReset = () => {
124
+ setPreviewUrl(null)
125
+ setResultUrl(null)
126
+ setImageFile(null)
127
+ setError(null)
128
+ setProgress(0)
129
+ if (fileInputRef.current) fileInputRef.current.value = ''
130
+ }
131
+
132
+ const handleDownload = () => {
133
+ if (!resultUrl) return
134
+ const a = document.createElement('a')
135
+ a.href = resultUrl
136
+ a.download = `rembg_${imageFile?.name?.replace(/\.[^.]+$/, '') || 'result'}.png`
137
+ a.click()
138
+ }
139
+
140
+ return (
141
+ <div className="app-wrapper">
142
+ {/* Header */}
143
+ <header className="header">
144
+ <div className="container">
145
+ <div className="header-inner">
146
+ <a className="logo" href="/">
147
+ <div className="logo-icon">✂️</div>
148
+ <span className="logo-text">RemBG</span>
149
+ </a>
150
+ <div className="header-badge">
151
+ <div className="badge-dot" />
152
+ Powered by ZeroGPU
153
+ </div>
154
+ </div>
155
+ </div>
156
+ </header>
157
+
158
+ {/* Hero */}
159
+ <section className="hero">
160
+ <div className="container">
161
+ <div className="hero-label">
162
+ 🤖 AI Background Removal
163
+ </div>
164
+ <h1>
165
+ Hapus Background Foto<br />
166
+ <span>Secara Otomatis</span>
167
+ </h1>
168
+ <p className="hero-desc">
169
+ Upload foto, pilih model AI terbaik, dan dapatkan hasil dengan background transparan dalam hitungan detik. Gratis, presisi tinggi, tanpa watermark.
170
+ </p>
171
+ </div>
172
+ </section>
173
+
174
+ {/* Main Tool */}
175
+ <main>
176
+ <div className="container">
177
+ <div className="main-card">
178
+ {/* Settings */}
179
+ <div className="settings-row">
180
+ <div>
181
+ <div className="field-label">Model AI</div>
182
+ <div className="select-wrapper">
183
+ <select
184
+ id="model-select"
185
+ value={model}
186
+ onChange={e => setModel(e.target.value)}
187
+ disabled={loading}
188
+ >
189
+ {MODELS.map(m => (
190
+ <option key={m.value} value={m.value}>{m.label}</option>
191
+ ))}
192
+ </select>
193
+ <span className="select-arrow">▾</span>
194
+ </div>
195
+ </div>
196
+ <div>
197
+ <div className="field-label">Alpha Matting</div>
198
+ <div className="toggle-group">
199
+ <label className="toggle">
200
+ <input
201
+ id="alpha-toggle"
202
+ type="checkbox"
203
+ checked={alphaMatting}
204
+ onChange={e => setAlphaMatting(e.target.checked)}
205
+ disabled={loading}
206
+ />
207
+ <div className="toggle-track" />
208
+ <div className="toggle-thumb" />
209
+ </label>
210
+ <span className="toggle-label">
211
+ {alphaMatting ? '✅ Aktif' : 'Haluskan edge rambut'}
212
+ </span>
213
+ </div>
214
+ </div>
215
+ </div>
216
+
217
+ {/* Upload Zone */}
218
+ <input
219
+ ref={fileInputRef}
220
+ id="file-input"
221
+ type="file"
222
+ accept="image/*"
223
+ onChange={handleFileInput}
224
+ />
225
+ <div
226
+ className={`upload-zone ${dragover ? 'dragover' : ''} ${previewUrl ? 'has-image' : ''}`}
227
+ onClick={() => !previewUrl && fileInputRef.current?.click()}
228
+ onDrop={handleDrop}
229
+ onDragOver={handleDragOver}
230
+ onDragLeave={handleDragLeave}
231
+ id="upload-dropzone"
232
+ >
233
+ {previewUrl ? (
234
+ <img src={previewUrl} alt="Preview" className="preview-image" />
235
+ ) : (
236
+ <>
237
+ <span className="upload-icon">🖼️</span>
238
+ <div className="upload-title">Drop gambar di sini atau klik untuk upload</div>
239
+ <div className="upload-sub">
240
+ Mendukung <span>JPG, PNG, WEBP</span> · Maks <span>20MB</span>
241
+ </div>
242
+ </>
243
+ )}
244
+ </div>
245
+
246
+ {/* Progress Bar */}
247
+ {loading && (
248
+ <div className="progress-bar">
249
+ <div className="progress-fill" style={{ width: `${progress}%` }} />
250
+ </div>
251
+ )}
252
+
253
+ {/* Error */}
254
+ {error && (
255
+ <div className="error-msg" id="error-message">
256
+ ⚠️ {error}
257
+ </div>
258
+ )}
259
+
260
+ {/* Action Buttons */}
261
+ <div style={{ marginTop: '20px', display: 'flex', gap: '12px' }}>
262
+ <button
263
+ id="remove-bg-btn"
264
+ className="btn-remove"
265
+ onClick={handleRemoveBg}
266
+ disabled={!imageFile || loading}
267
+ >
268
+ {loading ? (
269
+ <>
270
+ <div className="spinner" />
271
+ Memproses dengan AI...
272
+ </>
273
+ ) : (
274
+ <>
275
+ ✂️ Hapus Background
276
+ </>
277
+ )}
278
+ </button>
279
+ {previewUrl && (
280
+ <button
281
+ id="reset-btn"
282
+ className="btn-reset"
283
+ onClick={handleReset}
284
+ style={{ minWidth: 'auto', padding: '12px 16px' }}
285
+ >
286
+ 🔄
287
+ </button>
288
+ )}
289
+ </div>
290
+ </div>
291
+
292
+ {/* Result */}
293
+ {resultUrl && (
294
+ <div className="result-section" id="result-section">
295
+ <div className="section-title">
296
+ ✅ Hasil Background Removed
297
+ </div>
298
+ <div className="result-card">
299
+ <div className="result-canvas">
300
+ <img src={resultUrl} alt="Hasil remove background" className="result-img" id="result-image" />
301
+ </div>
302
+ <div className="result-actions">
303
+ <button
304
+ id="download-btn"
305
+ className="btn-download"
306
+ onClick={handleDownload}
307
+ >
308
+ ���️ Download PNG Transparan
309
+ </button>
310
+ <button
311
+ id="try-again-btn"
312
+ className="btn-reset"
313
+ onClick={handleReset}
314
+ >
315
+ 🔄 Coba Lagi
316
+ </button>
317
+ </div>
318
+ </div>
319
+ </div>
320
+ )}
321
+ </div>
322
+ </main>
323
+
324
+ {/* Features */}
325
+ <section className="features">
326
+ <div className="container">
327
+ <div className="features-grid">
328
+ {[
329
+ { icon: '🤖', title: 'Powered by ZeroGPU', desc: 'Berjalan di HuggingFace ZeroGPU — gratis, cepat, dan tanpa antrian.' },
330
+ { icon: '🎯', title: '4 Model AI', desc: 'Pilih model terbaik sesuai kebutuhan: portrait, produk, atau objek umum.' },
331
+ { icon: '💎', title: 'Kualitas HD', desc: 'Hasilkan PNG transparan berkualitas tinggi tanpa kompresi.' },
332
+ { icon: '🔒', title: 'Privasi Terjaga', desc: 'Gambar tidak disimpan. Diproses langsung dan langsung dibuang.' },
333
+ ].map((f) => (
334
+ <div key={f.title} className="feature-card">
335
+ <span className="feature-icon">{f.icon}</span>
336
+ <div className="feature-title">{f.title}</div>
337
+ <div className="feature-desc">{f.desc}</div>
338
+ </div>
339
+ ))}
340
+ </div>
341
+ </div>
342
+ </section>
343
+
344
+ {/* Footer */}
345
+ <footer className="footer">
346
+ <div className="container">
347
+ <p className="footer-text">
348
+ © 2026 RemBG · Backend:{' '}
349
+ <a href="https://huggingface.co/spaces/ilhamdev/rembg" target="_blank" rel="noopener">
350
+ HuggingFace Space ilhamdev/rembg
351
+ </a>{' '}
352
+ · Dibuat dengan ❤️
353
+ </p>
354
+ </div>
355
+ </footer>
356
+ </div>
357
+ )
358
+ }
frontend/vercel.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "framework": "nextjs",
3
+ "buildCommand": "npm run build",
4
+ "devCommand": "npm run dev",
5
+ "installCommand": "npm install",
6
+ "outputDirectory": ".next"
7
+ }