Spaces:
Running
Running
| import requests | |
| import time | |
| import json | |
| URL = "http://localhost:8000/api/v1/tts/stream" | |
| PAYLOAD = { | |
| "text": "The quick brown fox jumps over the lazy dog.", | |
| "voice": "en-US-AriaNeural", | |
| "speaking_rate": 1.0, | |
| "pitch": 0.0 | |
| } | |
| def test_api_stream(): | |
| print(f"Connecting to {URL}...") | |
| start = time.time() | |
| try: | |
| with requests.post(URL, json=PAYLOAD, stream=True) as r: | |
| print(f"Status: {r.status_code}") | |
| if r.status_code != 200: | |
| print(r.text) | |
| return | |
| iterator = r.iter_content(chunk_size=None) | |
| print("Request sent. Waiting for first chunk...") | |
| try: | |
| first_chunk = next(iterator) | |
| ttfb = time.time() - start | |
| print(f"FIRST CHUNK received after: {ttfb:.4f}s") | |
| print(f"First chunk size: {len(first_chunk)} bytes") | |
| except StopIteration: | |
| print("No content received.") | |
| return | |
| print("Consuming rest of stream...") | |
| total_bytes = len(first_chunk) | |
| chunks = 1 | |
| for chunk in iterator: | |
| total_bytes += len(chunk) | |
| chunks += 1 | |
| total_time = time.time() - start | |
| print(f"Total time: {total_time:.4f}s") | |
| print(f"Total bytes: {total_bytes}") | |
| print(f"Chunks: {chunks}") | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| if __name__ == "__main__": | |
| # Wait for server to be ready | |
| time.sleep(2) | |
| test_api_stream() | |