diff --git a/Dockerfile b/Dockerfile index 0a9e536..8e0a4da 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,6 +27,7 @@ RUN apt update && apt install -y \ texlive-xetex \ pandoc \ librsvg2-bin \ + curl \ && rm -rf /var/lib/apt/lists/* RUN curl -sL https://deb.nodesource.com/setup_20.x | bash - \ diff --git a/ielts_be/api/exam/grade.py b/ielts_be/api/exam/grade.py index 2428610..6312e1f 100644 --- a/ielts_be/api/exam/grade.py +++ b/ielts_be/api/exam/grade.py @@ -23,7 +23,6 @@ async def grade_writing_task( return await grade_controller.grade_writing_task(task, data, background_tasks) - @grade_router.post( '/speaking/{task}', dependencies=[Depends(Authorized([IsAuthenticatedViaBearerToken]))] diff --git a/ielts_be/configs/constants.py b/ielts_be/configs/constants.py index 56b9a54..c42780a 100644 --- a/ielts_be/configs/constants.py +++ b/ielts_be/configs/constants.py @@ -106,6 +106,7 @@ class FilePaths: FIREBASE_LISTENING_AUDIO_FILES_PATH = 'listening_recordings/' VIDEO_FILES_PATH = 'download-video/' FIREBASE_SPEAKING_VIDEO_FILES_PATH = 'speaking_videos/' + WRITING_ATTACHMENTS = 'writing_attachments/' class TemperatureSettings: diff --git a/ielts_be/controllers/impl/grade.py b/ielts_be/controllers/impl/grade.py index 830cbef..f4c70b6 100644 --- a/ielts_be/controllers/impl/grade.py +++ b/ielts_be/controllers/impl/grade.py @@ -25,9 +25,6 @@ class GradeController(IGradeController): self, task: int, dto: WritingGradeTaskDTO, background_tasks: BackgroundTasks ): - if task == 1 and dto.type == "academic" and dto.attachment is None: - raise HTTPException(status_code=400, detail="Academic writing task requires a picture!") - await self._evaluation_service.create_evaluation( dto.userId, dto.sessionId, dto.exerciseId, EvaluationType.WRITING, task ) diff --git a/ielts_be/dtos/writing.py b/ielts_be/dtos/writing.py index 4dc9b4d..468820c 100644 --- a/ielts_be/dtos/writing.py +++ b/ielts_be/dtos/writing.py @@ -9,4 +9,4 @@ class WritingGradeTaskDTO(BaseModel): exerciseId: str question: str answer: str - attachment: Optional[str] + attachment: Optional[str] = None diff --git a/ielts_be/services/impl/exam/writing/grade.py b/ielts_be/services/impl/exam/writing/grade.py index 1b22a30..2ebee83 100644 --- a/ielts_be/services/impl/exam/writing/grade.py +++ b/ielts_be/services/impl/exam/writing/grade.py @@ -1,8 +1,9 @@ import asyncio +import os from typing import Dict, Optional from uuid import uuid4 -from ielts_be.configs.constants import GPTModels, TemperatureSettings +from ielts_be.configs.constants import GPTModels, TemperatureSettings, FilePaths from ielts_be.helpers import TextHelper, ExercisesHelper, FileHelper from ielts_be.repositories import IFileStorage from ielts_be.services import ILLMService, IAIDetectorService @@ -54,19 +55,23 @@ class GradeWriting: }) else: uuid = str(uuid4()) - name = attachment.split('/')[-1] + name = attachment.split('%2F')[-1].split('?')[0] + os.makedirs(f'./tmp/{uuid}/', exist_ok=True) out_path = f'./tmp/{uuid}/{name}' - path = await self._file_storage.download_firebase_file(attachment, out_path) + path = await self._file_storage.download_firebase_file(f'{FilePaths.WRITING_ATTACHMENTS}{name}', out_path) + b64_image = await FileHelper.encode_image(path) messages.append( { "role": "user", - "content": { - "type": "image_url", - "image_url": { - "url": f"data:image/{name.split('.')[-1]};base64,{FileHelper.encode_image(path)}" - } + "content": [{ + "type": "image_url", + "image_url": { + "url": f"data:image/{name.split('.')[-1]};base64,{b64_image}" + } + }] } - }) + ) + FileHelper.remove_directory(f'./tmp/{uuid}') temperature = ( TemperatureSettings.GRADING_TEMPERATURE