From 25f3ffc7080040dfcd60f54c1800524fcfb780cf Mon Sep 17 00:00:00 2001 From: remg1997 Date: Fri, 13 Sep 2024 17:33:52 -0500 Subject: [PATCH 1/3] Include upload context functionality --- .gitignore | 1 + backend/app/api/endpoints/base/context.py | 8 ++++++++ backend/app/domain/services/base/context.py | 20 +++++++++++++++++++ .../infrastructure/repositories/context.py | 4 ++++ 4 files changed, 33 insertions(+) diff --git a/.gitignore b/.gitignore index bd5bf2c7..00b490d9 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ node_modules/ /playwright-report/ /blob-report/ /playwright/.cache/ +*.ipynb diff --git a/backend/app/api/endpoints/base/context.py b/backend/app/api/endpoints/base/context.py index 27747d7d..f44922ca 100644 --- a/backend/app/api/endpoints/base/context.py +++ b/backend/app/api/endpoints/base/context.py @@ -92,3 +92,11 @@ def get_random_context_from_key_value(model: GetRandomContext): return ContextService().get_random_context_from_key_value( model.key_name, model.key_value ) + + +@router.post("/upload_new_contexts") +def upload_new_contexts( + task_id: int, + file: UploadFile = File(...), +): + return ContextService().upload_new_contexts(task_id, file) diff --git a/backend/app/domain/services/base/context.py b/backend/app/domain/services/base/context.py index 154ac7a9..e0685e8c 100644 --- a/backend/app/domain/services/base/context.py +++ b/backend/app/domain/services/base/context.py @@ -324,3 +324,23 @@ def get_random_context_from_key_value(self, key_name: str, key_value: dict) -> d ] return random.choice(contexts) + + def upload_new_contexts(self, task_id, file): + task_round = self.task_service.get_task_info_by_task_id(task_id).cur_round + real_round_id = self.round_repository.get_round_info_by_round_and_task( + task_id, task_round + ).id + file.file.seek(0) + if file.filename.endswith(".jsonl"): + file_type = "jsonl" + elif file.filename.endswith(".csv"): + file_type = "csv" + else: + raise HTTPException(500, "File type not supported") + + if file_type == "jsonl": + for line in file.file: + base_format = {} + base_format["r_realid"] = real_round_id + base_format["context_json"] = line + self.context_repository.upload_contexts(base_format) diff --git a/backend/app/infrastructure/repositories/context.py b/backend/app/infrastructure/repositories/context.py index 79f8feb7..1a85ae58 100644 --- a/backend/app/infrastructure/repositories/context.py +++ b/backend/app/infrastructure/repositories/context.py @@ -81,3 +81,7 @@ def get_context_by_key_value_in_contextjson(self, search_txt: str): .filter(self.model.context_json.like(search_txt)) .all() ) + + def upload_contexts(self, context: dict): + self.session.add(self.model(**context)) + self.session.commit() From 12e6b9ee85018cc08a86986ac668a1fb1d4dd39f Mon Sep 17 00:00:00 2001 From: remg1997 Date: Mon, 16 Sep 2024 18:23:03 -0500 Subject: [PATCH 2/3] Update git ignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 00b490d9..97cff2f4 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ node_modules/ /blob-report/ /playwright/.cache/ *.ipynb +*.log +celerybeat-schedule From 6247d24faae5d1b8e80e0af828f4e067833ccbe5 Mon Sep 17 00:00:00 2001 From: remg1997 Date: Mon, 16 Sep 2024 18:31:24 -0500 Subject: [PATCH 3/3] Fix unused imports --- backend/app/infrastructure/connection.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/backend/app/infrastructure/connection.py b/backend/app/infrastructure/connection.py index 7d8b30bd..85db1479 100644 --- a/backend/app/infrastructure/connection.py +++ b/backend/app/infrastructure/connection.py @@ -8,13 +8,10 @@ import os -import uuid from dotenv import load_dotenv from sqlalchemy import MetaData, create_engine from sqlalchemy.orm import sessionmaker -from app.infrastructure.utils.singleton import Singleton - load_dotenv()