/
flask_app.py
659 lines (530 loc) · 19.2 KB
/
flask_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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import os
import html
import re
from datetime import datetime, date
from flask import Flask, redirect, render_template, request, url_for, session, send_from_directory, send_file, make_response
from skrutable.transliteration import Transliterator
from skrutable.scansion import Scanner
from skrutable.meter_identification import MeterIdentifier
from skrutable.meter_patterns import meter_melodies
from skrutable.splitter.wrapper import Splitter
app = Flask(__name__)
app.config["DEBUG"] = True
app.config["SECRET_KEY"] = "asdlkvumnxlapoiqyernxnfjtuzimzjdhryien" # for session, no actual need for secrecy
# for serving static files from assets folder
@app.route('/assets/<path:name>')
def serve_files(name):
return send_from_directory('assets', name)
# attempt at serving entire folder at once (not yet successful)
# @app.route('/assets/')
# def serve_files():
# return send_file('assets/index.html')
# this helps app work both publically (e.g. on PythonAnywhere) and locally
CURRENT_FOLDER = os.path.dirname(os.path.abspath(__file__))
# Skrutable main objects
T = Transliterator()
S = Scanner()
MI = MeterIdentifier()
Spl = Splitter()
# variable names for flask.session() object
select_element_names = [
"skrutable_action",
"text_input", "text_output",
"from_scheme", "to_scheme",
"resplit_option",
]
checkbox_element_names = [
"weights", "morae", "gaRas",
"alignment",
] # to be extended with corpus text abbreviations for textPrioritize
melody_variable_names = [
"meter_label", "melody_options"
]
session_variable_names = (
select_element_names +
checkbox_element_names +
melody_variable_names
)
# for updating session variables
def process_form(form):
# first do values of "select" elements (i.e. dropdowns)
for var_name in select_element_names:
# print(var_name, request.form[var_name])
session[var_name] = request.form[var_name]
# then do values of "checkbox" elements for scansion detail
scan_detail_option_choices = request.form.getlist("scan_detail")
for var_name in checkbox_element_names:
if var_name in scan_detail_option_choices:
session[var_name] = 1
else:
session[var_name] = 0
session.modified = True
return
# for meter-id resplit option, which has two parts
def parse_complex_resplit_option(complex_resplit_option):
if complex_resplit_option.endswith('_keep_mid'):
resplit_keep_midpoint = True
resplit_option = complex_resplit_option[:-len('_keep_mid')]
else:
resplit_keep_midpoint = False
resplit_option = complex_resplit_option
return resplit_option, resplit_keep_midpoint
def ensure_keys():
# just in case, make sure all keys in session
for var_name in session_variable_names:
if var_name not in session:
reset_variables()
@app.route("/", methods=["GET", "POST"])
def index():
ensure_keys()
if request.method == "GET":
return render_template(
"main.html",
skrutable_action=session["skrutable_action"],
text_input=session["text_input"], text_output=session["text_output"],
from_scheme=session["from_scheme"], to_scheme=session["to_scheme"],
weights=session["weights"],
morae=session["morae"],
gaRas=session["gaRas"],
alignment=session["alignment"],
resplit_option=session["resplit_option"],
meter_label=session["meter_label"],
melody_options=session["melody_options"]
)
if request.method == "POST":
process_form(request.form)
# carry out chosen action
if session["skrutable_action"] == "transliterate":
session["text_output"] = T.transliterate(
session["text_input"],
from_scheme=session["from_scheme"],
to_scheme=session["to_scheme"]
)
session["meter_label"] = ""; session["melody_options"] = [] # cancel these
elif session["skrutable_action"] == "scan":
V = S.scan(
session["text_input"] ,
from_scheme=session["from_scheme"]
)
session["text_output"] = V.summarize(
show_weights=session["weights"],
show_morae=session["morae"],
show_gaRas=session["gaRas"],
show_alignment=session["alignment"],
show_label=False
)
session["meter_label"] = ""; session["melody_options"] = [] # cancel these
elif session["skrutable_action"] == "identify meter":
r_o, r_k_m = parse_complex_resplit_option(
complex_resplit_option=session["resplit_option"]
)
V = MI.identify_meter(
session["text_input"] ,
resplit_option=r_o,
resplit_keep_midpoint=r_k_m,
from_scheme=session["from_scheme"]
)
session["text_output"] = V.summarize(
show_weights=session["weights"],
show_morae=session["morae"],
show_gaRas=session["gaRas"],
show_alignment=session["alignment"],
show_label=True
)
short_meter_label = V.meter_label[:V.meter_label.find(' ')]
if short_meter_label in meter_melodies:
session["meter_label"] = T.transliterate(
short_meter_label,
from_scheme='IAST',
to_scheme='HK'
)
session["melody_options"] = meter_melodies[ short_meter_label ]
else:
session["meter_label"] = ""; session["melody_options"] = [] # cancel these
elif session["skrutable_action"] == "split":
IAST_input = T.transliterate(
session["text_input"],
from_scheme=session["from_scheme"],
to_scheme='IAST'
)
split_result = Spl.split(
IAST_input,
prsrv_punc=True
)
session["text_output"] = T.transliterate(
split_result,
from_scheme='IAST',
to_scheme=session["to_scheme"]
)
session["meter_label"] = ""; session["melody_options"] = [] # cancel these
elif session["skrutable_action"] == "apte links":
session["meter_label"] = ""; session["melody_options"] = [] # cancel these
split_text = session["text_input"] # must already be split
output_HTML = prep_split_output_for_Apte(split_text)
return render_template( "main_HTML_output.html",
skrutable_action=session["skrutable_action"],
text_input=session["text_input"],
output_HTML=output_HTML,
from_scheme=session["from_scheme"], to_scheme=session["to_scheme"],
weights=session["weights"],
morae=session["morae"],
gaRas=session["gaRas"],
alignment=session["alignment"],
resplit_option=session["resplit_option"],
melody_options=session["melody_options"]
)
session.modified = True
return redirect(url_for('index'))
@app.route("/wholeFile", methods=["POST"])
def wholeFile():
ensure_keys()
# when form sent from GUI ("whole file" button clicked)
if request.form != {}:
process_form(request.form)
# send onward to upload form
return render_template(
"wholeFile.html",
skrutable_action=session["skrutable_action"],
text_input=session["text_input"], text_output=session["text_output"],
from_scheme=session["from_scheme"], to_scheme=session["to_scheme"],
weights=session["weights"], morae=session["morae"], gaRas=session["gaRas"],
alignment=session["alignment"],
resplit_option=session["resplit_option"]
)
# when file chosen for upload
elif request.files != {}:
# session variables already processed in previous step
# take in and read file
input_file = request.files["input_file"]
input_fn = input_file.filename
input_data = input_file.stream.read().decode('utf-8')
# carry out chosen action
if session["skrutable_action"] == "transliterate":
output_data = T.transliterate(
input_data,
from_scheme=session["from_scheme"],
to_scheme=session["to_scheme"]
)
output_fn_suffix = '_transliterated'
elif session["skrutable_action"] == "identify meter":
r_o, r_k_m = parse_complex_resplit_option(
complex_resplit_option=session["resplit_option"]
)
# record starting time
# now = datetime.now()
# timestamp1 = now.strftime("%H:%M:%S")
starting_time = datetime.now().time()
verses = input_data.splitlines() # during post \n >> \r\n
output_data = ''
# output_data = "%s\n\n" % timestamp1
for verse in verses:
result = MI.identify_meter(
verse,
resplit_option=r_o,
resplit_keep_midpoint=r_k_m,
from_scheme=session['from_scheme']
)
output_data += (
result.text_raw + '\n\n' +
result.summarize(
show_weights=session["weights"],
show_morae=session["morae"],
show_gaRas=session["gaRas"],
show_alignment=session["alignment"],
show_label=True
) +
'\n'
)
# record ending time
ending_time = datetime.now().time()
# report total duration
delta = datetime.combine(date.today(), ending_time) - datetime.combine(date.today(), starting_time)
duration_secs = delta.seconds + delta.microseconds / 1000000
output_data += "samāptam: %d padyāni, %f kṣaṇāḥ" % ( len(verses), duration_secs )
output_fn_suffix = '_meter_identified'
elif session["skrutable_action"] == "split":
IAST_input = T.transliterate(
input_data,
from_scheme=session["from_scheme"],
to_scheme='IAST'
)
split_result = Spl.split(
IAST_input,
prsrv_punc=True,
wholeFile=True
)
output_data = T.transliterate(
split_result,
from_scheme='IAST',
to_scheme=session["to_scheme"]
)
output_fn_suffix = '_split'
# prepare and return output file
output_fn = ( input_fn[:input_fn.find('.')] +
output_fn_suffix +
input_fn[input_fn.find('.'):]
)
response = make_response( output_data )
response.headers["Content-Disposition"] = "attachment; filename=%s" % output_fn
return response
@app.route('/api', methods=["GET"])
def api_landing():
return render_template("api.html")
def get_inputs(required_args, request):
if required_args[0] != "input_text":
return "The variable input_text should always be first in required_arg_list"
if not (request.form or request.json):
return "Received neither form nor json input."
data_source = dict(request.form or request.json)
error_msg = (
"Couldn't get all fields:\n" +
f"required_args: {required_args}\n" +
f"request.files {request.files}\n" +
"data_source (" + ("json" if request.json else "form") + f") {data_source}"
)
try:
if request.files:
input_file = request.files["input_file"]
input_fn = input_file.filename
input_text = input_file.stream.read().decode('utf-8')
else: # should all be in either form or json
input_text = data_source["input_text"]
except:
return error_msg
inputs = {"input_text": input_text}
for arg in required_args[1:]:
if arg not in data_source:
return error_msg
# convert boolean strings to real booleans
if data_source[arg].lower() == 'true':
data_source[arg] = True
elif data_source[arg].lower() == 'false':
data_source[arg] = False
inputs[arg] = data_source[arg]
return inputs
@app.route('/api/transliterate', methods=["GET", "POST"])
def api_transliterate():
# assume that GET request is person surfing in browser
if request.method == "GET":
return render_template("POSTonly.html")
inputs = get_inputs(["input_text", "from_scheme", "to_scheme"], request)
if isinstance(inputs, str):
return inputs # == error_msg
result = T.transliterate(
inputs["input_text"],
from_scheme=inputs["from_scheme"],
to_scheme=inputs["to_scheme"],
)
return result
@app.route('/api/scan', methods=["GET", "POST"])
def api_scan():
if request.method == "GET":
return render_template("POSTonly.html")
inputs = get_inputs(
[ "input_text",
"from_scheme",
"show_weights",
"show_morae",
"show_gaRas",
"show_alignment"
],
request
)
if isinstance(inputs, str):
return inputs # == error_msg
V = S.scan(
inputs["input_text"],
from_scheme=inputs["from_scheme"],
)
result = V.summarize(
show_weights=inputs["show_weights"],
show_morae=inputs["show_morae"],
show_gaRas=inputs["show_gaRas"],
show_alignment=inputs["show_alignment"],
show_label=False
)
return result
@app.route('/api/identify-meter', methods=["GET", "POST"])
def api_identify_meter():
if request.method == "GET":
return render_template("POSTonly.html")
inputs = get_inputs(
[ "input_text",
"from_scheme",
"show_weights",
"show_morae",
"show_gaRas",
"show_alignment",
"resplit_option"
],
request
)
if isinstance(inputs, str):
return inputs # == error_msg
r_o, r_k_m = parse_complex_resplit_option(
complex_resplit_option=inputs["resplit_option"]
)
V = MI.identify_meter(
inputs["input_text"] ,
resplit_option=r_o,
resplit_keep_midpoint=r_k_m,
from_scheme=inputs["from_scheme"]
)
result = V.summarize(
show_weights=inputs["show_weights"],
show_morae=inputs["show_morae"],
show_gaRas=inputs["show_gaRas"],
show_alignment=inputs["show_alignment"],
show_label=True
)
return result
@app.route('/api/split', methods=["GET", "POST"])
def api_split():
if request.method == "GET":
return render_template("POSTonly.html")
inputs = get_inputs(
[ "input_text",
"from_scheme",
"to_scheme",
],
request
)
if isinstance(inputs, str):
return inputs # == error_msg
IAST_input = T.transliterate(
inputs["input_text"],
from_scheme=inputs["from_scheme"],
to_scheme='IAST'
)
split_result = Spl.split(
IAST_input,
prsrv_punc=True,
)
result = T.transliterate(
split_result,
from_scheme='IAST',
to_scheme=inputs["to_scheme"]
)
return result
@app.route('/reset')
def reset_variables():
session["skrutable_action"] = "..."
session["text_input"] = ""; session["text_output"] = ""
session["from_scheme"] = "IAST"; session["to_scheme"] = "IAST"
session["weights"] = 1; session["morae"] = 1; session["gaRas"] = 1
session["alignment"] = 1
session["resplit_option"] = "resplit_lite_keep_mid"
session["meter_label"] = ""
session["melody_options"] = []
session.modified = True
return redirect(url_for('index'))
@app.route('/ex1')
def ex1():
session["text_input"] = "dharmakṣetre kurukṣetre samavetā yuyutsavaḥ /\nmāmakāḥ pāṇḍavāś caiva kim akurvata sañjaya //"
session["text_output"] = """धर्मक्षेत्रे कुरुक्षेत्रे समवेता युयुत्सवः /
मामकाः पाण्डवाश्चैव किमकुर्वत सञ्जय //"""
session["from_scheme"] = "IAST"; session["to_scheme"] = "DEV"
session["weights"] = 1; session["morae"] = 1; session["gaRas"] = 1
session["alignment"] = 1
session["resplit_option"] = "resplit_lite_keep_mid"
session["skrutable_action"] = "transliterate"
session.modified = True
return redirect(url_for('index'))
@app.route('/ex2')
def ex2():
session["text_input"] = """धात्वर्थं बाधते कश्चित् कश्चित् तमनुवर्तते |
तमेव विशिनष्ट्यन्य उपसर्गगतिस्त्रिधा ||"""
session["text_output"] = """gggglggl {m: 14} [8: mrgl]
gglllglg {m: 12} [8: tslg]
lglllggl {m: 11} [8: jsgl]
llgllglg {m: 11} [8: sslg]
dhā tva rthaṃ bā dha te ka ści
g g g g l g g l
tka ści tta ma nu va rta te
g g l l l g l g
ta me va vi śi na ṣṭya nya
l g l l l g g l
u pa sa rga ga ti stri dhā
l l g l l g l g
anuṣṭubh (1,2: pathyā, 3,4: pathyā)"""
session["from_scheme"] = "DEV"; session["to_scheme"] = "IAST"
session["weights"] = 1; session["morae"] = 1; session["gaRas"] = 1
session["alignment"] = 1
session["resplit_option"] = "resplit_lite_keep_mid"
session["skrutable_action"] = "identify meter"
session["meter_label"] = "anuSTubh"
session["melody_options"] = ['Madhura Godbole', 'H.V. Nagaraja Rao', 'Shatavadhani Ganesh', 'Diwakar Acarya']
session.modified = True
return redirect(url_for('index'))
@app.route('/ex3')
def ex3():
session["text_input"] = """तव करकमलस्थां स्फाटिकीमक्षमालां , नखकिरणविभिन्नां दाडिमीबीजबुद्ध्या |
प्रतिकलमनुकर्षन्येन कीरो निषिद्धः , स भवतु मम भूत्यै वाणि ते मन्दहासः ||"""
session["text_output"] = """llllllggglgglgg {m: 22} [15: nnmyy]
llllllggglgglgg {m: 22} [15: nnmyy]
llllllggglgglgg {m: 22} [15: nnmyy]
llllllggglgglgg {m: 22} [15: nnmyy]
ta va ka ra ka ma la sthāṃ sphā ṭi kī ma kṣa mā lāṃ
l l l l l l g g g l g g l g g
na kha ki ra ṇa vi bhi nnāṃ dā ḍi mī bī ja bu ddhyā
l l l l l l g g g l g g l g g
pra ti ka la ma nu ka rṣa nye na kī ro ni ṣi ddhaḥ
l l l l l l g g g l g g l g g
sa bha va tu ma ma bhū tyai vā ṇi te ma nda hā saḥ
l l l l l l g g g l g g l g g
mālinī [15: nnmyy]"""
session["from_scheme"] = "DEV"; session["to_scheme"] = "IAST"
session["weights"] = 1; session["morae"] = 1; session["gaRas"] = 1
session["alignment"] = 1
session["resplit_option"] = "resplit_lite_keep_mid"
session["skrutable_action"] = "identify meter"
session["meter_label"] = "mAlinI"
session["melody_options"] = ['Madhura Godbole', 'Sadananda Das', 'H.V. Nagaraja Rao', 'Shatavadhani Ganesh']
session.modified = True
return redirect(url_for('index'))
@app.route('/about')
def about_page():
return render_template("about.html")
@app.route('/tutorial')
def tutorial_page():
return render_template("tutorial.html")
@app.route('/next')
def next_page():
return render_template("next.html")
@app.route('/scanGRETIL')
def scanGRETIL_page():
return render_template("scanGRETIL.html")
@app.route('/scanGRETILresults')
def scanGRETILresults_page():
return render_template( "scanGRETILresults.html",
parent_dir = "https://raw.githubusercontent.com/tylergneill/skrutable_front_end/main/assets/meter_analyses/",
dir1 = "1_input_raw",
dir2 = "2_input_cleaned",
dir3 = "3_output_raw",
dir4 = "4_output_cleaned",
dir5 = "5_tallies",
dir6 = "6_notes"
)
@app.route('/reciters')
def reciters_page():
return render_template("reciters.html")
def prep_Apte_query(IAST_string):
dev_query = T.transliterate(IAST_string, from_scheme='IAST', to_scheme='DEV')
while dev_query[-1] == chr(0x94d): dev_query = dev_query[:-1] # remove final Devanagari virāma(s) (U+094d)
chicago_url = "https://dsal.uchicago.edu/cgi-bin/app/apte_query.py?qs=%s&searchhws=yes&matchtype=default" % dev_query
return chicago_url
@app.route('/ChicagoApteIAST') # hidden endpoint
def chicago_apte_iast():
iast_query = request.args.get('query')
chicago_url = prep_Apte_query(iast_query)
return redirect(chicago_url)
def prep_split_output_for_Apte(split_text):
split_words = split_text.split() # would be nice to retain original whitespace (tab etc.)
output_HTML = "<p>"
for word in split_words:
if re.sub('[,\|।—\?!\.\d\s]', '', word) == "": # expand punctuation set ...
output_HTML += word + " "
else:
output_HTML += "<a href='%s' target='apteTab'>%s</a> " % (prep_Apte_query(word), word)
output_HTML += "</p>"
return output_HTML