【问题标题】:how to send text as an input and receive sentiment or emotion as a response in azure API?如何在 azure API 中发送文本作为输入并接收情绪或情绪作为响应?
【发布时间】:2021-03-17 14:24:12
【问题描述】:

我想在 azure API 中实现一个 python 脚本。基本上,我想发送文本并接收情感或情绪。我假设 API 是编写方法还是 Web 应用程序? 您能否通过示例指导我查看文档?谢谢!

【问题讨论】:

    标签: azure-web-app-service azure-api-management azure-cognitive-services azure-api-apps azure-app-service-envrmnt


    【解决方案1】:

    我认为Azure Text Analytics API 可以满足您的要求,此 API 为您提供分析情绪功能和相关的 Python SDK。您可以尝试下面的代码开始使用 python SDK V2 sentiment 函数:

    from azure.cognitiveservices.language.textanalytics import TextAnalyticsClient
    from msrest.authentication import CognitiveServicesCredentials
    
    subscriptionKey = "<YOUR AZURE COGNITIVE SERVICE KEY>"
    endpoint = "<YOUR AZURE COGNITIVE SERVICE SERVICE ENDPOINT>"
    
    
    credentials = CognitiveServicesCredentials(subscriptionKey)
    
    text_analytics = TextAnalyticsClient(endpoint=endpoint, credentials=credentials)
    
    documents = [
        {
            "id": "1",
            "language": "en",
            "text": "I had the best day of my life."
        }
    ]
    response = text_analytics.sentiment(documents=documents)
    for document in response.documents:
        print("Document Id: ", document.id, ", Sentiment Score: ",
              "{:.2f}".format(document.score))
    

    结果:

    您可以在 Azure 门户上找到您的服务端点和订阅密钥:

    在 Azure 门户上Create a Cognitive Service 之后。

    您可以在Azure应用服务上部署您的应用程序,具体方法请参考this doc

    【讨论】:

    • 感谢您的回复。这是完美的解决方案。但小的变化是我的查询,因为我在 python 中编写了自己的代码,这可以帮助我识别可以作为输入传递的文本的情绪。想在 azure 环境中实现它,所以我可以为此使用应用程序服务,这将帮助我用我的代码维护一些抽象
    • @Pakard,很高兴知道它很有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 2023-03-27
    相关资源
    最近更新 更多