Spaces:
Sleeping
Sleeping
File size: 1,269 Bytes
ce4bb15 b148f92 ce4bb15 b148f92 ce4bb15 b148f92 ce4bb15 b148f92 ac1392a 976874c 99208da 48adf68 ce4bb15 |
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 39 40 41 42 43 44 45 46 |
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# Predict with test data (first 5 rows)
model_ckpt = "GenzNepal/mt5-summarize-nepali"
device = "cuda" if torch.cuda.is_available() else "cpu"
t5_tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
model = AutoModelForSeq2SeqLM.from_pretrained(model_ckpt).to(device)
def summarize(text):
inputs = t5_tokenizer(text, return_tensors="pt", max_length=1024, padding= "max_length", truncation=True, add_special_tokens=True)
generation = model.generate(
input_ids = inputs['input_ids'].to(device),
attention_mask=inputs['attention_mask'].to(device),
num_beams=6,
num_return_sequences=1,
no_repeat_ngram_size=2,
repetition_penalty=1.0,
min_length=100,
max_length=250,
length_penalty=2.0,
early_stopping=True
)
# # Convert id tokens to text
output = t5_tokenizer.decode(generation[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
return output
demo = gr.Interface(
fn=summarize,
inputs=gr.Textbox(placeholder="Enter news " , lines=5, max_lines=20, label="News"),
outputs=gr.Textbox(label="Generated Summary")
)
if __name__ == "__main__":
demo.launch()
|