-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_app.py
370 lines (349 loc) · 11.5 KB
/
function_app.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import azure.functions as func
import logging
import datetime
import json
from azure.storage.blob import BlobServiceClient
import helpers
from openai import AzureOpenAI
import requests
import os
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.route(route="Welcomepage")
def Welcomepage(req: func.HttpRequest) -> func.HttpResponse:
html_content = """
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url('WelcomeBackgroundSAS');
background-size: cover;
box-shadow: 0 0 200px rgba(10, 10, 10, 50);
}
.welcome-text {
font-size: 50px;
text-align: center;
margin-top: 300px;
color: #ffffff;
text-shadow: 5px 5px 5px rgba(2, 2, 2, 5);
font-family: 'Amasis MT Pro Black', sans-serif;
}
.start-button {
display: block;
width: 400px;
margin: 20px auto;
padding: 20px;
background-color: #0078D4;
color: #ffffff;
text-align: center;
text-decoration: none;
border-radius: 15px;
}
</style>
</head>
<body>
<div id="img.logo" text-align:center" >
<img height="60" src="LogoSAS" />
</div>
<div class="welcome-text">Call Center Insight Uygulamasına Hoşgeldiniz</div>
<a href="https://localhost:7071/api/Uploadpage" class="start-button">Let's Start</a>
</body>
</html>
"""
return func.HttpResponse(html_content, mimetype="text/html")
# @app.route('/Extractionpage', methods=['POST'])
@app.route(route="Extractionpage")
def Extractionpage(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
# file= req.get_body().decode('utf-8')
file = req.files.get('transcriptFile')
transcript_prompt = req.form.get('transcriptPrompt')
#file=req.files
file_content = file.read().decode('utf-8')
upload_json_file(file_content)
# transcript_prompt = req.params.get('transcriptPrompt')
result = helpers.insight_extraction(file_content, transcript_prompt)
html_content = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Call Center Insights Demo</title>
<style>
body {{
margin: 0;
padding: 0;
background-image: url('ExtractionBackgroundSAS');
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: 'Times New Roman', Times, serif;
}}
.container {{
text-align: center;
margin: 0 auto;
background-color: #ffffff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
max-width: 900px;
width: 100%;
}}
h1 {{
text-align: center;
color: #0078D4;
margin-bottom: 20px;
}}
.form-section {{
display: flex;
justify-content: space-between;
}}
.form-container {{
width: 48%;
text-align: left;
}}
h3 {{
font-size: 1.2em;
margin-bottom: 10px;
}}
label {{
display: block;
margin-bottom: 5px;
}}
textarea {{
width: 100%;
height: 300px;
margin-bottom: 15px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
resize: none;
}}
button {{
width: 100%;
padding: 10px;
border: none;
background-color: #0078D4;
color: white;
border-radius: 5px;
cursor: pointer;
}}
button:hover {{
background-color: #005a9e;
}}
img.logo {{
position: absolute;
top: 10px;
left: 10px;
}}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {{
var fileContent = {json.dumps(file_content)};
document.getElementById('transcript').textContent = fileContent;
}});
</script>
</head>
<body>
<div id="logo" text-align="center">
<img class="logo" height="50" src="LogoSAS" />
</div>
<div class="container">
<h1>Call Center Insights Demo</h1>
<div class="form-section">
<div class="form-container">
<h3>Inputs Preview:</h3>
<label for="transcript">Transcript:</label>
<textarea id="transcript" name="transcript"></textarea>
</div>
<div class="form-container">
<h3>Result:</h3>
<label for="blobContents">Json Output:</label>
<textarea id="blobContents" readonly>{result.decode('utf-8')}</textarea>
</div>
</div>
</div>
</body>
</html>
"""
return func.HttpResponse(html_content, mimetype="text/html")
@app.route(route="Uploadpage")
def Uploadpage(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
html_content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Call Center Insights Demo</title>
<link rel="icon" type="image/png" href="LogoSAS">
<style>
body {
margin: 0;
padding: 0;
background-image: url('UploadBackgroundSAS');
background-size: cover;
box-shadow: 0 0 50px rgba(0, 0, 0, 0.8);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
text-align: center;
margin: 0 auto;
background-color: #ffffff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
max-width: 900px;
width: 100%;
}
h1 {
text-align: center;
color: #0078D4;
}
.form-section {
display: flex;
justify-content: space-around;
}
.form-container {
width: 45%;
text-align: left;
}
h2 {
font-size: 1.2em;
margin-bottom: 10px;
}
form {
display: flex;
flex-direction: column;
align-items: center;
}
label {
margin-bottom: 5px;
text-align: left;
width: 100%;
}
input[type="file"] {
margin-bottom: 15px;
width: 100%;
}
textarea {
width: 100%;
height: 100px;
margin-bottom: 15px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px 15px;
border: none;
background-color: #0078D4;
color: white;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #357ae8;
}
img.logo {
position: absolute;
top: 10px;
left: 10px;
}
#gif {
display: none;
text-align: center;
}
#gif img {
height: 50px;
}
#gif h3 {
margin: 10px 0 0;
}
.left-align {
text-align: left;
}
</style>
</head>
<body>
<div id="logo" text-align="center">
<img class="logo" height="50" src="LogoSAS" />
</div>
<div class="container">
<h1>Call Center Insights Demo</h1>
<div class="form-section">
<div class="form-container">
<h2>From Transcript:</h2>
<form id="transcriptForm" action="Extractionpage" method="post" enctype="multipart/form-data">
<label for="transcriptFile">Transcript File (.json):</label>
<input type="file" id="transcriptFile" name="transcriptFile">
<label for="transcriptPrompt">Prompt:</label>
<textarea id="transcriptPrompt" name="transcriptPrompt" placeholder="You are an AI assistant..."></textarea>
<button type="submit" id="transcriptSubmit" onclick="submitForm('transcriptForm', 'transcriptSubmit')">Submit</button>
</body>
</form>
</div>
<div class="form-container">
<h2>From Voice Recording:</h2>
<form id="voiceForm" action="SpeechToTextpage" method="post" enctype="multipart/form-data">
<label for="voiceFile">Speech File (.wav):</label>
<input type="file" id="voiceFile" name="voiceFile">
<label for="voicePrompt">Prompt:</label>
<textarea id="voicePrompt" name="voicePrompt" placeholder="You are an AI assistant..."></textarea>
<button type="submit" id="voiceSubmit" onclick="submitForm('voiceForm', 'voiceSubmit')">Submit</button>
</form>
</div>
</div>
<div id="gif">
<img src="LoadingGifSAS" />
<h3>Please wait ...</h3>
</div>
</div>
<script>
function submitForm(formId, buttonId) {
var form = document.getElementById(formId);
var formData = new FormData(form);
var prompt = formData.get(formId === 'transcriptForm' ? 'transcriptPrompt' : 'voicePrompt');
formData.append('prompt', prompt);
var xhr = new XMLHttpRequest();
xhr.open('POST', form.action, true);
xhr.onload = function () {
if (xhr.status === 200) {
console.log('Form submission successful');
} else {
console.error('Form submission failed');
}
document.getElementById('gif').style.display = 'none';
document.getElementById('transcriptSubmit').style.display = 'block';
document.getElementById('voiceSubmit').style.display = 'block';
};
xhr.send(formData);
document.getElementById('gif').style.display = 'block';
document.getElementById('transcriptSubmit').style.display = 'none';
document.getElementById('voiceSubmit').style.display = 'none';
}
</script>
</body>
</html>
"""
return func.HttpResponse(html_content, mimetype="text/html")
def upload_json_file(req: func.HttpRequest) -> func.HttpResponse:
try:
file_content = req.get_body()
#upload to input file
now= datetime.datetime.now()
file_name = f"Conversation_{now.strftime('%Y-%m-%d_%H%M%S')}.json"
container_name = os.environ.get("StorageInputContainerName")
blob_service_client = BlobServiceClient.from_connection_string("BLOB_STORAGE_CONNEC_STRING")
blob_client = blob_service_client.get_blob_client(container=container_name, blob=file_name)
blob_client.upload_blob(file_content)
return func.HttpResponse(f"Uploaded {file_name} file", status_code=200)
except Exception as e:
return func.HttpResponse(f"An error occurred: {str(e)}", status_code=500)