Features: - Hybrid semantic + keyword quiz search (pgvector HNSW + PostgreSQL ILIKE) - AWS Bedrock Titan Embed V2 embeddings via LiteLLM proxy (0.71 cosine sim) - Multi-provider TTS: OpenAI, AWS Polly (neural), ElevenLabs, Google Cloud TTS - Unified Settings page (profile, theme, Nextcloud integration, admin shortcuts) - Good morning/afternoon greeting on dashboard - manage.py CLI: reset-password, list-users, reembed - Email verification enforced: register no longer returns JWT for unverified users - Quiz search with debounced input, semantic/keyword/title modes, highlighted snippets - TTS button: loading/playing states, voice selector locked during playback - TTS auto-stops when navigating between questions - Footer added; mobile quiz nav overflow fixed; markdown theme body selector fixed - OpenAI Alloy as default TTS voice; favicon added - SMTP configured via smtp2go; password reset rate limiting (3/hour) - PostgreSQL upgraded to pgvector/pgvector:pg16 Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
123 lines
4 KiB
Python
123 lines
4 KiB
Python
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, embedding_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,
|
|
model_id: str | 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 (use override if provided)
|
|
if model_id:
|
|
from app.models.ai_model_config import AIModelConfig
|
|
config = db.query(AIModelConfig).filter(AIModelConfig.model_id == model_id).first()
|
|
api_key = config.api_key if config and config.api_key else None
|
|
else:
|
|
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}")
|
|
|
|
# Collect skipped questions (those without a correct answer)
|
|
import json
|
|
skipped = []
|
|
if question_data and question_data[0].get("skipped"):
|
|
skipped = question_data[0].pop("skipped")
|
|
valid_questions = [q for q in question_data if q.get("correct_answer")]
|
|
|
|
# Create quiz
|
|
quiz = Quiz(
|
|
section_id=section_id,
|
|
user_id=user_id,
|
|
title=title,
|
|
questions_count=len(valid_questions),
|
|
mode=mode,
|
|
time_limit_minutes=time_limit_minutes,
|
|
skipped_questions=json.dumps(skipped) if skipped else None,
|
|
)
|
|
db.add(quiz)
|
|
db.flush()
|
|
|
|
# Create question records, associating images where possible
|
|
for q in valid_questions:
|
|
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.flush()
|
|
try:
|
|
embedding_service.embed_question(question)
|
|
except Exception as e:
|
|
logger.warning(f"Embedding generation failed for question {question.id}: {e}")
|
|
|
|
db.commit()
|
|
db.refresh(quiz)
|
|
return quiz
|