pngwn HF staff commited on
Commit
b5e785c
1 Parent(s): a4618d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -20
app.py CHANGED
@@ -47,50 +47,112 @@ async def route_with_config():
47
  return JSONResponse(content={"one": "hello", "two": "from", "three": "Python"})
48
 
49
 
50
- async def proxy_to_node(request: Request):
51
 
52
 
53
- # Preserve the full path including query parameters
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  full_path = request.url.path
 
 
55
  if request.url.query:
56
  full_path += f"?{request.url.query}"
57
 
58
- url = f"http://localhost:{NODE_PORT}{full_path}"
59
 
60
  headers = {
61
  k: v
62
  for k, v in request.headers.items()
63
- if k.lower() not in ["host", "content-length"]
64
  }
65
 
66
- print(headers)
67
- # body = await request.body()
 
 
 
 
 
 
 
 
 
 
68
 
69
- # async with client:
70
- # response = await client.request(
71
- # method=request.method, url=url, headers=headers, content=body
72
- # )
73
 
74
- # return StreamingResponse(
75
- # response.iter_bytes(),
76
- # status_code=response.status_code,
77
- # headers=response.headers,
78
- # )
79
 
80
- req = client.build_request("GET", httpx.URL(url), headers=headers)
81
- r = await client.send(req, stream=True)
82
 
83
- return StreamingResponse(
84
- r.aiter_raw(), headers=r.headers
85
  )
86
 
 
 
 
 
 
87
 
88
 
89
  @app.api_route(
90
  "/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]
91
  )
92
  async def catch_all(request: Request, path: str):
93
- return await proxy_to_node(request)
 
 
 
 
 
 
 
94
 
95
 
96
  if __name__ == "__main__":
 
47
  return JSONResponse(content={"one": "hello", "two": "from", "three": "Python"})
48
 
49
 
50
+ # async def proxy_to_node(request: Request):
51
 
52
 
53
+ # # Preserve the full path including query parameters
54
+ # full_path = request.url.path
55
+ # if request.url.query:
56
+ # full_path += f"?{request.url.query}"
57
+
58
+ # url = f"http://localhost:{NODE_PORT}{full_path}"
59
+
60
+ # headers = {
61
+ # k: v
62
+ # for k, v in request.headers.items()
63
+ # if k.lower() not in ["host", "content-length"]
64
+ # }
65
+
66
+ # print(headers)
67
+ # # body = await request.body()
68
+
69
+ # # async with client:
70
+ # # response = await client.request(
71
+ # # method=request.method, url=url, headers=headers, content=body
72
+ # # )
73
+
74
+ # # return StreamingResponse(
75
+ # # response.iter_bytes(),
76
+ # # status_code=response.status_code,
77
+ # # headers=response.headers,
78
+ # # )
79
+
80
+ # req = client.build_request("GET", httpx.URL(url), headers=headers)
81
+ # r = await client.send(req, stream=True)
82
+
83
+ # return StreamingResponse(
84
+ # r.aiter_raw(), headers=r.headers
85
+ # )
86
+
87
+ async def proxy_to_node(
88
+ request: Request,
89
+ server_name: str,
90
+ node_port: int,
91
+ python_port: int,
92
+ scheme: str = "http",
93
+ mounted_path: str = "",
94
+ ) -> Response:
95
+ start_time = time.time()
96
+
97
  full_path = request.url.path
98
+ if mounted_path:
99
+ full_path = full_path.replace(mounted_path, "")
100
  if request.url.query:
101
  full_path += f"?{request.url.query}"
102
 
103
+ url = f"{scheme}://{server_name}:{node_port}{full_path}"
104
 
105
  headers = {
106
  k: v
107
  for k, v in request.headers.items()
108
+ if k.lower() not in ["content-length"]
109
  }
110
 
111
+ print(
112
+ headers,
113
+ )
114
+
115
+ server_url = f"{scheme}://{server_name}"
116
+ if python_port:
117
+ server_url += f":{python_port}"
118
+ if mounted_path:
119
+ server_url += mounted_path
120
+
121
+ headers["x-gradio-server"] = server_url
122
+ headers["x-gradio-port"] = str(python_port)
123
 
124
+ print(
125
+ f"Proxying request from {request.url.path} to {url} with server url {server_url}"
126
+ )
 
127
 
128
+ if os.getenv("GRADIO_LOCAL_DEV_MODE"):
129
+ headers["x-gradio-local-dev-mode"] = "1"
 
 
 
130
 
131
+ print(f"Time to prepare request: {time.time() - start_time:.4f} seconds")
 
132
 
133
+ print(
134
+ f"Total setup time before streaming: {time.time() - start_time:.4f} seconds"
135
  )
136
 
137
+ req = App.client.build_request("GET", httpx.URL(url), headers=headers)
138
+ r = await App.client.send(req, stream=True)
139
+ print(f"Time to prepare request: {time.time() - start_time:.4f} seconds")
140
+
141
+ return StreamingResponse(r.aiter_raw(), headers=r.headers)
142
 
143
 
144
  @app.api_route(
145
  "/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]
146
  )
147
  async def catch_all(request: Request, path: str):
148
+ return await App.proxy_to_node(
149
+ request,
150
+ request.client.host,
151
+ 4321,
152
+ request.url.port,
153
+ request.url.scheme,
154
+ "",
155
+ )
156
 
157
 
158
  if __name__ == "__main__":