Update app.py
Browse files
app.py
CHANGED
@@ -27,6 +27,7 @@ common_phrases = {
|
|
27 |
"Good luck": "موفق باشید",
|
28 |
"Have a good trip": "سفر خوبی داشته باشید",
|
29 |
"What is your name?": "اسم شما چیست؟",
|
|
|
30 |
"Where are you from?": "اهل کجا هستید؟",
|
31 |
"I am from Iran": "من اهل ایران هستم",
|
32 |
"Do you speak English?": "آیا انگلیسی صحبت میکنید؟",
|
@@ -43,7 +44,7 @@ common_phrases = {
|
|
43 |
"Where is the hospital?": "بیمارستان کجاست؟",
|
44 |
}
|
45 |
|
46 |
-
#
|
47 |
def transliterate_farsi_to_cyrillic(farsi_text):
|
48 |
transliteration_map = {
|
49 |
"ا": "А", "ب": "Б", "پ": "П", "ت": "Т", "ث": "С", "ج": "Ҷ",
|
@@ -83,6 +84,21 @@ def transliterate_farsi_to_cyrillic(farsi_text):
|
|
83 |
|
84 |
return cyrillic_text
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
# Gradio Interface
|
88 |
interface = gr.Interface(
|
@@ -97,4 +113,5 @@ interface = gr.Interface(
|
|
97 |
)
|
98 |
|
99 |
# Launch the app
|
100 |
-
|
|
|
|
27 |
"Good luck": "موفق باشید",
|
28 |
"Have a good trip": "سفر خوبی داشته باشید",
|
29 |
"What is your name?": "اسم شما چیست؟",
|
30 |
+
"My name is Sara": "اسم من سارا است",
|
31 |
"Where are you from?": "اهل کجا هستید؟",
|
32 |
"I am from Iran": "من اهل ایران هستم",
|
33 |
"Do you speak English?": "آیا انگلیسی صحبت میکنید؟",
|
|
|
44 |
"Where is the hospital?": "بیمارستان کجاست؟",
|
45 |
}
|
46 |
|
47 |
+
# Improved transliteration function (Farsi to Cyrillic)
|
48 |
def transliterate_farsi_to_cyrillic(farsi_text):
|
49 |
transliteration_map = {
|
50 |
"ا": "А", "ب": "Б", "پ": "П", "ت": "Т", "ث": "С", "ج": "Ҷ",
|
|
|
84 |
|
85 |
return cyrillic_text
|
86 |
|
87 |
+
# Translation function
|
88 |
+
def translate_to_cyrillic_farsi(text):
|
89 |
+
# First, check if the phrase is in predefined common phrases
|
90 |
+
if text in common_phrases:
|
91 |
+
farsi_text = common_phrases[text]
|
92 |
+
else:
|
93 |
+
# Otherwise, use the translation model
|
94 |
+
tokenizer.src_lang = "en"
|
95 |
+
encoded_text = tokenizer(text, return_tensors="pt")
|
96 |
+
generated_tokens = model.generate(**encoded_text, forced_bos_token_id=tokenizer.get_lang_id("fa"))
|
97 |
+
farsi_text = tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
|
98 |
+
|
99 |
+
# Transliterate Farsi to Cyrillic
|
100 |
+
cyrillic_text = transliterate_farsi_to_cyrillic(farsi_text)
|
101 |
+
return farsi_text, cyrillic_text
|
102 |
|
103 |
# Gradio Interface
|
104 |
interface = gr.Interface(
|
|
|
113 |
)
|
114 |
|
115 |
# Launch the app
|
116 |
+
if __name__ == "__main__":
|
117 |
+
interface.launch()
|