Spaces:
Sleeping
Sleeping
| import requests | |
| import base64 | |
| import numpy as np | |
| import soundfile as sf | |
| import io | |
| def create_dummy_audio(): | |
| # Generate 1 second of silence/sine wave | |
| sr = 16000 | |
| t = np.linspace(0, 1.0, int(sr*1.0)) | |
| y = 0.5 * np.sin(2 * np.pi * 440 * t) # 440Hz sine wave | |
| # Save to memory buffer as WAV (librosa handles it fine, and easier than MP3 encoding without external tools valid in python-only) | |
| # The API expects MP3 but librosa.load can handle WAV if the mime type or header is detected, | |
| # or we can try to find a way to encode MP3 if essential. | |
| # The requirement says "Base64-encoded MP3 audio". | |
| # But usually decoders are flexible. Let's send a WAV and see if it works, | |
| # if strictly MP3 is enforced by a validator, we might fail. | |
| # But for our own logic: `librosa.load` supports whatever `soundfile` or `audioread` supports. | |
| buffer = io.BytesIO() | |
| sf.write(buffer, y, sr, format='WAV') | |
| buffer.seek(0) | |
| return buffer.read() | |
| def test_api(): | |
| url = "http://127.0.0.1:8000/detect" | |
| api_key = "my_secure_api_key_2024" | |
| audio_bytes = create_dummy_audio() | |
| b64_audio = base64.b64encode(audio_bytes).decode('utf-8') | |
| payload = { | |
| "audio_base64": b64_audio, | |
| "language": "en" | |
| } | |
| headers = { | |
| "X-API-Key": api_key, | |
| "Content-Type": "application/json" | |
| } | |
| try: | |
| response = requests.post(url, json=payload, headers=headers) | |
| print(f"Status Code: {response.status_code}") | |
| print(f"Response: {response.json()}") | |
| except Exception as e: | |
| print(f"Test Failed: {e}") | |
| if __name__ == "__main__": | |
| test_api() | |