nuernie
commited on
Commit
·
2259eec
1
Parent(s):
e0c9adf
adjust server
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI, WebSocket
|
2 |
from contextlib import asynccontextmanager
|
3 |
import uvicorn
|
4 |
from whisper_live.server import TranscriptionServer
|
@@ -10,14 +10,14 @@ import numpy as np
|
|
10 |
logging.basicConfig(level=logging.INFO)
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
-
# Initialize the transcription server
|
14 |
transcription_server = TranscriptionServer()
|
15 |
|
16 |
@asynccontextmanager
|
17 |
async def lifespan(app: FastAPI):
|
18 |
-
#
|
19 |
yield
|
20 |
-
# Cleanup
|
21 |
|
22 |
app = FastAPI(
|
23 |
title="Whisper Live Server",
|
@@ -28,9 +28,6 @@ app = FastAPI(
|
|
28 |
|
29 |
@app.get("/")
|
30 |
async def root():
|
31 |
-
"""
|
32 |
-
Root endpoint that returns a welcome message
|
33 |
-
"""
|
34 |
return {
|
35 |
"message": "Welcome to Whisper Live Server",
|
36 |
"status": "running",
|
@@ -38,64 +35,67 @@ async def root():
|
|
38 |
"health_endpoint": "/health"
|
39 |
}
|
40 |
|
|
|
|
|
|
|
|
|
|
|
41 |
@app.websocket("/ws")
|
42 |
async def websocket_endpoint(websocket: WebSocket):
|
43 |
await websocket.accept()
|
44 |
client_uid = None
|
|
|
45 |
|
46 |
try:
|
47 |
-
# Receive
|
48 |
config = await websocket.receive_json()
|
49 |
client_uid = config.get("uid")
|
50 |
-
|
51 |
if not client_uid:
|
52 |
await websocket.close(code=4000, reason="No client UID provided")
|
53 |
return
|
54 |
-
|
55 |
-
|
|
|
|
|
56 |
await websocket.send_json({
|
57 |
"uid": client_uid,
|
58 |
"message": "SERVER_READY",
|
59 |
"backend": "faster_whisper"
|
60 |
})
|
61 |
-
|
62 |
-
#
|
63 |
-
transcription_server.
|
64 |
-
|
65 |
-
port=7860,
|
66 |
-
backend="faster_whisper",
|
67 |
-
single_model=True,
|
68 |
language=config.get("language", "de"),
|
69 |
task=config.get("task", "transcribe"),
|
70 |
-
model=config.get("model", "tiny"),
|
71 |
use_vad=config.get("use_vad", True)
|
72 |
)
|
73 |
-
|
74 |
-
#
|
75 |
while True:
|
76 |
data = await websocket.receive()
|
77 |
-
|
78 |
if data["type"] == "websocket.disconnect":
|
79 |
break
|
80 |
-
|
81 |
-
if
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
except Exception as e:
|
100 |
logger.error(f"WebSocket error: {e}")
|
101 |
if client_uid:
|
@@ -107,17 +107,7 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
107 |
except:
|
108 |
pass
|
109 |
finally:
|
110 |
-
|
111 |
-
await websocket.close()
|
112 |
-
except:
|
113 |
-
pass
|
114 |
-
|
115 |
-
@app.get("/health")
|
116 |
-
def health_check():
|
117 |
-
"""
|
118 |
-
Health check endpoint to verify the server is running
|
119 |
-
"""
|
120 |
-
return {"status": "healthy"}
|
121 |
|
122 |
if __name__ == "__main__":
|
123 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
1 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
2 |
from contextlib import asynccontextmanager
|
3 |
import uvicorn
|
4 |
from whisper_live.server import TranscriptionServer
|
|
|
10 |
logging.basicConfig(level=logging.INFO)
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
+
# Initialize the transcription server once
|
14 |
transcription_server = TranscriptionServer()
|
15 |
|
16 |
@asynccontextmanager
|
17 |
async def lifespan(app: FastAPI):
|
18 |
+
# Setup if needed
|
19 |
yield
|
20 |
+
# Cleanup if needed
|
21 |
|
22 |
app = FastAPI(
|
23 |
title="Whisper Live Server",
|
|
|
28 |
|
29 |
@app.get("/")
|
30 |
async def root():
|
|
|
|
|
|
|
31 |
return {
|
32 |
"message": "Welcome to Whisper Live Server",
|
33 |
"status": "running",
|
|
|
35 |
"health_endpoint": "/health"
|
36 |
}
|
37 |
|
38 |
+
@app.get("/health")
|
39 |
+
def health_check():
|
40 |
+
return {"status": "healthy"}
|
41 |
+
|
42 |
+
|
43 |
@app.websocket("/ws")
|
44 |
async def websocket_endpoint(websocket: WebSocket):
|
45 |
await websocket.accept()
|
46 |
client_uid = None
|
47 |
+
config = {}
|
48 |
|
49 |
try:
|
50 |
+
# 1. Receive config from client
|
51 |
config = await websocket.receive_json()
|
52 |
client_uid = config.get("uid")
|
53 |
+
|
54 |
if not client_uid:
|
55 |
await websocket.close(code=4000, reason="No client UID provided")
|
56 |
return
|
57 |
+
|
58 |
+
logger.info(f"Client connected: {client_uid} | Config: {config}")
|
59 |
+
|
60 |
+
# 2. Confirm server readiness
|
61 |
await websocket.send_json({
|
62 |
"uid": client_uid,
|
63 |
"message": "SERVER_READY",
|
64 |
"backend": "faster_whisper"
|
65 |
})
|
66 |
+
|
67 |
+
# 3. Per-client session configuration
|
68 |
+
session = transcription_server.create_session(
|
69 |
+
model=config.get("model", "tiny"),
|
|
|
|
|
|
|
70 |
language=config.get("language", "de"),
|
71 |
task=config.get("task", "transcribe"),
|
|
|
72 |
use_vad=config.get("use_vad", True)
|
73 |
)
|
74 |
+
|
75 |
+
# 4. Start processing audio stream
|
76 |
while True:
|
77 |
data = await websocket.receive()
|
78 |
+
|
79 |
if data["type"] == "websocket.disconnect":
|
80 |
break
|
81 |
+
|
82 |
+
if "bytes" in data:
|
83 |
+
audio_data = np.frombuffer(data["bytes"], dtype=np.float32)
|
84 |
+
segments = session.process_audio(audio_data)
|
85 |
+
|
86 |
+
if segments:
|
87 |
+
await websocket.send_json({
|
88 |
+
"uid": client_uid,
|
89 |
+
"segments": segments
|
90 |
+
})
|
91 |
+
|
92 |
+
elif "text" in data:
|
93 |
+
if data["text"] == "END_OF_AUDIO":
|
94 |
+
logger.info(f"Client {client_uid} ended stream.")
|
95 |
+
break
|
96 |
+
|
97 |
+
except WebSocketDisconnect:
|
98 |
+
logger.warning(f"WebSocket disconnected: {client_uid}")
|
|
|
99 |
except Exception as e:
|
100 |
logger.error(f"WebSocket error: {e}")
|
101 |
if client_uid:
|
|
|
107 |
except:
|
108 |
pass
|
109 |
finally:
|
110 |
+
await websocket.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|