-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
98 lines (77 loc) · 2.32 KB
/
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
import json
from flask import Flask, render_template, request
import book
import data
from data_types import NameProfile, Profile, profile_descriptions
from trans import transliterate, transliterate_names
from vocalization import vocalize
app = Flask(__name__)
# Pages
@app.route("/")
def index():
# name, value, type, title, description
profile = [
(
name,
field.default,
field.type.__name__,
*profile_descriptions.get(
name, (" ".join(name.split("_")).capitalize(), "", "", "")
),
)
for (name, field) in Profile.__dataclass_fields__.items()
]
return render_template(
"index.html", profile=profile, input_map=data.input_conversion_map
)
@app.route("/names")
def names():
profile = [
(
name,
field.default,
field.type.__name__,
*profile_descriptions.get(
name, (" ".join(name.split("_")).capitalize(), "", "", "")
),
)
for (name, field) in NameProfile.__dataclass_fields__.items()
]
return render_template(
"names.html", profile=profile, input_map=data.input_conversion_map
)
@app.route("/book")
def _book():
return render_template(
"book.html", toc=book.toc_html, content=book.content, input_map=book.input_map
)
# API endpoints
@app.route("/transliterate", methods=["POST"])
def trans():
"""
Takes a string of Arabic text and settings and returns a transliteration
"""
data = json.loads(request.data)
text = data["text"]
profile = Profile(**data["profile"])
return transliterate(text, profile)
@app.route("/transliterate-names", methods=["POST"])
def trans_names():
data = json.loads(request.data)
text = data["text"]
profile = NameProfile(**data["profile"])
return transliterate_names(text, profile)
@app.route("/vocalize", methods=["POST"])
def vocalization():
"""
Takes a string of Arabic text and returns a vocalized version
"""
text = request.data.decode("utf-8")
return vocalize(text)
@app.route("/feedback", methods=["POST"])
def feedback():
with open("feedback.jsonl", "ab") as f:
f.write(request.data + b"\n")
return ""
if __name__ == "__main__":
app.run(host="0.0.0.0", port="5000", debug=True)