如何解决Google 自然语言预测示例
我是 Python 新手。已经训练了自定义的 Google 自然语言模型并尝试执行 google 提供的示例。
import sys
import os
from google.api_core.client_options import ClientOptions
from google.cloud import automl
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="my_service_account.json"
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 get_prediction(file_path,model_name):
options = ClientOptions(api_endpoint='eu-automl.googleapis.com')
prediction_client = automl.PredictionServiceClient(client_options=options)
payload = inline_text_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))
Traceback (most recent call last):
File "predict.py",line 33,in <module>
print(get_prediction(file_path,model_name))
File "predict.py",line 26,in get_prediction
request = prediction_client.predict(model_name,params)
TypeError: predict() takes from 1 to 2 positional arguments but 4 were given
我进行了多次搜索,但似乎无法找到问题所在。如果任何有经验的人可以看看并指出正确的方向,我将不胜感激。
解决方法
]更新]
不得不改写 prediction_client.predict
参数。工作代码:
import sys
from google.api_core.client_options import ClientOptions
from google.cloud import automl
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="my_service_account.json"
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='eu-automl.googleapis.com')
prediction_client = automl.PredictionServiceClient(client_options=options)
payload = inline_text_payload(file_path)
params = {}
request = prediction_client.predict(name=model_name,payload=payload,params=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))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。