File size: 1,665 Bytes
6c1314b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()