Skip to content

Commit

Permalink
handle case where glossaryTranslations is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
devxpy committed Oct 29, 2023
1 parent 677131f commit ee092ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
7 changes: 5 additions & 2 deletions daras_ai_v2/asr.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,11 @@ def _translate_text(
)
res.raise_for_status()
data = res.json()
translations = data.get("glossaryTranslations", data["translations"])
return translations[0]["translatedText"].strip()
try:
result = data["glossaryTranslations"][0]["translatedText"]
except (KeyError, IndexError):
result = data["translations"][0]["translatedText"]
return result.strip()


_session = None
Expand Down
32 changes: 4 additions & 28 deletions glossary_resources/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,18 @@
GLOSSARY = [
{
"en-US": "Gooey.AI",
"hi-IN": "गूई.एआई",
"hi-IN": "गुई ए आई",
"pos": "noun",
"description": "name of the Gooey.AI",
"description": "Translation of Gooey.AI from Hindi to English",
"random": "random",
},
{
"en-US": "Gooey.AI",
"hi-IN": "गुई डॉट ए आई",
"pos": "noun",
"description": "name of the Gooey.AI",
"description": "Translation of Gooey.AI from Hindi to English",
"random": get_random_doc_id(),
},
{
"en-US": "Gooey.AI",
"hi-IN": "गुई ए आई",
"pos": "noun",
"description": "name of the Gooey.AI",
"random": "random",
},
{
"en-US": "chilli",
"hi-IN": "मिर्ची",
Expand All @@ -44,34 +37,24 @@
TRANSLATION_TESTS_GLOSSARY = [
(
"मिर्च में बीज उपचार कैसे करें", # source
"en",
"How to Treat Seeds in Peppers", # no glossary
"How to Treat Seeds in Chilli", # with glosssary
),
(
"मिर्ची में बीज उपचार कैसे करें",
"en",
"How to Treat Seeds in Peppers",
"How to Treat Seeds in Chilli",
),
(
"गुई डॉट ए आई से हम क्या कर सकते हैं",
"en",
"What can we do with Gui.AI",
"What can we do with Gooey.AI",
),
(
"गुई ए आई से हम क्या कर सकते हैं",
"en",
"What can we do with AI",
"What can we do with Gooey.AI",
),
(
"Who is the founder of Gooey.AI?",
"hi",
"gooeyai के संस्थापक कौन हैं?",
"गूई.एआई के संस्थापक कौन हैं?",
),
]


Expand All @@ -92,22 +75,15 @@ def glossary_url():

@pytest.mark.django_db
def test_run_google_translate_glossary(glossary_url, threadpool_subtest):
for (
text,
target_lang,
expected,
expected_with_glossary,
) in TRANSLATION_TESTS_GLOSSARY:
for text, expected, expected_with_glossary in TRANSLATION_TESTS_GLOSSARY:
threadpool_subtest(
test_run_google_translate_one,
text,
expected,
target_lang=target_lang,
)
threadpool_subtest(
test_run_google_translate_one,
text,
expected_with_glossary,
target_lang=target_lang,
glossary_url=glossary_url,
)

0 comments on commit ee092ec

Please sign in to comment.