Fix build error
Browse files- app.py +14 -2
- tests/test_app_startup.py +15 -0
app.py
CHANGED
|
@@ -6,7 +6,8 @@ from typing import Any, Dict, List, Optional
|
|
| 6 |
|
| 7 |
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
|
| 8 |
from fastapi.staticfiles import StaticFiles
|
| 9 |
-
from gradio import Server
|
|
|
|
| 10 |
|
| 11 |
from backend.config import (
|
| 12 |
APP_TITLE,
|
|
@@ -215,6 +216,17 @@ async def serve_generated_file(session_id: str, kind: str, filename: str):
|
|
| 215 |
app.mount("/static", StaticFiles(directory=str(FRONTEND_DIR)), name="frontend-static")
|
| 216 |
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
def _port_is_available(port: int, host: str = "127.0.0.1") -> bool:
|
| 219 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
| 220 |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
@@ -237,4 +249,4 @@ def _choose_server_port(preferred: int = 7860, attempts: int = 10) -> int:
|
|
| 237 |
if __name__ == "__main__":
|
| 238 |
server_port = _choose_server_port()
|
| 239 |
print(f"Starting {APP_TITLE} on port {server_port}")
|
| 240 |
-
|
|
|
|
| 6 |
|
| 7 |
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
|
| 8 |
from fastapi.staticfiles import StaticFiles
|
| 9 |
+
from gradio import Blocks, Server
|
| 10 |
+
from gradio.events import api as gradio_api
|
| 11 |
|
| 12 |
from backend.config import (
|
| 13 |
APP_TITLE,
|
|
|
|
| 216 |
app.mount("/static", StaticFiles(directory=str(FRONTEND_DIR)), name="frontend-static")
|
| 217 |
|
| 218 |
|
| 219 |
+
def _build_demo() -> Blocks:
|
| 220 |
+
# Expose the launched Blocks at module scope so Gradio hot reload can find it.
|
| 221 |
+
with Blocks(mode="server") as demo:
|
| 222 |
+
for fn, api_kwargs in app._deferred_apis:
|
| 223 |
+
gradio_api(fn=fn, **api_kwargs)
|
| 224 |
+
return demo
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
demo = _build_demo()
|
| 228 |
+
|
| 229 |
+
|
| 230 |
def _port_is_available(port: int, host: str = "127.0.0.1") -> bool:
|
| 231 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
| 232 |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
| 249 |
if __name__ == "__main__":
|
| 250 |
server_port = _choose_server_port()
|
| 251 |
print(f"Starting {APP_TITLE} on port {server_port}")
|
| 252 |
+
demo.launch(_app=app, server_name="0.0.0.0", server_port=server_port)
|
tests/test_app_startup.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import importlib
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def test_app_module_exposes_top_level_server_demo() -> None:
|
| 8 |
+
sys.modules.pop("app", None)
|
| 9 |
+
|
| 10 |
+
module = importlib.import_module("app")
|
| 11 |
+
|
| 12 |
+
assert isinstance(module.app, gr.Server)
|
| 13 |
+
assert hasattr(module, "demo")
|
| 14 |
+
assert isinstance(module.demo, gr.Blocks)
|
| 15 |
+
assert module.demo.mode == "server"
|