demo mount to fastapi

This commit is contained in:
Xin Wang
2026-05-22 10:23:17 +08:00
parent 0d82acc0d2
commit 2c7f0f440b
10 changed files with 86 additions and 22 deletions

View File

@@ -10,6 +10,8 @@ class ServerConfig:
host: str = "0.0.0.0"
port: int = 8000
cors_origins: list[str] = field(default_factory=list)
serve_webpage: bool = True
webpage_mount: str = "/demo"
@dataclass(frozen=True)

View File

@@ -2,19 +2,30 @@ from __future__ import annotations
import argparse
from functools import lru_cache
from pathlib import Path
from fastapi import FastAPI, WebSocket
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from .config import EngineConfig, load_config
from .pipeline import run_product_voice_pipeline, run_voice_pipeline
WEBPAGE_DIR = Path(__file__).resolve().parent.parent / "examples" / "webpage"
@lru_cache(maxsize=8)
def get_config(path: str = "config.json") -> EngineConfig:
return load_config(path)
def _normalize_mount_path(path: str) -> str:
normalized = path.strip() or "/demo"
if not normalized.startswith("/"):
normalized = f"/{normalized}"
return normalized.rstrip("/") or "/"
def create_app(config_path: str = "config.json") -> FastAPI:
config = get_config(config_path)
app = FastAPI(title="AI VideoAssistant Engine v5 Pipecat Minimal", version="0.1.0")
@@ -28,6 +39,12 @@ def create_app(config_path: str = "config.json") -> FastAPI:
allow_headers=["*"],
)
webpage_mount = (
_normalize_mount_path(config.server.webpage_mount)
if config.server.serve_webpage
else None
)
@app.get("/health")
async def health() -> dict[str, object]:
return {
@@ -40,6 +57,7 @@ def create_app(config_path: str = "config.json") -> FastAPI:
"product_text_input": True,
"product_text_interrupt": True,
},
"demo": webpage_mount,
"llm_provider": config.services.llm.provider,
"stt_provider": config.services.stt.provider,
"tts_provider": config.services.tts.provider,
@@ -55,6 +73,13 @@ def create_app(config_path: str = "config.json") -> FastAPI:
await websocket.accept()
await run_product_voice_pipeline(websocket, config)
if config.server.serve_webpage and WEBPAGE_DIR.is_dir() and webpage_mount:
app.mount(
webpage_mount,
StaticFiles(directory=str(WEBPAGE_DIR), html=True),
name="webpage",
)
return app