Spaces:
Runtime error
Runtime error
Simon Salmon
commited on
Commit
·
67647ba
1
Parent(s):
78e9cc0
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
model_name = "ramsrigouthamg/t5_paraphraser"
|
| 6 |
+
model = T5ForConditionalGeneration.from_pretrained('ramsrigouthamg/t5_paraphraser')
|
| 7 |
+
tokenizer = T5Tokenizer.from_pretrained('ramsrigouthamg/t5_paraphraser')
|
| 8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
+
model = model.to(device)
|
| 10 |
+
|
| 11 |
+
def translate_to_english(model, tokenizer, text):
|
| 12 |
+
translated_text = []
|
| 13 |
+
text = "paraphrase: " + text + " </s>"
|
| 14 |
+
encoding = tokenizer.encode_plus(text,pad_to_max_length=True, return_tensors="pt")
|
| 15 |
+
input_ids, attention_masks = encoding["input_ids"].to(device), encoding["attention_mask"].to(device)
|
| 16 |
+
beam_outputs = model.generate(
|
| 17 |
+
input_ids=input_ids, attention_mask=attention_masks,
|
| 18 |
+
do_sample=True,
|
| 19 |
+
max_length=256,
|
| 20 |
+
top_k=120,
|
| 21 |
+
top_p=0.98,
|
| 22 |
+
early_stopping=True,
|
| 23 |
+
num_return_sequences=10
|
| 24 |
+
)
|
| 25 |
+
for beam_output in beam_outputs:
|
| 26 |
+
sent = tokenizer.decode(beam_output, skip_special_tokens=True,clean_up_tokenization_spaces=True)
|
| 27 |
+
print(sent)
|
| 28 |
+
translated_text.append(sent)
|
| 29 |
+
return translated_text
|
| 30 |
+
|
| 31 |
+
st.title("Auto Translate (To English)")
|
| 32 |
+
text = st.text_input("Okay")
|
| 33 |
+
st.text("What you wrote: ")
|
| 34 |
+
st.write(text)
|
| 35 |
+
st.text("English Translation: ")
|
| 36 |
+
if text:
|
| 37 |
+
translated_text = translate_to_english(model, tokenizer, text)
|
| 38 |
+
st.write(translated_text if translated_text else "No translation found")
|