Spaces:
Runtime error
Runtime error
Simon Salmon
commited on
Commit
·
41d0dc8
1
Parent(s):
e1cd7be
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,37 @@
|
|
| 1 |
import torch
|
| 2 |
-
from transformers import T5ForConditionalGeneration,T5Tokenizer, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
st.title("Auto Translate (To English)")
|
| 6 |
text = st.text_input("Okay")
|
| 7 |
st.text("What you wrote: ")
|
| 8 |
st.write(text)
|
| 9 |
st.text("English Translation: ")
|
| 10 |
-
|
| 11 |
-
model_name = 'tuner007/pegasus_paraphrase'
|
| 12 |
-
tokenizer = PegasusTokenizer.from_pretrained(model_name)
|
| 13 |
-
model = PegasusForConditionalGeneration.from_pretrained(model_name)
|
| 14 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
-
model = model.to(device)
|
| 16 |
-
|
| 17 |
-
def get_response(text):
|
| 18 |
-
batch = tokenizer([text],truncation=True,padding='longest',max_length=60, return_tensors="pt").to(device)
|
| 19 |
-
translated = model.generate(**batch,max_length=60, do_sample=True, num_return_sequences=10, temperature=1.5)
|
| 20 |
-
tgt_text = tokenizer.batch_decode(translated, skip_special_tokens=True)
|
| 21 |
-
return tgt_text
|
| 22 |
-
|
| 23 |
if text:
|
| 24 |
-
translated_text =
|
| 25 |
-
st.write(translated_text if translated_text else "No translation found")
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from transformers import T5ForConditionalGeneration,T5Tokenizer, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import streamlit as st
|
| 4 |
+
model_name = st.text_input("Pick a Model", "seduerr/t5-pawraphrase")
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
|
| 7 |
|
| 8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
+
model = model.to(device)
|
| 10 |
+
def translate_to_english(model, tokenizer, text):
|
| 11 |
+
translated_text = []
|
| 12 |
+
text = "paraphrase: " + text + " </s>"
|
| 13 |
+
encoding = tokenizer.encode_plus(text,pad_to_max_length=True, return_tensors="pt")
|
| 14 |
+
input_ids, attention_masks = encoding["input_ids"].to(device), encoding["attention_mask"].to(device)
|
| 15 |
+
beam_outputs = model.generate(
|
| 16 |
+
input_ids=input_ids, attention_mask=attention_masks,
|
| 17 |
+
do_sample=True,
|
| 18 |
+
max_length=256,
|
| 19 |
+
top_k=120,
|
| 20 |
+
top_p=0.98,
|
| 21 |
+
early_stopping=True,
|
| 22 |
+
num_return_sequences=10
|
| 23 |
+
)
|
| 24 |
+
for beam_output in beam_outputs:
|
| 25 |
+
sent = tokenizer.decode(beam_output, skip_special_tokens=True,clean_up_tokenization_spaces=True)
|
| 26 |
+
print(sent)
|
| 27 |
+
translated_text.append(sent)
|
| 28 |
+
return translated_text
|
| 29 |
+
|
| 30 |
st.title("Auto Translate (To English)")
|
| 31 |
text = st.text_input("Okay")
|
| 32 |
st.text("What you wrote: ")
|
| 33 |
st.write(text)
|
| 34 |
st.text("English Translation: ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if text:
|
| 36 |
+
translated_text = translate_to_english(model, tokenizer, text)
|
| 37 |
+
st.write(translated_text if translated_text else "No translation found")
|