Skip to content

Commit

Permalink
Configure MS Translator API region
Browse files Browse the repository at this point in the history
  • Loading branch information
patkub committed Oct 27, 2024
1 parent e478d0b commit da7c79f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Create an `.env` file with your API keys:
```
GENIUS_ACCESS_TOKEN=...
MS_TRANSLATOR_KEY=...
MS_TRANSLATOR_REGION=...
```
- [Genius API](https://docs.genius.com)
- [Azure Translator Resource](https://learn.microsoft.com/en-us/azure/ai-services/translator/create-translator-resource)
Expand Down
2 changes: 1 addition & 1 deletion songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
# Translate lyrics to English using Microsoft Azure AI Translator
#

lyrics_translator = TranslateLyrics(os.getenv("MS_TRANSLATOR_KEY"))
lyrics_translator = TranslateLyrics(os.getenv("MS_TRANSLATOR_KEY"), os.getenv("MS_TRANSLATOR_REGION"))
english_translation = lyrics_translator.translate_lyrics(song_lyrics)

#
Expand Down
5 changes: 3 additions & 2 deletions translate_lyrics/TranslateLyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from requests.exceptions import RequestException

class TranslateLyrics:
def __init__(self, translator_key):
def __init__(self, translator_key, region):
"""
Translate song lyrics using Microsoft Azure AI Translator
@param translator_key: string access token for Azure Translator Resource https://learn.microsoft.com/en-us/azure/ai-services/translator/create-translator-resource
"""
self.subscription_key = translator_key
self.region = region

def translate_lyrics(self, lyrics):
"""
Expand All @@ -28,7 +29,7 @@ def translate_lyrics(self, lyrics):

headers = {
'Ocp-Apim-Subscription-Key': self.subscription_key,
'Ocp-Apim-Subscription-Region': 'eastus',
'Ocp-Apim-Subscription-Region': self.region,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
Expand Down
9 changes: 6 additions & 3 deletions translate_lyrics/test_translate_lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def test_translate_lyrics_sets_translator_key():
Sets translator key
"""
translator_key = "fake_translator_key"
translate_lyrics = TranslateLyrics(translator_key)
region = "eastus"
translate_lyrics = TranslateLyrics(translator_key, region)
assert translate_lyrics.subscription_key == translator_key


Expand All @@ -66,7 +67,8 @@ def test_translate_lyrics(mock_req):

# Prepare: instantiate TranslateLyrics with fake translator key
translator_key = "fake_translator_key"
translate_lyrics_obj = TranslateLyrics(translator_key)
region = "eastus"
translate_lyrics_obj = TranslateLyrics(translator_key, region)

# mock_requests_post returns a mocked response from "https://api.cognitive.microsofttranslator.com/"
# call translate_lyrics_obj.translate_lyrics(lyrics)
Expand All @@ -84,7 +86,8 @@ def test_translate_lyrics_invalid(mock_req):

# Prepare: instantiate TranslateLyrics with fake translator key
translator_key = "fake_translator_key"
translate_lyrics_obj = TranslateLyrics(translator_key)
region = "eastus"
translate_lyrics_obj = TranslateLyrics(translator_key, region)

# mock_requests_post returns a mocked response from "https://api.cognitive.microsofttranslator.com/"
# call translate_lyrics_obj.translate_lyrics(lyrics)
Expand Down

0 comments on commit da7c79f

Please sign in to comment.