Spaces:
Sleeping
Sleeping
github-actions[bot] commited on
Commit ยท
f524bd9
1
Parent(s): 1959397
๐ Auto-deploy backend from GitHub (af4a835)
Browse files85 unmodified lines868788899091274 unmodified lines36636736836937037112788 unmodified lines13160131611316213163131641316585 unmodified linesbuild_analysis_curriculum_context,build_lesson_prompt,build_lesson_query,retrieve_curriculum_context,summarize_retrieval_confidence,)274 unmodified lines"/api/admin/model-config/override": ADMIN_ONLY,"/api/admin/model-config/reset": ADMIN_ONLY,"/api/lessons/videos/search": ALL_APP_ROLES,"/api/quiz-battle/generate": ALL_APP_ROLES,"/api/quiz-battle/ingest-pdf": TEACHER_OR_ADMIN,"/api/quiz-battle/bank-status": TEACHER_OR_ADMIN,12788 unmodified linesraise HTTPException(status_code=500, detail=f"Learning path error: {str(e)}")# โโโ Main โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโif __name__ == "__main__":85 unmodified lines86878889909192274 unmodified lines36736836937037137237312788 unmodified lines1316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328585 unmodified linesbuild_analysis_curriculum_context,build_lesson_prompt,build_lesson_query,format_retrieved_chunks,retrieve_curriculum_context,summarize_retrieval_confidence,)274 unmodified lines"/api/admin/model-config/override": ADMIN_ONLY,"/api/admin/model-config/reset": ADMIN_ONLY,"/api/lessons/videos/search": ALL_APP_ROLES,"/api/lesson/personalized": ALL_APP_ROLES,"/api/quiz-battle/generate": ALL_APP_ROLES,"/api/quiz-battle/ingest-pdf": TEACHER_OR_ADMIN,"/api/quiz-battle/bank-status": TEACHER_OR_ADMIN,12788 unmodified linesraise HTTPException(status_code=500, detail=f"Learning path error: {str(e)}")# โโโ Personalized Lesson Endpoint โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโclass PersonalizedLessonRequest(BaseModel):topic: str = Field(..., description="Lesson topic")student_uid: str = Field(..., description="Student UID for profile lookup")assessment_context: Optional[Dict[str, Any]] = Field(None, description="Optional assessment context")subject: Optional[str] = Field(None, description="Subject area")quarter: Optional[int] = Field(None, description="Quarter (1-4)")class PersonalizedLessonResponse(BaseModel):topic: strcontent: strpersonalization_notes: strsections: List[Dict[str, str]]suggested_exercises: List[str]difficulty_adjustment: str@app.post("/api/lesson/personalized", response_model=PersonalizedLessonResponse)async def generate_personalized_lesson(request: PersonalizedLessonRequest):"""Generate a personalized lesson based on student's assessment profile.Adapts content to address weaknesses and reinforce strengths."""try:# Load student's competency profile if availableweaknesses = []strengths = []if firebase_firestore and request.student_uid:try:db = firebase_firestore.client()profile_doc = db.collection("competencyProfiles").document(request.student_uid).get()if profile_doc.exists:profile_data = profile_doc.to_dict()if profile_data and "competencies" in profile_data:for comp_id, comp_data in profile_data["competencies"].items():if comp_data.get("score", 0) < 50:weaknesses.append(comp_id)elif comp_data.get("score", 0) >= 80:strengths.append(comp_id)except Exception as e:logger.warning(f"Could not load competency profile: {e}")# Retrieve curriculum contextcontext_chunks = retrieve_curriculum_context(query=build_lesson_query(request.topic, request.subject or "General Mathematics", request.quarter or 1),subject=request.subject,quarter=request.quarter,top_k=5,)context_text = format_retrieved_chunks(context_chunks)# Build personalized promptprompt = f"""Generate a DepEd-aligned SHS mathematics lesson on: {request.topic}Student Assessment Profile:- Weaknesses to address: {', '.join(weaknesses) if weaknesses else 'None identified'}- Strengths to reinforce: {', '.join(strengths) if strengths else 'None identified'}Curriculum Context:{context_text}Instructions:1. Structure the lesson with: Introduction, Key Concepts, Examples, Practice Problems, Summary2. Include extra practice on these weak areas: {', '.join(weaknesses) if weaknesses else 'general topic areas'}3. Provide advanced challenges on these strong areas: {', '.join(strengths) if strengths else 'related advanced topics'}4. Use Filipino Senior High School appropriate language and context5. Reference specific DepEd MELC competencies where applicableReturn as JSON with fields: topic, sections (array of title/content), suggested_exercises, personalization_notes"""req = InferenceRequest(messages=[{"role": "system", "content": "You are a precise DepEd-aligned curriculum assistant."},{"role": "user", "content": prompt},],task_type="rag_lesson",max_new_tokens=1800,temperature=0.2,top_p=0.9,enable_thinking=True,)response_text = get_inference_client().generate_from_messages(req)# Parse JSON responsetry:# Extract JSON from responsejson_match = re.search(r'\{.*\}', response_text, re.DOTALL)if json_match:lesson_data = json.loads(json_match.group())else:lesson_data = json.loads(response_text)return PersonalizedLessonResponse(topic=request.topic,content=lesson_data.get("content", response_text),personalization_notes=f"Personalized for weaknesses: {', '.join(weaknesses)}" if weaknesses else "General lesson",sections=lesson_data.get("sections", []),suggested_exercises=lesson_data.get("suggested_exercises", []),difficulty_adjustment="supportive" if weaknesses else "standard",)except json.JSONDecodeError:# Return raw text if JSON parsing failsreturn PersonalizedLessonResponse(topic=request.topic,content=response_text,personalization_notes="Raw response (JSON parsing failed)",sections=[{"title": "Content", "content": response_text}],suggested_exercises=[],difficulty_adjustment="standard",)except Exception as e:logger.error(f"Personalized lesson generation error: {e}")raise HTTPException(status_code=500, detail=f"Lesson generation error: {str(e)}")# โโโ Main โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโif __name__ == "__main__":
main.py+120