m7n commited on
Commit
94e50b9
·
verified ·
1 Parent(s): 82f4377

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -9,7 +9,6 @@ from fastapi import FastAPI, Request, Response
9
  from fastapi.staticfiles import StaticFiles
10
  import uvicorn
11
  import httpx
12
- from fastapi.middleware.base import BaseHTTPMiddleware
13
 
14
  import spaces
15
  from spaces.zero.client import _get_token
@@ -24,20 +23,21 @@ os.environ["GRADIO_ROOT_PATH"] = "/"
24
  # Create FastAPI app
25
  app = FastAPI()
26
 
27
- # Optional middleware to fix malformed SSR asset paths.
28
- class SSRPathRewriteMiddleware(BaseHTTPMiddleware):
29
- async def dispatch(self, request: Request, call_next):
30
- path = request.url.path
31
- # If the path starts with the node port number attached (e.g., "/7861")
32
- if path.startswith("/7861"):
33
- # Replace "/7861" with "/_app" (or adjust to the correct base path)
34
- fixed = path.replace("/7861", "/_app", 1)
35
- request.scope["path"] = fixed
36
- return await call_next(request)
 
 
 
37
 
38
- app.add_middleware(SSRPathRewriteMiddleware)
39
-
40
- # Create and configure static directory
41
  static_dir = Path("./static")
42
  static_dir.mkdir(parents=True, exist_ok=True)
43
  app.mount("/static", StaticFiles(directory="static"), name="static")
@@ -64,7 +64,7 @@ with gr.Blocks() as demo:
64
  output = gr.Textbox(label="Output")
65
  submit_btn.click(fn=process_and_save, inputs=[text_input], outputs=output)
66
 
67
- # Mount the Gradio app with SSR mode, using node_port 7861.
68
  app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=True, node_port=7861)
69
 
70
  if __name__ == "__main__":
 
9
  from fastapi.staticfiles import StaticFiles
10
  import uvicorn
11
  import httpx
 
12
 
13
  import spaces
14
  from spaces.zero.client import _get_token
 
23
  # Create FastAPI app
24
  app = FastAPI()
25
 
26
+ # Add middleware using the decorator
27
+ @app.middleware("http")
28
+ async def ssr_path_rewrite(request: Request, call_next):
29
+ path = request.scope.get("path", "")
30
+ if path.startswith("/7861"):
31
+ remainder = path[len("/7861"):]
32
+ if not remainder.startswith("/"):
33
+ remainder = "/" + remainder
34
+ new_path = "/_app" + remainder
35
+ request.scope["path"] = new_path
36
+ print(f"Rewrote path from {path} to {new_path}")
37
+ response = await call_next(request)
38
+ return response
39
 
40
+ # Set up static files (if needed)
 
 
41
  static_dir = Path("./static")
42
  static_dir.mkdir(parents=True, exist_ok=True)
43
  app.mount("/static", StaticFiles(directory="static"), name="static")
 
64
  output = gr.Textbox(label="Output")
65
  submit_btn.click(fn=process_and_save, inputs=[text_input], outputs=output)
66
 
67
+ # Mount the Gradio app with SSR mode, using node_port 7861
68
  app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=True, node_port=7861)
69
 
70
  if __name__ == "__main__":