1

get_prediction尝试在我的程序中多次从我的 AutoML 自定义模型运行时出现错误。我怎样才能解决这个问题?

def get_prediction(self,tweet,full_tweet):
    content = 'walgreens sucks'
    prediction_client = automl_v1beta1.PredictionServiceClient()
    name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
    payload = {'text_snippet': {'content': content, 'mime_type': 'text/plain' }}
    params = {}
    request = prediction_client.predict(name, payload, params)  
    return request

这是错误:

google.api_core.exceptions.ServiceUnavailable:503 从插件获取元数据失败并出现错误:HTTPSConnectionPool(host='oauth2.googleapis.com',port=443):最大重试次数超过 url:/token(由 NewConnectionError(':失败引起)建立一个新的连接:[Errno 8] nodename nor servname provided, or not known',))

4

2 回答 2

3

是的,我也遇到了同样的问题

就我而言,我犯了一个愚蠢的错误,我在函数内部启动客户端,并且从循环中调用该函数。这意味着它会在每次调用时启动客户端。这是代码:

from google.cloud import translate

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="locaions of credential"

def sample_translate_text(text):
 """Translating Text."""

 try:
     project_id="project id"
     client = translate.TranslationServiceClient()

     parent = client.location_path(project_id, "global")

     response = client.translate_text(
         parent=parent,
         contents=[text],
         mime_type="text/plain", 

         target_language_code="en-US",
     )

     for translation in response.translations:
         return translation.translated_text
 #      
 except Exception  as e:
     print(str(e))
     return None
  for i in range(0, 1000):
      sample_translate_text("আমার মাথা ব্যাথা")

输出:

most frequently I got this error 

503 Getting metadata from plugin failed with error: HTTPSConnectionPool(host='oauth2.googleapis.com', port=443): Max retries exceeded with url: /token (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))

然后启动客户端在函数外检查我的以下代码:

from google.cloud import translate

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="location of credential"
project_id="project id"
client = translate.TranslationServiceClient()

parent = client.location_path(project_id, "global")
def sample_translate_text(text):
    """Translating Text."""

    try:
        response = client.translate_text(
            parent=parent,
            contents=[text],
            mime_type="text/plain", 

            target_language_code="en-US",
        )

        for translation in response.translations:
            return translation.translated_text
    #      
    except Exception  as e:
        print(str(e))
        return None
   for i in range(0, 1000):
       sample_translate_text("আমার মাথা ব্যাথা")

这个案例我成功了

于 2020-04-13T03:55:26.200 回答
0

我不知道该函数所处的确切上下文,但可能发生的情况是您经常启动客户端,例如在一个循环中(每次调用该函数时)。解决方案是使客户端全局或将其作为参数传递给函数。这应该有效:

prediction_client = automl_v1beta1.PredictionServiceClient()

def get_prediction(self,tweet,full_tweet):
    content = 'walgreens sucks'
    name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
    payload = {'text_snippet': {'content': content, 'mime_type': 'text/plain' }}
    params = {}
    request = prediction_client.predict(name, payload, params)  
    return request
于 2020-01-15T19:27:37.440 回答