import requests import os from dotenv import load_dotenv import json #import pyttsx3 load_dotenv() API_KEY = os.getenv("DG_API_KEY") AUDIO_FILE=r".\media\ouput_file.mp3" #engine = pyttsx3.init() #voices = engine.getProperty('voices') #engine.setProperty('voice', voices[1].id) #def text_to_speech2(llm_response): # engine.save_to_file(llm_response, AUDIO_FILE) # engine.runAndWait() def text_to_speech(llm_response): # Define the API endpoint url = "https://api.deepgram.com/v1/speak?model=aura-asteria-en" # Define the headers headers = { "Authorization": f"Token {API_KEY}", "Content-Type": "application/json" } # Define the payload payload = { "text": llm_response } # Make the POST request response = requests.post(url, headers=headers, json=payload) # Check if the request was successful if response.status_code == 200: # Save the response content to a file with open(AUDIO_FILE, "wb") as f: f.write(response.content) print("File saved successfully.") else: print(f"Error: {response.status_code} - {response.text}") # Example usage #transcribed_text = "Hello, how can I help you today?" #tts(transcribed_text)