sdhanabal1 commited on
Commit
dcdc714
1 Parent(s): 4f3c9ea

Introduce caching for pipeline calls

Browse files
Files changed (2) hide show
  1. Summarizer.py +0 -2
  2. app.py +19 -7
Summarizer.py CHANGED
@@ -1,5 +1,3 @@
1
- import string
2
-
3
  import nltk
4
 
5
  from sumy.parsers import DocumentParser
 
 
 
1
  import nltk
2
 
3
  from sumy.parsers import DocumentParser
app.py CHANGED
@@ -26,7 +26,7 @@ def main() -> None:
26
 
27
  @st.cache(allow_output_mutation=True,
28
  suppress_st_warning=True,
29
- show_spinner=True)
30
  def create_pipeline():
31
  with st.spinner('Please wait for the model to load...'):
32
  terms_and_conditions_pipeline = pipeline(
@@ -103,15 +103,27 @@ def main() -> None:
103
 
104
  summarize_button = st.button(label='Summarize')
105
 
 
 
 
 
 
 
 
 
 
 
 
106
  if summarize_button:
107
 
108
- with st.spinner('Summarizing the text is in progress...'):
109
- if is_valid_url(tc_text_input):
110
- extract_summary_sentences = summarizer.extractive_summary_from_url(tc_text_input, sentences_length)
111
- else:
112
- extract_summary_sentences = summarizer.extractive_summary_from_text(tc_text_input, sentences_length)
113
 
114
- abstract_summary_list = summarizer.abstractive_summary(extract_summary_sentences)
 
 
115
 
116
  display_abstractive_summary(abstract_summary_list)
117
  display_extractive_summary(tc_text_input, extract_summary_sentences)
 
26
 
27
  @st.cache(allow_output_mutation=True,
28
  suppress_st_warning=True,
29
+ show_spinner=False)
30
  def create_pipeline():
31
  with st.spinner('Please wait for the model to load...'):
32
  terms_and_conditions_pipeline = pipeline(
 
103
 
104
  summarize_button = st.button(label='Summarize')
105
 
106
+ @st.cache(suppress_st_warning=True,
107
+ show_spinner=False,
108
+ allow_output_mutation=True,
109
+ hash_funcs={"torch.nn.parameter.Parameter": lambda _: None,
110
+ "tokenizers.Tokenizer": lambda _: None,
111
+ "tokenizers.AddedToken": lambda _: None,
112
+ })
113
+ def abstractive_summary_from_cache(summary_sentences: tuple) -> tuple:
114
+ with st.spinner('Summarizing the text is in progress...'):
115
+ return tuple(summarizer.abstractive_summary(list(summary_sentences)))
116
+
117
  if summarize_button:
118
 
119
+ if is_valid_url(tc_text_input):
120
+ extract_summary_sentences = summarizer.extractive_summary_from_url(tc_text_input, sentences_length)
121
+ else:
122
+ extract_summary_sentences = summarizer.extractive_summary_from_text(tc_text_input, sentences_length)
 
123
 
124
+ extract_summary_sentences_tuple = tuple(extract_summary_sentences)
125
+ abstract_summary_tuple = abstractive_summary_from_cache(extract_summary_sentences_tuple)
126
+ abstract_summary_list = list(abstract_summary_tuple)
127
 
128
  display_abstractive_summary(abstract_summary_list)
129
  display_extractive_summary(tc_text_input, extract_summary_sentences)