import urllib.request, json, wave, io, os api_key = None with open('.env', encoding='utf-8') as f: for line in f: if line.startswith('SARVAM_API_KEY='): api_key = line.strip().split('=', 1)[1].strip('"\' ') 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() 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'saaras:v4\r\n' f'--{boundary}\r\n' f'Content-Disposition: form-data; name="with_timestamps"\r\n\r\n' f'true\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.loads(resp.read().decode('utf-8')) print(json.dumps(res, indent=2)) except urllib.error.HTTPError as e: print(e.read().decode('utf-8'))