Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from model import model, tokenizer, translate_sentence, translate_beam_search, device
|
| 4 |
+
|
| 5 |
+
def translate(text, decoding_strategy, beam_size):
|
| 6 |
+
if decoding_strategy == "Greedy":
|
| 7 |
+
return translate_sentence(text, model, tokenizer, device)
|
| 8 |
+
else:
|
| 9 |
+
return translate_beam_search(text, model, tokenizer, device, pad_token_id=tokenizer.token_to_id('[PAD]'), beam_size=beam_size)
|
| 10 |
+
|
| 11 |
+
with gr.Blocks() as demo:
|
| 12 |
+
gr.Markdown("Hindi-English Translation")
|
| 13 |
+
|
| 14 |
+
with gr.Row():
|
| 15 |
+
inp = gr.Textbox(label="Input Sentence", placeholder="Enter sentence to translate", lines=2)
|
| 16 |
+
|
| 17 |
+
with gr.Row():
|
| 18 |
+
decoding = gr.Radio(["Greedy", "Beam Search"], value="Greedy", label="Decoding Strategy")
|
| 19 |
+
beam = gr.Slider(minimum=2, maximum=10, step=1, value=4, label="Beam Size (for Beam Search)")
|
| 20 |
+
|
| 21 |
+
out = gr.Textbox(label="Translation", lines=2)
|
| 22 |
+
|
| 23 |
+
def handle_translate(text, decoding_strategy, beam_size):
|
| 24 |
+
return translate(text, decoding_strategy, beam_size)
|
| 25 |
+
|
| 26 |
+
btn = gr.Button("Translate")
|
| 27 |
+
btn.click(fn=handle_translate, inputs=[inp, decoding, beam], outputs=out)
|
| 28 |
+
|
| 29 |
+
demo.launch()
|