jinksQspider commited on
Commit
963b7b1
Β·
unverified Β·
1 Parent(s): da14275

Upload 5 files

Browse files
Files changed (3) hide show
  1. Dockerfile +1 -0
  2. app.py +17 -1
  3. static/index.html +430 -0
Dockerfile CHANGED
@@ -22,6 +22,7 @@ RUN pip install --no-cache-dir -r requirements.txt
22
 
23
  # Copy application code
24
  COPY app.py .
 
25
 
26
  # Set environment variables
27
  ENV FACE_RECOGNITION_MODEL="buffalo_l"
 
22
 
23
  # Copy application code
24
  COPY app.py .
25
+ COPY static/ ./static/
26
 
27
  # Set environment variables
28
  ENV FACE_RECOGNITION_MODEL="buffalo_l"
app.py CHANGED
@@ -4,7 +4,8 @@ Extracts face embeddings and matches against known encodings
4
  """
5
 
6
  from fastapi import FastAPI, HTTPException, UploadFile, File, Form
7
- from fastapi.responses import JSONResponse
 
8
  from contextlib import asynccontextmanager
9
  import numpy as np
10
  import io
@@ -43,6 +44,21 @@ async def lifespan(app: FastAPI):
43
 
44
  app = FastAPI(title="Face Inference Service", version="1.0", lifespan=lifespan)
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  @app.get("/health")
47
  async def health_check():
48
  """Health check endpoint"""
 
4
  """
5
 
6
  from fastapi import FastAPI, HTTPException, UploadFile, File, Form
7
+ from fastapi.responses import JSONResponse, HTMLResponse, FileResponse
8
+ from fastapi.staticfiles import StaticFiles
9
  from contextlib import asynccontextmanager
10
  import numpy as np
11
  import io
 
44
 
45
  app = FastAPI(title="Face Inference Service", version="1.0", lifespan=lifespan)
46
 
47
+ # Mount static files
48
+ static_dir = os.path.join(os.path.dirname(__file__), "static")
49
+ if os.path.exists(static_dir):
50
+ app.mount("/static", StaticFiles(directory=static_dir), name="static")
51
+ logger.info(f"βœ“ Static files mounted from {static_dir}")
52
+
53
+ @app.get("/", response_class=HTMLResponse)
54
+ async def root():
55
+ """Serve the web UI"""
56
+ html_path = os.path.join(os.path.dirname(__file__), "static", "index.html")
57
+ if os.path.exists(html_path):
58
+ with open(html_path, "r", encoding="utf-8") as f:
59
+ return f.read()
60
+ return "<h1>Face Inference Service</h1><p>API is running. Visit /docs for API documentation.</p>"
61
+
62
  @app.get("/health")
63
  async def health_check():
64
  """Health check endpoint"""
static/index.html ADDED
@@ -0,0 +1,430 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Face Inference Service</title>
7
+ <style>
8
+ * {
9
+ margin: 0;
10
+ padding: 0;
11
+ box-sizing: border-box;
12
+ }
13
+
14
+ body {
15
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
16
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
17
+ min-height: 100vh;
18
+ padding: 20px;
19
+ color: #333;
20
+ }
21
+
22
+ .container {
23
+ max-width: 900px;
24
+ margin: 0 auto;
25
+ }
26
+
27
+ header {
28
+ text-align: center;
29
+ color: white;
30
+ margin-bottom: 30px;
31
+ }
32
+
33
+ header h1 {
34
+ font-size: 2.5rem;
35
+ margin-bottom: 10px;
36
+ text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
37
+ }
38
+
39
+ header p {
40
+ font-size: 1.1rem;
41
+ opacity: 0.95;
42
+ }
43
+
44
+ .card {
45
+ background: white;
46
+ border-radius: 15px;
47
+ padding: 30px;
48
+ box-shadow: 0 10px 30px rgba(0,0,0,0.3);
49
+ margin-bottom: 20px;
50
+ }
51
+
52
+ .upload-section {
53
+ text-align: center;
54
+ margin-bottom: 30px;
55
+ }
56
+
57
+ .upload-area {
58
+ border: 3px dashed #667eea;
59
+ border-radius: 10px;
60
+ padding: 40px;
61
+ transition: all 0.3s ease;
62
+ cursor: pointer;
63
+ background: #f8f9ff;
64
+ }
65
+
66
+ .upload-area:hover {
67
+ border-color: #764ba2;
68
+ background: #f0f1ff;
69
+ }
70
+
71
+ .upload-area.dragover {
72
+ background: #e8e9ff;
73
+ border-color: #764ba2;
74
+ }
75
+
76
+ .upload-icon {
77
+ font-size: 3rem;
78
+ margin-bottom: 15px;
79
+ }
80
+
81
+ input[type="file"] {
82
+ display: none;
83
+ }
84
+
85
+ .btn {
86
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
87
+ color: white;
88
+ border: none;
89
+ padding: 12px 30px;
90
+ border-radius: 25px;
91
+ font-size: 1rem;
92
+ cursor: pointer;
93
+ transition: transform 0.2s;
94
+ margin: 5px;
95
+ box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
96
+ }
97
+
98
+ .btn:hover {
99
+ transform: translateY(-2px);
100
+ box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
101
+ }
102
+
103
+ .btn:active {
104
+ transform: translateY(0);
105
+ }
106
+
107
+ .btn:disabled {
108
+ opacity: 0.5;
109
+ cursor: not-allowed;
110
+ }
111
+
112
+ .preview-section {
113
+ margin: 20px 0;
114
+ text-align: center;
115
+ }
116
+
117
+ .preview-image {
118
+ max-width: 100%;
119
+ max-height: 400px;
120
+ border-radius: 10px;
121
+ box-shadow: 0 5px 15px rgba(0,0,0,0.2);
122
+ display: none;
123
+ }
124
+
125
+ .results-section {
126
+ margin-top: 30px;
127
+ }
128
+
129
+ .result-item {
130
+ background: #f8f9ff;
131
+ padding: 15px;
132
+ border-radius: 8px;
133
+ margin: 10px 0;
134
+ border-left: 4px solid #667eea;
135
+ }
136
+
137
+ .result-item h4 {
138
+ color: #667eea;
139
+ margin-bottom: 8px;
140
+ }
141
+
142
+ .result-item pre {
143
+ background: white;
144
+ padding: 12px;
145
+ border-radius: 5px;
146
+ overflow-x: auto;
147
+ font-size: 0.85rem;
148
+ max-height: 300px;
149
+ overflow-y: auto;
150
+ }
151
+
152
+ .status {
153
+ padding: 8px 15px;
154
+ border-radius: 20px;
155
+ display: inline-block;
156
+ font-size: 0.9rem;
157
+ margin: 10px 0;
158
+ }
159
+
160
+ .status.success {
161
+ background: #d4edda;
162
+ color: #155724;
163
+ }
164
+
165
+ .status.error {
166
+ background: #f8d7da;
167
+ color: #721c24;
168
+ }
169
+
170
+ .status.info {
171
+ background: #d1ecf1;
172
+ color: #0c5460;
173
+ }
174
+
175
+ .loading {
176
+ display: none;
177
+ text-align: center;
178
+ margin: 20px 0;
179
+ }
180
+
181
+ .spinner {
182
+ border: 4px solid #f3f3f3;
183
+ border-top: 4px solid #667eea;
184
+ border-radius: 50%;
185
+ width: 40px;
186
+ height: 40px;
187
+ animation: spin 1s linear infinite;
188
+ margin: 0 auto;
189
+ }
190
+
191
+ @keyframes spin {
192
+ 0% { transform: rotate(0deg); }
193
+ 100% { transform: rotate(360deg); }
194
+ }
195
+
196
+ .api-docs {
197
+ background: #f8f9ff;
198
+ padding: 20px;
199
+ border-radius: 10px;
200
+ margin-top: 20px;
201
+ }
202
+
203
+ .api-docs h3 {
204
+ color: #667eea;
205
+ margin-bottom: 15px;
206
+ }
207
+
208
+ .endpoint {
209
+ background: white;
210
+ padding: 12px;
211
+ border-radius: 5px;
212
+ margin: 10px 0;
213
+ font-family: 'Courier New', monospace;
214
+ font-size: 0.9rem;
215
+ }
216
+
217
+ .endpoint .method {
218
+ color: #28a745;
219
+ font-weight: bold;
220
+ margin-right: 10px;
221
+ }
222
+
223
+ footer {
224
+ text-align: center;
225
+ color: white;
226
+ margin-top: 30px;
227
+ opacity: 0.9;
228
+ }
229
+ </style>
230
+ </head>
231
+ <body>
232
+ <div class="container">
233
+ <header>
234
+ <h1>🎭 Face Inference Service</h1>
235
+ <p>AI-Powered Face Detection & Recognition API</p>
236
+ </header>
237
+
238
+ <div class="card">
239
+ <div class="upload-section">
240
+ <h2 style="margin-bottom: 20px; color: #667eea;">Test Face Detection</h2>
241
+ <div class="upload-area" id="uploadArea">
242
+ <div class="upload-icon">πŸ“Έ</div>
243
+ <h3>Click or Drop Image Here</h3>
244
+ <p style="color: #666; margin-top: 10px;">Supported formats: JPG, PNG</p>
245
+ <input type="file" id="fileInput" accept="image/*">
246
+ </div>
247
+ </div>
248
+
249
+ <div class="preview-section">
250
+ <img id="previewImage" class="preview-image" alt="Preview">
251
+ </div>
252
+
253
+ <div style="text-align: center; margin: 20px 0;">
254
+ <button class="btn" id="testEmbeddingBtn" disabled>πŸ” Extract Face Embedding</button>
255
+ <button class="btn" id="clearBtn">πŸ—‘οΈ Clear</button>
256
+ </div>
257
+
258
+ <div class="loading" id="loading">
259
+ <div class="spinner"></div>
260
+ <p style="margin-top: 10px; color: #667eea;">Processing...</p>
261
+ </div>
262
+
263
+ <div class="results-section" id="results" style="display: none;">
264
+ <h3 style="color: #667eea; margin-bottom: 15px;">Results</h3>
265
+ <div id="resultContent"></div>
266
+ </div>
267
+ </div>
268
+
269
+ <div class="card api-docs">
270
+ <h3>πŸ“š API Endpoints</h3>
271
+ <div class="endpoint">
272
+ <span class="method">GET</span> /health
273
+ <p style="color: #666; margin-top: 5px; font-family: 'Segoe UI';">Health check endpoint</p>
274
+ </div>
275
+ <div class="endpoint">
276
+ <span class="method">POST</span> /get_embedding
277
+ <p style="color: #666; margin-top: 5px; font-family: 'Segoe UI';">Extract face embedding from image</p>
278
+ </div>
279
+ <div class="endpoint">
280
+ <span class="method">POST</span> /recognize
281
+ <p style="color: #666; margin-top: 5px; font-family: 'Segoe UI';">Recognize faces against known encodings</p>
282
+ </div>
283
+ <div class="endpoint">
284
+ <span class="method">POST</span> /recognize_batch
285
+ <p style="color: #666; margin-top: 5px; font-family: 'Segoe UI';">Batch recognition for multiple frames</p>
286
+ </div>
287
+ </div>
288
+
289
+ <footer>
290
+ <p>Built with FastAPI + InsightFace | Running on Hugging Face Spaces πŸš€</p>
291
+ </footer>
292
+ </div>
293
+
294
+ <script>
295
+ const uploadArea = document.getElementById('uploadArea');
296
+ const fileInput = document.getElementById('fileInput');
297
+ const previewImage = document.getElementById('previewImage');
298
+ const testEmbeddingBtn = document.getElementById('testEmbeddingBtn');
299
+ const clearBtn = document.getElementById('clearBtn');
300
+ const loading = document.getElementById('loading');
301
+ const results = document.getElementById('results');
302
+ const resultContent = document.getElementById('resultContent');
303
+
304
+ let selectedFile = null;
305
+
306
+ // Click to upload
307
+ uploadArea.addEventListener('click', () => fileInput.click());
308
+
309
+ // File input change
310
+ fileInput.addEventListener('change', (e) => {
311
+ handleFile(e.target.files[0]);
312
+ });
313
+
314
+ // Drag and drop
315
+ uploadArea.addEventListener('dragover', (e) => {
316
+ e.preventDefault();
317
+ uploadArea.classList.add('dragover');
318
+ });
319
+
320
+ uploadArea.addEventListener('dragleave', () => {
321
+ uploadArea.classList.remove('dragover');
322
+ });
323
+
324
+ uploadArea.addEventListener('drop', (e) => {
325
+ e.preventDefault();
326
+ uploadArea.classList.remove('dragover');
327
+ handleFile(e.dataTransfer.files[0]);
328
+ });
329
+
330
+ function handleFile(file) {
331
+ if (!file || !file.type.startsWith('image/')) {
332
+ alert('Please select a valid image file');
333
+ return;
334
+ }
335
+
336
+ selectedFile = file;
337
+ const reader = new FileReader();
338
+
339
+ reader.onload = (e) => {
340
+ previewImage.src = e.target.result;
341
+ previewImage.style.display = 'block';
342
+ testEmbeddingBtn.disabled = false;
343
+ results.style.display = 'none';
344
+ };
345
+
346
+ reader.readAsDataURL(file);
347
+ }
348
+
349
+ // Test embedding extraction
350
+ testEmbeddingBtn.addEventListener('click', async () => {
351
+ if (!selectedFile) return;
352
+
353
+ const reader = new FileReader();
354
+ reader.onload = async (e) => {
355
+ const base64Data = e.target.result;
356
+
357
+ loading.style.display = 'block';
358
+ results.style.display = 'none';
359
+ testEmbeddingBtn.disabled = true;
360
+
361
+ try {
362
+ const formData = new FormData();
363
+ formData.append('image_data', base64Data);
364
+
365
+ const response = await fetch('/get_embedding', {
366
+ method: 'POST',
367
+ body: formData
368
+ });
369
+
370
+ const data = await response.json();
371
+
372
+ loading.style.display = 'none';
373
+ results.style.display = 'block';
374
+ testEmbeddingBtn.disabled = false;
375
+
376
+ if (data.success) {
377
+ resultContent.innerHTML = `
378
+ <div class="status success">βœ“ Face Detected Successfully</div>
379
+ <div class="result-item">
380
+ <h4>Detection Info</h4>
381
+ <p><strong>Faces Detected:</strong> ${data.faces_detected}</p>
382
+ <p><strong>Embedding Dimensions:</strong> ${data.embedding.length}</p>
383
+ </div>
384
+ <div class="result-item">
385
+ <h4>Face Embedding Vector (first 10 values)</h4>
386
+ <pre>${JSON.stringify(data.embedding.slice(0, 10), null, 2)}...</pre>
387
+ </div>
388
+ `;
389
+ } else {
390
+ resultContent.innerHTML = `
391
+ <div class="status error">βœ— ${data.detail || 'Processing failed'}</div>
392
+ `;
393
+ }
394
+ } catch (error) {
395
+ loading.style.display = 'none';
396
+ results.style.display = 'block';
397
+ testEmbeddingBtn.disabled = false;
398
+ resultContent.innerHTML = `
399
+ <div class="status error">βœ— Error: ${error.message}</div>
400
+ `;
401
+ }
402
+ };
403
+
404
+ reader.readAsDataURL(selectedFile);
405
+ });
406
+
407
+ // Clear
408
+ clearBtn.addEventListener('click', () => {
409
+ selectedFile = null;
410
+ fileInput.value = '';
411
+ previewImage.style.display = 'none';
412
+ previewImage.src = '';
413
+ testEmbeddingBtn.disabled = true;
414
+ results.style.display = 'none';
415
+ loading.style.display = 'none';
416
+ });
417
+
418
+ // Check service health on load
419
+ window.addEventListener('load', async () => {
420
+ try {
421
+ const response = await fetch('/health');
422
+ const data = await response.json();
423
+ console.log('Service health:', data);
424
+ } catch (error) {
425
+ console.error('Health check failed:', error);
426
+ }
427
+ });
428
+ </script>
429
+ </body>
430
+ </html>