Charan5775 commited on
Commit
b364fbe
·
verified ·
1 Parent(s): 824938d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -18
app.py CHANGED
@@ -2,7 +2,7 @@ from fastapi import FastAPI, UploadFile, File, WebSocket
2
  from pywhispercpp.model import Model
3
  import uvicorn, tempfile, os
4
  from time import time
5
-
6
  app = FastAPI(title="pyWhisperCPP API")
7
  model = Model("base.en")
8
 
@@ -27,33 +27,67 @@ async def transcribe(file: UploadFile = File(...)):
27
  # ================================
28
  # 🔥 Real-time streaming endpoint
29
  # ================================
 
 
 
 
 
 
 
 
 
 
 
 
30
  @app.websocket("/ws/live")
31
  async def websocket_live(websocket: WebSocket):
32
  await websocket.accept()
 
 
33
  buffer = b""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- while True:
36
- data = await websocket.receive_bytes()
37
- if data == b"__END__":
38
- break
39
 
40
- buffer += data
41
- if len(buffer) > 32000: # ~1 second PCM16 16kHz
42
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp:
43
- temp.write(buffer)
44
- temp.flush()
45
- audio_path = temp.name
46
 
47
- segments = model.transcribe(audio_path)
48
- text = " ".join(seg.text for seg in segments)
49
 
50
- await websocket.send_text(text)
51
- buffer = b""
52
- os.remove(audio_path)
53
 
54
- await websocket.send_text("[END]")
55
- await websocket.close()
56
 
 
 
 
57
 
58
  if __name__ == "__main__":
59
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
2
  from pywhispercpp.model import Model
3
  import uvicorn, tempfile, os
4
  from time import time
5
+ import wave
6
  app = FastAPI(title="pyWhisperCPP API")
7
  model = Model("base.en")
8
 
 
27
  # ================================
28
  # 🔥 Real-time streaming endpoint
29
  # ================================
30
+
31
+ def save_pcm16_as_wav(data: bytes, sample_rate=16000, channels=1):
32
+ """Convert PCM16 stream into a valid WAV file"""
33
+ temp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
34
+ with wave.open(temp.name, "wb") as wf:
35
+ wf.setnchannels(channels)
36
+ wf.setsampwidth(2) # PCM16 = 2 bytes per sample
37
+ wf.setframerate(sample_rate)
38
+ wf.writeframes(data)
39
+ return temp.name
40
+
41
+
42
  @app.websocket("/ws/live")
43
  async def websocket_live(websocket: WebSocket):
44
  await websocket.accept()
45
+ print("Client connected!")
46
+
47
  buffer = b""
48
+ SAMPLE_RATE = 16000
49
+ MIN_PCM_SIZE = SAMPLE_RATE * 2 * 2 # 2 seconds PCM16
50
+
51
+ try:
52
+ while True:
53
+ data = await websocket.receive()
54
+
55
+ # Text (control)
56
+ if data.get("text"):
57
+ if data["text"] == "__END__":
58
+ break
59
+ continue
60
+
61
+ # Binary PCM audio
62
+ chunk = data.get("bytes", b"")
63
+ if not chunk:
64
+ continue
65
+
66
+ buffer += chunk
67
+
68
+ if len(buffer) >= MIN_PCM_SIZE:
69
+ print(f"Transcribing {len(buffer)} bytes")
70
+
71
+ # Convert PCM16 → WAV with header
72
+ audio_path = save_pcm16_as_wav(buffer, SAMPLE_RATE)
73
 
74
+ segments = model.transcribe(audio_path)
75
+ text = " ".join(seg.text for seg in segments).strip()
 
 
76
 
77
+ os.remove(audio_path)
78
+ buffer = b""
 
 
 
 
79
 
80
+ if text:
81
+ await websocket.send_text(text)
82
 
83
+ await websocket.send_text("[END]")
 
 
84
 
85
+ except Exception as e:
86
+ print("WebSocket Error:", e)
87
 
88
+ finally:
89
+ await websocket.close()
90
+ print("Client disconnected")
91
 
92
  if __name__ == "__main__":
93
  uvicorn.run(app, host="0.0.0.0", port=7860)