gpt4 book ai didi

python - 谷歌自然语言api python进入无限循环

转载 作者:太空宇宙 更新时间:2023-11-03 21:08:33 24 4
gpt4 key购买 nike

我正在使用谷歌自然语言 API 来分析句子中的情绪。但是,每次我运行脚本时。它进入无限循环的 sleep 。

我的代码。

comments = "This product is really good"
credentials = GoogleAuth.get_instance()
client = language.LanguageServiceClient(credentials=credentials)
document = types.Document(
content=comments,
type=enums.Document.Type.PLAIN_TEXT
)
sentiment = client.analyze_sentiment(document=document)

堆栈跟踪。

Traceback (most recent call last):
File "/Users/my_mac/app/sentiment_analysis_runner.py", line 14, in <module>
main()
File "/Users/my_mac/app/sentiment_analysis_runner.py", line 8, in main
sentiment = sentiment_analyser.analyse(url)
File "/Users/my_mac/app/sentiment_analysis/product_sentiment_analysis.py", line 20, in analyse
return self.analyse_comments(comments)
File "/Users/my_mac/app/sentiment_analysis/product_sentiment_analysis.py", line 32, in analyse_comments
sentiment = client.analyze_sentiment(document=document)
File "/Users/my_mac/PycharmProjects/HelloWorld/venv/lib/python3.6/site-packages/google/cloud/language_v1/gapic/language_service_client.py", line 230, in analyze_sentiment
request, retry=retry, timeout=timeout, metadata=metadata
File "/Users/my_mac/PycharmProjects/HelloWorld/venv/lib/python3.6/site-packages/google/api_core/gapic_v1/method.py", line 143, in __call__
return wrapped_func(*args, **kwargs)
File "/Users/my_mac/PycharmProjects/HelloWorld/venv/lib/python3.6/site-packages/google/api_core/retry.py", line 270, in retry_wrapped_func
on_error=on_error,
File "/Users/my_mac/PycharmProjects/HelloWorld/venv/lib/python3.6/site-packages/google/api_core/retry.py", line 205, in retry_target
time.sleep(sleep)
KeyboardInterrupt

Process finished with exit code 1

最佳答案

我刚刚尝试了以下代码,基于client library文档,并且它运行没有问题。

from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
from google.oauth2 import service_account

#Setting the Service Account key
credentials = service_account.Credentials.from_service_account_file('../key.json')
client = language.LanguageServiceClient(credentials=credentials)
text = u'This product is really good'
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
sentiment = client.analyze_sentiment(document=document).document_sentiment

print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))

并得到输出:

Text: This product is really good
Sentiment: 0.800000011921, 0.800000011921

希望有帮助。

关于python - 谷歌自然语言api python进入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55234013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com