-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeak.py
29 lines (24 loc) · 1.04 KB
/
Speak.py
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
import requests # pip install requests
from playsound import playsound # pip install playsound==1.2.2
import os
from typing import Union # pip install typing
from os import getcwd
def generate_audio(message: str,voice : str = "Aditi"):
url: str = f"https://api.streamelements.com/kappa/v2/speech?voice={voice}&text={{{message}}}"
headers = {'User-Agent':'Mozilla/5.0(Maciontosh;intel Mac OS X 10_15_7)AppleWebKit/537.36(KHTML,like Gecoko)Chrome/119.0.0.0 Safari/537.36'}
try:
result = requests.get(url=url, headers=headers)
return result.content
except:
return None
def speak(message: str, voice: str = "Aditi", folder: str = "", extension: str = ".mp3") -> Union[None,str]:
try:
result_content = generate_audio(message,voice)
file_path = os.path.join(folder,rf"{getcwd()}\{voice}{extension}")
with open(file_path,"wb") as file:
file.write(result_content)
playsound(file_path)
os.remove(file_path)
return None
except Exception as e:
print(e)