cnn-classification-service/cnn_classification_service/magic_doer.py
marcsello be361a7c18
All checks were successful
continuous-integration/drone/push Build is passing
Fixed things
2021-08-04 15:35:38 +02:00

77 lines
2.7 KiB
Python

#!/usr/bin/env python3
import os
import logging
import tempfile
from requests_opentracing import SessionTracing
import opentracing # ez kell ide hogy a session tracer jolegyen
import requests
import time
from urllib.parse import urljoin
from config import Config
from classifier_cache import ClassifierCache
class MagicDoer:
classifier_cache = ClassifierCache(Config.MODEL_INFO_URL)
requests_session = SessionTracing(propagate=True)
@classmethod
def run_everything(cls, parameters: dict) -> dict:
tag = parameters['tag']
sample_file_handle, sample_file_path = tempfile.mkstemp(prefix=f"{tag}_", suffix=".wav", dir="/dev/shm")
span = opentracing.tracer.scope_manager.active.span
span.log_kv({'event': 'sampleFileOpened', 'sampleTag': tag})
response = None
try:
with opentracing.tracer.start_active_span('magicDoer.downloadSample'):
# Download Sample
object_path = urljoin(Config.STORAGE_SERVICE_URL, f"object/{tag}")
logging.info(f"Downloading sample: {tag} from {object_path}")
r = cls.requests_session.get(object_path)
with open(sample_file_handle, 'wb') as f:
f.write(r.content)
logging.debug(f"Downloaded sample to {sample_file_path}")
with opentracing.tracer.start_active_span('magicDoer.getClassifier'):
# Get a classifier that uses the default model
model_details, classifier = cls.classifier_cache.get_default_classifier()
with opentracing.tracer.start_active_span('magicDoer.runClassifier'):
# do the majic
classification_start_time = time.time()
predicted_class_name, labeled_predictions = classifier.predict(sample_file_path)
classification_duration = time.time() - classification_start_time
response = {
"tag": tag,
"probability": labeled_predictions[model_details['target_class_name']],
"all_predictions": labeled_predictions,
"class": predicted_class_name,
"model": model_details['id'],
"classification_duration": classification_duration
}
finally:
try:
os.remove(sample_file_path)
except FileNotFoundError:
pass
span = opentracing.tracer.scope_manager.active.span
span.log_kv({'event': 'sampleFileDeleted', 'sampleTag': tag})
if not response:
logging.error("Something went wrong during classification!")
else:
logging.info(f"Classification done!")
logging.debug(f"Results: {response}")
return response