Syams uddin
commited on
Commit
·
b8db96b
1
Parent(s):
aac8096
coba
Browse files- app.py +20 -1
- translator.py +10 -0
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 4 |
|
| 5 |
def respond(
|
| 6 |
message,
|
|
@@ -65,6 +65,25 @@ with gr.Blocks() as demo:
|
|
| 65 |
gr.LoginButton()
|
| 66 |
chatbot.render()
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from translator import translate
|
| 4 |
|
| 5 |
def respond(
|
| 6 |
message,
|
|
|
|
| 65 |
gr.LoginButton()
|
| 66 |
chatbot.render()
|
| 67 |
|
| 68 |
+
# ===== Translator =====
|
| 69 |
+
def translate_ui(text, direction):
|
| 70 |
+
return translate(text, direction)
|
| 71 |
+
|
| 72 |
+
# ===== Layout Gradio =====
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
with gr.Sidebar():
|
| 75 |
+
gr.LoginButton()
|
| 76 |
+
|
| 77 |
+
with gr.Tabs():
|
| 78 |
+
with gr.Tab("Chatbot"):
|
| 79 |
+
chatbot.render()
|
| 80 |
+
|
| 81 |
+
with gr.Tab("Translator"):
|
| 82 |
+
input_text = gr.Textbox(lines=4, label="Input")
|
| 83 |
+
direction = gr.Dropdown(["ID → EN", "EN → ID"], label="Direction")
|
| 84 |
+
output_text = gr.Textbox(label="Output")
|
| 85 |
+
translate_btn = gr.Button("Translate")
|
| 86 |
+
translate_btn.click(fn=translate_ui, inputs=[input_text, direction], outputs=output_text)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
demo.launch()
|
translator.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
translator_id_en = pipeline("translation", model="Helsinki-NLP/opus-mt-id-en")
|
| 4 |
+
translator_en_id = pipeline("translation", model="Helsinki-NLP/opus-mt-en-id")
|
| 5 |
+
|
| 6 |
+
def translate(text, direction):
|
| 7 |
+
if direction == "ID → EN":
|
| 8 |
+
return translator_id_en(text)[0]['translation_text']
|
| 9 |
+
else:
|
| 10 |
+
return translator_en_id(text)[0]['translation_text']
|