-
Notifications
You must be signed in to change notification settings - Fork 2
/
web_ui.py
executable file
·190 lines (159 loc) · 7.17 KB
/
web_ui.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
#!/usr/bin/env python
'''
Launches a web server serving a UI through which recordings and species can be
manually selected.
'''
import argparse
import logging
import os.path
import sys
from flask import Flask, request, abort, render_template, send_file, redirect
from sqlalchemy import func
from sqlalchemy.orm import joinedload
import analysis
import db
from recordings import Recording, SelectedRecording, RecordingOverrides
from species import Species, SelectedSpecies
from select_recordings import select_recordings
from trim_recordings import trim_recording
app = Flask(__name__)
session = None
recording_overrides = None
@app.route('/')
def _root_route():
return render_template('root.html')
@app.route('/species')
def _species_list_route():
recording_counts_subquery = session\
.query(Species.species_id, func.count('*').label('num_recordings'))\
.join(Recording, Species.scientific_name == Recording.scientific_name)\
.group_by(Species.species_id)\
.subquery()
species_with_selection = session.query(Species, SelectedSpecies)\
.outerjoin(SelectedSpecies, Species.species_id == SelectedSpecies.species_id)\
.join(recording_counts_subquery, recording_counts_subquery.c.species_id == Species.species_id)\
.options(joinedload(Species.common_names))\
.order_by(SelectedSpecies.ranking, recording_counts_subquery.c.num_recordings.desc())\
.all()
selected_species = [species for (species, selected) in species_with_selection if selected]
unselected_species = [species for (species, selected) in species_with_selection if not selected]
recording_counts = dict(session\
.query(Species.species_id, func.count('*').label('num_recordings'))\
.join(Recording, Species.scientific_name == Recording.scientific_name)\
.group_by(Species.species_id)\
.all())
selected_recording_counts = dict(session\
.query(Species.species_id, func.count('*').label('num_recordings'))\
.join(Recording, Species.scientific_name == Recording.scientific_name)\
.join(SelectedRecording, SelectedRecording.recording_id == Recording.recording_id)\
.group_by(Species.species_id)\
.all())
return render_template(
'species_list.html',
selected_species=selected_species,
unselected_species=unselected_species,
recording_counts=recording_counts,
selected_recording_counts=selected_recording_counts)
@app.route('/species/<string:scientific_name>')
def _species_route(scientific_name):
highlight_recording_id = request.args.get('highlight_recording_id', None)
species = session.query(Species)\
.filter(Species.scientific_name == scientific_name)\
.one_or_none()
if not species:
abort(404)
recordings = session.query(Recording)\
.options(joinedload(Recording.sonogram_analysis))\
.filter(Recording.scientific_name == scientific_name)\
.all()
recordings.sort(key=analysis.recording_quality, reverse=True)
group_size_limit = int(request.args.get('group_size_limit', 30))
groups = {
'song': [],
'call': [],
'other': [],
}
group_sizes = {group: 0 for group in groups}
for recording in recordings:
types = recording.types
song = any('song' in type for type in types)
call = any('call' in type for type in types)
if song and not call:
groups['song'].append(recording)
elif call and not song:
groups['call'].append(recording)
else:
groups['other'].append(recording)
for group in list(groups.keys()):
group_sizes[group] = len(groups[group])
groups[group] = groups[group][:group_size_limit]
selected_recordings_by_id = {
selected_recording.recording_id: selected_recording
for selected_recording in session.query(SelectedRecording)\
.join(Recording)\
.filter(Recording.scientific_name == scientific_name)
}
return render_template(
'species.html',
species=species,
groups=groups,
group_sizes=group_sizes,
group_size_limit=group_size_limit,
selected_recordings_by_id=selected_recordings_by_id,
recording_overrides=recording_overrides,
highlight_recording_id=highlight_recording_id)
@app.route('/recordings')
def _recordings_route():
recording_id = request.args['recording_id']
recording = session.query(Recording).filter(Recording.recording_id == recording_id).one_or_none()
if not recording:
recording = session.query(Recording).filter(Recording.recording_id.like(f'%{recording_id}%')).first()
if not recording:
abort(404)
species = session.query(Species).filter(Species.scientific_name == recording.scientific_name).one()
return redirect(f'/species/{species.scientific_name}?highlight_recording_id={recording.recording_id}')
@app.route('/recordings/trimmed/<string:recording_id>')
def _trimmed_recording_route(recording_id):
recording = session.query(Recording).filter(Recording.recording_id == recording_id).one()
file_name = trim_recording(recording, skip_if_exists=True)
return send_file(file_name, mimetype='audio/ogg')
@app.route('/recording_overrides/<string:recording_id>', methods=['POST'])
def _recording_overrides_route(recording_id):
status = request.form['status']
reason = request.form['reason']
if not session.query(Recording).filter(Recording.recording_id == recording_id).one_or_none():
abort(404)
if status:
logging.info(f'Setting override for {recording_id} to {status} ({reason})')
recording_overrides.set(recording_id, status, reason)
else:
logging.info(f'Removing override for {recording_id}')
recording_overrides.delete(recording_id)
recording_overrides.save()
recording = session.query(Recording).filter(Recording.recording_id == recording_id).one()
species = session.query(Species).filter(Species.scientific_name == recording.scientific_name).one()
select_recordings(session, species, recording_overrides)
session.commit()
return redirect(f'/species/{species.scientific_name}')
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'--log_level', default='info', choices=['debug', 'info', 'warning', 'error', 'critical'],
help='Verbosity of logging')
parser.add_argument(
'--watch', action='store_true',
help='Automatically restart the server if source code or templates are changed')
args = parser.parse_args()
log_level = getattr(logging, args.log_level.upper())
logging.basicConfig(level=log_level)
global session # pylint: disable=global-statement
global recording_overrides # pylint: disable=global-statement
session = db.create_session(os.path.join(os.path.dirname(__file__), 'master.db'))
recording_overrides = RecordingOverrides()
host = 'localhost'
port = 8080
logging.info(f'Launching web server on http://{host}:{port}/')
# Database session is not thread safe, so we need to disable threading here.
app.run(host=host, port=port, debug=args.watch, threaded=False)
if __name__ == '__main__':
sys.exit(main())