生成文章标签的后端服务

11 min read

这个项目使用 Python 和 FastAPI 生成文章标签,它可以接受一篇文章的文本作为输入,并生成相应的标签。该项目使用 NLTK 库进行分词和去停用词处理,使用 Jieba 库进行中文分词,然后使用 TextRank 算法生成标签。

安装依赖

为了运行该项目,你需要安装 Python 3 和 pip。在安装了 Python 3 和 pip 后,你可以使用以下命令安装项目依赖:

bash
pip install -r requirements.txt

在安装依赖之前,你需要先将 stopwords.txtnltk_data 数据集下载到本地。你可以使用以下命令下载 nltk_data

bash
python -m nltk.downloader stopwords

运行应用程序

在安装了项目依赖项之后,你可以使用以下命令运行 FastAPI 应用程序:

bash
uvicorn main:app --reload

这将启动 FastAPI 应用程序,并使其在 http://localhost:8000 上可用。

生成文章标签

要生成文章标签,请使用 POST 请求将文章文本发送到 /tags 端点。例如,你可以使用 curl 发送请求:

bash
curl -X POST "http://localhost:8000/tags" -H "Content-Type: application/json" -d '{"text": "This is an example article."}'

这将生成一个包含标签的 JSON 响应,例如:

json
{"tags": ["example", "article"]}

运行测试

为了运行项目测试,请使用以下命令:

bash
pytest

这将运行项目中的所有测试,并输出测试结果。

使用 Docker

该项目还提供了一个 Dockerfile 和一个 docker-compose.yml 文件,你可以使用它们来在 Docker 容器中运行应用程序。要使用 Docker 运行该应用程序,请先构建 Docker 镜像:

bash
docker build -t article-tags .

然后,使用以下命令启动应用程序:

bash
docker-compose up

这将启动两个 Docker 容器,一个用于运行 FastAPI 应用程序,另一个用于运行 PostgreSQL 数据库。应用程序将在 http://localhost:8000 上可用。

持久化 nltk_data

如果要在 Docker 容器中持久化 nltk_data,可以使用 Docker 卷来实现。首先,将 nltk_data 复制到 Docker 卷中:

bash
docker run -v /my/nltk_data:/root/nltk_data python:3.9-alpine sh -c "cp -r /root/nltk_data /mnt"

然后,在 Docker Compose 文件中将 Docker 卷与容器中的目录进行绑定:

yaml
volumes:
  - /my/nltk_data:/root/nltk_data

这样,在启动容器时,Docker 将从卷中加载 nltk_data 文件。