ValueError:源代码字符串不能包含空字节,尝试运行 predict.py 进行 gcp 多标签分类

问题描述

我正在尝试从谷歌云多标签文本分类操作运行 predict.py 文件

import sys

from google.api_core.client_options import ClientOptions
from googl.cloud import automl_v1
from google.cloud.automl_v1.proto import service_pb2

def inline_text_payload(file_path):
  with open(file_path,'rb') as ff:
    content = ff.read()
  return {'text_snippet': {'content': content,'mime_type': 'text/plain'} }

def pdf_payload(file_path):
  return {'document': {'input_config': {'gcs_source': {'input_uris': [file_path] } } } }

def get_prediction(file_path,model_name):
  options = ClientOptions(api_endpoint='automl.googleapis.com')
  prediction_client = automl_v1.PredictionServiceClient(client_options=options)

  payload = inline_text_payload(file_path)
  # Uncomment the following line (and comment the above line) if want to predict on PDFs.
  # payload = pdf_payload(file_path)

  params = {}
  request = prediction_client.predict(model_name,payload,params)
  return request  # waits until request is returned

if __name__ == '__main__':
  file_path = sys.argv[1]
  model_name = sys.argv[2]

  print(get_prediction(file_path,model_name))

出现以下错误信息:

(.env) root@DESKTOP-JKJMO4T:/mnt/c/proj# python3 predict.py ./descripcion.txt projects/706727545968/locations/us-central1/models/TCN6099257081087393792
Traceback (most recent call last):
  File "predict.py",line 4,in <module>
    from google.cloud import automl_v1
  File "/mnt/c/proj/.env/lib/python3.8/site-packages/google/cloud/automl_v1/__init__.py",line 18,in <module>
    from .services.auto_ml import AutoMlClient
  File "/mnt/c/proj/.env/lib/python3.8/site-packages/google/cloud/automl_v1/services/auto_ml/__init__.py",in <module>
    from .client import AutoMlClient
  File "/mnt/c/proj/.env/lib/python3.8/site-packages/google/cloud/automl_v1/services/auto_ml/client.py",line 23,in <module>
    import pkg_resources
ValueError: source code string cannot contain null bytes

我的虚拟环境中有以下库:

google-api-core==1.26.0
google-cloud-automl==2.1.0

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)