forked from monicatang/coursestamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (80 loc) · 3.7 KB
/
main.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
from flask import flash, Flask, render_template, request, redirect
from forms import QueryForm
from flask_wtf.csrf import CSRFProtect
import os, sys, houndify
import speech_recognition as sr
from phrase_occurrences import find_occurrences
app = Flask(__name__)
app.config.from_pyfile('config.py', silent=True)
csrf = CSRFProtect(app)
search_string = ''
RESULT_LIMIT = 100
SECRET_KEY = 'go bears'
app.config['SECRET_KEY'] = SECRET_KEY
client_id = app.config['HOUNDIFY_ID']
client_key = app.config['HOUNDIFY_KEY']
user_id = app.config['HOUNDIFY_USR']
client = houndify.StreamingHoundClient(client_id, client_key, user_id, sampleRate=8000)
@app.route('/', methods=('GET', 'POST'))
def home():
select = QueryForm(request.form)
if request.method == 'POST':
prof_name = request.form['profs']
return redirect('/'+prof_name)
opt_list = [x[0] for x in os.walk('examples')]
opt_list = [i.replace('examples\\', '') for i in opt_list if 'raw_subtitles' not in i]
opt_list = [i.replace('examples/', '') for i in opt_list if 'raw_subtitles' not in i]
opt_list = [(i.replace('_', ' ').title(), i) for i in opt_list][1:]
opt_list = [(open("examples/"+i[1]+"/"+i[1]+".txt", "r").read()+" - "+i[0], i[1]) for i in opt_list]
return render_template('index.html', form=select, option_list=opt_list, dropdown = True, search_menu = False)
@app.route('/<prof_name>', methods=('GET', 'POST'))
def pick_prof(prof_name):
search = QueryForm(request.form)
if request.method == 'POST':
if request.form['search'] == "voice":
search_string = listen()
else:
search_string = request.form['query']
if search_string != "":
search_string = '/' + search_string
return redirect('/'+prof_name+search_string)
f = open("examples/"+prof_name+"/"+prof_name+".txt", "r")
prof_name = prof_name.replace("_", " ").title()
course_name = f.read()
return render_template('index.html', form=search, dropdown = False, search_menu = True, course_name=course_name, prof_name=prof_name)
@app.route('/<prof_name>/<search_term>', methods=('GET', 'POST'))
def results(prof_name, search_term):
titles, urls, raw_phrases, term_idxes = find_occurrences(search_term, "examples/"+prof_name+"/"+prof_name+".json")
leftContexts = []
rightContexts = []
titleContexts = []
for idx, phrase in enumerate(raw_phrases):
leftContexts.append(phrase[:term_idxes[idx]])
rightContexts.append(phrase[term_idxes[idx]+len(search_term):])
titleContexts.append(titles[idx])
search = QueryForm(request.form)
if request.method == 'POST':
if request.form['search'] == "voice":
search_string = listen()
else:
search_string = request.form['query']
if search_string != "":
search_string = '/' + search_string
return redirect('/'+prof_name+search_string)
f = open("examples/"+prof_name+"/"+prof_name+".txt", "r")
prof_name = prof_name.replace("_", " ").title()
course_name = f.read()
return render_template('index.html', form=search,
name=search_term, urls=urls[:RESULT_LIMIT], leftContexts=leftContexts,
rightContexts=rightContexts, titleContexts=titleContexts, showViewer=len(urls)>0, dropdown = False, search_menu = True, course_name=course_name, prof_name=prof_name)
def listen():
search = QueryForm(request.form)
rec = sr.Recognizer()
with sr.Microphone() as source:
rec.adjust_for_ambient_noise(source)
app.logger.info("I'm listening...")
audio = rec.listen(source, phrase_time_limit=3)
search_string = rec.recognize_houndify(audio, client_id, client_key)
return search_string
if __name__ == "__main__":
app.run(debug=True)