kadabengaran commited on
Commit
67835a5
·
verified ·
1 Parent(s): 3edb801

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -46
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 asyncio
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
- def run_gradio_ui():
108
- """
109
- Function to run Gradio in a separate thread
110
- """
111
- # Create and set an event loop for this thread
112
- loop = asyncio.new_event_loop()
113
- asyncio.set_event_loop(loop)
 
 
114
 
115
- def face_verification_ui(img1, img2, dist, model, detector):
116
- result = face_verification_uii(img1, img2, dist, model, detector)
117
- return result
118
 
119
- with gr.Blocks() as demo:
120
- img1 = gr.Image(type="filepath", label="Image 1")
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
- btn.click(face_verification_ui, inputs=[img1, img2, dist, model, detector], outputs=output)
129
- demo.launch(server_name="0.0.0.0", server_port=7861, show_api=False)
130
-
131
- # FastAPI Startup Event
132
- # FastAPI Startup Event
133
- @app.on_event("startup")
134
- def startup_event():
135
  """
136
- Start Gradio UI in a separate thread
137
  """
138
- thread = Thread(target=run_gradio_ui)
139
- thread.start()
 
 
 
 
 
 
 
 
 
 
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