Dhanush S Gowda commited on
Commit
27d07c4
·
verified ·
1 Parent(s): fc085dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -1,39 +1,40 @@
1
  import streamlit as st
2
  from transformers import pipeline
 
3
 
4
- # Load models and tokenizers using Hugging Face's pipeline
5
- def load_pipelines():
6
- bart_pipeline = pipeline("summarization", model="facebook/bart-large-cnn")
7
- t5_pipeline = pipeline("summarization", model="t5-large")
8
- pegasus_pipeline = pipeline("summarization", model="google/pegasus-cnn_dailymail")
9
-
10
- return {
11
- 'BART': bart_pipeline,
12
- 'T5': t5_pipeline,
13
- 'Pegasus': pegasus_pipeline,
14
- }
15
-
16
- prompt = """
17
- Summarize the below paragraph
18
- """
19
 
20
  # Streamlit app layout
21
  st.title("Text Summarization with Pre-trained Models (BART, T5, Pegasus)")
22
 
23
  text_input = st.text_area("Enter text to summarize:")
24
 
25
- if st.button("Generate Summary"):
26
- if text_input:
27
- pipelines = load_pipelines()
28
- summaries = {}
29
- for model_name, pipeline in pipelines.items():
30
- summary = pipeline(f"{prompt}\n{text_input}", max_length=150, min_length=50, length_penalty=2.0, num_beams=4, early_stopping=True)[0]['summary_text']
31
- summaries[model_name] = summary
 
 
 
 
 
 
 
32
 
33
- st.subheader("Summaries")
34
- for model_name, summary in summaries.items():
35
- st.write(f"**{model_name}**")
36
- st.write(summary.replace('<n>', ''))
37
- st.write("---")
38
- else:
39
- st.error("Please enter text to summarize.")
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ import time
4
 
5
+ # Load selected model's pipeline
6
+ @st.cache_resource
7
+ def load_pipeline(model_name):
8
+ if model_name == 'BART':
9
+ return pipeline("summarization", model="facebook/bart-large-cnn")
10
+ elif model_name == 'T5':
11
+ return pipeline("summarization", model="t5-large")
12
+ elif model_name == 'Pegasus':
13
+ return pipeline("summarization", model="google/pegasus-cnn_dailymail")
 
 
 
 
 
 
14
 
15
  # Streamlit app layout
16
  st.title("Text Summarization with Pre-trained Models (BART, T5, Pegasus)")
17
 
18
  text_input = st.text_area("Enter text to summarize:")
19
 
20
+ if text_input:
21
+ # Display word count of input text
22
+ word_count = len(text_input.split())
23
+ st.write(f"**Word Count:** {word_count}")
24
+
25
+ model_choice = st.selectbox("Choose a model:", ['BART', 'T5', 'Pegasus'])
26
+
27
+ if st.button("Generate Summary"):
28
+ with st.spinner(f"Generating summary using {model_choice}..."):
29
+ start_time = time.time()
30
+ summarizer = load_pipeline(model_choice)
31
+ summary = summarizer(text_input, max_length=150, min_length=50, length_penalty=2.0, num_beams=4, early_stopping=True)[0]['summary_text']
32
+ end_time = time.time()
33
+ summary_word_count = len(summary.split())
34
 
35
+ st.subheader(f"Summary using {model_choice}")
36
+ st.write(summary.replace('<n>', ''))
37
+ st.write(f"**Summary Word Count:** {summary_word_count}")
38
+ st.write(f"**Time Taken:** {end_time - start_time:.2f} seconds")
39
+ else:
40
+ st.error("Please enter text to summarize.")