微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

由于 flairnlp 导入问题,无法将 python 脚本部署到谷歌云函数

如何解决由于 flairnlp 导入问题,无法将 python 脚本部署到谷歌云函数

我正在尝试部署一个 Google Cloud Function,它使用 fair nlp 模型对推文执行情绪分析。代码部署得非常好,没有“import flair”行或“from flair import x,y,z”之类的替代方法。一旦我包含了 flair 的 import 语句,该函数就无法部署。以下是我在使用 import 语句进行部署时遇到的错误错误是从 Firebase 日志中复制的)。这是我第一次在 StackOverflow 上发帖,如果帖子看起来很丑,请原谅我。

{"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"Function Failed on loading user code. This is likely due to a bug in the user code. Error message: Code in file main.py can't be loaded.\nDetailed stack trace:\nTraceback (most recent call last):\n File \"/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py\",line 359,in check_or_load_user_function\n _function_handler.load_user_function()\n File \"/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py\",line 236,in load_user_function\n spec.loader.exec_module(main_module)\n File \"<frozen importlib._bootstrap_external>\",line 728,in exec_module\n File \"<frozen importlib._bootstrap>\",line 219,in _call_with_frames_removed\n File \"/user_code/main.py\",line 5,in <module>\n from flair import models,data\n File \"/env/local/lib/python3.7/site-packages/flair/__init__.py\",line 20,in <module>\n from . import models\n File \"/env/local/lib/python3.7/site-packages/flair/models/__init__.py\",line 1,in <module>\n from .sequence_tagger_model import SequenceTagger,MultiTagger\n File \"/env/local/lib/python3.7/site-packages/flair/models/sequence_tagger_model.py\",line 21,in <module>\n from flair.embeddings import TokenEmbeddings,StackedEmbeddings,Embeddings\n File \"/env/local/lib/python3.7/site-packages/flair/embeddings/__init__.py\",line 6,in <module>\n from .token import TokenEmbeddings\n File \"/env/local/lib/python3.7/site-packages/flair/embeddings/token.py\",line 10,in <module>\n from transformers import AutoTokenizer,AutoConfig,AutoModel,CONfig_MAPPING,PreTrainedTokenizer\nImportError: cannot import name 'AutoModel' from 'transformers' (unkNown location)\n. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation."},"authenticationInfo":

这是我尝试部署的脚本,以及 requirements.txt 文件

main.py

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
from datetime import datetime,timedelta
import flair
# or from flair import models,data

class FirestoreHandler():
    cred = credentials.Certificate("serviceAccountKey.json")
    firebase_admin.initialize_app(cred)

    db = firestore.client()
    
    def analysis_on_create(self):
            docs = self.db.collection('tweets').order_by(u'time',direction=firestore.Query.DESCENDING).limit(1).get()
            data = docs[0].to_dict()
            most_recent_tweet = data['full-text']
            sentiment_model = flair.models.TextClassifier.load('en-sentiment')
            sentence = flair.data.Sentence(str(most_recent_tweet))
            sentiment_model.predict(sentence)
            result = sentence.labels[0]
            if result.value == "POSITIVE":
                val= 1 * result.score
            else:
                val= -1 * result.score

            self.db.collection('sentiment').add({'sentiment':val,'timestamp':datetime.Now()+timedelta(hours=3)}) 
            
    def add_test(self):
        self.db.collection('test3').add({"status":"success",'timestamp':datetime.Now()+timedelta(hours=3)})

def hello_firestore(event,context):
    """Triggered by a change to a Firestore document.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    resource_string = context.resource
    # print out the resource string that triggered the function
    print(f"Function triggered by change to: {resource_string}.")
    # Now print out the entire event object
    print(str(event))
    fire = FirestoreHandler()
    fire.add_test()
    fire.analysis_on_create()

要求.txt

# Function dependencies,for example:
# package>=version

firebase-admin==5.0.1
https://download.pytorch.org/whl/cpu/torch-1.0.1.post2-cp37-cp37m-linux_x86_64.whl
flair

我在 pytorch 下载中包含了 url,因为 flair 是基于 pytorch 构建的,如果没有 url,该函数将无法部署(即使我没有在 main.py 中导入 flair)。我也试过为天赋指定不同的版本无济于事。

对于可能导致此问题的原因的任何直觉将不胜感激!我是 Google Cloud 生态系统的新手,这是我的第一个项目。如果我可以提供任何其他信息,请告诉我。

编辑:我正在从网站部署(不使用 CLI)

解决方法

我不确定所提供的 requirements.txt 是否适合 GCP 云功能部署。不确定是否会正确处理显式 https 网址...

Specifying dependencies in Python 文档页面描述了如何声明依赖项 - using the pip package manager's requirements.txt file or packaging local dependencies alongside your function.

您能否在 flair 文件中简单地提及具有必要版本的 requirements.txt?它会起作用吗?

此外,您提供的错误突出显示需要 transformers 包。是不是需要某个特定的版本?

====

作为旁注 - 我不知道您的背景和要求,但我不确定是否要从云功能内部使用 Firestore

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore 

是必需的,因为最好完全避免使用 serviceAccountKey.json,而只需将相关的 IAM 角色分配给用于给定云功能执行的服务帐户。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。