PerryCheng614 commited on
Commit
c68be50
1 Parent(s): 81ebbfd

change to http

Browse files
Files changed (1) hide show
  1. app.py +64 -38
app.py CHANGED
@@ -1,56 +1,77 @@
1
  import gradio as gr
2
- import websockets
3
- import asyncio
4
  import json
5
- import base64
6
 
7
- async def process_audio_stream(audio_path, max_tokens):
 
 
 
 
8
  """
9
- Process audio with streaming response via WebSocket
10
  """
11
  if not audio_path:
12
  yield "Please upload or record an audio file first."
13
  return
14
 
15
  try:
16
- # Read audio file and convert to base64 bytes
17
- with open(audio_path, 'rb') as f:
18
- audio_bytes = f.read()
19
- base64_bytes = base64.b64encode(audio_bytes)
20
-
21
- # Connect to WebSocket
22
- async with websockets.connect('wss://nexa-omni.nexa4ai.com/ws/process-audio/') as websocket:
23
- # Send binary base64 audio data as bytes
24
- await websocket.send(base64_bytes) # Send the raw base64 bytes
 
 
 
25
 
26
- # Send parameters as JSON string
27
- await websocket.send(json.dumps({
28
- "prompt": "",
29
- "max_tokens": max_tokens
30
- }))
 
 
 
31
 
 
 
 
 
32
  # Initialize response
33
- response = ""
 
34
 
35
- # Receive streaming response
36
- async for message in websocket:
37
- try:
38
- data = json.loads(message)
39
- if data["status"] == "generating":
40
- response += data["token"]
41
- yield response
42
- elif data["status"] == "complete":
43
- break
44
- elif data["status"] == "error":
45
- yield f"Error: {data['error']}"
46
- break
47
- except json.JSONDecodeError:
48
- continue
 
 
 
 
 
 
 
49
 
50
  except Exception as e:
51
- yield f"Error connecting to server: {str(e)}"
52
 
53
- # Create Gradio interface
54
  demo = gr.Interface(
55
  fn=process_audio_stream,
56
  inputs=[
@@ -70,7 +91,6 @@ demo = gr.Interface(
70
  outputs=gr.Textbox(label="Response", interactive=False),
71
  title="NEXA OmniAudio-2.6B",
72
  description=f"""
73
-
74
  OmniAudio-2.6B is a compact audio-language model optimized for edge deployment.
75
 
76
  Model Repo: <a href="https://huggingface.co/NexaAIDev/OmniAudio-2.6B">NexaAIDev/OmniAudio-2.6B</a>
@@ -88,4 +108,10 @@ demo = gr.Interface(
88
  )
89
 
90
  if __name__ == "__main__":
91
- demo.queue().launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
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=[
 
91
  outputs=gr.Textbox(label="Response", interactive=False),
92
  title="NEXA OmniAudio-2.6B",
93
  description=f"""
 
94
  OmniAudio-2.6B is a compact audio-language model optimized for edge deployment.
95
 
96
  Model Repo: <a href="https://huggingface.co/NexaAIDev/OmniAudio-2.6B">NexaAIDev/OmniAudio-2.6B</a>
 
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
+ )