shasee commited on
Commit
d3a5644
·
verified ·
1 Parent(s): 02e8bbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -6
app.py CHANGED
@@ -1,10 +1,95 @@
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- with gr.Blocks() as demo:
4
- gr.Markdown("test")
 
 
 
 
 
5
 
6
- print(gr.__version__)
7
- print(type(demo.app))
8
- print(hasattr(demo.app, "proxy_to_node"))
 
 
9
 
10
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
1
+ import subprocess
2
+ import threading
3
+
4
  import gradio as gr
5
+ import spaces
6
+ import httpx
7
+ from fastapi import Request, Response
8
+
9
+ # -------------------------
10
+ # ZeroGPU placeholder
11
+ # -------------------------
12
+
13
+ @spaces.GPU
14
+ def dummy():
15
+ return "ok"
16
+
17
+ # -------------------------
18
+ # Node
19
+ # -------------------------
20
+
21
+ node = None
22
+
23
+ def start_node():
24
+ global node
25
+
26
+ if node and node.poll() is None:
27
+ return "Already running"
28
+
29
+ node = subprocess.Popen(
30
+ ["node", "server.js"],
31
+ cwd="/home/user/app",
32
+ )
33
+
34
+ return "Started"
35
+
36
+ threading.Thread(target=start_node, daemon=True).start()
37
+
38
+ # -------------------------
39
+ # UI
40
+ # -------------------------
41
+
42
+ with gr.Blocks(title="Node Proxy") as demo:
43
+ gr.Markdown("# Node Proxy")
44
+
45
+ status = gr.Textbox(value="Running")
46
+
47
+ gr.Button("Restart").click(
48
+ fn=start_node,
49
+ outputs=status
50
+ )
51
+
52
+ # -------------------------
53
+ # Proxy
54
+ # -------------------------
55
+
56
+ @demo.app.api_route(
57
+ "/node/{path:path}",
58
+ methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
59
+ )
60
+ async def proxy(path: str, request: Request):
61
+
62
+ body = await request.body()
63
+
64
+ headers = {
65
+ k: v
66
+ for k, v in request.headers.items()
67
+ if k.lower() not in (
68
+ "host",
69
+ "content-length",
70
+ )
71
+ }
72
+
73
+ async with httpx.AsyncClient(
74
+ follow_redirects=True,
75
+ timeout=None,
76
+ ) as client:
77
 
78
+ r = await client.request(
79
+ request.method,
80
+ f"http://127.0.0.1:8888/{path}",
81
+ params=request.query_params,
82
+ headers=headers,
83
+ content=body,
84
+ )
85
 
86
+ return Response(
87
+ content=r.content,
88
+ status_code=r.status_code,
89
+ headers=dict(r.headers),
90
+ )
91
 
92
+ demo.launch(
93
+ server_name="0.0.0.0",
94
+ server_port=7860,
95
+ )