import os import logging from sqlalchemy.orm import Session from app.config import settings from app.models.section import Section from app.models.pdf_document import PDFDocument from app.models.quiz import Quiz from app.models.question import Question from app.services import ai_service, vector_service, pdf_service logger = logging.getLogger(__name__) def create_quiz_from_section( db: Session, user_id: int, section_id: int, title: str, mode: str = "timed", time_limit_minutes: int | None = None, ) -> Quiz: """Extract questions from a section's page range using AI.""" section = db.query(Section).filter(Section.id == section_id).first() if not section: raise ValueError("Section not found") document = db.query(PDFDocument).filter(PDFDocument.id == section.document_id).first() if not document: raise ValueError("Document not found") # Get text from vector store for this page range content = vector_service.get_pages_text( document_id=section.document_id, start_page=section.start_page, end_page=section.end_page, ) if not content: raise ValueError("No content found for this section's page range") # Get configured model model_id, api_key = ai_service.get_model_for_task(db, "extraction") # Extract questions via AI (not generate — questions already exist in PDF) page_info = f"{section.start_page}-{section.end_page}" question_data = ai_service.extract_questions( content, page_info=page_info, model_id=model_id, api_key=api_key, ) # Extract images for the page range file_path = os.path.join(settings.UPLOAD_DIR, document.filename) page_images = {} if os.path.exists(file_path): try: page_images = pdf_service.extract_all_images( file_path, document.id, section.start_page, section.end_page ) except Exception as e: logger.warning(f"Image extraction failed: {e}") # Create quiz quiz = Quiz( section_id=section_id, user_id=user_id, title=title, questions_count=len(question_data), mode=mode, time_limit_minutes=time_limit_minutes, ) db.add(quiz) db.flush() # Create question records, associating images where possible for q in question_data: page_ref = q.get("page_reference") image_path = None # Try to associate an image with this question if page_ref and page_ref in page_images and page_images[page_ref]: # Take the first unassigned image from this page image_path = page_images[page_ref].pop(0) if not page_images[page_ref]: del page_images[page_ref] question = Question( quiz_id=quiz.id, question_text=q["question_text"], question_type=q["question_type"], options=q.get("options"), correct_answer=q["correct_answer"], explanation=q.get("explanation", ""), page_reference=page_ref, image_path=image_path, ) db.add(question) db.commit() db.refresh(quiz) return quiz