Spaces:
Running
Running
PerryCheng614
commited on
Commit
•
50ad4b3
1
Parent(s):
c68be50
change back to wss
Browse files
app.py
CHANGED
@@ -1,77 +1,61 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
3 |
import json
|
|
|
4 |
import os
|
5 |
|
6 |
-
API_KEY = os.getenv(
|
7 |
if not API_KEY:
|
8 |
-
raise ValueError("API_KEY
|
9 |
|
10 |
-
def process_audio_stream(audio_path, max_tokens):
|
11 |
"""
|
12 |
-
Process audio with streaming response via
|
13 |
"""
|
14 |
if not audio_path:
|
15 |
yield "Please upload or record an audio file first."
|
16 |
return
|
17 |
|
18 |
try:
|
19 |
-
# Read and
|
20 |
-
with open(audio_path, 'rb') as
|
21 |
-
|
22 |
-
|
23 |
-
}
|
24 |
-
data = {
|
25 |
-
'prompt': "",
|
26 |
-
'max_tokens': max_tokens
|
27 |
-
}
|
28 |
-
headers = {
|
29 |
-
'X-API-Key': API_KEY
|
30 |
-
}
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
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 |
-
|
47 |
-
token_count = 0
|
48 |
|
49 |
-
#
|
50 |
-
for
|
51 |
-
|
52 |
-
|
53 |
-
if
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
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
|
73 |
|
74 |
-
# Create Gradio interface
|
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 |
-
|
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)
|
|
|
|
|
|
|
|
|
|
|
|