File size: 1,432 Bytes
67647ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import streamlit as st
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast


model_name = "ramsrigouthamg/t5_paraphraser"
model = T5ForConditionalGeneration.from_pretrained('ramsrigouthamg/t5_paraphraser')
tokenizer = T5Tokenizer.from_pretrained('ramsrigouthamg/t5_paraphraser')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)

def translate_to_english(model, tokenizer, text):
  translated_text = []
  text = "paraphrase: " + text + " </s>"
  encoding = tokenizer.encode_plus(text,pad_to_max_length=True, return_tensors="pt")
  input_ids, attention_masks = encoding["input_ids"].to(device), encoding["attention_mask"].to(device)
  beam_outputs = model.generate(
    input_ids=input_ids, attention_mask=attention_masks,
    do_sample=True,
    max_length=256,
    top_k=120,
    top_p=0.98,
    early_stopping=True,
    num_return_sequences=10
  )
  for beam_output in beam_outputs:
    sent = tokenizer.decode(beam_output, skip_special_tokens=True,clean_up_tokenization_spaces=True)
    print(sent)
    translated_text.append(sent)
  return translated_text
    
st.title("Auto Translate (To English)")
text = st.text_input("Okay")
st.text("What you wrote: ")
st.write(text)
st.text("English Translation: ")
if text:
    translated_text = translate_to_english(model, tokenizer, text)
    st.write(translated_text if translated_text else "No translation found")