构建一个文本摘要应用

概述

在本指南中,您将学习如何构建和运行一个文本摘要应用程序。您将使用Python和Bert Extractive Summarizer构建应用程序,然后设置环境并使用Docker运行应用程序。

示例文本摘要应用程序使用了Bert提取式摘要器。 该工具利用HuggingFace Pytorch transformers库来运行 提取式摘要。其工作原理是首先对句子进行嵌入,然后 运行聚类算法,找到最接近聚类中心的句子。

先决条件

  • 您已安装最新版本的 Docker Desktop。Docker 定期添加新功能,本指南的某些部分可能仅适用于最新版本的 Docker Desktop。
  • 您有一个 Git客户端。本节中的示例使用基于命令行的Git客户端,但您可以使用任何客户端。

获取示例应用程序

  1. 打开终端,使用以下命令克隆示例应用程序的仓库。

    $ git clone https://github.com/harsh4870/Docker-NLP.git
    
  2. 验证您是否克隆了存储库。

    你应该在你的Docker-NLP目录中看到以下文件。

    01_sentiment_analysis.py
    02_name_entity_recognition.py
    03_text_classification.py
    04_text_summarization.py
    05_language_translation.py
    entrypoint.sh
    requirements.txt
    Dockerfile
    README.md

探索应用程序代码

文本摘要应用程序的源代码位于Docker-NLP/04_text_summarization.py文件中。在文本或代码编辑器中打开04_text_summarization.py,按照以下步骤探索其内容。

  1. 导入所需的库。

    from summarizer import Summarizer

    这行代码从summarizer包中导入了Summarizer类,这对于你的文本摘要应用程序至关重要。摘要模块实现了Bert提取式摘要器,利用了HuggingFace Pytorch transformers库,该库在NLP(自然语言处理)领域享有盛誉。该库提供了对预训练模型(如BERT)的访问,这些模型彻底改变了语言理解任务,包括文本摘要。

    BERT模型,即来自Transformers的双向编码器表示,擅长理解语言中的上下文,使用一种称为“注意力”的机制来确定句子中单词的重要性。对于摘要生成,该模型嵌入句子,然后使用聚类算法来识别关键句子,这些句子最接近这些聚类的中心点,从而有效地捕捉文本的主要思想。

  2. 指定主执行块。

    if __name__ == "__main__":

    这个Python习惯用法确保以下代码块仅在脚本作为主程序运行时执行。它提供了灵活性,允许脚本既可以作为独立程序运行,也可以作为导入的模块使用。

  3. 创建一个无限循环以进行连续输入。

       while True:
          input_text = input("Enter the text for summarization (type 'exit' to end): ")
    
          if input_text.lower() == 'exit':
             print("Exiting...")
             break

    一个无限循环会持续提示你输入文本,确保交互性。当你输入exit时,循环会中断,从而有效地控制应用程序的流程。

  4. 创建一个Summarizer的实例。

          bert_model = Summarizer()

    在这里,您创建了一个名为bert_model的Summarizer类的实例。这个实例现在准备使用BERT模型执行摘要任务,将复杂的句子嵌入和聚类过程简化为一个易于访问的接口。

  5. 生成并打印摘要。

    summary = bert_model(input_text)
    print(summary)

    您的输入文本由bert_model实例处理,然后返回一个摘要版本。这展示了Python高级库的强大功能,能够以最少的代码实现复杂的操作。

  6. 创建requirements.txt。示例应用程序已经包含了requirements.txt文件,用于指定应用程序导入的必要模块。在代码或文本编辑器中打开requirements.txt以探索其内容。

    ...
    
    # 04 text_summarization
    bert-extractive-summarizer==0.10.1
    
    ...
    
    torch==2.1.2

    文本摘要应用程序需要bert-extractive-summarizertorch模块。摘要模块生成输入文本的摘要。这需要PyTorch,因为用于生成摘要的底层BERT模型是在PyTorch中实现的。

探索应用程序环境

您将使用Docker在容器中运行应用程序。Docker允许您将应用程序容器化,提供一个一致且隔离的运行环境。这意味着无论底层系统有何差异,应用程序都将在其Docker容器中按预期运行。

要在容器中运行应用程序,需要一个Dockerfile。Dockerfile是一个文本文件,其中包含您将在命令行上调用的所有命令来组装镜像。镜像是一个只读模板,其中包含创建Docker容器的指令。

示例应用程序已经包含一个Dockerfile。在代码或文本编辑器中打开Dockerfile以探索其内容。

以下步骤解释了Dockerfile的每个部分。更多详细信息,请参阅 Dockerfile参考

  1. 指定基础镜像。

    FROM python:3.8-slim

    此命令为构建奠定了基础。python:3.8-slim 是 Python 3.8 镜像的轻量级版本,针对大小和速度进行了优化。使用这个精简镜像可以减少 Docker 镜像的总体大小,从而加快下载速度并减少安全漏洞的攻击面。这对于基于 Python 的应用程序特别有用,因为您可能不需要完整的标准 Python 镜像。

  2. 设置工作目录。

    WORKDIR /app

    WORKDIR 设置 Docker 镜像中的当前工作目录。通过将其设置为 /app,您可以确保 Dockerfile 中的所有后续命令(如 COPYRUN)都在此目录中执行。这也有助于组织您的 Docker 镜像,因为所有与应用程序相关的文件都包含在一个特定的目录中。

  3. 将需求文件复制到镜像中。

    COPY requirements.txt /app

    COPY 命令将 requirements.txt 文件从您的本地机器传输到 Docker 镜像中。该文件列出了应用程序所需的所有 Python 依赖项。将其复制到容器中,使得下一个命令(RUN pip install)可以在镜像环境中安装这些依赖项。

  4. 在镜像中安装Python依赖项。

    RUN pip install --no-cache-dir -r requirements.txt

    这一行使用pip,Python的包安装器,来安装requirements.txt中列出的包。--no-cache-dir选项禁用缓存,通过不存储不必要的缓存数据来减小Docker镜像的大小。

  5. 运行额外的命令。

    RUN python -m spacy download en_core_web_sm

    此步骤特定于需要spaCy库的NLP应用程序。它下载了en_core_web_sm模型,这是一个用于spaCy的小型英语语言模型。虽然此应用程序不需要它,但为了与其他可能使用此Dockerfile的NLP应用程序兼容,它被包含在内。

  6. 将应用程序代码复制到镜像中。

    COPY *.py /app
    COPY entrypoint.sh /app

    这些命令将您的Python脚本和entrypoint.sh脚本复制到镜像的/app目录中。这至关重要,因为容器需要这些脚本来运行应用程序。entrypoint.sh脚本尤为重要,因为它决定了应用程序在容器内的启动方式。

  7. entrypoint.sh脚本设置权限。

    RUN chmod +x /app/entrypoint.sh

    此命令修改了entrypoint.sh的文件权限,使其可执行。此步骤是确保Docker容器能够运行此脚本来启动应用程序的必要步骤。

  8. 设置入口点。

    ENTRYPOINT ["/app/entrypoint.sh"]

    ENTRYPOINT 指令配置容器以运行 entrypoint.sh 作为其默认可执行文件。这意味着当容器启动时,它会自动执行该脚本。

    你可以通过在代码或文本编辑器中打开entrypoint.sh脚本来探索它。由于示例包含多个应用程序,该脚本允许你在容器启动时指定要运行的应用程序。

运行应用程序

要使用Docker运行应用程序:

  1. 构建镜像。

    在终端中,在Dockerfile所在的目录内运行以下命令。

    $ docker build -t basic-nlp .
    

    以下是命令的分解:

    • docker build: This is the primary command used to build a Docker image from a Dockerfile and a context. The context is typically a set of files at a specified location, often the directory containing the Dockerfile.
    • -t basic-nlp: This is an option for tagging the image. The -t flag stands for tag. It assigns a name to the image, which in this case is basic-nlp. Tags are a convenient way to reference images later, especially when pushing them to a registry or running containers.
    • .: This is the last part of the command and specifies the build context. The period (.) denotes the current directory. Docker will look for a Dockerfile in this directory. The build context (the current directory, in this case) is sent to the Docker daemon to enable the build. It includes all the files and subdirectories in the specified directory.

    更多详情,请参阅 docker build CLI 参考

    Docker在构建镜像时会向控制台输出多条日志。你会看到它下载并安装依赖项。根据你的网络连接情况,这可能需要几分钟。Docker确实有一个缓存功能,因此后续的构建可以更快。当构建完成时,控制台将返回到提示符。

  2. 将镜像作为容器运行。

    在终端中,运行以下命令。

    $ docker run -it basic-nlp 04_text_summarization.py
    

    以下是命令的分解:

    • docker run: This is the primary command used to run a new container from a Docker image.
    • -it: This is a combination of two options:
      • -i or --interactive: This keeps the standard input (STDIN) open even if not attached. It lets the container remain running in the foreground and be interactive.
      • -t or --tty: This allocates a pseudo-TTY, essentially simulating a terminal, like a command prompt or a shell. It's what lets you interact with the application inside the container.
    • basic-nlp: This specifies the name of the Docker image to use for creating the container. In this case, it's the image named basic-nlp that you created with the docker build command.
    • 04_text_summarization.py: This is the script you want to run inside the Docker container. It gets passed to the entrypoint.sh script, which runs it when the container starts.

    更多详情,请参阅 docker run CLI reference

    注意

    对于Windows用户,运行容器时可能会遇到错误。请验证entrypoint.sh中的行尾是LF\n)而不是CRLF\r\n),然后重新构建镜像。更多详情,请参阅避免意外的语法错误,使用Unix风格的行尾格式用于容器中的文件

    容器启动后,您将在控制台中看到以下内容。

    Enter the text for summarization (type 'exit' to end):
    
  3. 测试应用程序。

    输入一些文本以获取文本摘要。

    Enter the text for summarization (type 'exit' to end): Artificial intelligence (AI) is a branch of computer science that aims to create machines capable of intelligent behavior. These machines are designed to mimic human cognitive functions such as learning, problem-solving, and decision-making. AI technologies can be classified into two main types: narrow or weak AI, which is designed for a particular task, and general or strong AI, which possesses the ability to understand, learn, and apply knowledge across various domains. One of the most popular approaches in AI is machine learning, where algorithms are trained on large datasets to recognize patterns and make predictions.
    
    Artificial intelligence (AI) is a branch of computer science that aims to create machines capable of intelligent behavior. These machines are designed to mimic human cognitive functions such as learning, problem-solving, and decision-making.
    

摘要

在本指南中,您学习了如何构建和运行一个文本摘要应用程序。您学习了如何使用Python和Bert Extractive Summarizer构建应用程序,然后设置环境并使用Docker运行应用程序。

相关信息:

下一步

探索更多 自然语言处理指南