Fixed level issues
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
from asyncio import gather
|
||||
from typing import Dict, Optional
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import UploadFile
|
||||
|
||||
import random
|
||||
|
||||
from app.configs.constants import EducationalContent
|
||||
from app.dtos.level import LevelExercisesDTO
|
||||
from app.repositories.abc import IDocumentStore
|
||||
from app.services.abc import (
|
||||
@@ -41,48 +47,61 @@ class LevelService(ILevelService):
|
||||
)
|
||||
|
||||
|
||||
async def upload_level(self, upload: UploadFile) -> Dict:
|
||||
return await self._upload_module.generate_level_from_file(upload)
|
||||
async def upload_level(self, upload: UploadFile, solutions: Optional[UploadFile] = None) -> Dict:
|
||||
return await self._upload_module.generate_level_from_file(upload, solutions)
|
||||
|
||||
async def _generate_exercise(self, req_exercise, start_id):
|
||||
if req_exercise.type == "mcBlank":
|
||||
questions = await self._mc.gen_multiple_choice("blank_space", req_exercise.quantity, start_id)
|
||||
questions["variant"] = "mcBlank"
|
||||
questions["type"] = "multipleChoice"
|
||||
questions["prompt"] = "Choose the correct word or group of words that completes the sentences."
|
||||
return questions
|
||||
|
||||
elif req_exercise.type == "mcUnderline":
|
||||
questions = await self._mc.gen_multiple_choice("underline", req_exercise.quantity, start_id)
|
||||
questions["variant"] = "mcUnderline"
|
||||
questions["type"] = "multipleChoice"
|
||||
questions["prompt"] = "Choose the underlined word or group of words that is not correct."
|
||||
return questions
|
||||
|
||||
elif req_exercise.type == "passageUtas":
|
||||
topic = req_exercise.topic if req_exercise.topic else random.choice(EducationalContent.TOPICS)
|
||||
exercise = await self._passage_utas.gen_reading_passage_utas(
|
||||
start_id,
|
||||
req_exercise.quantity,
|
||||
topic,
|
||||
req_exercise.text_size
|
||||
)
|
||||
exercise["prompt"] = "Read the text and answer the questions below."
|
||||
|
||||
return exercise
|
||||
|
||||
elif req_exercise.type == "fillBlanksMC":
|
||||
exercise = await self._fill_blanks.gen_fill_blanks(
|
||||
start_id,
|
||||
req_exercise.quantity,
|
||||
req_exercise.text_size,
|
||||
req_exercise.topic
|
||||
)
|
||||
exercise["prompt"] = "Read the text below and choose the correct word for each space."
|
||||
return exercise
|
||||
|
||||
async def generate_exercises(self, dto: LevelExercisesDTO):
|
||||
exercises = []
|
||||
start_id = 1
|
||||
|
||||
start_ids = []
|
||||
current_id = 1
|
||||
for req_exercise in dto.exercises:
|
||||
if req_exercise.type == "multipleChoice":
|
||||
questions = await self._mc.gen_multiple_choice("normal", req_exercise.quantity, start_id)
|
||||
exercises.append(questions)
|
||||
start_ids.append(current_id)
|
||||
current_id += req_exercise.quantity
|
||||
|
||||
elif req_exercise.type == "mcBlank":
|
||||
questions = await self._mc.gen_multiple_choice("blank_space", req_exercise.quantity, start_id)
|
||||
questions["variant"] = "mc"
|
||||
exercises.append(questions)
|
||||
tasks = [
|
||||
self._generate_exercise(req_exercise, start_id)
|
||||
for req_exercise, start_id in zip(dto.exercises, start_ids)
|
||||
]
|
||||
questions = await gather(*tasks)
|
||||
questions = [{'id': str(uuid4()), **exercise} for exercise in questions]
|
||||
|
||||
elif req_exercise.type == "mcUnderline":
|
||||
questions = await self._mc.gen_multiple_choice("underline", req_exercise.quantity, start_id)
|
||||
exercises.append(questions)
|
||||
|
||||
elif req_exercise.type == "blankSpaceText":
|
||||
questions = await self._blank_space.gen_blank_space_text_utas(
|
||||
req_exercise.quantity, start_id, req_exercise.text_size, req_exercise.topic
|
||||
)
|
||||
exercises.append(questions)
|
||||
|
||||
elif req_exercise.type == "passageUtas":
|
||||
questions = await self._passage_utas.gen_reading_passage_utas(
|
||||
start_id, req_exercise.mc_qty, req_exercise.text_size
|
||||
)
|
||||
exercises.append(questions)
|
||||
|
||||
elif req_exercise.type == "fillBlanksMC":
|
||||
questions = await self._passage_utas.gen_reading_passage_utas(
|
||||
start_id, req_exercise.mc_qty, req_exercise.text_size
|
||||
)
|
||||
exercises.append(questions)
|
||||
|
||||
start_id = start_id + req_exercise.quantity
|
||||
|
||||
return exercises
|
||||
return {"exercises": questions}
|
||||
|
||||
# Just here to support other modules that I don't know if they are supposed to still be used
|
||||
async def gen_multiple_choice(self, mc_variant: str, quantity: int, start_id: int = 1):
|
||||
|
||||
Reference in New Issue
Block a user