File size: 1,151 Bytes
329daab
 
 
 
 
 
dd9ee64
329daab
 
 
 
 
 
 
 
 
 
 
dd9ee64
 
329daab
 
 
 
a5a91fa
7e5facf
a5a91fa
 
 
 
 
 
 
 
 
329daab
 
 
dd9ee64
7e5facf
329daab
 
dd9ee64
 
7e5facf
329daab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import subprocess
import threading
import os


def run_fastapi():
    """Run FastAPI backend on port 8000."""
    subprocess.run(
        [
            "uvicorn",
            "app.main:app",
            "--host",
            "0.0.0.0",
            "--port",
            "8000",
            "--proxy-headers",
            "--workers",
            str(os.getenv("UVICORN_WORKERS", 1)),
        ],
        check=True,
    )


def run_gradio():
    """Build and launch Gradio UI on the Space's PORT."""
    print("[start] about to launch gradio on PORT=", os.getenv("PORT"), flush=True)
    from demo.app import build_ui

    demo = build_ui()
    demo.launch(
        server_name="0.0.0.0",
        server_port=int(os.getenv("PORT", "7860")),
        show_api=False,
        debug=False,
    )


if __name__ == "__main__":
    # Start FastAPI in background thread
    print("[start] launching uvicorn thread...", flush=True)
    t = threading.Thread(target=run_fastapi, daemon=True)
    t.start()

    # Run Gradio in foreground (keeps container alive)
    print("[start] about to launch gradio on PORT=", os.getenv("PORT"), flush=True)
    run_gradio()