Upload 2 files
Browse files- app.py +43 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
from optimum.bettertransformer import BetterTransformer
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
7 |
+
"google/madlad400-3b-mt",
|
8 |
+
use_fast=True
|
9 |
+
)
|
10 |
+
|
11 |
+
model_hf = AutoModelForSeq2SeqLM.from_pretrained(
|
12 |
+
"google/madlad400-3b-mt",
|
13 |
+
torch_dtype=torch.bfloat16
|
14 |
+
)
|
15 |
+
|
16 |
+
model = BetterTransformer.transform(model_hf, keep_original=True)
|
17 |
+
|
18 |
+
def translate(text):
|
19 |
+
"""
|
20 |
+
Translates the input text from English to Hawaiian.
|
21 |
+
"""
|
22 |
+
text = "<2haw> " + text
|
23 |
+
|
24 |
+
inputs = tokenizer(
|
25 |
+
text,
|
26 |
+
return_tensors="pt"
|
27 |
+
)
|
28 |
+
|
29 |
+
outputs = model.generate(**inputs, max_new_tokens=1000)
|
30 |
+
text_translated = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
31 |
+
|
32 |
+
return text_translated[0]
|
33 |
+
|
34 |
+
demo = gr.Interface(
|
35 |
+
fn=translate,
|
36 |
+
inputs=[gr.Textbox(label="English")],
|
37 |
+
outputs=[gr.Textbox(label="Hawaiian")],
|
38 |
+
title="MADLAD-400-3B-MT English-to-Hawaiian Translation",
|
39 |
+
description="[Code](https://github.com/darylalim/madlad-400-3b-mt-eng-to-haw-translation)")
|
40 |
+
|
41 |
+
demo.queue()
|
42 |
+
|
43 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
accelerate
|
4 |
+
sentencepiece
|
5 |
+
tokenizers
|
6 |
+
optimum
|
7 |
+
gradio
|