Spaces:
Sleeping
Sleeping
Update tools/translate_phrase.py
Browse files- tools/translate_phrase.py +106 -1
tools/translate_phrase.py
CHANGED
@@ -36,4 +36,109 @@ class TranslatePhraseTool(Tool):
|
|
36 |
"french": {"text": "Merci", "pronunciation": "mair-see"},
|
37 |
"italian": {"text": "Grazie", "pronunciation": "graht-see-eh"},
|
38 |
"german": {"text": "Danke", "pronunciation": "dahn-kuh"},
|
39 |
-
"japanese": {"text": "ありがとう (Arigatou)", "pronunciation": "ah-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
"french": {"text": "Merci", "pronunciation": "mair-see"},
|
37 |
"italian": {"text": "Grazie", "pronunciation": "graht-see-eh"},
|
38 |
"german": {"text": "Danke", "pronunciation": "dahn-kuh"},
|
39 |
+
"japanese": {"text": "ありがとう (Arigatou)", "pronunciation": "ah-ree-gah-toh"},
|
40 |
+
"mandarin": {"text": "谢谢 (Xièxiè)", "pronunciation": "shyeh-shyeh"},
|
41 |
+
"arabic": {"text": "شكرا (Shukran)", "pronunciation": "shoo-kran"},
|
42 |
+
"russian": {"text": "Спасибо (Spasibo)", "pronunciation": "spah-see-boh"},
|
43 |
+
"portuguese": {"text": "Obrigado/a", "pronunciation": "oh-bree-gah-doo/dah"},
|
44 |
+
"thai": {"text": "ขอบคุณ (Khop khun)", "pronunciation": "kop-koon"}
|
45 |
+
},
|
46 |
+
"excuse me": {
|
47 |
+
"spanish": {"text": "Disculpe", "pronunciation": "dees-kool-peh"},
|
48 |
+
"french": {"text": "Excusez-moi", "pronunciation": "ex-koo-zay mwah"},
|
49 |
+
"italian": {"text": "Scusi", "pronunciation": "skoo-zee"},
|
50 |
+
"german": {"text": "Entschuldigung", "pronunciation": "ent-shool-di-goong"},
|
51 |
+
"japanese": {"text": "すみません (Sumimasen)", "pronunciation": "soo-mee-mah-sen"},
|
52 |
+
"mandarin": {"text": "对不起 (Duìbùqǐ)", "pronunciation": "dway-boo-chee"},
|
53 |
+
"arabic": {"text": "عفوا (Afwan)", "pronunciation": "af-wan"},
|
54 |
+
"russian": {"text": "Извините (Izvinite)", "pronunciation": "eez-vee-nee-tye"},
|
55 |
+
"portuguese": {"text": "Com licença", "pronunciation": "com lee-sen-sah"},
|
56 |
+
"thai": {"text": "ขอโทษ (Kho thot)", "pronunciation": "kor-toht"}
|
57 |
+
},
|
58 |
+
"where is the bathroom": {
|
59 |
+
"spanish": {"text": "¿Dónde está el baño?", "pronunciation": "don-deh es-tah el ban-yo"},
|
60 |
+
"french": {"text": "Où sont les toilettes?", "pronunciation": "oo son lay twa-let"},
|
61 |
+
"italian": {"text": "Dov'è il bagno?", "pronunciation": "doh-veh eel ban-yo"},
|
62 |
+
"german": {"text": "Wo ist die Toilette?", "pronunciation": "vo ist dee twa-let-te"},
|
63 |
+
"japanese": {"text": "トイレはどこですか (Toire wa doko desu ka)", "pronunciation": "toy-reh wah doh-koh des-kah"},
|
64 |
+
"mandarin": {"text": "厕所在哪里 (Cèsuǒ zài nǎlǐ)", "pronunciation": "tsuh-swor dzeye nah-lee"},
|
65 |
+
"arabic": {"text": "أين الحمام (Ayna al-hammam)", "pronunciation": "eye-nah al-ham-mam"},
|
66 |
+
"russian": {"text": "Где туалет (Gde tualet)", "pronunciation": "g-dyeh too-ah-lyet"},
|
67 |
+
"portuguese": {"text": "Onde fica o banheiro?", "pronunciation": "on-jee fee-ka oo ban-yay-roo"},
|
68 |
+
"thai": {"text": "ห้องน้ำอยู่ที่ไหน (Hong nam yu tee nai)", "pronunciation": "hong nam yoo tee nai"}
|
69 |
+
},
|
70 |
+
"how much": {
|
71 |
+
"spanish": {"text": "¿Cuánto cuesta?", "pronunciation": "kwan-toh kwes-tah"},
|
72 |
+
"french": {"text": "Combien ça coûte?", "pronunciation": "kom-bee-en sa koot"},
|
73 |
+
"italian": {"text": "Quanto costa?", "pronunciation": "kwan-toh kos-tah"},
|
74 |
+
"german": {"text": "Wie viel kostet das?", "pronunciation": "vee feel kos-tet das"},
|
75 |
+
"japanese": {"text": "いくらですか (Ikura desu ka)", "pronunciation": "ee-koo-rah des-kah"},
|
76 |
+
"mandarin": {"text": "多少钱 (Duōshǎo qián)", "pronunciation": "dwor-shaow chyen"},
|
77 |
+
"arabic": {"text": "كم الثمن (Kam althaman)", "pronunciation": "kam al-tha-man"},
|
78 |
+
"russian": {"text": "Сколько это стоит (Skol'ko eto stoit)", "pronunciation": "skol-ka eh-ta stoh-eet"},
|
79 |
+
"portuguese": {"text": "Quanto custa?", "pronunciation": "kwan-too koos-tah"},
|
80 |
+
"thai": {"text": "ราคาเท่าไหร่ (Raka tao rai)", "pronunciation": "ra-ka tao-rai"}
|
81 |
+
}
|
82 |
+
}
|
83 |
+
|
84 |
+
def forward(self, text: str, language: str) -> str:
|
85 |
+
try:
|
86 |
+
# Try to use a real translation API if the API key is available
|
87 |
+
if self.api_key:
|
88 |
+
try:
|
89 |
+
url = "https://libretranslate.de/translate"
|
90 |
+
payload = {
|
91 |
+
"q": text,
|
92 |
+
"source": "auto",
|
93 |
+
"target": language.lower()[:2], # Use first 2 chars as language code
|
94 |
+
"format": "text"
|
95 |
+
}
|
96 |
+
|
97 |
+
response = requests.post(url, data=payload)
|
98 |
+
data = response.json()
|
99 |
+
|
100 |
+
if 'translatedText' in data:
|
101 |
+
return f"🗣️ '{text}' in {language.capitalize()}:\n\n{data['translatedText']}"
|
102 |
+
else:
|
103 |
+
# Fall back to stored phrases if API call fails
|
104 |
+
return self._translate_with_stored_phrases(text, language)
|
105 |
+
|
106 |
+
except Exception:
|
107 |
+
# Fall back to stored phrases if any error occurs
|
108 |
+
return self._translate_with_stored_phrases(text, language)
|
109 |
+
|
110 |
+
# If no API key is available, use the stored phrases
|
111 |
+
return self._translate_with_stored_phrases(text, language)
|
112 |
+
|
113 |
+
except Exception as e:
|
114 |
+
return f"Error translating text: {str(e)}"
|
115 |
+
|
116 |
+
def _translate_with_stored_phrases(self, text: str, language: str) -> str:
|
117 |
+
# Normalize inputs
|
118 |
+
text_lower = text.lower().strip()
|
119 |
+
language = language.lower().strip()
|
120 |
+
|
121 |
+
# Find the phrase key that most closely matches the input text
|
122 |
+
matched_phrase = None
|
123 |
+
for phrase in self.phrase_translations:
|
124 |
+
if phrase in text_lower or text_lower in phrase:
|
125 |
+
matched_phrase = phrase
|
126 |
+
break
|
127 |
+
|
128 |
+
if not matched_phrase:
|
129 |
+
return f"I don't have a translation for '{text}'. Try common travel phrases like 'hello', 'thank you', 'excuse me', etc."
|
130 |
+
|
131 |
+
# Find the language that most closely matches the input language
|
132 |
+
matched_language = None
|
133 |
+
for lang in self.phrase_translations[matched_phrase]:
|
134 |
+
if lang in language or language in lang:
|
135 |
+
matched_language = lang
|
136 |
+
break
|
137 |
+
|
138 |
+
if not matched_language:
|
139 |
+
return f"I don't have translations for {language}. Try languages like Spanish, French, Italian, German, Japanese, etc."
|
140 |
+
|
141 |
+
# Get the translation
|
142 |
+
translation = self.phrase_translations[matched_phrase][matched_language]
|
143 |
+
|
144 |
+
return f"🗣️ '{text}' in {matched_language.capitalize()}:\n\n{translation['text']}\n\nPronunciation: {translation['pronunciation']}"
|