Spaces:
Paused
Paused
HARI PREETHAM commited on
Commit ·
cd14d03
1
Parent(s): c736ef6
feat: stable optimized backend with large-v3-turbo and loop prevention
Browse files
app.py
CHANGED
|
@@ -15,7 +15,7 @@ from pathlib import Path
|
|
| 15 |
from typing import Optional, Dict, Any, List
|
| 16 |
from collections import deque
|
| 17 |
|
| 18 |
-
from fastapi import FastAPI, File, UploadFile, HTTPException, Form
|
| 19 |
from fastapi.responses import Response
|
| 20 |
from fastapi.middleware.cors import CORSMiddleware
|
| 21 |
from pydantic import BaseModel
|
|
@@ -412,72 +412,6 @@ async def transcribe_audio(
|
|
| 412 |
if temp_path and os.path.exists(temp_path):
|
| 413 |
os.unlink(temp_path)
|
| 414 |
|
| 415 |
-
# --- WEBSOCKET STREAMING ---
|
| 416 |
-
|
| 417 |
-
@app.websocket("/api/transcribe/stream")
|
| 418 |
-
async def websocket_transcribe(websocket: WebSocket, language: str = "en"):
|
| 419 |
-
await websocket.accept()
|
| 420 |
-
logger.info(f"New WebSocket connection for streaming transcription (Language: {language})")
|
| 421 |
-
|
| 422 |
-
model = load_whisper_model()
|
| 423 |
-
# Get the initial prompt for the language if available
|
| 424 |
-
initial_prompt = LANGUAGE_PROMPTS.get(language, "")
|
| 425 |
-
|
| 426 |
-
audio_buffer = bytearray()
|
| 427 |
-
|
| 428 |
-
try:
|
| 429 |
-
while True:
|
| 430 |
-
# Receive binary audio data
|
| 431 |
-
data = await websocket.receive_bytes()
|
| 432 |
-
if not data:
|
| 433 |
-
break
|
| 434 |
-
|
| 435 |
-
audio_buffer.extend(data)
|
| 436 |
-
|
| 437 |
-
# Process if buffer is large enough (e.g., > 1 sec at 16k mono = 32000 bytes)
|
| 438 |
-
if len(audio_buffer) > 48000: # ~1.5 seconds of audio
|
| 439 |
-
# Write buffer to a temporary file for Whisper
|
| 440 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
| 441 |
-
tmp.write(audio_buffer)
|
| 442 |
-
tmp_path = tmp.name
|
| 443 |
-
|
| 444 |
-
try:
|
| 445 |
-
# Transcribe the current buffer
|
| 446 |
-
segments, info = model.transcribe(
|
| 447 |
-
tmp_path,
|
| 448 |
-
task="transcribe",
|
| 449 |
-
language=language,
|
| 450 |
-
initial_prompt=initial_prompt,
|
| 451 |
-
vad_filter=True,
|
| 452 |
-
vad_parameters=dict(min_silence_duration_ms=400),
|
| 453 |
-
condition_on_previous_text=False
|
| 454 |
-
)
|
| 455 |
-
|
| 456 |
-
full_text = " ".join([s.text.strip() for s in segments]).strip()
|
| 457 |
-
|
| 458 |
-
# Send result back
|
| 459 |
-
await websocket.send_json({
|
| 460 |
-
"text": full_text,
|
| 461 |
-
"language": info.language,
|
| 462 |
-
"confidence": info.language_probability,
|
| 463 |
-
"is_final": False # Streaming is continuous
|
| 464 |
-
})
|
| 465 |
-
|
| 466 |
-
# Keep the buffer for context (max 30 seconds)
|
| 467 |
-
if len(audio_buffer) > 960000: # 30 seconds
|
| 468 |
-
audio_buffer = audio_buffer[-480000:] # Keep last 15 seconds
|
| 469 |
-
|
| 470 |
-
finally:
|
| 471 |
-
if os.path.exists(tmp_path):
|
| 472 |
-
os.unlink(tmp_path)
|
| 473 |
-
|
| 474 |
-
except WebSocketDisconnect:
|
| 475 |
-
logger.info("WebSocket disconnected")
|
| 476 |
-
except Exception as e:
|
| 477 |
-
logger.error(f"WebSocket error: {e}")
|
| 478 |
-
finally:
|
| 479 |
-
logger.info("Cleaning up WebSocket connection")
|
| 480 |
-
|
| 481 |
# --- SPECIALIZED WHISPER ENDPOINTS ---
|
| 482 |
|
| 483 |
@app.post("/api/transcribe/azure", response_model=TranscriptionResponse)
|
|
|
|
| 15 |
from typing import Optional, Dict, Any, List
|
| 16 |
from collections import deque
|
| 17 |
|
| 18 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Form
|
| 19 |
from fastapi.responses import Response
|
| 20 |
from fastapi.middleware.cors import CORSMiddleware
|
| 21 |
from pydantic import BaseModel
|
|
|
|
| 412 |
if temp_path and os.path.exists(temp_path):
|
| 413 |
os.unlink(temp_path)
|
| 414 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
# --- SPECIALIZED WHISPER ENDPOINTS ---
|
| 416 |
|
| 417 |
@app.post("/api/transcribe/azure", response_model=TranscriptionResponse)
|