Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import nltk
|
| 3 |
+
from nltk.corpus import stopwords
|
| 4 |
+
from nltk.tokenize import word_tokenize, sent_tokenize
|
| 5 |
+
|
| 6 |
+
# Download NLTK resources
|
| 7 |
+
nltk.download('punkt')
|
| 8 |
+
nltk.download('stopwords')
|
| 9 |
+
|
| 10 |
+
# Define the summarization function
|
| 11 |
+
def summarize_text(text, num_sentences=2):
|
| 12 |
+
# Tokenize the text into sentences
|
| 13 |
+
sentences = sent_tokenize(text)
|
| 14 |
+
|
| 15 |
+
# Tokenize the text into words and remove stopwords
|
| 16 |
+
stop_words = set(stopwords.words('english'))
|
| 17 |
+
word_tokens = word_tokenize(text.lower())
|
| 18 |
+
filtered_tokens = [word for word in word_tokens if word.isalnum() and word not in stop_words]
|
| 19 |
+
|
| 20 |
+
# Calculate word frequency
|
| 21 |
+
word_freq = nltk.FreqDist(filtered_tokens)
|
| 22 |
+
|
| 23 |
+
# Assign scores to sentences based on the sum of their word frequencies
|
| 24 |
+
sent_scores = {}
|
| 25 |
+
for sentence in sentences:
|
| 26 |
+
for word in word_tokenize(sentence.lower()):
|
| 27 |
+
if word in word_freq.keys():
|
| 28 |
+
if len(sentence.split(' ')) < 30: # Consider only sentences with less than 30 words
|
| 29 |
+
if sentence not in sent_scores.keys():
|
| 30 |
+
sent_scores[sentence] = word_freq[word]
|
| 31 |
+
else:
|
| 32 |
+
sent_scores[sentence] += word_freq[word]
|
| 33 |
+
|
| 34 |
+
# Select the top N sentences with highest scores for summarization
|
| 35 |
+
summary_sentences = sorted(sent_scores, key=sent_scores.get, reverse=True)[:num_sentences]
|
| 36 |
+
|
| 37 |
+
# Generate the summary
|
| 38 |
+
summary = ' '.join(summary_sentences)
|
| 39 |
+
return summary
|
| 40 |
+
|
| 41 |
+
# Streamlit app
|
| 42 |
+
def main():
|
| 43 |
+
st.title("Text Summarization App")
|
| 44 |
+
|
| 45 |
+
st.write("Enter your text below and select the number of sentences for the summary.")
|
| 46 |
+
|
| 47 |
+
# Text input
|
| 48 |
+
text_input = st.text_area("Text", "Paste your text here...")
|
| 49 |
+
|
| 50 |
+
# Number of sentences for summary
|
| 51 |
+
num_sentences = st.slider("Number of sentences in summary", min_value=1, max_value=10, value=2)
|
| 52 |
+
|
| 53 |
+
if st.button("Generate Summary"):
|
| 54 |
+
if text_input:
|
| 55 |
+
summary = summarize_text(text_input, num_sentences)
|
| 56 |
+
st.write("### Summary:")
|
| 57 |
+
st.write(summary)
|
| 58 |
+
else:
|
| 59 |
+
st.write("Please enter some text to summarize.")
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
main()
|