blazingbunny commited on
Commit
de3b0ca
·
verified ·
1 Parent(s): 4d3ca74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -1,7 +1,10 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
- import textwrap
4
  import re
 
 
 
 
5
 
6
  st.title('Hugging Face BERT Summarizer')
7
 
@@ -20,14 +23,16 @@ keywords = st.text_input("Enter keywords (comma-separated)")
20
  scale_percentage = st.sidebar.slider('Scale %', min_value=1, max_value=100, value=50)
21
 
22
  # Add slider for the chunk size
23
- chunk_size = st.sidebar.slider('Chunk size', min_value=100, max_value=1000, value=500)
24
 
25
  if uploaded_file is not None and keywords:
26
  user_input = uploaded_file.read().decode('utf-8')
27
  keywords = [keyword.strip() for keyword in keywords.split(",")]
28
 
 
 
 
29
  # Filter sentences based on keywords
30
- sentences = re.split(r'(?<=[^A-Z].[.?]) +(?=[A-Z])', user_input)
31
  filtered_sentences = [sentence for sentence in sentences if any(keyword.lower() in sentence.lower() for keyword in keywords)]
32
  filtered_text = ' '.join(filtered_sentences)
33
 
@@ -35,16 +40,17 @@ if uploaded_file is not None and keywords:
35
  summarizer = pipeline('summarization', model=model)
36
  summarized_text = ""
37
 
38
- # Split the filtered text into chunks of approximately the selected chunk size each
39
- chunks = textwrap.wrap(filtered_text, chunk_size)
 
40
 
41
  # Summarize each chunk
42
  for chunk in chunks:
43
  chunk_length = len(chunk.split())
44
- min_length_percentage = max(scale_percentage - 10, 1) # Ensure min_length_percentage is not less than 1
45
- max_length_percentage = min(scale_percentage + 10, 100) # Ensure max_length_percentage is not more than 100
46
- min_length = max(int(chunk_length * min_length_percentage / 100), 1) # Calculate min_length based on the percentage of the chunk length
47
- max_length = int(chunk_length * max_length_percentage / 100) # Calculate max_length based on the percentage of the chunk length
48
  summarized = summarizer(chunk, max_length=max_length, min_length=min_length, do_sample=False)
49
  summarized_text += summarized[0]['summary_text'] + " "
50
 
 
1
  import streamlit as st
2
  from transformers import pipeline
 
3
  import re
4
+ import nltk
5
+
6
+ nltk.download('punkt')
7
+ from nltk.tokenize import sent_tokenize
8
 
9
  st.title('Hugging Face BERT Summarizer')
10
 
 
23
  scale_percentage = st.sidebar.slider('Scale %', min_value=1, max_value=100, value=50)
24
 
25
  # Add slider for the chunk size
26
+ chunk_size = st.sidebar.slider('Chunk size (words)', min_value=100, max_value=1000, value=500)
27
 
28
  if uploaded_file is not None and keywords:
29
  user_input = uploaded_file.read().decode('utf-8')
30
  keywords = [keyword.strip() for keyword in keywords.split(",")]
31
 
32
+ # Split text into sentences
33
+ sentences = sent_tokenize(user_input)
34
+
35
  # Filter sentences based on keywords
 
36
  filtered_sentences = [sentence for sentence in sentences if any(keyword.lower() in sentence.lower() for keyword in keywords)]
37
  filtered_text = ' '.join(filtered_sentences)
38
 
 
40
  summarizer = pipeline('summarization', model=model)
41
  summarized_text = ""
42
 
43
+ # Split filtered text into chunks by words
44
+ words = filtered_text.split()
45
+ chunks = [' '.join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
46
 
47
  # Summarize each chunk
48
  for chunk in chunks:
49
  chunk_length = len(chunk.split())
50
+ min_length_percentage = max(scale_percentage - 10, 1)
51
+ max_length_percentage = min(scale_percentage + 10, 100)
52
+ min_length = max(int(chunk_length * min_length_percentage / 100), 1)
53
+ max_length = int(chunk_length * max_length_percentage / 100)
54
  summarized = summarizer(chunk, max_length=max_length, min_length=min_length, do_sample=False)
55
  summarized_text += summarized[0]['summary_text'] + " "
56