langtrans / app.py
richylyq's picture
add translation materials
dc149ba
raw
history blame
2.34 kB
import gradio as gr
"""
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 argparse
import langid
from langdetect import DetectorFactory
DetectorFactory.seed = 0
from langdetect import detect
from transformers import pipeline
def detect_lang(article, target_lang):
"""
Language Detection using library langdetect
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 = detect(article)
print(result_lang)
if result_lang == target_lang:
return result_lang
else:
return result_lang
def lang_detect(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(message, result_lang, 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
"""
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(message)[0]["translation_text"]
print(translated)
return translated
def greet(name):
return "Hello " + name + "!!"
iface = gr.ChatInterface(opus_trans)
iface.launch()