-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_app.py
More file actions
138 lines (120 loc) · 4.41 KB
/
Copy pathfunction_app.py
File metadata and controls
138 lines (120 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import azure.functions as func # type: ignore
from azure.core.credentials import AzureKeyCredential # type: ignore
from azure.ai.textanalytics import TextAnalyticsClient # type: ignore
import datetime
import json
import logging
import os
app = func.FunctionApp()
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]
@app.route(route="HttpExample", auth_level=func.AuthLevel.ANONYMOUS)
def HttpExample(req: func.HttpRequest) -> func.HttpResponse:
logging.info("Python HTTP trigger function processed a request.")
name = req.params.get("name")
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get("name")
if name:
return func.HttpResponse(
f"Hello, {name}. This HTTP triggered function executed successfully."
)
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200,
)
@app.route(route="detect_language", auth_level=func.AuthLevel.ANONYMOUS)
def detect_language(req: func.HttpRequest) -> func.HttpResponse:
logging.info("Python HTTP trigger function processed a request.")
sentence = req.params.get("sentence")
if not sentence:
try:
req_body = req.get_json()
except ValueError:
pass
else:
sentence = req_body.get("sentence")
text_analytics_client = TextAnalyticsClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)
detected_language = text_analytics_client.detect_language(
[
sentence,
]
)
result = detected_language[0]
if result.primary_language:
return func.HttpResponse(
f"detected language of sentence '{sentence}': {result.primary_language.name} with confidence score {result.primary_language.confidence_score}",
status_code=200,
)
else:
return func.HttpResponse(
"The HTTP Trigger Function is running but received either no or no useful input. Pass a query string like /?sentence=i+like+clouds",
status_code=200,
)
@app.route(route="translate", auth_level=func.AuthLevel.ANONYMOUS)
def translate(req: func.HttpRequest) -> func.HttpResponse:
sentence = req.params.get("sentence")
if not sentence:
try:
req_body = req.get_json()
except ValueError:
pass
else:
sentence = req_body.get("sentence")
text_analytics_client = TextAnalyticsClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)
sentiment = text_analytics_client.analyze_sentiment(
[
sentence,
]
)
result = sentiment[0]
if result.sentiment:
return func.HttpResponse(
f"detected sentiment of sentence {sentence}: {result.sentiment}",
status_code=200,
)
else:
return func.HttpResponse(
"The HTTP Trigger Function is running but received either no or no useful input. Pass a query string like /?sentence=i+like+clouds",
status_code=500,
)
return sentence
@app.route(route="detect_sentiment", auth_level=func.AuthLevel.ANONYMOUS)
def detect_sentiment(req: func.HttpRequest) -> func.HttpResponse:
sentence = req.params.get("sentence")
if not sentence:
try:
req_body = req.get_json()
except ValueError:
pass
else:
sentence = req_body.get("sentence")
text_analytics_client = TextAnalyticsClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)
sentiment = text_analytics_client.analyze_sentiment(
[
sentence,
]
)
result = sentiment[0]
if result.sentiment:
return func.HttpResponse(
f"detected sentiment of sentence {sentence}: {result.sentiment}",
status_code=200,
)
else:
return func.HttpResponse(
"The HTTP Trigger Function is running but received either no or no useful input. Pass a query string like /?sentence=i+like+clouds",
status_code=500,
)
return sentence