Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import subprocess, os, sys, time
|
| 4 |
+
import urllib.request, urllib.error
|
| 5 |
+
from fastapi import Request
|
| 6 |
+
from fastapi.responses import Response
|
| 7 |
+
|
| 8 |
+
SECOND_PORT = 9001
|
| 9 |
+
FLASK_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "flask-app")
|
| 10 |
+
|
| 11 |
+
# --- Launch YOUR flask-app as a background process on its own port ---
|
| 12 |
+
# cwd=FLASK_DIR so its templates/relative paths resolve correctly.
|
| 13 |
+
subprocess.Popen(
|
| 14 |
+
[sys.executable, "app.py"],
|
| 15 |
+
cwd=FLASK_DIR,
|
| 16 |
+
stdout=sys.stdout,
|
| 17 |
+
stderr=sys.stderr,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# --- Required ZeroGPU function (top level) ---
|
| 21 |
+
@spaces.GPU
|
| 22 |
+
def gpu_function():
|
| 23 |
+
import torch
|
| 24 |
+
return "GPU OK" if torch.cuda.is_available() else "GPU function executed"
|
| 25 |
+
|
| 26 |
+
# --- Gradio owns the ONLY public port 7860 ---
|
| 27 |
+
with gr.Blocks() as demo:
|
| 28 |
+
gr.Markdown("Host running. Your Flask app is on 9001, bridged at /app")
|
| 29 |
+
gr.Button("GPU check").click(gpu_function, outputs=gr.Textbox())
|
| 30 |
+
|
| 31 |
+
demo.queue()
|
| 32 |
+
demo.launch(
|
| 33 |
+
server_name="0.0.0.0",
|
| 34 |
+
server_port=7860,
|
| 35 |
+
ssr_mode=False, # prevents the Node SSR shutdown
|
| 36 |
+
prevent_thread_lock=True, # returns control so we can mount the proxy after launch
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
app = demo.app # Gradio's FastAPI app on 7860
|
| 40 |
+
|
| 41 |
+
# --- A session-aware, redirect-aware proxy: /app/... -> 127.0.0.1:9001/app/... ---
|
| 42 |
+
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
| 43 |
+
def redirect_request(self, *args, **kwargs):
|
| 44 |
+
return None # pass 3xx straight to the browser so cookies stick
|
| 45 |
+
|
| 46 |
+
@app.api_route("/app", methods=["GET", "POST"])
|
| 47 |
+
@app.api_route("/app/{path:path}", methods=["GET", "POST"])
|
| 48 |
+
async def proxy_to_flask(request: Request, path: str = ""):
|
| 49 |
+
url = f"http://127.0.0.1:{SECOND_PORT}/app/{path}"
|
| 50 |
+
if request.url.query:
|
| 51 |
+
url += "?" + request.url.query
|
| 52 |
+
body = await request.body()
|
| 53 |
+
fwd_headers = {k: v for k, v in request.headers.items() if k.lower() != "host"}
|
| 54 |
+
req = urllib.request.Request(url, data=body or None, method=request.method, headers=fwd_headers)
|
| 55 |
+
try:
|
| 56 |
+
opener = urllib.request.build_opener(NoRedirect)
|
| 57 |
+
with opener.open(req) as r:
|
| 58 |
+
resp_body, status, headers = r.read(), r.status, r.headers
|
| 59 |
+
except urllib.error.HTTPError as e:
|
| 60 |
+
resp_body, status, headers = e.read(), e.code, e.headers
|
| 61 |
+
except urllib.error.URLError:
|
| 62 |
+
return Response(content=b"flask-app not up yet on 9001", status_code=502)
|
| 63 |
+
out_headers = {k: v for k, v in headers.items()
|
| 64 |
+
if k.lower() in ("set-cookie", "location", "content-type")}
|
| 65 |
+
return Response(content=resp_body, status_code=status, headers=out_headers)
|
| 66 |
+
|
| 67 |
+
# --- Keep the process alive (launch returned control) ---
|
| 68 |
+
while True:
|
| 69 |
+
time.sleep(3600)
|