nuernie commited on
Commit
e0c9adf
·
1 Parent(s): f486429

adjust app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -10
app.py CHANGED
@@ -3,6 +3,8 @@ from contextlib import asynccontextmanager
3
  import uvicorn
4
  from whisper_live.server import TranscriptionServer
5
  import logging
 
 
6
 
7
  # Configure logging
8
  logging.basicConfig(level=logging.INFO)
@@ -39,27 +41,76 @@ async def root():
39
  @app.websocket("/ws")
40
  async def websocket_endpoint(websocket: WebSocket):
41
  await websocket.accept()
 
 
42
  try:
43
- # Start the transcription server with the WebSocket connection
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  transcription_server.run(
45
  host="0.0.0.0",
46
- port=7860, # Hugging Face Spaces uses port 7860
47
- backend="faster_whisper", # Using faster_whisper as the backend
48
- single_model=True # Use single model mode for better resource usage
 
 
 
 
49
  )
50
 
51
- # Handle WebSocket communication
52
  while True:
53
  data = await websocket.receive()
54
- if data.get("type") == "websocket.disconnect":
 
55
  break
56
- # Process the received data through the transcription server
57
- # Note: You might need to adapt this part based on your specific needs
58
- await websocket.send_text(data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  except Exception as e:
60
  logger.error(f"WebSocket error: {e}")
 
 
 
 
 
 
 
 
61
  finally:
62
- await websocket.close()
 
 
 
63
 
64
  @app.get("/health")
65
  def health_check():
 
3
  import uvicorn
4
  from whisper_live.server import TranscriptionServer
5
  import logging
6
+ import json
7
+ import numpy as np
8
 
9
  # Configure logging
10
  logging.basicConfig(level=logging.INFO)
 
41
  @app.websocket("/ws")
42
  async def websocket_endpoint(websocket: WebSocket):
43
  await websocket.accept()
44
+ client_uid = None
45
+
46
  try:
47
+ # Receive initial configuration
48
+ config = await websocket.receive_json()
49
+ client_uid = config.get("uid")
50
+
51
+ if not client_uid:
52
+ await websocket.close(code=4000, reason="No client UID provided")
53
+ return
54
+
55
+ # Send ready message
56
+ await websocket.send_json({
57
+ "uid": client_uid,
58
+ "message": "SERVER_READY",
59
+ "backend": "faster_whisper"
60
+ })
61
+
62
+ # Start the transcription server
63
  transcription_server.run(
64
  host="0.0.0.0",
65
+ port=7860,
66
+ backend="faster_whisper",
67
+ single_model=True,
68
+ language=config.get("language", "de"),
69
+ task=config.get("task", "transcribe"),
70
+ model=config.get("model", "tiny"),
71
+ use_vad=config.get("use_vad", True)
72
  )
73
 
74
+ # Handle incoming audio data
75
  while True:
76
  data = await websocket.receive()
77
+
78
+ if data["type"] == "websocket.disconnect":
79
  break
80
+
81
+ if data["type"] == "websocket.receive":
82
+ if isinstance(data.get("bytes"), bytes):
83
+ # Process binary audio data
84
+ audio_data = np.frombuffer(data["bytes"], dtype=np.float32)
85
+ # Process audio data through transcription server
86
+ segments = transcription_server.process_audio(audio_data)
87
+
88
+ if segments:
89
+ await websocket.send_json({
90
+ "uid": client_uid,
91
+ "segments": segments
92
+ })
93
+ elif isinstance(data.get("text"), str):
94
+ # Handle text messages (like END_OF_AUDIO)
95
+ msg = data["text"]
96
+ if msg == "END_OF_AUDIO":
97
+ break
98
+
99
  except Exception as e:
100
  logger.error(f"WebSocket error: {e}")
101
+ if client_uid:
102
+ try:
103
+ await websocket.send_json({
104
+ "uid": client_uid,
105
+ "error": str(e)
106
+ })
107
+ except:
108
+ pass
109
  finally:
110
+ try:
111
+ await websocket.close()
112
+ except:
113
+ pass
114
 
115
  @app.get("/health")
116
  def health_check():