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

Feat/upload contexts #305

Merged
merged 4 commits into from
Sep 16, 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ node_modules/
/playwright-report/
/blob-report/
/playwright/.cache/
*.ipynb
*.log
celerybeat-schedule
8 changes: 8 additions & 0 deletions backend/app/api/endpoints/base/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
20 changes: 20 additions & 0 deletions backend/app/domain/services/base/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 0 additions & 3 deletions backend/app/infrastructure/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 4 additions & 0 deletions backend/app/infrastructure/repositories/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading