DocDoeAI / scripts /seed_hse_pyqs.py
asnannp's picture
Deploy backend cd4237ff: support routes + rate limit + exam_date nullable + upload 413 fix
7c6ffa6
Raw
History Blame Contribute Delete
6.62 kB
from __future__ import annotations
import sys
from pathlib import Path
# Add backend directory to sys.path
backend_dir = Path(__file__).resolve().parent.parent
sys.path.append(str(backend_dir))
from app.core.database import SessionLocal
from app.models.previous_paper import PreviousPaper
from app.models.previous_question import PreviousQuestion
from app.models.user import User
def seed_default_hse_papers(db, user_id: str) -> None:
# 5 years of Physics, Chemistry, Biology, Mathematics
papers_data = [
{
"title": "HSE Physics March 2024",
"subject": "Physics",
"year": 2024,
"questions": [
{"question_number": "1", "question_text": "State Lenz's Law of electromagnetic induction.", "marks": 1, "chapter": "Electromagnetic Induction", "topic": "Lenz's Law", "question_type": "definition", "difficulty": "easy", "keywords": ["Lenz", "induction", "direction"]},
{"question_number": "2", "question_text": "Derive an expression for the induced EMF in a moving conductor in a uniform magnetic field.", "marks": 4, "chapter": "Electromagnetic Induction", "topic": "Motional EMF", "question_type": "long", "difficulty": "medium", "keywords": ["motional EMF", "EMF derivation"]},
{"question_number": "3", "question_text": "Write any two properties of electromagnetic waves.", "marks": 2, "chapter": "Electromagnetic Waves", "topic": "EM Wave Properties", "question_type": "short", "difficulty": "easy", "keywords": ["properties", "transverse"]}
]
},
{
"title": "HSE Chemistry March 2024",
"subject": "Chemistry",
"year": 2024,
"questions": [
{"question_number": "1", "question_text": "What is the unit of rate constant for a first-order reaction?", "marks": 1, "chapter": "Chemical Kinetics", "topic": "First Order Reaction", "question_type": "mcq", "difficulty": "easy", "keywords": ["rate constant", "unit"]},
{"question_number": "2", "question_text": "Explain the SN2 mechanism with a suitable example.", "marks": 4, "chapter": "Haloalkanes and Haloarenes", "topic": "Nucleophilic Substitution", "question_type": "long", "difficulty": "medium", "keywords": ["SN2", "mechanism", "substitution"]}
]
},
{
"title": "HSE Biology March 2024",
"subject": "Biology",
"year": 2024,
"questions": [
{"question_number": "1", "question_text": "Draw a neat labelled diagram of the female gametophyte of angiosperms.", "marks": 3, "chapter": "Sexual Reproduction in Flowering Plants", "topic": "Embryo Sac Diagram", "question_type": "diagram", "difficulty": "medium", "keywords": ["diagram", "embryo sac", "female gametophyte"]},
{"question_number": "2", "question_text": "Explain the process of double fertilization in flowering plants.", "marks": 4, "chapter": "Sexual Reproduction in Flowering Plants", "topic": "Double Fertilization", "question_type": "long", "difficulty": "medium", "keywords": ["fertilization", "double fertilization", "syngamy"]}
]
},
{
"title": "HSE Mathematics March 2024",
"subject": "Mathematics",
"year": 2024,
"questions": [
{"question_number": "1", "question_text": "Find the derivative of sin(x^2) with respect to x.", "marks": 2, "chapter": "Continuity and Differentiability", "topic": "Chain Rule", "question_type": "numerical", "difficulty": "easy", "keywords": ["derivative", "chain rule"]}
]
},
{
"title": "HSE Physics March 2023",
"subject": "Physics",
"year": 2023,
"questions": [
{"question_number": "1", "question_text": "Define self-inductance of a coil and write its SI unit.", "marks": 2, "chapter": "Electromagnetic Induction", "topic": "Self Inductance", "question_type": "definition", "difficulty": "easy", "keywords": ["self-inductance", "Henry"]}
]
}
]
for p in papers_data:
# Check if already seeded to prevent duplication
existing = db.query(PreviousPaper).filter(
PreviousPaper.user_id == user_id,
PreviousPaper.title == p["title"]
).first()
if existing:
continue
paper = PreviousPaper(
user_id=user_id,
title=p["title"],
subject=p["subject"],
syllabus="Kerala HSE",
year=p["year"],
file_name=f"{p['title'].replace(' ', '_').lower()}.pdf",
file_type="application/pdf",
file_path=f"uploads/hse/{p['title'].replace(' ', '_').lower()}.pdf",
status="ready",
extracted_text=f"This is the pre-seeded official textbook grounding for {p['title']}."
)
db.add(paper)
db.commit()
db.refresh(paper)
saved_questions = []
for q in p["questions"]:
question = PreviousQuestion(
previous_paper_id=paper.id,
question_number=q["question_number"],
question_text=q["question_text"],
marks=q["marks"],
subject=p["subject"],
syllabus="Kerala HSE",
year=p["year"],
chapter=q["chapter"],
topic=q["topic"],
question_type=q["question_type"],
difficulty=q["difficulty"],
keywords_json=q["keywords"]
)
db.add(question)
saved_questions.append(question)
db.commit()
paper.extracted_questions_json = [
{
"question_number": q.question_number,
"question_text": q.question_text,
"marks": q.marks,
"chapter": q.chapter,
"topic": q.topic,
"question_type": q.question_type,
"difficulty": q.difficulty,
"keywords": q.keywords_json,
}
for q in saved_questions
]
db.add(paper)
db.commit()
def seed_all_users() -> None:
db = SessionLocal()
try:
users = db.query(User).all()
print(f"Seeding {len(users)} users with default Kerala HSE papers...")
for user in users:
seed_default_hse_papers(db, user.id)
print("Seeding completed successfully!")
finally:
db.close()
if __name__ == "__main__":
seed_all_users()