import os, urllib.request, json, wave, io api_key = None if os.path.exists('.env'): with open('.env') as f: for line in f: if line.startswith('SARVAM_API_KEY='): api_key = line.strip().split('=', 1)[1].strip('"\' ') print("Testing with API Key:", api_key[:8] + "..." if api_key else "None") # Make a small 1s 16kHz mono wav wav_io = io.BytesIO() with wave.open(wav_io, 'wb') as wf: wf.setnchannels(1) wf.setsampwidth(2) wf.setframerate(16000) wf.writeframes(b'\x00\x00' * 16000) wav_bytes = wav_io.getvalue() for model_name in ['saaras:v4', 'saarika:v2.5']: print(f"\n--- Testing model: {model_name} ---") boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW' body = ( f'--{boundary}\r\n' f'Content-Disposition: form-data; name="file"; filename="audio.wav"\r\n' f'Content-Type: audio/wav\r\n\r\n' ).encode('utf-8') + wav_bytes + ( f'\r\n--{boundary}\r\n' f'Content-Disposition: form-data; name="model"\r\n\r\n' f'{model_name}\r\n' f'--{boundary}\r\n' f'Content-Disposition: form-data; name="language_code"\r\n\r\n' f'unknown\r\n' f'--{boundary}--\r\n' ).encode('utf-8') req = urllib.request.Request( 'https://api.sarvam.ai/speech-to-text', data=body, headers={ 'api-subscription-key': api_key, 'Content-Type': f'multipart/form-data; boundary={boundary}' } ) try: with urllib.request.urlopen(req) as resp: res_json = json.loads(resp.read().decode('utf-8')) print("Success! Response:", res_json) except urllib.error.HTTPError as e: print("HTTP Error:", e.code) print("Response text:", e.read().decode('utf-8')) except Exception as e: print("Error:", e)