Skip to content

Commit

Permalink
Save the inference result in db #85
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvanie85 committed May 30, 2024
1 parent fcf89f5 commit ca4cf82
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
14 changes: 10 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ async def inference_request():
container_name = data["container_name"]
imageDims = data["imageDims"]
image_base64 = data["image"]
#email = data.get["email"]
#user_id = data.get["user_id"]
email = "[email protected]"

area_ratio = data.get("area_ratio", 0.5)
Expand Down Expand Up @@ -374,11 +374,15 @@ async def inference_request():
CONNECTION_STRING, container_name, create_container=True
)

user_id = await datastore.validate_user(email, CONNECTION_STRING)
# Open db connection
connection = datastore.get_connection()
cursor = datastore.get_cursor(connection)

user = await datastore.validate_user(cursor, email, CONNECTION_STRING)

image_hash_value = await azure_storage.generate_hash(image_bytes)
picture_id = await datastore.get_picture_id(
user_id, image_hash_value, container_client
cursor, user.id, image_hash_value, container_client
)

pipeline = pipelines_endpoints.get(pipeline_name)
Expand All @@ -405,7 +409,9 @@ async def inference_request():
result_json_string,
image_hash_value,
)
saved_result_json = await datastore.save_inference_result(container_name, processed_result_json[0], picture_id, pipeline_name, 1)
saved_result_json = await datastore.save_inference_result(cursor, user.id, processed_result_json[0], picture_id, pipeline_name, 1)

datastore.end_query(connection, cursor)

# return the inference results to the client
print(f"Took: {'{:10.4f}'.format(time.perf_counter() - seconds)} seconds") # TODO: Transform into logging
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ cryptography
pyyaml
pydantic
python-magic
nachet-datastore @git+https://github.com/ai-cfia/nachet-datastore.git@651f1da5802a354de4af3d061d671d515e315203 #TODO : change the branch to main
nachet-datastore @git+https://github.com/ai-cfia/nachet-datastore.git@1f1c449c82da1af8fa3547e3d407521ab5910ba3 #TODO : change the branch to main
61 changes: 38 additions & 23 deletions storage/datastore_storage_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@ class SeedNotFoundError(DatastoreError):
class GetPipelinesError(DatastoreError):
pass


def get_cursor():
return db.cursor(db.connect_db())


def get_connection() :
return db.connect_db()

def get_cursor(connection):
return db.cursor(connection)

def end_query(connection, cursor):
db.end_query(connection, cursor)

def get_all_seeds() -> list:

"""
Return all seeds name register in the Datastore.
"""
try:
return seed_queries.get_all_seeds(get_cursor())
connection = get_connection()
cursor = get_cursor(connection)
return seed_queries.get_all_seeds(cursor)
except Exception as error: # TODO modify Exception for more specific exception
raise SeedNotFoundError(error.args[0])

Expand All @@ -46,7 +52,9 @@ def get_all_seeds_names() -> list:
Return all seeds name register in the Datastore.
"""
try:
return seed_queries.get_all_seeds_names(get_cursor())
connection = get_connection()
cursor = get_cursor(connection)
return seed_queries.get_all_seeds_names(cursor)
except Exception as error: # TODO modify Exception for more specific exception
raise SeedNotFoundError(error.args[0])

Expand All @@ -55,56 +63,63 @@ def get_seeds(expression: str) -> list:
"""
Return a list of all seed that contains the expression
"""
return list(filter(lambda x: expression in x, get_all_seeds_names(get_cursor())))
connection = get_connection()
cursor = get_cursor(connection)
return list(filter(lambda x: expression in x, get_all_seeds_names(cursor)))


async def validate_user(email: str, connection_string) -> datastore.User:
async def validate_user(cursor, email: str, connection_string) -> datastore.User:
"""
Return True if user is valid, False otherwise
"""

cursor = get_cursor()
if user_datastore.is_user_registered(cursor, email):
user = datastore.get_User(email, cursor)
else :
user = await datastore.new_user(cursor, email, connection_string)
return user


async def get_picture_id(user_id, image_hash_value, container_client) :
async def get_picture_id(cursor, user_id, image_hash_value, container_client) :
"""
Return the picture_id of the image
"""
cursor = get_cursor()
print("sfbvfvzs")
picture_id = await datastore.upload_picture(cursor, str(user_id), image_hash_value, container_client)
print(picture_id)
return picture_id

def upload_picture_set(**kwargs):
return datastore.bin.upload_picture_set.upload_picture_set(get_cursor(), **kwargs)
connection = get_connection()
cursor = get_cursor(connection)
return datastore.bin.upload_picture_set.upload_picture_set(cursor, **kwargs)

async def get_pipelines() -> list:

"""
Retrieves the pipelines from the Datastore
"""
try:
pipelines = await datastore.get_ml_structure(get_cursor())
connection = get_connection()
cursor = get_cursor(connection)
pipelines = await datastore.get_ml_structure(cursor)
return pipelines
except Exception as error: # TODO modify Exception for more specific exception
raise GetPipelinesError(error.args[0])

async def save_inference_result(user_id:str, inference_dict, picture_id:str, pipeline_id:str, type:int):
print(user_id)
print(picture_id)
return await datastore.register_inference_result(get_cursor(), user_id, inference_dict, picture_id, pipeline_id, type)
async def save_inference_result(cursor, user_id:str, inference_dict, picture_id:str, pipeline_id:str, type:int):
nb_object = int(inference_dict["totalBoxes"])
for box_index in range(nb_object):
print(inference_dict["boxes"][box_index]["label"])
print(get_all_seeds())
return await datastore.register_inference_result(cursor, user_id, inference_dict, picture_id, pipeline_id, type)

async def save_perfect_feedback(inference_id:str, user_id:str):
# peut-être --> user_id = user.get_user_id(cursor, email) (genre j'ai l'email et pas le id direct)
await datastore.register_perfect_inference_feeback(inference_id, user_id, get_cursor())
connection = get_connection()
cursor = get_cursor(connection)
await datastore.register_perfect_inference_feeback(inference_id, user_id, cursor)

async def save_annoted_feedback(inference_id:str, user_id:str, inference_feedback:dict):
# peut-être --> user_id = user.get_user_id(cursor, email) (genre j'ai l'email et pas le id direct)
await datastore.register_annoted_inference_feeback(inference_id, user_id, inference_feedback, get_cursor())
connection = get_connection()
cursor = get_cursor(connection)
await datastore.register_annoted_inference_feeback(inference_id, user_id, inference_feedback, cursor)

0 comments on commit ca4cf82

Please sign in to comment.