Search code examples
azure-web-app-serviceazure-api-managementazure-cognitive-servicesazure-api-appsazure-app-service-envrmnt

how to send text as an input and receive sentiment or emotion as a response in azure API?


I would like to implement a python script in an azure API. Basically, I would like to send text and receive emotion or sentiment. I am assuming that the API is the writing approach or is it a web app? Would you be able to direct me to documentation with an example? Thank you!


Solution

  • I think this Azure Text Analytics API could meet your requirement, this API provides you with Analyze sentiment functionality and associated python SDK. You can try the code below to get started with python SDK V2 sentiment function:

    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))
    

    Result: enter image description here

    You can find your service endpoint and subscriptionKey on Azure portal:

    enter image description here

    After you Create a Cognitive Service on Azure portal.

    You can deploy your application on the Azure app service, for how to do it, just refer to this doc.