有没有办法将 TextBlob 语料库下载到 Google Cloud Run? [英] Is there a way to download TextBlob corpora to Google Cloud Run?

查看:103
本文介绍了有没有办法将 TextBlob 语料库下载到 Google Cloud Run?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Python 和 TextBlob 进行情绪分析.我想使用 Google Cloud Build(不使用 Docker)将我的应用程序(在 Plotly Dash 中构建)部署到 Google Cloud Run.在我的虚拟环境中本地使用时一切正常,但在云上部署后,语料库未下载.查看 requriements.txt 文件,也没有提到这个语料库.

I am using Python with TextBlob for sentiment analysis. I want to deploy my app (build in Plotly Dash) to Google Cloud Run with Google Cloud Build (without using Docker). When using locally on my virtual environment all goes fine, but after deploying it on the cloud the corpora is not downloaded. Looking at the requriements.txt file, there was also no reference to this corpora.

我尝试将 python -m textblob.download_corpora 添加到我的 requriements.txt 文件中,但在我部署它时它没有下载.我也尝试添加

I have tried to add python -m textblob.download_corpora to my requriements.txt file but it doesn't download when I deploy it. I have also tried to add

import textblob
import subprocess
cmd = ['python','-m','textblob.download_corpora']
subprocess.run(cmd)

import nltk
nltk.download('movie_reviews')

到我的脚本(callbacks.py,我使用 Plotly Dash 来制作我的应用程序),都没有成功.

to my script (callbacks.py, I am using Plotly Dash to make my app), all without success.

有没有办法将此语料库添加到我的 requirements.txt 文件中?或者是否有另一种解决方法来下载此语料库?我该如何解决这个问题?

Is there a way to add this corpus to my requirements.txt file? Or is there another workaround to download this corpus? How can I fix this?

提前致谢!

维杰

推荐答案

由于 Cloud Run 会根据流量级别的需要创建和销毁容器,因此您需要将语料库嵌入预先构建的容器中以确保快速冷启动时间(而不是在容器启动时下载)

Since Cloud Run creates and destroys containers as needed for your traffic levels you'll want to embed your corpora in the pre-built container to ensure a fast cold start time (instead of downloading it when the container starts)

最简单的方法是在 docker 文件中添加另一行,在构建时下载和安装语料库,如下所示:

The easiest way to do this is add another line inside of a docker file that downloads and installs the corpora at build time like so:

RUN python -m textblob.download_corpora 

这是一个完整的 docker 文件供您参考:

Here's a full docker file for your reference:

# Python image to use.
FROM python:3.8

# Set the working directory to /app
WORKDIR /app

# copy the requirements file used for dependencies
COPY requirements.txt .

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
RUN python -m textblob.download_corpora

# Copy the rest of the working directory contents into the container at /app
COPY . .

# Run app.py when the container launches
ENTRYPOINT ["python", "app.py"]

祝你好运乔希

这篇关于有没有办法将 TextBlob 语料库下载到 Google Cloud Run?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆