-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextToSpeech.py
More file actions
34 lines (26 loc) · 860 Bytes
/
Copy pathtextToSpeech.py
File metadata and controls
34 lines (26 loc) · 860 Bytes
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
import openai
from pathlib import Path
import pygame
# function converts text to speech
def derived_speech_from_text(text):
# Create a client using OpenAI API key
client = openai.OpenAI(api_key="sk-proj-f2t71ZJg3ebk88Sn56v5T3BlbkFJQE9k4CAPw7LJZFF4YXPL")
# Define the path to save the speech file
speech_file_path = Path(__file__).parent / "speech.mp3"
# Create speech using the OpenAI API
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input=text
)
# Save the response to a file
with open(speech_file_path, 'wb') as f:
f.write(response.content)
# Initialize pygame
pygame.mixer.init()
# Load and play the audio file
pygame.mixer.music.load(speech_file_path)
pygame.mixer.music.play()
# Keep the script running until the audio is finished
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(50)