File size: 1,651 Bytes
2d00549 3abafc4 2d00549 3abafc4 2d00549 3abafc4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
from handler import EndpointHandler
import requests
import base64
import numpy as np
import sounddevice as sd
import time
my_handler = EndpointHandler('')
def play_audio(audio_data, sample_rate=16000):
sd.play(audio_data, sample_rate)
sd.wait()
def stream_audio(session_id):
audio_chunks = []
while True:
continue_payload = {
"request_type": "continue",
"session_id": session_id
}
response = my_handler(continue_payload)
if response["status"] == "completed" and response["output"] is None:
break
if response["output"]:
audio_bytes = base64.b64decode(response["output"])
audio_np = np.frombuffer(audio_bytes, dtype=np.int16)
audio_chunks.append(audio_np)
# Play the chunk immediately (optional)
play_audio(audio_np)
time.sleep(0.01) # Small delay to prevent overwhelming the server
return np.concatenate(audio_chunks) if audio_chunks else None
# Test with text input
text_payload = {
"request_type": "start",
"inputs": "Tell me a cool fact about Messi.",
"input_type": "text",
}
start_response = my_handler(text_payload)
if "session_id" in start_response:
print(f"Session started. Session ID: {start_response['session_id']}")
print("Streaming audio response...")
full_audio = stream_audio(start_response['session_id'])
if full_audio is not None:
print("Received complete audio response. Playing...")
else:
print("No audio received.")
else:
print("Error:", start_response)
|