Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sp24 bugs #225

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/admin/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def get_available_years():
try:
available_years = shared_models.get_available_years()

if available_years == "":
raise Exception("No years recorded")

return jsonify(available_years), 200
except Exception as e:
return json.dumps({e}), 400
Expand Down Expand Up @@ -120,7 +123,10 @@ def admin_page(action):
elif action == "hardReset":
# Delete all images in CV, upload all orignal images and retrain
classifier.delete_all_images()
storage.clear_dataset()
try:
storage.clear_dataset()
except Exception as e:
current_app.logger.error(e)
Thread(target=classifier.hard_reset_retrain).start()
response = {"success": "All images deleted, model now training"}
return json.dumps(response), 200
Expand All @@ -135,7 +141,7 @@ def admin_page(action):
"BLOB_image_count": new_blob_image_count,
}
except Exception as e:
current_app.logger.error("Something in admin/status failed: " + e)
current_app.logger.error("Something in admin/status failed: " + str(e))
return json.dumps(data), 200

elif action == "logging":
Expand Down
1 change: 0 additions & 1 deletion src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
import random
import json
import sys


class Iteration(db.Model):
Expand Down
19 changes: 14 additions & 5 deletions src/multiplayer/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,12 @@ def get_example_drawings(json_data, emitEndpoint="getExampleDrawings"):
label, number_of_images
)

example_drawings = storage.get_images_from_relative_url(
example_drawing_urls
)
try:
example_drawings = storage.get_images_from_relative_url(
example_drawing_urls
)
except Exception as e:
current_app.logger.error(e)
emit(emitEndpoint, json.dumps(example_drawings), room=game_id)


Expand Down Expand Up @@ -257,7 +260,10 @@ def handle_classify(data, image, correct_label=None):
if time_out:
# to break race condition if both players timeout
time.sleep(0.5 * random.random())
storage.save_image(image, correct_label, best_certainty)
try:
storage.save_image(image, correct_label, best_certainty)
except Exception as e:
current_app.logger.error(e)
player = shared_models.get_player(player_id)
opponent = models.get_opponent(game_id, player_id)
if opponent.state == "Done":
Expand Down Expand Up @@ -292,7 +298,10 @@ def handle_classify(data, image, correct_label=None):
emit("prediction", response)

if has_won:
storage.save_image(image, correct_label, best_certainty)
try:
storage.save_image(image, correct_label, best_certainty)
except Exception as e:
current_app.logger.error(e)
player = shared_models.get_player(player_id)
opponent = models.get_opponent(game_id, player_id)
if opponent.state == "Done":
Expand Down
10 changes: 8 additions & 2 deletions src/singleplayer/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ def classify():
player.game_id, player_id, 1, "Done"
)
# save image
storage.save_image(image, label, best_certainty)
try:
storage.save_image(image, label, best_certainty)
except Exception as e:
current_app.logger.error(e)
# Update game state to be done
game_state = "Done"
# Insert statistic for label
Expand Down Expand Up @@ -256,7 +259,10 @@ def get_n_drawings_by_label():
image_urls = shared_models.get_n_random_example_images(
label, number_of_images
)
images = storage.get_images_from_relative_url(image_urls)
try:
images = storage.get_images_from_relative_url(image_urls)
except Exception as e:
current_app.logger.error(e)
current_app.logger.info(
"singleplayer /getExampleDrawings " + " label: " + str(label)
)
Expand Down
17 changes: 7 additions & 10 deletions src/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import uuid
import time
import logging
from src.singleplayer import api
from threading import Thread
from azure.storage.blob import BlobClient
from azure.storage.blob import BlobServiceClient
Expand Down Expand Up @@ -36,7 +34,7 @@ def save_image(image, label, certainty):
blob_name=file_name,
)
blob.upload_blob(image)
container_client = blob_connection()
container_client = blob_connection(setup.CONTAINER_NAME_NEW)
# update metadata in blob
try:
image_count = int(
Expand All @@ -49,9 +47,8 @@ def save_image(image, label, certainty):
metadata = {"image_count": str(image_count + 1)}
container_client.set_container_metadata(metadata=metadata)
except Exception as e:
api.app.logger.error(e)
raise Exception("Could not save image from storage.py" + str(e))
url = base_url + "/" + container_name + "/" + file_name
logging.info(url)
return url


Expand All @@ -62,7 +59,7 @@ def clear_dataset():
NOTE: container is deleted by garbage collection, which does not
happen instantly. A new blob cannot be initalized before old is collected.
"""
container_client = blob_connection()
container_client = blob_connection(setup.CONTAINER_NAME_NEW)
try:
container_client.delete_container()
except Exception as e:
Expand All @@ -77,7 +74,7 @@ def create_container():
"""
tries = setup.CREATE_CONTAINER_TRIES
waiting_time = setup.CREATE_CONTAINER_WAITER
container_client = blob_connection()
container_client = blob_connection(setup.CONTAINER_NAME_NEW)
success = False
metadata = {"image_count": "0"}
for i in range(tries):
Expand All @@ -90,8 +87,8 @@ def create_container():
metadata=metadata, public_access="container"
)
success = True
except Exception as e:
api.app.logger.error(e)
except Exception:
raise Exception("Could not create container")


def image_count():
Expand Down Expand Up @@ -128,7 +125,7 @@ def get_n_random_images_from_label(n, label):
"""
Returns n random images from the blob storage container with the given label.
"""
container_client = blob_connection(setup.CONTAINER_NAME_ORIGINAL)
container_client = blob_connection(Keys.get("CONTAINER_NAME"))
blob_prefix = f"{label}/"
blobs = list(container_client.list_blobs(name_starts_with=blob_prefix))
selected_blobs = random.sample(blobs, min(n, len(blobs)))
Expand Down