4
0
This repository has been archived on 2020-07-25. You can view files and clone it, but cannot push or open issues or pull requests.
classification-service/classification_service/mule.py

97 lines
2.9 KiB
Python
Raw Normal View History

2020-04-20 22:53:12 +02:00
#!/usr/bin/env python3
import sentry_sdk
import os
import requests
import tempfile
import numpy
import json
import pika
import uwsgi
from pyAudioAnalysis.audioTrainTest import load_model, load_model_knn, classifier_wrapper
SENTRY_DSN = os.environ.get("SENTRY_DSN")
if SENTRY_DSN:
sentry_sdk.init(
dsn=SENTRY_DSN,
send_default_pii=True,
release=os.environ.get('RELEASE_ID', 'test'),
environment=os.environ.get('RELEASEMODE', 'dev')
)
2020-05-29 17:11:54 +02:00
def run_classification(task, target_class_name: str):
2020-04-20 22:53:12 +02:00
_, temp_model_name = tempfile.mkstemp()
temp_means_name = temp_model_name + "MEANS"
r = requests.get(f"http://model-service/model/{task['model']}/details")
r.raise_for_status()
model_details = r.json()
try:
r = requests.get(f"http://model-service/model/{task['model']}")
r.raise_for_status()
with open(temp_model_name, 'wb') as f:
f.write(r.content)
r = requests.get(f"http://model-service/model/{task['model']}?means")
r.raise_for_status()
with open(temp_means_name, 'wb') as f:
f.write(r.content)
if model_details['type'] == 'knn':
classifier, mean, std, classes, mid_window, mid_step, short_window, short_step, compute_beat \
= load_model_knn(temp_model_name)
else:
classifier, mean, std, classes, mid_window, mid_step, short_window, short_step, compute_beat \
= load_model(temp_model_name)
2020-05-29 17:11:54 +02:00
target_id = classes.index(target_class_name) # Might raise ValueError
2020-04-20 22:53:12 +02:00
feature_vector = (numpy.array(task['features']) - mean) / std
class_id, probability = classifier_wrapper(classifier, model_details['type'], feature_vector)
finally: # bruuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuh
try:
os.remove(temp_model_name)
except FileNotFoundError:
pass
try:
os.remove(temp_means_name)
except FileNotFoundError:
pass
results = {
"tag": task['tag'],
"model": task['model'],
2020-05-29 17:50:18 +02:00
"is_target": bool(class_id == target_id),
2020-05-29 17:11:54 +02:00
"probability": probability[target_id]
2020-04-20 22:53:12 +02:00
}
return results
def main():
while True:
message = uwsgi.mule_get_msg()
task = json.loads(message)
2020-05-29 17:11:54 +02:00
results = run_classification(task, os.environ['TARGET_CLASS_NAME'])
2020-06-20 20:57:23 +02:00
# For some reason Pika loves to disconnect randomly...
connection = pika.BlockingConnection(pika.connection.URLParameters(os.environ['PIKA_URL']))
channel = connection.channel()
channel.exchange_declare(exchange=os.environ['PIKA_EXCHANGE'], exchange_type='fanout')
2020-05-29 17:11:54 +02:00
channel.basic_publish(exchange=os.environ['PIKA_EXCHANGE'], routing_key='classification-result',
body=json.dumps(results).encode("utf-8"))
2020-04-20 22:53:12 +02:00
if __name__ == '__main__':
main()