kabita-choudhary commited on
Commit
bb2fe0d
1 Parent(s): 161d598

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ from transformers import pipeline
4
+ from transformers import PegasusForConditionalGeneration, PegasusTokenizer
5
+
6
+
7
+ def summarize(data, modelname):
8
+ if (modelname == 'Bart'):
9
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
10
+ print("world")
11
+ output = summarizer(data, max_length=130, min_length=30, do_sample=False)
12
+ return output[0]["summary_text"]
13
+ elif (modelname == 'Pegasus'):
14
+ model = PegasusForConditionalGeneration.from_pretrained("google/pegasus-xsum")
15
+ tokenizer = PegasusTokenizer.from_pretrained("google/pegasus-xsum")
16
+
17
+ # Create tokens - number representation of our text
18
+ tokens = tokenizer(data, truncation=True, padding="longest", return_tensors="pt")
19
+ summary = model.generate(**tokens)
20
+ return tokenizer.decode(summary[0])
21
+
22
+
23
+ st.sidebar.title("Text Summarization")
24
+
25
+ uploaded_file = st.sidebar.file_uploader("Choose a file")
26
+ data = ""
27
+ output = ""
28
+ if uploaded_file is not None:
29
+ # To read file as bytes:
30
+ bytes_data = uploaded_file.getvalue()
31
+
32
+ data = bytes_data.decode("utf-8")
33
+ modelname = st.sidebar.radio("Choose your model",
34
+ ["Bart", "Pegasus"],
35
+ help=" you can choose between 2 models (Bart or Pegasus) to summarize your text. More to come!", )
36
+ col1, col2 = st.columns(2)
37
+
38
+ with col1:
39
+ st.header("Copy paste your text or Upload file")
40
+ if (uploaded_file is not None):
41
+ with st.expander("Text to summarize", expanded=True):
42
+ st.write(
43
+ data
44
+ )
45
+ else:
46
+ with st.expander("Text to summarize", expanded=True):
47
+ data = st.text_area("Paste your text below (max 500 words)", height=510, )
48
+
49
+ MAX_WORDS = 500
50
+ import re
51
+
52
+ res = len(re.findall(r"\w+", data))
53
+ if res > MAX_WORDS:
54
+ st.warning(
55
+ "⚠️ Your text contains "
56
+ + str(res)
57
+ + " words."
58
+ + " Only the first 500 words will be reviewed. Stay tuned as increased allowance is coming! 😊")
59
+ data = data[:MAX_WORDS]
60
+ Summarizebtn = st.button("Summarize")
61
+ if (Summarizebtn):
62
+ output = summarize(data, modelname)
63
+
64
+ with col2:
65
+ st.header("Summary")
66
+ if (len(output) > 0):
67
+ with st.expander("", expanded=True):
68
+ st.write(output)
69
+
70
+
71
+
72
+
73
+
74
+
75
+