Spaces:
Runtime error
Runtime error
# -*- coding: utf-8 -*- | |
"""Gradio Course 04 - Test.ipynb | |
Automatically generated by Colaboratory. | |
Original file is located at | |
https://colab.research.google.com/drive/1QdsZHyXpY78iytNDhpBZmMTKRcb_27nQ | |
""" | |
#!pip install gradio transformers googletrans==4.0.0rc1 transformers[sentencepiece] --quiet | |
import gradio as gr | |
from transformers import pipeline | |
from googletrans import Translator | |
model_card_en2ar = 'Shularp/model-translate-en-to-ar-from-320k-dataset-ar-en-th2301191458' | |
model_card_th2en = 'Helsinki-NLP/opus-mt-th-en' | |
translate_en2ar = pipeline("translation", model = model_card_en2ar) | |
translate_th2en = pipeline("translation", model = model_card_th2en) | |
def tx_th2en(text): | |
result_en = translate_th2en(text)[0]['translation_text'] | |
return (result_en) | |
def tx_en2ar(text): | |
result_ar = translate_en2ar(text)[0]['translation_text'] | |
return (result_ar) | |
def tx_th2ar(text): | |
result_en = translate_th2en(text)[0]['translation_text'] | |
result_ar = translate_en2ar(result_en)[0]['translation_text'] | |
#return result_en, result_ar | |
return result_ar | |
def trans_via_google(text,source, target): | |
google_trans = Translator() #reduce problem timeout runtime. | |
return google_trans.translate(text = text, src = source, dest = target).text | |
def translate_en2th(text): | |
google_trans = Translator() #reduce problem timeout runtime. | |
return google_trans.translate(text = text, src = 'en', dest = 'th').text | |
def null(text): | |
return "ยังไม่ได้รองการแปลจากภาษาอังกฤษเป็นภาษาไทย" | |
with gr.Blocks() as demo: | |
with gr.Row(): | |
with gr.Column(): | |
input_thai = gr.Textbox(label = 'ข้อความภาษาไทย') | |
translate_btn = gr.Button(value = 'Translate to En and Ar') | |
with gr.Column(): | |
input_en = gr.Textbox(label = 'English Text') | |
translate_en_btn = gr.Button(value = 'Translate to Ar') | |
with gr.Column(): | |
result_ar = gr.Textbox(label = 'ข้อความภาษาอาหรับ') | |
translate_btn.click(tx_th2en, inputs = input_thai, outputs=input_en) | |
translate_btn.click(tx_th2ar, inputs = input_thai, outputs=result_ar) | |
translate_en_btn.click(tx_en2ar, inputs = input_en, outputs=result_ar) | |
translate_en_btn.click(translate_en2th,inputs = input_en, outputs= input_thai) | |
demo.launch() | |