Download API_routes/solution_details.py from Muxperts/Veda: direct link, hf CLI and curl.
- Browser
- Download file 3.74 kB
-
https://huggingface.co/spaces/Muxperts/Veda/resolve/main/API_routes/solution_details.py
- Command line
-
hf download hf://spaces/Muxperts/Veda/API_routes/solution_details.py
-
curl -L -o solution_details.py https://huggingface.co/spaces/Muxperts/Veda/resolve/main/API_routes/solution_details.py
3.74 kB
| """ | |
| API Route: Solution Details | |
| --------------------------- | |
| Fetches detailed solution content after teacher selects a solution. | |
| Now integrates VEDA (LLM) to provide context-aware, humanized advice | |
| based on the teacher's original problem. | |
| """ | |
| from fastapi import APIRouter, Depends, Query | |
| from API_routes.auth import require_login | |
| from decision_engine.solution_service import get_solution_details | |
| from decision_engine.llm_service import get_conversational_advice | |
| from deep_translator import GoogleTranslator | |
| router = APIRouter() | |
| async def fetch_solution_details( | |
| solution_id: str, | |
| problem: str = Query(None, description="The original classroom problem text"), | |
| lang: str = Query("en", description="Target language code (e.g., 'hi', 'te')"), | |
| user=Depends(require_login), | |
| ): | |
| """ | |
| Returns detailed execution steps for a selected solution. | |
| If 'problem' is provided, it uses the LLM to generate a personalized mentor response. | |
| """ | |
| # 1. Fetch original English details from library | |
| details = get_solution_details(solution_id) | |
| if not details: | |
| return { | |
| "success": False, | |
| "message": "Solution not found" | |
| } | |
| # 2. GENERATE AI ADVICE (Short-term context logic) | |
| # We do this BEFORE translating the 'details' dict so the LLM gets clean English data. | |
| ai_advice = None | |
| if problem: | |
| ai_advice = get_conversational_advice( | |
| problem_text=problem, | |
| solution_data=details, | |
| lang=lang | |
| ) | |
| # 3. Translate raw UI elements if requested | |
| if lang and lang != 'en': | |
| try: | |
| translator = GoogleTranslator(source='auto', target=lang) | |
| # Translate Pedagogy | |
| if "pedagogy" in details and "why_it_works" in details["pedagogy"]: | |
| original_why = details["pedagogy"]["why_it_works"] | |
| if original_why: | |
| details["pedagogy"]["why_it_works"] = translator.translate(original_why) | |
| # Translate Steps | |
| steps_list = [] | |
| if "details" in details and "steps" in details["details"]: | |
| steps_list = details["details"]["steps"] | |
| elif "steps" in details: | |
| steps_list = details["steps"] | |
| if steps_list and isinstance(steps_list, list): | |
| translated_steps = [translator.translate(step) for step in steps_list] | |
| if "details" in details and "steps" in details["details"]: | |
| details["details"]["steps"] = translated_steps | |
| else: | |
| details["steps"] = translated_steps | |
| # Translate Objective | |
| if "details" in details and "objective" in details["details"]: | |
| obj = details["details"]["objective"] | |
| if obj: | |
| details["details"]["objective"] = translator.translate(obj) | |
| # Translate Title/Action | |
| if "preview" in details: | |
| if "title" in details["preview"]: | |
| details["preview"]["title"] = translator.translate(details["preview"]["title"]) | |
| if "action_text" in details["preview"]: | |
| details["preview"]["action_text"] = translator.translate(details["preview"]["action_text"]) | |
| except Exception as e: | |
| print(f"WARNING: Detail translation failed for {lang}: {e}") | |
| # 4. Return combined response | |
| return { | |
| "success": True, | |
| "data": details, | |
| "ai_advice": ai_advice # This is the unique, humanized mentor response | |
| } |