Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,19 +9,34 @@ from fastapi import FastAPI, Request, Response
|
|
9 |
from fastapi.staticfiles import StaticFiles
|
10 |
import uvicorn
|
11 |
import httpx
|
|
|
12 |
|
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"] = "
|
|
|
21 |
|
22 |
# Create FastAPI app
|
23 |
app = FastAPI()
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# Create and configure static directory
|
26 |
static_dir = Path("./static")
|
27 |
static_dir.mkdir(parents=True, exist_ok=True)
|
@@ -47,24 +62,10 @@ with gr.Blocks() as demo:
|
|
47 |
text_input = gr.Textbox(label="Enter some text")
|
48 |
submit_btn = gr.Button("Process and Download")
|
49 |
output = gr.Textbox(label="Output")
|
50 |
-
|
51 |
submit_btn.click(fn=process_and_save, inputs=[text_input], outputs=output)
|
52 |
|
53 |
-
# Mount the Gradio app with SSR mode, using node_port 7861
|
54 |
app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=True, node_port=7861)
|
55 |
|
56 |
-
# Manual reverse proxy for SSR assets
|
57 |
-
@app.get("/_app/{full_path:path}")
|
58 |
-
async def proxy_to_node(full_path: str, request: Request):
|
59 |
-
node_server = "http://127.0.0.1:7861"
|
60 |
-
url = f"{node_server}/_app/{full_path}"
|
61 |
-
async with httpx.AsyncClient() as client:
|
62 |
-
node_response = await client.get(url)
|
63 |
-
return Response(
|
64 |
-
content=node_response.content,
|
65 |
-
status_code=node_response.status_code,
|
66 |
-
media_type=node_response.headers.get("content-type"),
|
67 |
-
)
|
68 |
-
|
69 |
if __name__ == "__main__":
|
70 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
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
|
16 |
|
17 |
+
# Set environment variables
|
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 |
+
os.environ["GRADIO_NODE_SERVER_NAME"] = "127.0.0.1" # host only
|
22 |
+
os.environ["GRADIO_ROOT_PATH"] = "/"
|
23 |
|
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)
|
|
|
62 |
text_input = gr.Textbox(label="Enter some text")
|
63 |
submit_btn = gr.Button("Process and Download")
|
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__":
|
71 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|