68 lines
1.7 KiB
Python
68 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
import os
|
|
import logging
|
|
import tempfile
|
|
import requests
|
|
|
|
from cnn_classifier import Classifier
|
|
|
|
|
|
def run_everything(parameters: dict):
|
|
tag = parameters['tag']
|
|
|
|
_, file_path = tempfile.mkstemp(prefix=f"{tag}_", suffix=".wav")
|
|
_, temp_model_name = tempfile.mkstemp(suffix=".json")
|
|
_, temp_weights_name = tempfile.mkstemp(suffix=".h5")
|
|
|
|
try:
|
|
|
|
logging.info(f"Downloading sample: {tag}")
|
|
r = requests.get(f"http://storage-service/object/{tag}")
|
|
with open(file_path, 'wb') as f:
|
|
f.write(r.content)
|
|
|
|
logging.debug(f"Downloaded sample to {file_path}")
|
|
|
|
r = requests.get(f"http://model-service/model/cnn/$default")
|
|
r.raise_for_status()
|
|
|
|
with open(temp_model_name, 'wb') as f:
|
|
f.write(r.content)
|
|
|
|
r = requests.get(f"http://model-service/model/cnn/$default?weights")
|
|
r.raise_for_status()
|
|
|
|
with open(temp_weights_name, 'wb') as f:
|
|
f.write(r.content)
|
|
|
|
# magic happens here
|
|
classifier = Classifier(temp_model_name, temp_weights_name)
|
|
results = classifier.predict(file_path)
|
|
|
|
finally: # bruuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuh
|
|
try:
|
|
os.remove(temp_model_name)
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
try:
|
|
os.remove(temp_weights_name)
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
try:
|
|
os.remove(file_path)
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
response = {
|
|
"tag": tag,
|
|
"probability": 1.0 if results[0] == 'sturnus' else 0.0,
|
|
"model": ...
|
|
}
|
|
|
|
logging.info(f"Classification done!")
|
|
logging.debug(f"Results: {response}")
|
|
|
|
return response
|