from fastapi import FastAPI, WebSocket, WebSocketDisconnect from contextlib import asynccontextmanager import uvicorn from whisper_live.server import TranscriptionServer import logging import numpy as np import threading # ————————————————————————————— # Logging # ————————————————————————————— logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Global server instance transcription_server = None server_thread = None @asynccontextmanager async def lifespan(app: FastAPI): # Startup: create and start the transcription server global transcription_server, server_thread transcription_server = TranscriptionServer() server_thread = threading.Thread( target=transcription_server.run, kwargs={ 'host': '0.0.0.0', 'port': 9090, # WebSocket port for transcription 'backend': 'faster_whisper' } ) server_thread.daemon = True server_thread.start() yield # Cleanup if transcription_server: transcription_server.cleanup() app = FastAPI( title="Whisper Live Server", version="1.0.0", lifespan=lifespan ) @app.get("/") async def root(): return { "message": "Welcome to Whisper Live Server", "websocket_endpoint": "ws://localhost:9090", # Direct WebSocket connection "health_endpoint": "/health" } @app.get("/health") async def health_check(): global transcription_server, server_thread if transcription_server and server_thread.is_alive(): return {"status": "healthy"} return {"status": "unhealthy"} if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)