update with up to date example
Browse files
README.md
CHANGED
@@ -142,52 +142,79 @@ whisperlivekit-server --host 0.0.0.0 --port 8000 --model medium --diarization --
|
|
142 |
```
|
143 |
|
144 |
### Python API Integration (Backend)
|
|
|
145 |
|
146 |
```python
|
147 |
-
from whisperlivekit import
|
148 |
-
from
|
149 |
-
from fastapi import FastAPI, WebSocket
|
150 |
-
import asyncio
|
151 |
from fastapi.responses import HTMLResponse
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
-
|
154 |
-
app
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
# Serve the web interface
|
158 |
@app.get("/")
|
159 |
async def get():
|
160 |
-
return HTMLResponse(
|
161 |
|
162 |
# Process WebSocket connections
|
163 |
-
async def handle_websocket_results(websocket, results_generator):
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
166 |
|
167 |
@app.websocket("/asr")
|
168 |
async def websocket_endpoint(websocket: WebSocket):
|
169 |
-
|
170 |
-
await websocket.accept()
|
171 |
-
results_generator = await audio_processor.create_tasks()
|
172 |
-
websocket_task = asyncio.create_task(
|
173 |
-
handle_websocket_results(websocket, results_generator)
|
174 |
-
)
|
175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
try:
|
177 |
while True:
|
178 |
message = await websocket.receive_bytes()
|
179 |
-
await audio_processor.process_audio(message)
|
|
|
|
|
180 |
except Exception as e:
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
183 |
```
|
184 |
|
185 |
### Frontend Implementation
|
186 |
|
187 |
-
The package includes a simple HTML/JavaScript implementation that you can adapt for your project. You can
|
188 |
|
189 |
```python
|
190 |
-
|
|
|
|
|
|
|
191 |
```
|
192 |
|
193 |
## ⚙️ Configuration Reference
|
|
|
142 |
```
|
143 |
|
144 |
### Python API Integration (Backend)
|
145 |
+
Check [basic_server.py](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/whisperlivekit/basic_server.py) for a complete example.
|
146 |
|
147 |
```python
|
148 |
+
from whisperlivekit import TranscriptionEngine, AudioProcessor, get_web_interface_html, parse_args
|
149 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
|
|
|
|
150 |
from fastapi.responses import HTMLResponse
|
151 |
+
from contextlib import asynccontextmanager
|
152 |
+
import asyncio
|
153 |
+
|
154 |
+
# Global variable for the transcription engine
|
155 |
+
transcription_engine = None
|
156 |
|
157 |
+
@asynccontextmanager
|
158 |
+
async def lifespan(app: FastAPI):
|
159 |
+
global transcription_engine
|
160 |
+
# Example: Initialize with specific parameters directly
|
161 |
+
# You can also load from command-line arguments using parse_args()
|
162 |
+
# args = parse_args()
|
163 |
+
# transcription_engine = TranscriptionEngine(**vars(args))
|
164 |
+
transcription_engine = TranscriptionEngine(model="medium", diarization=True, lan="en")
|
165 |
+
yield
|
166 |
+
|
167 |
+
app = FastAPI(lifespan=lifespan)
|
168 |
|
169 |
# Serve the web interface
|
170 |
@app.get("/")
|
171 |
async def get():
|
172 |
+
return HTMLResponse(get_web_interface_html())
|
173 |
|
174 |
# Process WebSocket connections
|
175 |
+
async def handle_websocket_results(websocket: WebSocket, results_generator):
|
176 |
+
try:
|
177 |
+
async for response in results_generator:
|
178 |
+
await websocket.send_json(response)
|
179 |
+
await websocket.send_json({"type": "ready_to_stop"})
|
180 |
+
except WebSocketDisconnect:
|
181 |
+
print("WebSocket disconnected during results handling.")
|
182 |
|
183 |
@app.websocket("/asr")
|
184 |
async def websocket_endpoint(websocket: WebSocket):
|
185 |
+
global transcription_engine
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
+
# Create a new AudioProcessor for each connection, passing the shared engine
|
188 |
+
audio_processor = AudioProcessor(transcription_engine=transcription_engine)
|
189 |
+
results_generator = await audio_processor.create_tasks()
|
190 |
+
send_results_to_client = handle_websocket_results(websocket, results_generator)
|
191 |
+
results_task = asyncio.create_task(send_results_to_client)
|
192 |
+
await websocket.accept()
|
193 |
try:
|
194 |
while True:
|
195 |
message = await websocket.receive_bytes()
|
196 |
+
await audio_processor.process_audio(message)
|
197 |
+
except WebSocketDisconnect:
|
198 |
+
print(f"Client disconnected: {websocket.client}")
|
199 |
except Exception as e:
|
200 |
+
await websocket.close(code=1011, reason=f"Server error: {e}")
|
201 |
+
finally:
|
202 |
+
results_task.cancel()
|
203 |
+
try:
|
204 |
+
await results_task
|
205 |
+
except asyncio.CancelledError:
|
206 |
+
logger.info("Results task successfully cancelled.")
|
207 |
```
|
208 |
|
209 |
### Frontend Implementation
|
210 |
|
211 |
+
The package includes a simple HTML/JavaScript implementation that you can adapt for your project. You can find it in `whisperlivekit/web/live_transcription.html`, or load its content using the `get_web_interface_html()` function from `whisperlivekit`:
|
212 |
|
213 |
```python
|
214 |
+
from whisperlivekit import get_web_interface_html
|
215 |
+
|
216 |
+
# ... later in your code where you need the HTML string ...
|
217 |
+
html_content = get_web_interface_html()
|
218 |
```
|
219 |
|
220 |
## ⚙️ Configuration Reference
|