add translation materials
Browse files
app.py
CHANGED
@@ -1,9 +1,95 @@
|
|
1 |
import gradio as gr
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
def greet(name):
|
5 |
return "Hello " + name + "!!"
|
6 |
|
7 |
|
8 |
-
iface = gr.
|
9 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
"""
|
4 |
+
translation program for simple text
|
5 |
+
1. detect language from langdetect
|
6 |
+
2. translate to target language given by user
|
7 |
+
|
8 |
+
Example from
|
9 |
+
https://www.thepythoncode.com/article/machine-translation-using-huggingface-transformers-in-python
|
10 |
+
|
11 |
+
user_input:
|
12 |
+
string: string to be translated
|
13 |
+
target_lang: language to be translated to
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
string: translated string of text
|
17 |
+
"""
|
18 |
+
|
19 |
+
import argparse
|
20 |
+
|
21 |
+
import langid
|
22 |
+
from langdetect import DetectorFactory
|
23 |
+
|
24 |
+
DetectorFactory.seed = 0
|
25 |
+
|
26 |
+
from langdetect import detect
|
27 |
+
from transformers import pipeline
|
28 |
+
|
29 |
+
|
30 |
+
def detect_lang(article, target_lang):
|
31 |
+
"""
|
32 |
+
Language Detection using library langdetect
|
33 |
+
|
34 |
+
Args:
|
35 |
+
article (string): article that user wish to translate
|
36 |
+
target_lang (string): language user want to translate article into
|
37 |
+
|
38 |
+
Returns:
|
39 |
+
string: detected language short form
|
40 |
+
"""
|
41 |
+
result_lang = detect(article)
|
42 |
+
print(result_lang)
|
43 |
+
if result_lang == target_lang:
|
44 |
+
return result_lang
|
45 |
+
else:
|
46 |
+
return result_lang
|
47 |
+
|
48 |
+
|
49 |
+
def lang_detect(article, target_lang):
|
50 |
+
"""
|
51 |
+
Language Detection using library langid
|
52 |
+
|
53 |
+
Args:
|
54 |
+
article (string): article that user wish to translate
|
55 |
+
target_lang (string): language user want to translate article into
|
56 |
+
|
57 |
+
Returns:
|
58 |
+
string: detected language short form
|
59 |
+
"""
|
60 |
+
|
61 |
+
result_lang = langid.classify(article)
|
62 |
+
print(result_lang[0])
|
63 |
+
if result_lang == target_lang:
|
64 |
+
return result_lang[0]
|
65 |
+
else:
|
66 |
+
return result_lang[0]
|
67 |
+
|
68 |
+
|
69 |
+
def opus_trans(message, result_lang, target_lang):
|
70 |
+
"""
|
71 |
+
Translation by Helsinki-NLP model
|
72 |
+
|
73 |
+
Args:
|
74 |
+
article (string): article that user wishes to translate
|
75 |
+
result_lang (string): detected language in short form
|
76 |
+
target_lang (string): language that user wishes to translate article into
|
77 |
+
|
78 |
+
Returns:
|
79 |
+
string: translated piece of article based off target_lang
|
80 |
+
"""
|
81 |
+
|
82 |
+
task_name = f"translation_{result_lang}_to_{target_lang}"
|
83 |
+
model_name = f"Helsinki-NLP/opus-mt-{result_lang}-{target_lang}"
|
84 |
+
translator = pipeline(task_name, model=model_name, tokenizer=model_name)
|
85 |
+
translated = translator(message)[0]["translation_text"]
|
86 |
+
print(translated)
|
87 |
+
return translated
|
88 |
+
|
89 |
|
90 |
def greet(name):
|
91 |
return "Hello " + name + "!!"
|
92 |
|
93 |
|
94 |
+
iface = gr.ChatInterface(opus_trans)
|
95 |
iface.launch()
|