Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,70 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
from pywhispercpp.model import Model
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
-
app = FastAPI(title="
|
| 8 |
|
| 9 |
-
# Load tiny model once at startup
|
| 10 |
-
#model = Model("tiny.en-q5_1")
|
| 11 |
model = Model('base.en')
|
|
|
|
|
|
|
| 12 |
@app.get("/")
|
| 13 |
-
|
| 14 |
-
return {"status": "Whisper
|
|
|
|
| 15 |
|
| 16 |
-
# --------- File Upload Transcription ---------
|
| 17 |
@app.post("/transcribe")
|
| 18 |
-
async def
|
| 19 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp:
|
| 20 |
temp.write(await file.read())
|
| 21 |
temp.flush()
|
| 22 |
audio_path = temp.name
|
| 23 |
|
|
|
|
| 24 |
segments = model.transcribe(audio_path)
|
|
|
|
|
|
|
| 25 |
os.remove(audio_path)
|
| 26 |
|
| 27 |
-
text
|
| 28 |
-
return {"text": text}
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
while True:
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp:
|
| 40 |
-
temp.write(
|
| 41 |
temp.flush()
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
await
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, WebSocket, UploadFile, File
|
|
|
|
| 2 |
from pywhispercpp.model import Model
|
| 3 |
+
import uvicorn
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
+
from time import time
|
| 7 |
|
| 8 |
+
app = FastAPI(title="pyWhisperCPP API")
|
| 9 |
|
|
|
|
|
|
|
| 10 |
model = Model('base.en')
|
| 11 |
+
|
| 12 |
+
|
| 13 |
@app.get("/")
|
| 14 |
+
def root():
|
| 15 |
+
return {"status": "Whisper.cpp API is running!"}
|
| 16 |
+
|
| 17 |
|
|
|
|
| 18 |
@app.post("/transcribe")
|
| 19 |
+
async def transcribe(file: UploadFile = File(...)):
|
| 20 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp:
|
| 21 |
temp.write(await file.read())
|
| 22 |
temp.flush()
|
| 23 |
audio_path = temp.name
|
| 24 |
|
| 25 |
+
start = time()
|
| 26 |
segments = model.transcribe(audio_path)
|
| 27 |
+
text = " ".join([seg.text for seg in segments])
|
| 28 |
+
elapsed = round(time() - start, 3)
|
| 29 |
os.remove(audio_path)
|
| 30 |
|
| 31 |
+
return {"text": text, "processing_time_seconds": elapsed}
|
|
|
|
| 32 |
|
| 33 |
+
|
| 34 |
+
# ==========================================================
|
| 35 |
+
# 🔥 Real-time Speech Recognition (WebSocket)
|
| 36 |
+
# ==========================================================
|
| 37 |
+
@app.websocket("/ws/transcribe_stream")
|
| 38 |
+
async def websocket_transcription(websocket: WebSocket):
|
| 39 |
+
await websocket.accept()
|
| 40 |
+
buffer = b""
|
| 41 |
|
| 42 |
while True:
|
| 43 |
+
chunk = await websocket.receive_bytes()
|
| 44 |
+
if chunk == b"__END__":
|
| 45 |
+
break
|
| 46 |
|
| 47 |
+
buffer += chunk
|
| 48 |
+
|
| 49 |
+
# When buffer > 1 sec of audio, transcribe
|
| 50 |
+
if len(buffer) >= 16000 * 2: # 16kHz * 2 bytes = 1 second PCM16
|
| 51 |
+
# Save buffer temporarily
|
| 52 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp:
|
| 53 |
+
temp.write(buffer)
|
| 54 |
temp.flush()
|
| 55 |
+
audio_path = temp.name
|
| 56 |
|
| 57 |
+
segments = model.transcribe(audio_path)
|
| 58 |
+
text = " ".join([seg.text for seg in segments])
|
| 59 |
|
| 60 |
+
await websocket.send_text(text)
|
| 61 |
+
buffer = b"" # clear for next batch
|
| 62 |
+
|
| 63 |
+
os.remove(audio_path)
|
| 64 |
+
|
| 65 |
+
await websocket.send_text("stream_end")
|
| 66 |
+
await websocket.close()
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|