/ examples / article.py
article.py
 1  """
 2  Application that builds a summary of an article.
 3  
 4  Requires streamlit to be installed.
 5    pip install streamlit
 6  """
 7  
 8  import os
 9  
10  import streamlit as st
11  
12  from txtai.pipeline import Summary, Textractor
13  from txtai.workflow import UrlTask, Task, Workflow
14  
15  
16  class Application:
17      """
18      Main application.
19      """
20  
21      def __init__(self):
22          """
23          Creates a new application.
24          """
25  
26          textract = Textractor(paragraphs=True, minlength=100, join=True)
27          summary = Summary("sshleifer/distilbart-cnn-12-6")
28  
29          self.workflow = Workflow([UrlTask(textract), Task(summary)])
30  
31      def run(self):
32          """
33          Runs a Streamlit application.
34          """
35  
36          st.title("Article Summary")
37          st.markdown("This application builds a summary of an article.")
38  
39          url = st.text_input("URL")
40          if url:
41              # Run workflow and get summary
42              summary = list(self.workflow([url]))[0]
43  
44              # Write results
45              st.write(summary)
46              st.markdown("*Source: " + url + "*")
47  
48  
49  @st.cache(allow_output_mutation=True)
50  def create():
51      """
52      Creates and caches a Streamlit application.
53  
54      Returns:
55          Application
56      """
57  
58      return Application()
59  
60  
61  if __name__ == "__main__":
62      os.environ["TOKENIZERS_PARALLELISM"] = "false"
63  
64      # Create and run application
65      app = create()
66      app.run()