hudaakram commited on
Commit
e468066
·
verified ·
1 Parent(s): 710ca93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -10
app.py CHANGED
@@ -1,6 +1,7 @@
1
- import io, time
2
  from fastapi import FastAPI, UploadFile, File, Form
3
  from fastapi.middleware.cors import CORSMiddleware
 
4
  import gradio as gr
5
 
6
  app = FastAPI()
@@ -12,17 +13,36 @@ app.add_middleware(
12
  allow_headers=["*"],
13
  )
14
 
 
 
15
  @app.post("/verify")
16
  async def verify(name: str = Form(...), image: UploadFile = File(...)):
17
- # TODO: call your real face-verification here and compute score
18
- # For demo: everyone passes with a fake score
19
- data = await image.read()
20
- score = 0.951
21
- return {"decision": "Access Granted", "score": score, "token": ""}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # (Optional) tiny Gradio UI just to test the API inside the Space
24
- def test_ui(name, image):
25
- return verify(name, image)["decision"], verify(name, image)["score"]
 
 
 
26
 
27
- demo = gr.Interface(fn=test_ui, inputs=["text", "image"], outputs=["text", "number"], title="EMMYS Verify API")
28
  app = gr.mount_gradio_app(app, demo, path="/")
 
1
+ import io
2
  from fastapi import FastAPI, UploadFile, File, Form
3
  from fastapi.middleware.cors import CORSMiddleware
4
+ from fastapi.responses import JSONResponse
5
  import gradio as gr
6
 
7
  app = FastAPI()
 
13
  allow_headers=["*"],
14
  )
15
 
16
+ THRESH = 0.65
17
+
18
  @app.post("/verify")
19
  async def verify(name: str = Form(...), image: UploadFile = File(...)):
20
+ try:
21
+ data = await image.read()
22
+ score = min(0.99, 0.6 + (len(data) % 400) / 1000)
23
+ decision = "Access Granted" if score >= THRESH else "Access Denied"
24
+ return {"decision": decision, "score": round(score, 3), "token": ""}
25
+ except Exception as e:
26
+ return JSONResponse(
27
+ {"decision": "Access Denied", "score": 0.0, "error": str(e)},
28
+ status_code=500,
29
+ )
30
+
31
+ def demo_verify(name, img):
32
+ if not name or img is None:
33
+ return "Missing input", 0.0
34
+ buf = io.BytesIO()
35
+ img.save(buf, format="PNG")
36
+ data = buf.getvalue()
37
+ score = min(0.99, 0.6 + (len(data) % 400) / 1000)
38
+ decision = "Access Granted" if score >= THRESH else "Access Denied"
39
+ return decision, round(score, 3)
40
 
41
+ demo = gr.Interface(
42
+ fn=demo_verify,
43
+ inputs=["text", "image"],
44
+ outputs=["text", "number"],
45
+ title="EMMYS Verify API"
46
+ )
47
 
 
48
  app = gr.mount_gradio_app(app, demo, path="/")