PerryCheng614 commited on
Commit
9759803
1 Parent(s): b97cf3c

changed websocket binary type to base64

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import websockets
3
  import asyncio
4
  import json
 
5
 
6
  async def process_audio_stream(audio_path, max_tokens):
7
  """
@@ -12,14 +13,17 @@ async def process_audio_stream(audio_path, max_tokens):
12
  return
13
 
14
  try:
15
- # Read audio file
16
  with open(audio_path, 'rb') as f:
17
- audio_data = f.read()
18
-
 
 
 
19
  # Connect to WebSocket
20
  async with websockets.connect('ws://localhost:8330/ws/process-audio/') as websocket:
21
- # Send audio data
22
- await websocket.send(audio_data)
23
 
24
  # Send parameters
25
  await websocket.send(json.dumps({
@@ -30,15 +34,23 @@ async def process_audio_stream(audio_path, max_tokens):
30
  # Initialize response
31
  response = ""
32
 
33
- # Receive streaming tokens
34
  async for message in websocket:
35
- if message == "[DONE]":
36
- break
37
- response += message
38
- yield response
 
 
 
 
 
 
 
 
39
 
40
  except Exception as e:
41
- yield f"Error processing audio: {str(e)}"
42
 
43
  # Create Gradio interface
44
  demo = gr.Interface(
@@ -65,9 +77,5 @@ demo = gr.Interface(
65
  ]
66
  )
67
 
68
- def clear_output(audio, max_tokens):
69
- return ""
70
- demo.load_examples = clear_output
71
-
72
  if __name__ == "__main__":
73
- demo.queue().launch()
 
2
  import websockets
3
  import asyncio
4
  import json
5
+ import base64
6
 
7
  async def process_audio_stream(audio_path, max_tokens):
8
  """
 
13
  return
14
 
15
  try:
16
+ # Read audio file and convert to base64
17
  with open(audio_path, 'rb') as f:
18
+ audio_bytes = f.read()
19
+ base64_bytes = base64.b64encode(audio_bytes)
20
+ # Convert to binary for websocket
21
+ binary_data = base64_bytes
22
+
23
  # Connect to WebSocket
24
  async with websockets.connect('ws://localhost:8330/ws/process-audio/') as websocket:
25
+ # Send base64 encoded audio data
26
+ await websocket.send(binary_data)
27
 
28
  # Send parameters
29
  await websocket.send(json.dumps({
 
34
  # Initialize response
35
  response = ""
36
 
37
+ # Receive streaming response
38
  async for message in websocket:
39
+ try:
40
+ data = json.loads(message)
41
+ if data["status"] == "generating":
42
+ response += data["token"]
43
+ yield response
44
+ elif data["status"] == "complete":
45
+ break
46
+ elif data["status"] == "error":
47
+ yield f"Error: {data['error']}"
48
+ break
49
+ except json.JSONDecodeError:
50
+ continue
51
 
52
  except Exception as e:
53
+ yield f"Error connecting to server: {str(e)}"
54
 
55
  # Create Gradio interface
56
  demo = gr.Interface(
 
77
  ]
78
  )
79
 
 
 
 
 
80
  if __name__ == "__main__":
81
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860)