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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -5
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  from pathlib import Path
3
  import json
4
  import base64
@@ -13,23 +14,50 @@ import httpx
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")
31
  os.environ["GRADIO_ALLOWED_PATHS"] = str(static_dir.resolve())
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  @spaces.GPU(duration=4*60)
34
  def process_text(text):
35
  return text.upper()
@@ -51,7 +79,7 @@ with gr.Blocks() as demo:
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__":
 
1
  import os
2
+ import re
3
  from pathlib import Path
4
  import json
5
  import base64
 
14
  import spaces
15
  from spaces.zero.client import _get_token
16
 
17
+ # Set environment variables so Gradio builds its URLs.
18
  os.environ["GRADIO_SSR_MODE"] = "True"
19
  os.environ["GRADIO_SERVER_PORT"] = "7860"
20
  os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
21
+ # Tell Gradio what the node server is (host and port)
22
+ os.environ["GRADIO_NODE_SERVER_NAME"] = "127.0.0.1:7861"
23
  os.environ["GRADIO_ROOT_PATH"] = "/"
24
 
25
  # Create FastAPI app
26
  app = FastAPI()
27
 
28
+ # (Optional) Mount static files
29
  static_dir = Path("./static")
30
  static_dir.mkdir(parents=True, exist_ok=True)
31
  app.mount("/static", StaticFiles(directory="static"), name="static")
32
  os.environ["GRADIO_ALLOWED_PATHS"] = str(static_dir.resolve())
33
 
34
+ # RESPONSE MIDDLEWARE TO FIX SSR ASSET URLS
35
+ @app.middleware("http")
36
+ async def fix_asset_urls(request: Request, call_next):
37
+ response = await call_next(request)
38
+ content_type = response.headers.get("content-type", "")
39
+ # Process only HTML responses
40
+ if "text/html" in content_type:
41
+ # Read the entire response body
42
+ body = b""
43
+ async for chunk in response.body_iterator:
44
+ body += chunk
45
+ # Step 1: Insert a slash after ':7861' if not already present.
46
+ fixed_body = re.sub(rb'(:7861)(?!/)', rb'\1/', body)
47
+ # Step 2: Fix the _appimmutable segment:
48
+ # Replace "_appimmutable" with "_app/immutable"
49
+ fixed_body = re.sub(rb'(_app)(immutable)', rb'\1/immutable', fixed_body)
50
+ # Step 3: Ensure that after "_app/immutable" there is a slash.
51
+ fixed_body = re.sub(rb'(_app/immutable)(?!/)', rb'\1/', fixed_body)
52
+ # Step 4: If "assets" is immediately followed by an uppercase letter, insert a slash.
53
+ fixed_body = re.sub(rb'(assets)(?=[A-Z])', rb'\1/', fixed_body)
54
+ # Build a new HTML response using the fixed body.
55
+ response = Response(content=fixed_body,
56
+ status_code=response.status_code,
57
+ headers=dict(response.headers),
58
+ media_type="text/html")
59
+ return response
60
+
61
  @spaces.GPU(duration=4*60)
62
  def process_text(text):
63
  return text.upper()
 
79
  output = gr.Textbox(label="Output")
80
  submit_btn.click(fn=process_and_save, inputs=[text_input], outputs=output)
81
 
82
+ # Mount the Gradio app into FastAPI using SSR mode (node_port 7861).
83
  app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=True, node_port=7861)
84
 
85
  if __name__ == "__main__":