#!/usr/bin/env python3 import os from flask import request, jsonify from flask_classful import FlaskView from utils import json_required import requests import tempfile from pyAudioAnalysis.audioTrainTest import load_model, load_model_knn, classifier_wrapper class ClassifyView(FlaskView): @json_required def post(self): request.json _, temp_model_name = tempfile.mkstemp() temp_means_name = temp_model_name + "MEANS" r = requests.get("http://model-service/model/$default/details") r.raise_for_status() model_details = r.json() try: r = requests.get("http://model-service/model/$default") r.raise_for_status() with open(temp_model_name, 'wb') as f: f.write(r.content) r = requests.get("http://model-service/model/$default?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) # 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 return jsonify({})