-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
131 lines (104 loc) · 3 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
130
# Project libs
from naturalLanguageProcessing import Nlp
from pathFindingProcessing import PathFinder
from voiceProcessing import VoiceProcessing
from controllers import MainController
import speech_recognition as sr
# Others
import sys
import os
import requests
# Flask
from flask import Flask, session, request, render_template, redirect, url_for
app = Flask(__name__)
# Set the secret key to some random bytes. Keep this really secret!
app.secret_key = b'_8#y2o"J4Q9z\n\xec]/'
# initialise components
VP = VoiceProcessing()
NLP = Nlp()
PF = PathFinder()
print('Done initialisation component')
def resetNlp():
NLP = Nlp()
NLP.reset()
NLP.train()
NLP.test()
""" Reset Nlp route """
@app.route('/reset')
def reset():
resetNlp()
return "Nlp a été reset avec succès."
""" Home route """
@app.route('/')
def home():
# generate uniq id for our user
if 'userId' not in session:
userId = os.urandom(16)
session['userId'] = userId
# clean session for result
if 'result' in session:
session.pop('result')
# render home page
return render_template('home.html')
""" Result route """
@app.route('/result')
def result():
# store it in session
if 'userId' in session and 'result' in session:
# render result page
return render_template('result.html')
else:
return redirect(url_for('home'))
""" Process route """
@app.route('/process', methods=['POST'])
def process():
# clean session errors
if 'errors' in session:
session.pop('errors')
userId = request.form['userId']
audio = request.files['audio']
# get the userId in request args and check it's egal to our session['userId]
if session['userId'] and str(userId) == str(session['userId']):
# initialise main controller
processor = MainController(VP, NLP, PF)
# dispatch request
res = processor.process_audio(audio)
if int(res[1]) == 666: # Error
# save error message to session
session['errors'] = res[0]
else:
# save result to session
session['result'] = res[0]
# return result status
return dict({'status': res[1]})
else:
return dict({'status': 401})
""" Main program """
def main():
voice_process = VoiceProcessing()
# A remplacer avec la récupéation du model
NLP = Nlp()
### a enlever si l'entrainement n'est pas nécessaire pour vous
# NLP.reset()
# NLP.train(n_iter=400)
###
NLP.test()
try:
# Usecase: handling from a microphone
# resultFromVoice = voice_process.from_audio()
# Usecase: handling from an audiofile
# resultFromVoice = voice_process.from_file(pathfile="oss117.mp4")
# start, end = NLP.predict(resultFromVoice)
# print("Trajet", start, " - ", end)
pass
except sr.RequestError:
print("Connection problem, please try again later")
return 1
except sr.UnknownValueError:
print("Unintelligible text, please provide a new record ")
return 1
except Exception as identifier:
print(identifier)
if __name__ == "__main__":
# Reset NLP by hand if you want a MAJ on it before pushing repo into production (heroku)
resetNlp()