Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import nltk
|
| 2 |
+
import ntlk_utils #nltk are download in different file
|
| 3 |
+
from nltk.corpus import wordnet as wn
|
| 4 |
+
from nltk.tokenize import sent_tokenize
|
| 5 |
+
|
| 6 |
+
import streamlit as st
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import T5ForConditionalGeneration,T5Tokenizer
|
| 9 |
+
|
| 10 |
+
import time
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
st.set_page_config(
|
| 14 |
+
page_title = "Home",
|
| 15 |
+
)
|
| 16 |
+
st.title("NLP Shortcut")
|
| 17 |
+
st.subheader("ParaSummarize is an advanced Natural Language Processing (NLP) model tailored to simplify the process of digesting lengthy paragraphs. With ParaSummarize, complex texts are distilled into concise, coherent summaries with just a click. This invaluable tool empowers researchers, students, and professionals to save time and gain quick insights from extensive content.")
|
| 18 |
+
|
| 19 |
+
@st.cache_resource
|
| 20 |
+
def get_model():
|
| 21 |
+
summary_model = T5ForConditionalGeneration.from_pretrained('t5-base')
|
| 22 |
+
summary_tokenizer = T5Tokenizer.from_pretrained('t5-base')
|
| 23 |
+
return summary_model,summary_tokenizer
|
| 24 |
+
|
| 25 |
+
summary_model,summary_tokenizer = get_model()
|
| 26 |
+
|
| 27 |
+
input_summary = st.text_area("Input the text to get the summary:",placeholder="Enter the text", height=200) # height in pixel
|
| 28 |
+
button = st.button("Press to summarise")
|
| 29 |
+
|
| 30 |
+
def postprocesstext (content):
|
| 31 |
+
final=""
|
| 32 |
+
for sent in sent_tokenize(content):
|
| 33 |
+
sent = sent.capitalize()
|
| 34 |
+
final = final +" "+sent
|
| 35 |
+
return final
|
| 36 |
+
|
| 37 |
+
def summarizer(text,model,tokenizer):
|
| 38 |
+
text = text.strip().replace("\n"," ")
|
| 39 |
+
text = "summarize: "+text
|
| 40 |
+
print (text)
|
| 41 |
+
max_len = 512
|
| 42 |
+
encoding = tokenizer.encode_plus(text,max_length=max_len, pad_to_max_length=False,truncation=True, return_tensors="pt")
|
| 43 |
+
|
| 44 |
+
input_ids, attention_mask = encoding["input_ids"], encoding["attention_mask"]
|
| 45 |
+
|
| 46 |
+
outs = model.generate(input_ids=input_ids,
|
| 47 |
+
attention_mask=attention_mask,
|
| 48 |
+
early_stopping=True,
|
| 49 |
+
num_beams=3,
|
| 50 |
+
num_return_sequences=1,
|
| 51 |
+
no_repeat_ngram_size=2,
|
| 52 |
+
min_length = 75,
|
| 53 |
+
max_length=1000)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
dec = [tokenizer.decode(ids,skip_special_tokens=True) for ids in outs]
|
| 57 |
+
summary = dec[0]
|
| 58 |
+
summary = postprocesstext(summary)
|
| 59 |
+
summary= summary.strip()
|
| 60 |
+
|
| 61 |
+
return summary
|
| 62 |
+
|
| 63 |
+
if input_summary and button:
|
| 64 |
+
with st.spinner('Please wait...model is processing your input'):
|
| 65 |
+
time.sleep(5)
|
| 66 |
+
summarized_text = summarizer(input_summary,summary_model,summary_tokenizer)
|
| 67 |
+
st.success("Success")
|
| 68 |
+
st.balloons()
|
| 69 |
+
st.write(summarized_text)
|
| 70 |
+
|
| 71 |
+
#print("Original: ",input_summary)
|
| 72 |
+
#print("After : ",summarized_text)
|