karthi311 commited on
Commit
4c68814
Β·
verified Β·
1 Parent(s): edeb228

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -1,12 +1,8 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
- # Cache the summarizer to avoid reloading on every interaction
5
- @st.cache_resource
6
- def load_summarizer():
7
- return pipeline("summarization", model="facebook/bart-large-cnn")
8
-
9
- summarizer = load_summarizer()
10
 
11
  # Streamlit UI setup
12
  st.title("πŸ“ Text Summarization App")
@@ -53,21 +49,39 @@ else: # Long
53
  # Display the selected range for feedback
54
  st.write(f"Selected Summary Length: {'Short' if summary_length == 1 else 'Medium' if summary_length == 2 else 'Long'}")
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  # Button to trigger summarization
57
  if st.button("Summarize"):
58
  if user_input.strip(): # Ensure there's input before summarizing
59
  try:
60
- # Generate summary using the model with custom min and max length
61
- summarized_text = summarizer(user_input,
62
- max_length=max_len,
63
- min_length=min_len,
64
- length_penalty=2.0,
65
- num_beams=4,
66
- early_stopping=True)[0]['summary_text']
 
 
 
 
67
 
68
  # Display the generated summary
69
  st.subheader("Summarized Text:")
70
- st.write(summarized_text)
71
 
72
  except Exception as e:
73
  st.error(f"An error occurred while summarizing: {e}")
 
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
+ # Initialize the summarization pipeline
5
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
 
 
 
 
6
 
7
  # Streamlit UI setup
8
  st.title("πŸ“ Text Summarization App")
 
49
  # Display the selected range for feedback
50
  st.write(f"Selected Summary Length: {'Short' if summary_length == 1 else 'Medium' if summary_length == 2 else 'Long'}")
51
 
52
+ # Function to split text into manageable chunks
53
+ def chunk_text(text, max_chunk_size=1024):
54
+ tokens = text.split()
55
+ chunks = []
56
+ current_chunk = []
57
+
58
+ for token in tokens:
59
+ current_chunk.append(token)
60
+ if len(' '.join(current_chunk)) > max_chunk_size:
61
+ chunks.append(' '.join(current_chunk[:-1]))
62
+ current_chunk = [token]
63
+ chunks.append(' '.join(current_chunk)) # Add the final chunk
64
+ return chunks
65
+
66
  # Button to trigger summarization
67
  if st.button("Summarize"):
68
  if user_input.strip(): # Ensure there's input before summarizing
69
  try:
70
+ # Split the input into chunks
71
+ text_chunks = chunk_text(user_input)
72
+
73
+ # Summarize each chunk separately
74
+ summaries = []
75
+ for chunk in text_chunks:
76
+ summary = summarizer(chunk, max_length=max_len, min_length=min_len, length_penalty=2.0, num_beams=4, early_stopping=True)[0]['summary_text']
77
+ summaries.append(summary)
78
+
79
+ # Combine summaries from all chunks
80
+ full_summary = " ".join(summaries)
81
 
82
  # Display the generated summary
83
  st.subheader("Summarized Text:")
84
+ st.write(full_summary)
85
 
86
  except Exception as e:
87
  st.error(f"An error occurred while summarizing: {e}")