Spaces:
Sleeping
Sleeping
Update app.py
Browse files123125673451011121314151617181920212267891011122627282930313233341314371516171819434445464720212223484950515253542425265758596061626364272829303132336768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291303413235363738394013413513613713841424344454614014114214347484950145146515253148149150151152545556575815659from fastapi import FastAPI, UploadFile, File, WebSocket, WebSocketDisconnectfrom fastapi.responses import HTMLResponse, JSONResponsefrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi import FastAPI, UploadFile, File, WebSocketfrom pywhispercpp.model import Modelimport uvicornimport tempfileimport osimport uvicorn, tempfile, osfrom time import time
app = FastAPI(title="pyWhisperCPP Streaming API")
# Allow CORS (useful if you host frontend separately, but fine on Spaces too)app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],)
# Load Whisper.cpp model ONCE# OPTIONS: 'tiny.en', 'base.en', etc.app = FastAPI(title="pyWhisperCPP API")model = Model("base.en")
@app.get("/")def root(): return {"status": "Whisper.cpp API is running!"}
# ---------- Simple HTML frontend ----------@app.get("/", response_class=HTMLResponse)async def index(): # Serve the index.html file with open("index.html", "r", encoding="utf-8") as f: return HTMLResponse(f.read())
# ---------- Normal file upload transcription ----------@app.post("/transcribe")async def transcribe(file: UploadFile = File(...)): # Save uploaded audio temporarily with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp: temp.write(await file.read()) temp.flush() audio_path = temp.name
try: start = time() segments = model.transcribe(audio_path) text = " ".join(seg.text for seg in segments) elapsed = round(time() - start, 3) start = time() segments = model.transcribe(audio_path) text = " ".join(seg.text for seg in segments]) os.remove(audio_path)
return { "text": text, "processing_time_seconds": elapsed } finally: os.remove(audio_path)
return {"text": text}
# ---------- WebSocket streaming transcription ----------@app.websocket("/ws/transcribe_stream")async def websocket_transcription(websocket: WebSocket): """ Receives binary audio chunks (WebM/Opus) from the browser via WebSocket, periodically transcribes the buffered audio with Whisper.cpp, and sends back partial text. """# ================================# 🔥 Real-time streaming endpoint# ================================@app.websocket("/ws/live")async def websocket_live(websocket: WebSocket): await websocket.accept() buffer = b"" MIN_CHUNK_SIZE = 40_000 # bytes before running a transcription (tune this)
try: while True: message = await websocket.receive()
# Handle text messages (control) if "text" in message and message["text"] is not None: text_msg = message["text"] if text_msg == "__END__": # Finish stream break # Ignore other text controls for now continue
# Handle binary audio data chunk = message.get("bytes") if not chunk: continue
buffer += chunk
# When enough audio collected, transcribe if len(buffer) >= MIN_CHUNK_SIZE: with tempfile.NamedTemporaryFile(delete=False, suffix=".webm") as temp: temp.write(buffer) temp.flush() audio_path = temp.name
try: segments = model.transcribe(audio_path) text = " ".join(seg.text for seg in segments).strip() finally: os.remove(audio_path)
# Send partial transcript to client if text: await websocket.send_text(text)
# Clear buffer (or keep tail if you want overlap) buffer = b""
# End-of-stream message await websocket.send_text("[stream ended]") except WebSocketDisconnect: # Client disconnected pass finally: await websocket.close()
@app.websocket("/ws/transcribe_pcm")async def websocket_transcription_pcm(websocket: WebSocket): await websocket.accept() buffer = b"" SAMPLE_RATE = 16000 MIN_PCM_SIZE = SAMPLE_RATE * 2 * 3 # 3 seconds buffer
try: while True: chunk = await websocket.receive_bytes()
# If end control message (optional) if chunk == b"__END__": break
buffer += chunk while True: data = await websocket.receive_bytes() if data == b"__END__": break
buffer += data if len(buffer) >= MIN_PCM_SIZE: with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp: temp.write(buffer) temp.flush() audio_path = temp.name if len(buffer) > 32000: # ~1 second PCM16 16kHz with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp: temp.write(buffer) temp.flush() audio_path = temp.name
segments = model.transcribe(audio_path) text = " ".join(seg.text for seg in segments).strip() if text: await websocket.send_text(text) segments = model.transcribe(audio_path) text = " ".join(seg.text for seg in segments])
await websocket.send_text(text) buffer = b"" os.remove(audio_path) buffer = b"" os.remove(audio_path)
await websocket.send_text("[stream ended]") except: pass finally: await websocket.close() await websocket.send_text("[END]") await websocket.close()
if __name__ == "__main__": # For local testing. On Spaces, you don't usually run uvicorn manually. uvicorn.run(app, host="0.0.0.0", port=7860)app.py+33-131