PerryCheng614 commited on
Commit
22c5bdb
1 Parent(s): 6155b02

support streaming

Browse files
Files changed (1) hide show
  1. app.py +33 -37
app.py CHANGED
@@ -1,50 +1,47 @@
1
  import gradio as gr
2
- import requests
3
- import os
 
4
 
5
- # FastAPI endpoint
6
- API_URL = "https://nexa-omni.nexa4ai.com/process-audio/"
7
-
8
- # Add this global variable to track the last valid audio file
9
- last_valid_audio = None
10
-
11
- def process_audio(audio_path, max_tokens):
12
  """
13
- Send audio file to FastAPI backend for processing
14
  """
15
- global last_valid_audio
16
-
17
- # Check audio file availability
18
- if audio_path and os.path.exists(audio_path):
19
- # New audio uploaded/recorded
20
- last_valid_audio = audio_path
21
- elif not audio_path and not last_valid_audio:
22
- # No audio provided and no previous valid audio
23
  return "Please upload or record an audio file first."
24
 
25
- # Use the appropriate audio path
26
- current_audio = last_valid_audio if last_valid_audio else audio_path
27
-
28
  try:
29
- # Only proceed if we have a valid audio file
30
- if current_audio and os.path.exists(current_audio):
31
- files = {
32
- 'file': ('audio.wav', open(current_audio, 'rb'), 'audio/wav')
33
- }
34
- data = {'max_tokens': max_tokens}
 
 
 
 
 
 
 
 
35
 
36
- response = requests.post(API_URL, files=files, data=data)
37
- response.raise_for_status()
38
 
39
- return response.json()['response']
40
- else:
41
- return "No valid audio file available."
 
 
 
 
42
  except Exception as e:
43
- return f"Error processing audio: {str(e)}"
44
 
45
  # Create Gradio interface
46
  demo = gr.Interface(
47
- fn=process_audio,
48
  inputs=[
49
  gr.Audio(
50
  type="filepath",
@@ -59,12 +56,11 @@ demo = gr.Interface(
59
  label="Max Tokens"
60
  )
61
  ],
62
- outputs=gr.Textbox(label="Response"),
63
  title="Nexa Omni",
64
  description="Upload an audio file and optionally provide a prompt to analyze the audio content.",
65
  examples=[
66
  ["example_audios/example_1.wav", 200],
67
- # ["example_audios/example_2.wav", 100],
68
  ]
69
  )
70
 
@@ -73,4 +69,4 @@ def clear_output(audio, max_tokens):
73
  demo.load_examples = clear_output
74
 
75
  if __name__ == "__main__":
76
- demo.launch()
 
1
  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
  """
8
+ Process audio with streaming response via WebSocket
9
  """
10
+ if not audio_path:
 
 
 
 
 
 
 
11
  return "Please upload or record an audio file first."
12
 
 
 
 
13
  try:
14
+ # Read audio file
15
+ with open(audio_path, 'rb') as f:
16
+ audio_data = f.read()
17
+
18
+ # Connect to WebSocket
19
+ async with websockets.connect('ws://localhost:8330/ws/process-audio/') as websocket:
20
+ # Send audio data
21
+ await websocket.send(audio_data)
22
+
23
+ # Send parameters
24
+ await websocket.send(json.dumps({
25
+ "prompt": "",
26
+ "max_tokens": max_tokens
27
+ }))
28
 
29
+ # Initialize response
30
+ response = ""
31
 
32
+ # Receive streaming tokens
33
+ async for message in websocket:
34
+ if message == "[DONE]":
35
+ break
36
+ response += message
37
+ yield response
38
+
39
  except Exception as e:
40
+ yield f"Error processing audio: {str(e)}"
41
 
42
  # Create Gradio interface
43
  demo = gr.Interface(
44
+ fn=process_audio_stream,
45
  inputs=[
46
  gr.Audio(
47
  type="filepath",
 
56
  label="Max Tokens"
57
  )
58
  ],
59
+ outputs=gr.Textbox(label="Response", interactive=False),
60
  title="Nexa Omni",
61
  description="Upload an audio file and optionally provide a prompt to analyze the audio content.",
62
  examples=[
63
  ["example_audios/example_1.wav", 200],
 
64
  ]
65
  )
66
 
 
69
  demo.load_examples = clear_output
70
 
71
  if __name__ == "__main__":
72
+ demo.queue().launch()