PerryCheng614 commited on
Commit
50ad4b3
1 Parent(s): c68be50

change back to wss

Browse files
Files changed (1) hide show
  1. app.py +39 -61
app.py CHANGED
@@ -1,77 +1,61 @@
1
  import gradio as gr
2
- import requests
 
3
  import json
 
4
  import os
5
 
6
- API_KEY = os.getenv("API_KEY")
7
  if not API_KEY:
8
- raise ValueError("API_KEY environment variable must be set")
9
 
10
- def process_audio_stream(audio_path, max_tokens):
11
  """
12
- Process audio with streaming response via HTTP
13
  """
14
  if not audio_path:
15
  yield "Please upload or record an audio file first."
16
  return
17
 
18
  try:
19
- # Read and prepare audio file
20
- with open(audio_path, 'rb') as audio_file:
21
- files = {
22
- 'audio_file': ('audio.wav', audio_file, 'audio/wav')
23
- }
24
- data = {
25
- 'prompt': "",
26
- 'max_tokens': max_tokens
27
- }
28
- headers = {
29
- 'X-API-Key': API_KEY
30
- }
31
 
32
- # Make streaming request
33
- response = requests.post(
34
- 'https://nexa-omni.nexa4ai.com/process-audio/',
35
- files=files,
36
- data=data,
37
- headers=headers,
38
- stream=True
39
- )
 
 
40
 
41
- if response.status_code != 200:
42
- yield f"Error: Server returned status code {response.status_code}"
43
- return
44
-
45
  # Initialize response
46
- response_text = ""
47
- token_count = 0
48
 
49
- # Process the streaming response
50
- for line in response.iter_lines():
51
- if line:
52
- line = line.decode('utf-8')
53
- if line.startswith('data: '):
54
- try:
55
- data = json.loads(line[6:]) # Skip 'data: ' prefix
56
- if data["status"] == "generating":
57
- if token_count < 3 and data["token"] in [" ", " \n", "\n", "<|im_start|>", "assistant"]:
58
- token_count += 1
59
- continue
60
- response_text += data["token"]
61
- gr.update(value=response_text)
62
- yield response_text
63
- elif data["status"] == "complete":
64
- break
65
- elif data["status"] == "error":
66
- yield f"Error: {data['error']}"
67
- break
68
- except json.JSONDecodeError:
69
- continue
70
 
71
  except Exception as e:
72
- yield f"Error processing request: {str(e)}"
73
 
74
- # Create Gradio interface with specific queue configurations
75
  demo = gr.Interface(
76
  fn=process_audio_stream,
77
  inputs=[
@@ -108,10 +92,4 @@ demo = gr.Interface(
108
  )
109
 
110
  if __name__ == "__main__":
111
- # Configure the queue for better streaming performance
112
- demo.queue(
113
- max_size=20,
114
- ).launch(
115
- server_name="0.0.0.0",
116
- server_port=7860,
117
- )
 
1
  import gradio as gr
2
+ import websockets
3
+ import asyncio
4
  import json
5
+ import base64
6
  import os
7
 
8
+ API_KEY = os.getenv('API_KEY')
9
  if not API_KEY:
10
+ raise ValueError("API_KEY must be set in environment variables")
11
 
12
+ async def process_audio_stream(audio_path, max_tokens):
13
  """
14
+ Process audio with streaming response via WebSocket
15
  """
16
  if not audio_path:
17
  yield "Please upload or record an audio file first."
18
  return
19
 
20
  try:
21
+ # Read audio file and convert to base64 bytes
22
+ with open(audio_path, 'rb') as f:
23
+ audio_bytes = f.read()
24
+ base64_bytes = base64.b64encode(audio_bytes)
 
 
 
 
 
 
 
 
25
 
26
+ # Connect to WebSocket
27
+ async with websockets.connect('wss://nexa-omni.nexa4ai.com/ws/process-audio/?api_key=' + API_KEY) as websocket:
28
+ # Send binary base64 audio data as bytes
29
+ await websocket.send(base64_bytes) # Send the raw base64 bytes
30
+
31
+ # Send parameters as JSON string
32
+ await websocket.send(json.dumps({
33
+ "prompt": "",
34
+ "max_tokens": max_tokens
35
+ }))
36
 
 
 
 
 
37
  # Initialize response
38
+ response = ""
 
39
 
40
+ # Receive streaming response
41
+ async for message in websocket:
42
+ try:
43
+ data = json.loads(message)
44
+ if data["status"] == "generating":
45
+ response += data["token"]
46
+ yield response
47
+ elif data["status"] == "complete":
48
+ break
49
+ elif data["status"] == "error":
50
+ yield f"Error: {data['error']}"
51
+ break
52
+ except json.JSONDecodeError:
53
+ continue
 
 
 
 
 
 
 
54
 
55
  except Exception as e:
56
+ yield f"Error connecting to server: {str(e)}"
57
 
58
+ # Create Gradio interface
59
  demo = gr.Interface(
60
  fn=process_audio_stream,
61
  inputs=[
 
92
  )
93
 
94
  if __name__ == "__main__":
95
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860)