Spaces:
Running
Running
github-actions[bot] commited on
Commit ·
de4b0cd
1
Parent(s): eba7c64
🚀 Auto-deploy backend from GitHub (10d155e)
Browse filesFiles changed (5)
123456# Inference provider selection# CI trigger marker: keep this file touchable to force backend deploy workflow runs when needed.INFERENCE_PROVIDER=hf_inferenceINFERENCE_PRO_ENABLED=trueINFERENCE_PRO_PROVIDER=hf_inferenceINFERENCE_GPU_PROVIDER=hf_inference123456# Inference provider selection# CI trigger marker: keep this file touchable to force backend deploy workflow runs when needed.INFERENCE_PROVIDER=deepseekINFERENCE_PRO_ENABLED=trueINFERENCE_PRO_PROVIDER=hf_inferenceINFERENCE_GPU_PROVIDER=hf_inference
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455models:primary:id: Qwen/Qwen3-32Bdescription: Global default instruction model for interactive Grade 11-12 math tutoringmax_new_tokens: 640temperature: 0.25top_p: 0.9backup:- id: meta-llama/Meta-Llama-3-70B-Instructdescription: High-quality model used for harder multi-step promptsmax_new_tokens: 768temperature: 0.3top_p: 0.9- id: google/gemma-2-2b-itdescription: Secondary backup with broad instruction coveragemax_new_tokens: 384temperature: 0.2top_p: 0.9experimental:- id: mistralai/Mistral-7B-Instruct-v0.3notes: Prompt/procedure experimentation- id: meta-llama/Meta-Llama-3-8B-Instructnotes: Baseline comparison against legacy deploymentrouting:task_model_map:# Keep all task defaults aligned to Qwen3-32B.# Hard prompts can still escalate via runtime policy in inference_client.chat: Qwen/Qwen3-32Bverify_solution: Qwen/Qwen3-32Blesson_generation: Qwen/Qwen3-32Bquiz_generation: Qwen/Qwen3-32Blearning_path: Qwen/Qwen3-32Bdaily_insight: Qwen/Qwen3-32Brisk_classification: Qwen/Qwen3-32Brisk_narrative: Qwen/Qwen3-32Btask_fallback_model_map:chat: [] # Chat is strict-primary only (no fallback chain)verify_solution:- meta-llama/Meta-Llama-3-70B-Instruct # Higher-capacity fallback- meta-llama/Llama-3.1-8B-Instruct # Second fallbacktask_provider_map:# All tasks use hf_inference router (Qwen/Qwen3-32B natively supported)chat: hf_inferenceverify_solution: hf_inferencelesson_generation: hf_inferencequiz_generation: hf_inferencelearning_path: hf_inferencedaily_insight: hf_inferencerisk_narrative: hf_inferencerisk_classification: hf_inference12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485models:primary:id: deepseek-chatdescription: Default DeepSeek chat model — all chat tasks, quizzes, lessons, reasoningmax_new_tokens: 800temperature: 0.7top_p: 0.9rag_primary:id: deepseek-reasonerdescription: DeepSeek reasoner — extended reasoning for complex RAG tasksmax_new_tokens: 1800temperature: 0.2top_p: 0.9enable_thinking_tasks:- rag_lesson- verify_solution- risk_narrativeno_thinking_tasks:- chat- quiz_generation- learning_path- daily_insightembedding:id: BAAI/bge-small-en-v1.5description: Embedding model for RAG retrieval — curriculum vectorstore ingestion and semantic searchnote: Not part of the generation pipeline. Read from EMBEDDING_MODEL env var only. Not swappable via admin panel.model_capabilities:sequential_only:- deepseek-reasonersupports_thinking:- deepseek-reasonerrouting:task_model_map:chat: deepseek-chatverify_solution: deepseek-reasonerlesson_generation: deepseek-chatquiz_generation: deepseek-chatlearning_path: deepseek-chatdaily_insight: deepseek-chatrisk_classification: deepseek-chatrisk_narrative: deepseek-reasonerrag_lesson: deepseek-reasonerrag_problem: deepseek-chatrag_analysis_context: deepseek-chattask_fallback_model_map:chat:- deepseek-chatverify_solution:- deepseek-chatlesson_generation:- deepseek-chatquiz_generation:- deepseek-chatlearning_path:- deepseek-chatdaily_insight:- deepseek-chatrisk_classification:- deepseek-chatrisk_narrative:- deepseek-chatrag_lesson:- deepseek-chatrag_problem:- deepseek-chatrag_analysis_context:- deepseek-chattask_provider_map:chat: deepseekverify_solution: deepseeklesson_generation: deepseekquiz_generation: deepseeklearning_path: deepseekdaily_insight: deepseekrisk_classification: deepseekrisk_narrative: deepseekrag_lesson: deepseekrag_problem: deepseekrag_analysis_context: deepseek
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184"""Firebase Storage PDF loader for curriculum ingestion.Downloads PDFs from Firebase Storage and extracts text for ChromaDB indexing."""
from __future__ import annotations
import loggingimport osfrom pathlib import Pathfrom typing import Dict, List, Optional, Tuple
logger = logging.getLogger("mathpulse.fb_storage_loader")
_FIREBASE_INITIALIZED = False
def _init_firebase_storage() -> Tuple[any, any]: global _FIREBASE_INITIALIZED
if _FIREBASE_INITIALIZED: try: from firebase_admin import storage as fb_storage bucket = fb_storage.bucket() return fb_storage, bucket except Exception as e: logger.warning("Firebase storage unavailable: %s", e) _FIREBASE_INITIALIZED = False return None, None
try: import firebase_admin from firebase_admin import credentials, storage except ImportError: logger.warning("firebase_admin not installed") return None, None
if firebase_admin._apps: _FIREBASE_INITIALIZED = True try: bucket = storage.bucket() return storage, bucket except Exception as e: logger.warning("Firebase storage bucket unavailable: %s", e) return None, None
sa_json = os.getenv("FIREBASE_SERVICE_ACCOUNT_JSON") # Also check HF Spaces secret mount path if not sa_json: secret_path = "/secret/FIREBASE_SERVICE_ACCOUNT_JSON" if Path(secret_path).exists(): try: sa_json = Path(secret_path).read_text(encoding="utf-8").strip() except Exception: pass
sa_file = os.getenv("FIREBASE_SERVICE_ACCOUNT_FILE") bucket_name = os.getenv("FIREBASE_STORAGE_BUCKET", "mathpulse-ai-2026.firebasestorage.app")
try: if sa_json: import json as _json creds = credentials.Certificate(_json.loads(sa_json)) elif sa_file and Path(sa_file).exists(): creds = credentials.Certificate(sa_file) else: creds = credentials.ApplicationDefault()
firebase_admin.initialize_app(creds, {"storageBucket": bucket_name}) _FIREBASE_INITIALIZED = True bucket = storage.bucket() return storage, bucket except Exception as e: logger.warning("Firebase init failed: %s", e) return None, None
def download_pdf_from_storage(storage_path: str, dest_path: Optional[str] = None) -> Optional[bytes]: """Download a PDF from Firebase Storage and return its bytes.""" _, bucket = _init_firebase_storage() if bucket is None: logger.warning("Firebase Storage not available, skipping download") return None
try: blob = bucket.blob(storage_path) if not blob.exists(): logger.warning("Blob does not exist: %s", storage_path) return None bytes_data = blob.download_as_bytes() logger.info("Downloaded %s (%d bytes)", storage_path, len(bytes_data))
if dest_path: Path(dest_path).parent.mkdir(parents=True, exist_ok=True) with open(dest_path, "wb") as f: f.write(bytes_data) logger.info("Saved to %s", dest_path)
return bytes_data except Exception as e: logger.error("Failed to download %s: %s", storage_path, e) return None
def list_curriculum_blobs(prefix: str = "curriculum/") -> List[Dict[str, str]]: """List all blobs under a prefix in Firebase Storage.""" _, bucket = _init_firebase_storage() if bucket is None: return []
blobs = bucket.list_blobs(prefix=prefix) result = [] for blob in blobs: if blob.name.endswith(".pdf"): result.append({ "name": blob.name, "size": blob.size, "updated": str(blob.updated) if blob.updated else None, "download_url": f"https://storage.googleapis.com/{bucket.name}/{blob.name}", }) return result
# NOTE: Curriculum guide PDFs (shaping papers) are stored in Firebase Storage# for system reference but are NOT included in RAG ingestion because they# contain only learning objectives and course descriptions — insufficient# content for lesson generation (typically <10 chunks each).## Only SDO teaching modules (full lesson content with examples and problems)# are included in the RAG pipeline.
PDF_METADATA: Dict[str, dict] = { # General Mathematics Q1 — SDO Navotas teaching module (100 pages, ~117k chars) "curriculum/gen_math_sdo/SDO_Navotas_Gen.Math_SHS_1stSem.FV.pdf": { "subject": "General Mathematics", "subjectId": "gen-math", "type": "sdo_module", "content_domain": "general", "quarter": 1, "storage_path": "curriculum/gen_math_sdo/SDO_Navotas_Gen.Math_SHS_1stSem.FV.pdf", }, # General Mathematics Q2 — Interest & Annuities modules (~27-35 pages each) "curriculum/general_math/genmath_q2_mod1_simpleandcompoundinterests_v2.pdf": { "subject": "General Mathematics", "subjectId": "gen-math", "type": "sdo_module", "content_domain": "general", "quarter": 2, "storage_path": "curriculum/general_math/genmath_q2_mod1_simpleandcompoundinterests_v2.pdf", }, "curriculum/general_math/genmath_q2_mod2_interestmaturityfutureandpresentvaluesinsimpleandcompoundinterests_v2.pdf": { "subject": "General Mathematics", "subjectId": "gen-math", "type": "sdo_module", "content_domain": "general", "quarter": 2, "storage_path": "curriculum/general_math/genmath_q2_mod2_interestmaturityfutureandpresentvaluesinsimpleandcompoundinterests_v2.pdf", }, "curriculum/general_math/genmath_q2_mod3_SolvingProblemsInvolvingSimpleandCompoundInterest_v2.pdf": { "subject": "General Mathematics", "subjectId": "gen-math", "type": "sdo_module", "content_domain": "general", "quarter": 2, "storage_path": "curriculum/general_math/genmath_q2_mod3_SolvingProblemsInvolvingSimpleandCompoundInterest_v2.pdf", }, "curriculum/general_math/genmath_q2_mod4_simpleandgeneralannuities_v2.pdf": { "subject": "General Mathematics", "subjectId": "gen-math", "type": "sdo_module", "content_domain": "general", "quarter": 2, "storage_path": "curriculum/general_math/genmath_q2_mod4_simpleandgeneralannuities_v2.pdf", }, # Statistics and Probability — Full textbook (331 pages, ~607k chars) "curriculum/stat_prob/Full.pdf": { "subject": "Statistics and Probability", "subjectId": "stats-prob", "type": "sdo_module", "content_domain": "statistics", "quarter": 1, "storage_path": "curriculum/stat_prob/Full.pdf", },}13 unmodified lines1415161718192013 unmodified linessys.path.insert(0, str(Path(__file__).resolve().parents[2]))from backend.rag.firebase_storage_loader import _init_firebase_storageREMOTE_PREFIX = "vectorstore/"LOCAL_DEST_DIR = Path("/app/datasets/vectorstore")13 unmodified lines1415161718192013 unmodified linessys.path.insert(0, str(Path(__file__).resolve().parents[2]))from hf_space_test.rag.firebase_storage_loader import _init_firebase_storageREMOTE_PREFIX = "vectorstore/"LOCAL_DEST_DIR = Path("/app/datasets/vectorstore")
99 unmodified lines100101102103104105485 unmodified lines591592593594595596597598332 unmodified lines93193293393493593699 unmodified lines# Featherless AI for Qwen math models (used as fallback when HF router fails)self.featherless_api_key = os.getenv("FEATHERLESS_API_KEY", "")self.featherless_chat_url = os.getenv("FEATHERLESS_CHAT_URL", "https://api.featherless.ai/openai/v1/chat/completions")self.local_space_url = _normalize_local_space_url(os.getenv("INFERENCE_LOCAL_SPACE_URL", "http://127.0.0.1:7860")485 unmodified linesroute = self._resolve_route_label(provider, req.task_type)if provider == "local_space":return self._call_local_space(req, provider=provider, route=route, fallback_depth=fallback_depth)# All models use HF inference router directly (including Qwen/Qwen3-32B)return self._call_hf_inference(req, provider=provider, route=route, fallback_depth=fallback_depth)def _messages_to_prompt(self, messages: List[Dict[str, str]]) -> str:332 unmodified linesself._bump_metric("requests_ok", 1)return textdef _call_local_space(self, req: InferenceRequest, *, provider: str, route: str, fallback_depth: int) -> str:target_model = req.model or self.default_modelurl = f"{self.local_space_url.rstrip('/')}{self.local_generate_path}"99 unmodified lines100101102103104105106107108109110485 unmodified lines596597598599600601602603604605606332 unmodified lines93994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101799 unmodified lines# Featherless AI for Qwen math models (used as fallback when HF router fails)self.featherless_api_key = os.getenv("FEATHERLESS_API_KEY", "")self.featherless_chat_url = os.getenv("FEATHERLESS_CHAT_URL", "https://api.featherless.ai/openai/v1/chat/completions")# DeepSeek API (primary inference provider)self.deepseek_api_key = os.getenv("DEEPSEEK_API_KEY", "")self.deepseek_base_url = os.getenv("DEEPSEEK_BASE_URL", "https://api.deepseek.com").rstrip("/")self.deepseek_chat_url = f"{self.deepseek_base_url}/v1/chat/completions"self.local_space_url = _normalize_local_space_url(os.getenv("INFERENCE_LOCAL_SPACE_URL", "http://127.0.0.1:7860")485 unmodified linesroute = self._resolve_route_label(provider, req.task_type)if provider == "local_space":return self._call_local_space(req, provider=provider, route=route, fallback_depth=fallback_depth)if provider == "deepseek":return self._call_deepseek(req, provider=provider, route=route, fallback_depth=fallback_depth)# All other providers use HF inference routerreturn self._call_hf_inference(req, provider=provider, route=route, fallback_depth=fallback_depth)def _messages_to_prompt(self, messages: List[Dict[str, str]]) -> str:332 unmodified linesself._bump_metric("requests_ok", 1)return textdef _call_deepseek(self, req: InferenceRequest, *, provider: str, route: str, fallback_depth: int) -> str:"""Call DeepSeek API (OpenAI-compatible endpoint)."""if not self.deepseek_api_key:raise RuntimeError("DEEPSEEK_API_KEY is not set")target_model = req.model or self.default_modelurl = self.deepseek_chat_urlmodel_base = target_model.split(":")[0] if ":" in target_model else target_modelLOGGER.debug(f"📌 Calling DeepSeek: task={req.task_type} model={model_base} "f"route={route} depth={fallback_depth}")payload: Dict[str, object] = {"model": target_model,"messages": req.messages,"stream": False,"max_tokens": req.max_new_tokens or self.default_max_new_tokens,"temperature": req.temperature,"top_p": req.top_p,}headers = {"Authorization": f"Bearer {self.deepseek_api_key}","Content-Type": "application/json","X-MathPulse-Task": (req.task_type or "default").strip().lower(),}timeout = self._timeout_for(req, provider)resp, latency_ms, retry_attempt = self._post_with_retry(url,headers=headers,payload=payload,timeout=timeout,provider=provider,model=target_model,task_type=req.task_type,request_tag=req.request_tag,fallback_depth=fallback_depth,route=route,)self._bump_bucket("status_code_counts", str(resp.status_code), 1)if resp.status_code != 200:self._bump_metric("requests_error", 1)raise RuntimeError(f"DeepSeek API error {resp.status_code}: {resp.text}")data = resp.json()text = self._extract_text(data)LOGGER.info(f"✅ DeepSeek success: task={req.task_type} model={model_base} "f"latency={latency_ms:.0f}ms tokens_out={len(text.split())}")log_model_call(LOGGER,provider=provider,model=target_model,endpoint=url,latency_ms=latency_ms,input_tokens=None,output_tokens=None,status="ok",task_type=req.task_type,request_tag=req.request_tag,retry_attempt=retry_attempt,fallback_depth=fallback_depth,route=route,)self._bump_metric("requests_ok", 1)return textdef _call_local_space(self, req: InferenceRequest, *, provider: str, route: str, fallback_depth: int) -> str:target_model = req.model or self.default_modelurl = f"{self.local_space_url.rstrip('/')}{self.local_generate_path}"
config/env.sample+1-1
config/models.yaml+72-42
rag/firebase_storage_loader.pyAdded+184
scripts/download_vectorstore_from_firebase.py+1-1
services/inference_client.py+83-2