File size: 6,623 Bytes
7c6ffa6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | 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()
|