Spaces:
Sleeping
Sleeping
kadabengaran
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
2 |
-
from fastapi.responses import JSONResponse, HTMLResponse
|
3 |
import gradio as gr
|
4 |
from deepface import DeepFace
|
5 |
import os
|
6 |
from threading import Thread
|
7 |
-
import
|
8 |
|
9 |
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
|
10 |
|
@@ -36,21 +36,6 @@ def face_verification_uii(img1, img2, dist="cosine", model="Facenet", detector="
|
|
36 |
except Exception as e:
|
37 |
return {"error": str(e)}
|
38 |
|
39 |
-
# FastAPI Endpoint
|
40 |
-
@app.get("/", response_class=HTMLResponse)
|
41 |
-
async def gradio_ui():
|
42 |
-
html_content = """
|
43 |
-
<html>
|
44 |
-
<head>
|
45 |
-
<title>Gradio UI</title>
|
46 |
-
</head>
|
47 |
-
<body>
|
48 |
-
<iframe src="http://0.0.0.0:7861" width="100%" height="100%" frameborder="0"></iframe>
|
49 |
-
</body>
|
50 |
-
</html>
|
51 |
-
"""
|
52 |
-
return HTMLResponse(content=html_content)
|
53 |
-
|
54 |
@app.post("/face_verification")
|
55 |
async def face_verification(
|
56 |
img1: UploadFile = File(...),
|
@@ -104,39 +89,38 @@ async def face_verification(
|
|
104 |
raise HTTPException(status_code=500, detail=str(e))
|
105 |
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
"""
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
114 |
|
115 |
-
|
116 |
-
result = face_verification_uii(img1, img2, dist, model, detector)
|
117 |
-
return result
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
img2 = gr.Image(type="filepath", label="Image 2")
|
122 |
-
dist = gr.Dropdown(choices=["cosine", "euclidean", "euclidean_l2"], label="Distance Metric", value="cosine")
|
123 |
-
model = gr.Dropdown(choices=["VGG-Face", "Facenet", "Facenet512", "ArcFace"], label="Model", value="Facenet")
|
124 |
-
detector = gr.Dropdown(choices=["opencv", "ssd", "mtcnn", "retinaface", "mediapipe"], label="Detector", value="ssd")
|
125 |
-
btn = gr.Button("Verify")
|
126 |
-
output = gr.Textbox()
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
# FastAPI Startup Event
|
132 |
-
# FastAPI Startup Event
|
133 |
-
@app.on_event("startup")
|
134 |
-
def startup_event():
|
135 |
"""
|
136 |
-
|
137 |
"""
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
# Running Both Servers
|
141 |
if __name__ == "__main__":
|
142 |
import uvicorn
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, Form, HTTPException, Request
|
2 |
+
from fastapi.responses import RedirectResponse, JSONResponse, HTMLResponse
|
3 |
import gradio as gr
|
4 |
from deepface import DeepFace
|
5 |
import os
|
6 |
from threading import Thread
|
7 |
+
from gradio.routes import App as GradioApp
|
8 |
|
9 |
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
|
10 |
|
|
|
36 |
except Exception as e:
|
37 |
return {"error": str(e)}
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
@app.post("/face_verification")
|
40 |
async def face_verification(
|
41 |
img1: UploadFile = File(...),
|
|
|
89 |
raise HTTPException(status_code=500, detail=str(e))
|
90 |
|
91 |
|
92 |
+
# Define Gradio Blocks
|
93 |
+
with gr.Blocks() as demo:
|
94 |
+
img1 = gr.Image(type="filepath", label="Image 1")
|
95 |
+
img2 = gr.Image(type="filepath", label="Image 2")
|
96 |
+
dist = gr.Dropdown(choices=["cosine", "euclidean", "euclidean_l2"], label="Distance Metric", value="cosine")
|
97 |
+
model = gr.Dropdown(choices=["VGG-Face", "Facenet", "Facenet512", "ArcFace"], label="Model", value="Facenet")
|
98 |
+
detector = gr.Dropdown(choices=["opencv", "ssd", "mtcnn", "retinaface", "mediapipe"], label="Detector", value="ssd")
|
99 |
+
btn = gr.Button("Verify")
|
100 |
+
output = gr.Textbox()
|
101 |
|
102 |
+
btn.click(face_verification_uii, inputs=[img1, img2, dist, model, detector], outputs=output)
|
|
|
|
|
103 |
|
104 |
+
gradio_app = GradioApp.create_app(demo)
|
105 |
+
app.mount("/gradio", gradio_app)
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
+
@app.get("/", response_class=HTMLResponse)
|
108 |
+
async def root():
|
|
|
|
|
|
|
|
|
|
|
109 |
"""
|
110 |
+
Redirect root to the Gradio app
|
111 |
"""
|
112 |
+
return HTMLResponse("""
|
113 |
+
<!DOCTYPE html>
|
114 |
+
<html>
|
115 |
+
<head>
|
116 |
+
<title>Gradio App</title>
|
117 |
+
</head>
|
118 |
+
<body style="margin: 0; padding: 0; overflow: hidden;">
|
119 |
+
<iframe src="/gradio" style="width: 100%; height: 100vh; border: none;"></iframe>
|
120 |
+
</body>
|
121 |
+
</html>
|
122 |
+
""")
|
123 |
+
|
124 |
# Running Both Servers
|
125 |
if __name__ == "__main__":
|
126 |
import uvicorn
|