|
import torch |
|
from transformers import (T5ForConditionalGeneration,T5Tokenizer) |
|
import gradio as gr |
|
|
|
def set_seed(seed): |
|
torch.manual_seed(seed) |
|
set_seed(42) |
|
|
|
best_model_path = "aditi2222/t5-paraphrase" |
|
model = T5ForConditionalGeneration.from_pretrained(best_model_path) |
|
tokenizer = T5Tokenizer.from_pretrained("aditi2222/t5-paraphrase") |
|
|
|
def tokenize_data(text): |
|
|
|
|
|
|
|
max_len = 64 |
|
|
|
tokenized_inputs = tokenizer(input_, padding='max_length', truncation=True, max_length=max_len, return_attention_mask=True, return_tensors='pt') |
|
|
|
inputs={"input_ids": tokenized_inputs['input_ids'], |
|
"attention_mask": tokenized_inputs['attention_mask'] |
|
} |
|
|
|
return inputs |
|
|
|
def generate_answers(text): |
|
inputs = tokenize_data(text) |
|
results= model.generate(input_ids= inputs['input_ids'].to(device), attention_mask=inputs['attention_mask'].to(device), do_sample=True, |
|
max_length=64, |
|
top_k=120, |
|
top_p=0.98, |
|
early_stopping=True, |
|
num_return_sequences=1) |
|
answer = tokenizer.decode(results[0], skip_special_tokens=True) |
|
return answer |
|
|
|
iface = gr.Interface(fn=generate_answers, inputs=['text'],results=["text"]) |
|
iface.launch(share=True) |