Spaces:
Running
Running
PerryCheng614
commited on
Commit
•
c68be50
1
Parent(s):
81ebbfd
change to http
Browse files
app.py
CHANGED
@@ -1,56 +1,77 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import asyncio
|
4 |
import json
|
5 |
-
import
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
"""
|
9 |
-
Process audio with streaming response via
|
10 |
"""
|
11 |
if not audio_path:
|
12 |
yield "Please upload or record an audio file first."
|
13 |
return
|
14 |
|
15 |
try:
|
16 |
-
# Read
|
17 |
-
with open(audio_path, 'rb') as
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
31 |
|
|
|
|
|
|
|
|
|
32 |
# Initialize response
|
33 |
-
|
|
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
if data
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
except Exception as e:
|
51 |
-
yield f"Error
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
)
|