CoolFace
Apppublic

SolusOps/Study-with-ChampAI

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
model_router.py50 linesDownload Raw Back to services
1from __future__ import annotations2from services import hf_provider, featherless_provider3 4class ModelRouter:5    """6    Routes tasks to the correct model and provider.7    understand() → MiniCPM-V (HF) — all document tasks8    reason()     → Nemotron 4B (Featherless) — all reasoning tasks9    translate()  → Tiny Aya (HF) — multilingual10    transcribe() → Whisper (HF) — speech to text11    """12    def __init__(self, ocr_model: str, reasoning_model: str,13                 multilingual_model: str, speech_model: str,14                 hf_api_key: str = "", featherless_api_key: str = "",15                 max_tokens: int = 1024, temperature: float = 0.3):16        self._ocr_model = ocr_model17        self._reason_model = reasoning_model18        self._multi_model = multilingual_model19        self._speech_model = speech_model20        self._hf_key = hf_api_key21        self._fl_key = featherless_api_key22        self._max_tokens = max_tokens23        self._temperature = temperature24 25    def understand(self, prompt: str, image_b64: str = "") -> str:26        """MiniCPM-V via HuggingFace — OCR, diagram reading, concept extraction."""27        if image_b64:28            return hf_provider.vision_generate(29                self._ocr_model, image_b64, prompt,30                self._hf_key, self._max_tokens)31        return hf_provider.generate(32            self._ocr_model, prompt, api_key=self._hf_key,33            max_tokens=self._max_tokens, temperature=self._temperature)34 35    def reason(self, prompt: str, system: str = "") -> str:36        """Nemotron 3 Nano 4B via Featherless — quests, questions, tutor."""37        return featherless_provider.generate(38            self._reason_model, prompt, system,39            self._fl_key, self._max_tokens, self._temperature)40 41    def translate(self, prompt: str, system: str = "") -> str:42        """Tiny Aya 3.3B via HuggingFace — multilingual explanations."""43        return hf_provider.generate(44            self._multi_model, prompt, system,45            self._hf_key, self._max_tokens, self._temperature)46 47    def transcribe(self, audio_bytes: bytes) -> str:48        """Whisper via HuggingFace — speech to text."""49        return hf_provider.transcribe(self._speech_model, audio_bytes, self._hf_key)50