-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
129 lines (93 loc) · 3.16 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
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
import json
import os
from src import main
from src import gtts
TESTING = False
if TESTING:
from src import mock as backend
else:
import script as backend
from flask import Flask, render_template, request, redirect, url_for, flash
app = Flask(__name__)
app.config['SECRET_KEY'] = r"-sadfji3_dsfjjn"
@app.route("/")
def root():
return render_template("home.html")
@app.route("/home")
def home():
return render_template("home.html")
@app.route("/about")
def about():
return render_template("about.html")
@app.route('/create/', methods=('GET', 'POST'))
def create():
print(request.form)
if request.method == 'POST':
name = request.form['name'].lower().strip()
if not name:
flash('name is required!')
elif main.user_exists(name):
flash('name has already been taken!')
else:
main.make_new_user(request.form)
return redirect(url_for('use', name=name))
return render_template('create.html')
@app.route("/login", methods=('GET', 'POST'))
def login():
if request.method == 'POST':
name = request.form['name'].lower()
if not name:
flash('Name is required!')
elif main.user_exists(name):
return redirect(url_for('use', name=name))
else:
flash('Name does not exist.')
return render_template("login.html")
last_msg:str = ""
@app.route("/use")
def use():
if not "name" in request.args:
return "bad request, missing name"
name = request.args['name']
return render_template("use.html", name = name)
@app.route('/handle_prompt')
def handle_prompt():
global last_msg
if not "data" in request.args:
return "bad request, missing data"
prompt = request.args["data"]
if prompt == "END":
return main.parse_respone(backend.end("End conversation", request.args['name']))
if prompt == "GRAPH":
return main.parse_respone(backend.end("graph", request.args['name']))
print(f"sending prompt: {prompt}")
respone = backend.chat_with_gpt(prompt)
last_msg = respone
return main.parse_respone(respone)
@app.route('/handle_innit')
def call_back_innit():
main.remove_all_in("static/audio")
global last_msg
if not "name" in request.args:
return "bad request, missing name"
name = request.args['name']
print(f"sending innit")
respone = backend.init(name)
last_msg = respone
return main.parse_respone(respone)
@app.route('/handle_tts')
def handle_tts():
if not "name" in request.args:
return "bad request, missing data"
f = open("users/" + request.args['name'] + "/usrdata.json", "r")
lang = json.load(f)["user_information"]["personal_information"]["language_choice"]
speak = backend.getTTSString(last_msg)
gender = "male"
if "num" in request.args:
print("\n ======= in to tts with num \n", lang, gender, speak)
return gtts.synthesise(speak, lang, gender, request.args['num'])
else:
print("\n ======= in to tts\n", lang, gender, speak)
return gtts.synthesise(speak, lang, gender)
if __name__ == "__main__":
app.run(host="127.0.0.1", port = 8080, debug=True)