cnn-classification-service/cnn_classification_service/magic_doer.py

73 lines
2.7 KiB
Python
Raw Normal View History

2020-07-28 18:10:19 +02:00
#!/usr/bin/env python3
import os
import logging
import tempfile
2021-07-30 12:17:18 +02:00
from requests_opentracing import SessionTracing
import opentracing # ez kell ide hogy a session tracer jolegyen
2020-07-28 18:10:19 +02:00
import requests
2021-06-14 03:14:58 +02:00
import time
2021-07-27 17:44:14 +02:00
from urllib.parse import urljoin
2020-07-28 18:10:19 +02:00
2021-07-30 11:43:38 +02:00
from config import Config
2020-10-06 00:41:54 +02:00
from classifier_cache import ClassifierCache
2020-10-02 03:59:09 +02:00
2020-07-28 18:10:19 +02:00
2020-10-06 00:41:54 +02:00
class MagicDoer:
2021-07-30 11:43:38 +02:00
classifier_cache = ClassifierCache(Config.MODEL_INFO_URL)
2021-07-30 12:17:18 +02:00
requests_session = SessionTracing(propagate=True)
2020-07-28 18:10:19 +02:00
2020-10-06 00:41:54 +02:00
@classmethod
2021-07-30 15:24:57 +02:00
def run_everything(cls, parameters: dict, span: opentracing.span.Span) -> dict:
2020-10-06 00:41:54 +02:00
tag = parameters['tag']
sample_file_handle, sample_file_path = tempfile.mkstemp(prefix=f"{tag}_", suffix=".wav", dir="/dev/shm")
2021-07-30 15:24:57 +02:00
span.log_kv({'event': 'sampleFileOpened', 'sampleTag': tag})
2021-06-14 03:12:44 +02:00
response = None
2020-10-06 00:41:54 +02:00
try:
2020-07-28 18:10:19 +02:00
2021-07-30 15:24:57 +02:00
with opentracing.tracer.start_span('downloadSample', child_of=span):
# Download Sample
object_path = urljoin(Config.STORAGE_SERVICE_URL, f"object/{tag}")
2020-07-28 18:10:19 +02:00
2021-07-30 15:24:57 +02:00
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)
2020-07-28 18:10:19 +02:00
2021-07-30 15:24:57 +02:00
logging.debug(f"Downloaded sample to {sample_file_path}")
2020-07-28 18:10:19 +02:00
2021-07-30 15:24:57 +02:00
with opentracing.tracer.start_span('loadClassifier', child_of=span):
# Get a classifier that uses the default model
model_details, classifier = cls.classifier_cache.get_default_classifier()
2020-07-28 18:10:19 +02:00
2021-07-30 15:24:57 +02:00
with opentracing.tracer.start_span('runClassifier', child_of=span) as child_span:
# do the majic
classification_start_time = time.time()
predicted_class_name, labeled_predictions = classifier.predict(sample_file_path, child_span)
classification_duration = time.time() - classification_start_time
2021-06-14 03:12:44 +02:00
response = {
"tag": tag,
"probability": labeled_predictions[model_details['target_class_name']],
"all_predictions": labeled_predictions,
"class": predicted_class_name,
2021-06-14 03:14:58 +02:00
"model": model_details['id'],
"classification_duration": classification_duration
2021-06-14 03:12:44 +02:00
}
2020-07-28 18:10:19 +02:00
2020-10-06 00:41:54 +02:00
finally:
try:
os.remove(sample_file_path)
except FileNotFoundError:
pass
2020-07-28 18:10:19 +02:00
2021-07-30 15:24:57 +02:00
span.log_kv({'event': 'sampleFileDeleted', 'sampleTag': tag})
2021-06-14 03:12:44 +02:00
if not response:
logging.error("Something went wrong during classification!")
else:
logging.info(f"Classification done!")
logging.debug(f"Results: {response}")
2020-07-28 18:10:19 +02:00
2020-10-06 00:41:54 +02:00
return response