Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ngoc #18

Merged
merged 4 commits into from
Aug 1, 2023
Merged

Ngoc #18

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions python/text-to-speech/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ def validate_request(self, req: requests) -> None:

def get_token(self, subscription_key):
"""Grabs token with subscription key for Azure."""
fetch_token_url = 'https://westus.api.cognitive.microsoft.com/sts/v1.0/issuetoken'
fetch_token_url = (
"https://westus.api.cognitive.microsoft.com/sts/v1.0/issuetoken"
)
headers = {
'Ocp-Apim-Subscription-Key': subscription_key
"Ocp-Apim-Subscription-Key": subscription_key
}
response = requests.post(fetch_token_url, headers=headers, timeout=10)
access_token = str(response.text)
Expand All @@ -120,15 +122,27 @@ def speech(self, text: str, language: str) -> bytes:
Returns:
bytes: The synthezied speech in bytes.
"""
url = f"https://{self.region_key}.tts.speech.microsoft.com/cognitiveservices/v1"
url = (
f"https://{self.region_key}."
f"tts.speech.microsoft.com/cognitiveservices/v1"
)
headers_azure = {
'Content-type': 'application/ssml+xml',
# 'Ocp-Apim-Subscription-Key': self.api_key,
'Authorization': 'Bearer ' + self.get_token(self.api_key),
'X-Microsoft-OutputFormat': 'audio-16khz-32kbitrate-mono-mp3',
"Content-type": "application/ssml+xml",
"Authorization": "Bearer " + self.get_token(self.api_key),
"X-Microsoft-OutputFormat": "audio-16khz-32kbitrate-mono-mp3",
}
data_azure = f"<speak version='1.0' xml:lang='{language}'><voice xml:lang='{language}' xml:gender='Male' name='en-US-ChristopherNeural'>{text}</voice></speak>"
response = requests.request("POST", url, headers=headers_azure, data=data_azure, timeout=10)
data_azure = (
f"<speak version='1.0' xml:lang='{language}'><voice"
f"xml:lang='{language}' xml:gender='Male'"
f"name='en-US-ChristopherNeural'>{text}</voice></speak>"
)
response = requests.request(
"POST",
url,
headers=headers_azure,
data=data_azure,
timeout=10,
)
response.raise_for_status()
return response.content

Expand Down
1 change: 1 addition & 0 deletions python/text-to-speech/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
boto3==1.28.9
google-cloud-texttospeech==2.14.1
# azure-cognitiveservices-speech==1.30.0
parameterized==0.9.0
requests==2.31.0
1 change: 1 addition & 0 deletions python/text-to-speech/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def get_aws_instance(self, key, secret_key):
(None, None), # Missing Both
])
def test_validate_request(self, key, secret_key):

self.assertRaises(ValueError, self.get_aws_instance, key, secret_key)

def test_speech_happy(self):
Expand Down