File size: 1,788 Bytes
2259eec 28ad8e5 7222c68 28ad8e5 e0c9adf 0c553fd 7222c68 bb2aa1c 28ad8e5 7222c68 0c553fd 28ad8e5 0c553fd 28ad8e5 0c553fd 28ad8e5 f486429 0c553fd f486429 28ad8e5 2259eec bb2aa1c 0c553fd bb2aa1c 7222c68 2259eec |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
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)
|