File size: 2,026 Bytes
dc149ba 158e38e dc149ba 3708fd3 dc149ba 6c18750 dc149ba 9ee6383 dc149ba 6c18750 dc149ba cc4bac3 a7e8544 350bfee 55f0629 3708fd3 55f0629 b44d3df aad89b6 55f0629 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
"""
translation program for simple text
1. detect language from langdetect
2. translate to target language given by user
Example from
https://www.thepythoncode.com/article/machine-translation-using-huggingface-transformers-in-python
user_input:
string: string to be translated
target_lang: language to be translated to
Returns:
string: translated string of text
"""
import gradio as gr
import argparse
import langid
from transformers import pipeline
def detect_lang(article, target_lang):
"""
Language Detection using library langid
Args:
article (string): article that user wish to translate
target_lang (string): language user want to translate article into
Returns:
string: detected language short form
"""
result_lang = langid.classify(article)
print(result_lang[0])
if result_lang == target_lang:
return result_lang[0]
else:
return result_lang[0]
def opus_trans(article, target_lang):
"""
Translation by Helsinki-NLP model
Args:
article (string): article that user wishes to translate
result_lang (string): detected language in short form
target_lang (string): language that user wishes to translate article into
Returns:
string: translated piece of article based off target_lang
"""
if target_lang == "en":
result_lang = "zh"
elif target_lang == "zh":
result_lang = "en"
# result_lang = detect_lang(article, target_lang)
task_name = f"translation_{result_lang}_to_{target_lang}"
model_name = f"Helsinki-NLP/opus-mt-{result_lang}-{target_lang}"
translator = pipeline(task_name, model=model_name, tokenizer=model_name)
translated = translator(article)[0]["translation_text"]
print(translated)
return translated
article = gr.Textbox()
lang_select = gr.Radio(["en", "zh"])
translate = gr.Interface(
opus_trans,
[
article,
lang_select,
],
outputs=gr.Textbox(),
)
translate.launch()
|