parent
10b0744cad
commit
6b2ad9b99e
@ -1,43 +0,0 @@
|
|||||||
from typing import List
|
|
||||||
import logging
|
|
||||||
|
|
||||||
import azure.functions as func
|
|
||||||
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
from azure.cognitiveservices.language.luis.runtime import LUISRuntimeClient
|
|
||||||
from msrest.authentication import CognitiveServicesCredentials
|
|
||||||
|
|
||||||
def main(events: List[func.EventHubEvent]):
|
|
||||||
luis_key = os.environ['LUIS_KEY']
|
|
||||||
endpoint_url = os.environ['LUIS_ENDPOINT_URL']
|
|
||||||
app_id = os.environ['LUIS_APP_ID']
|
|
||||||
|
|
||||||
credentials = CognitiveServicesCredentials(luis_key)
|
|
||||||
client = LUISRuntimeClient(endpoint=endpoint_url, credentials=credentials)
|
|
||||||
|
|
||||||
for event in events:
|
|
||||||
logging.info('Python EventHub trigger processed an event: %s',
|
|
||||||
event.get_body().decode('utf-8'))
|
|
||||||
|
|
||||||
event_body = json.loads(event.get_body().decode('utf-8'))
|
|
||||||
prediction_request = { 'query' : event_body['speech'] }
|
|
||||||
|
|
||||||
prediction_response = client.prediction.get_slot_prediction(app_id, 'Staging', prediction_request)
|
|
||||||
|
|
||||||
if prediction_response.prediction.top_intent == 'set timer':
|
|
||||||
numbers = prediction_response.prediction.entities['number']
|
|
||||||
time_units = prediction_response.prediction.entities['time unit']
|
|
||||||
total_seconds = 0
|
|
||||||
|
|
||||||
for i in range(0, len(numbers)):
|
|
||||||
number = numbers[i]
|
|
||||||
time_unit = time_units[i][0]
|
|
||||||
|
|
||||||
if time_unit == 'minute':
|
|
||||||
total_seconds += number * 60
|
|
||||||
else:
|
|
||||||
total_seconds += number
|
|
||||||
|
|
||||||
logging.info(f'Timer required for {total_seconds} seconds')
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"scriptFile": "__init__.py",
|
|
||||||
"bindings": [
|
|
||||||
{
|
|
||||||
"type": "eventHubTrigger",
|
|
||||||
"name": "events",
|
|
||||||
"direction": "in",
|
|
||||||
"eventHubName": "samples-workitems",
|
|
||||||
"connection": "IOT_HUB_CONNECTION_STRING",
|
|
||||||
"cardinality": "many",
|
|
||||||
"consumerGroup": "$Default",
|
|
||||||
"dataType": "binary"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
@ -0,0 +1,44 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
import azure.functions as func
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from azure.cognitiveservices.language.luis.runtime import LUISRuntimeClient
|
||||||
|
from msrest.authentication import CognitiveServicesCredentials
|
||||||
|
|
||||||
|
|
||||||
|
def main(req: func.HttpRequest) -> func.HttpResponse:
|
||||||
|
luis_key = os.environ['LUIS_KEY']
|
||||||
|
endpoint_url = os.environ['LUIS_ENDPOINT_URL']
|
||||||
|
app_id = os.environ['LUIS_APP_ID']
|
||||||
|
|
||||||
|
credentials = CognitiveServicesCredentials(luis_key)
|
||||||
|
client = LUISRuntimeClient(endpoint=endpoint_url, credentials=credentials)
|
||||||
|
|
||||||
|
text = req.params.get('text')
|
||||||
|
prediction_request = { 'query' : text }
|
||||||
|
|
||||||
|
prediction_response = client.prediction.get_slot_prediction(app_id, 'Staging', prediction_request)
|
||||||
|
|
||||||
|
if prediction_response.prediction.top_intent == 'set timer':
|
||||||
|
numbers = prediction_response.prediction.entities['number']
|
||||||
|
time_units = prediction_response.prediction.entities['time unit']
|
||||||
|
total_seconds = 0
|
||||||
|
|
||||||
|
for i in range(0, len(numbers)):
|
||||||
|
number = numbers[i]
|
||||||
|
time_unit = time_units[i][0]
|
||||||
|
|
||||||
|
if time_unit == 'minute':
|
||||||
|
total_seconds += number * 60
|
||||||
|
else:
|
||||||
|
total_seconds += number
|
||||||
|
|
||||||
|
logging.info(f'Timer required for {total_seconds} seconds')
|
||||||
|
|
||||||
|
payload = {
|
||||||
|
'seconds': total_seconds
|
||||||
|
}
|
||||||
|
return func.HttpResponse(json.dumps(payload), status_code=200)
|
||||||
|
|
||||||
|
return func.HttpResponse(status_code=404)
|
@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"scriptFile": "__init__.py",
|
||||||
|
"bindings": [
|
||||||
|
{
|
||||||
|
"authLevel": "function",
|
||||||
|
"type": "httpTrigger",
|
||||||
|
"direction": "in",
|
||||||
|
"name": "req",
|
||||||
|
"methods": [
|
||||||
|
"get",
|
||||||
|
"post"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "http",
|
||||||
|
"direction": "out",
|
||||||
|
"name": "$return"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in new issue