akagtag commited on
Commit
0c93f3d
·
1 Parent(s): b854243

Register CORS before Gradio app startup

Browse files
Files changed (2) hide show
  1. app.py +14 -9
  2. tests/test_zero_gpu_contract.py +4 -1
app.py CHANGED
@@ -9,8 +9,9 @@ from typing import Any
9
 
10
  import gradio as gr
11
  from fastapi import File, UploadFile
12
- from fastapi.middleware.cors import CORSMiddleware
13
  from fastapi.responses import RedirectResponse
 
 
14
 
15
  from src.api.main import detect_image as api_detect_image
16
  from src.api.main import detect_video as api_detect_video
@@ -51,13 +52,6 @@ def _build_demo() -> gr.Blocks:
51
 
52
 
53
  def _attach_api_routes(app: Any) -> None:
54
- app.add_middleware(
55
- CORSMiddleware,
56
- allow_origins=["*"],
57
- allow_methods=["*"],
58
- allow_headers=["*"],
59
- )
60
-
61
  async def health() -> dict:
62
  return await api_health()
63
 
@@ -128,7 +122,18 @@ if __name__ == "__main__":
128
  prevent_thread_lock=True,
129
  show_error=True,
130
  ssr_mode=False,
131
- app_kwargs={"docs_url": "/docs", "redoc_url": "/redoc"},
 
 
 
 
 
 
 
 
 
 
 
132
  )
133
  _attach_api_routes(app)
134
  while True:
 
9
 
10
  import gradio as gr
11
  from fastapi import File, UploadFile
 
12
  from fastapi.responses import RedirectResponse
13
+ from starlette.middleware import Middleware
14
+ from fastapi.middleware.cors import CORSMiddleware
15
 
16
  from src.api.main import detect_image as api_detect_image
17
  from src.api.main import detect_video as api_detect_video
 
52
 
53
 
54
  def _attach_api_routes(app: Any) -> None:
 
 
 
 
 
 
 
55
  async def health() -> dict:
56
  return await api_health()
57
 
 
122
  prevent_thread_lock=True,
123
  show_error=True,
124
  ssr_mode=False,
125
+ app_kwargs={
126
+ "docs_url": "/docs",
127
+ "redoc_url": "/redoc",
128
+ "middleware": [
129
+ Middleware(
130
+ CORSMiddleware,
131
+ allow_origins=["*"],
132
+ allow_methods=["*"],
133
+ allow_headers=["*"],
134
+ )
135
+ ],
136
+ },
137
  )
138
  _attach_api_routes(app)
139
  while True:
tests/test_zero_gpu_contract.py CHANGED
@@ -38,7 +38,10 @@ def test_app_mounts_gradio_onto_fastapi():
38
  assert "demo = _build_demo().queue()" in source
39
  assert "prevent_thread_lock=True" in source
40
  assert "while True:" in source
41
- assert 'app_kwargs={"docs_url": "/docs", "redoc_url": "/redoc"}' in source
 
 
 
42
  assert 'app.add_api_route("/api/health"' in source
43
  assert "/api/detect/image" in source
44
  assert "/api/detect/video" in source
 
38
  assert "demo = _build_demo().queue()" in source
39
  assert "prevent_thread_lock=True" in source
40
  assert "while True:" in source
41
+ assert '"docs_url": "/docs"' in source
42
+ assert '"redoc_url": "/redoc"' in source
43
+ assert "Middleware(" in source
44
+ assert "CORSMiddleware" in source
45
  assert 'app.add_api_route("/api/health"' in source
46
  assert "/api/detect/image" in source
47
  assert "/api/detect/video" in source