| 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_audio(audio_np) |
| |
| time.sleep(0.01) |
| |
| return np.concatenate(audio_chunks) if audio_chunks else None |
|
|
| |
| 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) |
|
|