File size: 3,443 Bytes
c287584
 
 
 
 
 
 
b31fbb8
 
c287584
 
f770c62
c287584
 
984fb40
 
 
 
d44ac17
984fb40
 
c287584
 
 
 
 
 
 
 
 
984fb40
 
 
 
 
 
c287584
b31fbb8
c287584
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
984fb40
c287584
 
 
 
b31fbb8
7d5d64d
 
b31fbb8
c287584
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import gradio as gr

import nltk
import random
import numpy as np
import torch
from transformers import T5ForConditionalGeneration,T5Tokenizer
model = T5ForConditionalGeneration.from_pretrained('t5-base')
tokenizer = T5Tokenizer.from_pretrained('t5-base')

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)


# nltk.download('punkt')
# nltk.download('brown')
# nltk.download('wordnet')
# nltk.download('omw-1.4')

# from nltk.corpus import wordnet as wn
# from nltk.tokenize import sent_tokenize

def set_seed(seed: int):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)

set_seed(42)

# def postprocesstext (content):
#   final=""
#   for sent in sent_tokenize(content):
#     sent = sent.capitalize()
#     final = final +" "+sent
#   return final

def summarizer(text):
  text = text.strip().replace("\n"," ")
  text = "summarize: "+text
  # print (text)
  max_len = 512
  encoding = tokenizer.encode_plus(text,max_length=max_len, pad_to_max_length=False,truncation=True, return_tensors="pt").to(device)

  input_ids, attention_mask = encoding["input_ids"], encoding["attention_mask"]

  outs = model.generate(input_ids=input_ids,
                                  attention_mask=attention_mask,
                                  early_stopping=True,
                                  num_beams=3,
                                  num_return_sequences=1,
                                  no_repeat_ngram_size=2,
                                  min_length = 75,
                                  max_length=300)


  dec = [tokenizer.decode(ids,skip_special_tokens=True) for ids in outs]
  summary = dec[0]
  #summary = postprocesstext(summary)
  summary= summary.strip()

  return summary

demo = gr.Interface(fn=summarizer, inputs="text", outputs="text", 
                   examples=["Cristiano Ronaldo is a Portuguese professional soccer player who currently plays as a forward for Manchester United and the Portugal national team. He is widely considered one of the greatest soccer players of all time, having won numerous awards and accolades throughout his career. Ronaldo began his professional career with Sporting CP in Portugal before moving to Manchester United in 2003. He spent six seasons with the club, winning three Premier League titles and one UEFA Champions League title. In 2009, he transferred to Real Madrid for a then-world record transfer fee of $131 million. He spent nine seasons with the club, winning four UEFA Champions League titles, two La Liga titles, and two Copa del Rey titles. In 2018, he transferred to Juventus, where he spent three seasons before returning to Manchester United in 2021. He has also had a successful international career with the Portugal national team, having won the UEFA European Championship in 2016 and the UEFA Nations League in 2019.", 
                            "One rule of thumb which applies to everything that we do - professionally and personally : Know what the customer want and deliver. In this case, it is important to know what the organisation what from employee. Connect the same to the KRA. Are you part of a delivery which directly ties to the larger organisational objective. If yes, then the next question is success rate of one’s delivery. If the KRAs are achieved or exceeded, then the employee is entitled for a decent hike."]
)
demo.launch()