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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -18
app.py CHANGED
@@ -13,31 +13,18 @@ import httpx
13
  import spaces
14
  from spaces.zero.client import _get_token
15
 
16
- # Set environment variables
17
  os.environ["GRADIO_SSR_MODE"] = "True"
18
  os.environ["GRADIO_SERVER_PORT"] = "7860"
19
  os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
20
- os.environ["GRADIO_NODE_SERVER_NAME"] = "127.0.0.1" # host only
 
21
  os.environ["GRADIO_ROOT_PATH"] = "/"
22
 
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,7 +51,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__":
 
13
  import spaces
14
  from spaces.zero.client import _get_token
15
 
16
+ # Set environment variables so Gradio builds correct URLs:
17
  os.environ["GRADIO_SSR_MODE"] = "True"
18
  os.environ["GRADIO_SERVER_PORT"] = "7860"
19
  os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
20
+ # IMPORTANT: include both the node port and a trailing slash!
21
+ os.environ["GRADIO_NODE_SERVER_NAME"] = "127.0.0.1:7861/"
22
  os.environ["GRADIO_ROOT_PATH"] = "/"
23
 
24
  # Create FastAPI app
25
  app = FastAPI()
26
 
27
+ # Configure and mount static files if needed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  static_dir = Path("./static")
29
  static_dir.mkdir(parents=True, exist_ok=True)
30
  app.mount("/static", StaticFiles(directory="static"), name="static")
 
51
  output = gr.Textbox(label="Output")
52
  submit_btn.click(fn=process_and_save, inputs=[text_input], outputs=output)
53
 
54
+ # Mount the Gradio app with SSR mode, specifying the node port (7861)
55
  app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=True, node_port=7861)
56
 
57
  if __name__ == "__main__":