| import requests | |
| import base64 | |
| class NovaSession: | |
| def __init__(self): | |
| self.session_url = "https://api.novasonic.com/start_session" | |
| self.audio_to_text_url = "https://api.novasonic.com/audio_to_text" | |
| self.text_to_audio_url = "https://api.novasonic.com/text_to_audio" | |
| def start_session(self): | |
| response = requests.post(self.session_url, json={ | |
| "inferenceConfiguration": { | |
| "maxTokens": 1024, | |
| "topP": 0.9, | |
| "temperature": 0.7 | |
| } | |
| }) | |
| return response.json().get("session_id", "new_session") | |
| def audio_to_text(self, audio_path): | |
| with open(audio_path, "rb") as f: | |
| encoded_audio = base64.b64encode(f.read()).decode() | |
| response = requests.post(self.audio_to_text_url, json={"audio": encoded_audio}) | |
| return response.json().get("text", "") | |
| def text_to_audio(self, text): | |
| response = requests.post(self.text_to_audio_url, json={"text": text}) | |
| audio_content = base64.b64decode(response.json().get("audio", "")) | |
| output_path = "/tmp/nova_response.wav" | |
| with open(output_path, "wb") as f: | |
| f.write(audio_content) | |
| return output_path |