Shularp commited on
Commit
b877b12
1 Parent(s): 11b8b7b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Gradio Course 04 - Test.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1QdsZHyXpY78iytNDhpBZmMTKRcb_27nQ
8
+ """
9
+
10
+ !pip install gradio transformers googletrans==4.0.0rc1 transformers[sentencepiece] --quiet
11
+
12
+ import gradio as gr
13
+ from transformers import pipeline
14
+ from googletrans import Translator
15
+
16
+ model_card_en2ar = 'Shularp/model-translate-en-to-ar-from-320k-dataset-ar-en-th2301191458'
17
+ model_card_th2en = 'Helsinki-NLP/opus-mt-th-en'
18
+
19
+ translate_en2ar = pipeline("translation", model = model_card_en2ar)
20
+ translate_th2en = pipeline("translation", model = model_card_th2en)
21
+
22
+ def tx_th2en(text):
23
+ result_en = translate_th2en(text)[0]['translation_text']
24
+ return (result_en)
25
+
26
+ def tx_en2ar(text):
27
+ result_ar = translate_en2ar(text)[0]['translation_text']
28
+ return (result_ar)
29
+
30
+ def tx_th2ar(text):
31
+ result_en = translate_th2en(text)[0]['translation_text']
32
+ result_ar = translate_en2ar(result_en)[0]['translation_text']
33
+ #return result_en, result_ar
34
+ return result_ar
35
+
36
+ def trans_via_google(text,source, target):
37
+ google_trans = Translator() #reduce problem timeout runtime.
38
+ return google_trans.translate(text = text, src = source, dest = target).text
39
+
40
+ def translate_en2th(text):
41
+ google_trans = Translator() #reduce problem timeout runtime.
42
+ return google_trans.translate(text = text, src = 'en', dest = 'th').text
43
+
44
+ def null(text):
45
+ return "ยังไม่ได้รองการแปลจากภาษาอังกฤษเป็นภาษาไทย"
46
+
47
+ with gr.Blocks() as demo:
48
+ with gr.Row():
49
+ with gr.Column():
50
+ input_thai = gr.Textbox(label = 'ข้อความภาษาไทย')
51
+ translate_btn = gr.Button(value = 'Translate to En and Ar')
52
+
53
+ with gr.Column():
54
+ input_en = gr.Textbox(label = 'English Text')
55
+ translate_en_btn = gr.Button(value = 'Translate to Ar')
56
+
57
+ with gr.Column():
58
+ result_ar = gr.Textbox(label = 'ข้อความภาษาอาหรับ')
59
+ translate_btn.click(tx_th2en, inputs = input_thai, outputs=input_en)
60
+ translate_btn.click(tx_th2ar, inputs = input_thai, outputs=result_ar)
61
+ translate_en_btn.click(tx_en2ar, inputs = input_en, outputs=result_ar)
62
+ translate_en_btn.click(translate_en2th,inputs = input_en, outputs= input_thai)
63
+
64
+ demo.launch()
65
+