File size: 1,147 Bytes
437844c
 
 
88a2a7d
 
 
 
437844c
 
 
 
 
 
 
 
 
 
 
88a2a7d
437844c
 
 
88a2a7d
437844c
 
 
88a2a7d
 
 
 
 
 
 
437844c
0e9f065
88a2a7d
 
 
 
0e9f065
88a2a7d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
Entrypoint untuk Hugging Face Spaces (SDK Gradio / ZeroGPU gratis).

Space gratis hanya menyediakan ZeroGPU, yang mensyaratkan Gradio dijalankan lewat
demo.launch() dan ada fungsi @spaces.GPU. Fungsi GPU itu tidak pernah dipanggil
(kuota GPU tidak terpakai); ekstraksi wajah jalan di CPU lewat endpoint Gradio
`extract`, yang dipanggil Laravel via /gradio_api/call/extract.
"""
import gradio as gr

try:
    import spaces

    gpu = spaces.GPU
except ImportError:  # menjalankan di luar Hugging Face
    def gpu(fn):
        return fn

from main import embed_from_url, key_ok


@gpu
def ping() -> str:
    return "ok"


def extract(image_url: str, key: str) -> dict:
    if not key_ok(key):
        return {"error": "Face key tidak valid."}
    try:
        return {"embedding": embed_from_url(image_url)}
    except ValueError as e:
        return {"error": str(e)}


with gr.Blocks(title="Face Embedding Service") as demo:
    gr.Markdown("Face Embedding Service - API: `extract`")
    gr.api(extract, api_name="extract")
    gr.api(ping, api_name="ping")

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)