Study-with-ChampAI / services /model_router.py
SolusOps's picture
feat: services package
63645ac verified
Raw
History Blame Contribute Delete
2.27 kB
from __future__ import annotations
from services import hf_provider, featherless_provider
class ModelRouter:
"""
Routes tasks to the correct model and provider.
understand() β†’ MiniCPM-V (HF) β€” all document tasks
reason() β†’ Nemotron 4B (Featherless) β€” all reasoning tasks
translate() β†’ Tiny Aya (HF) β€” multilingual
transcribe() β†’ Whisper (HF) β€” speech to text
"""
def __init__(self, ocr_model: str, reasoning_model: str,
multilingual_model: str, speech_model: str,
hf_api_key: str = "", featherless_api_key: str = "",
max_tokens: int = 1024, temperature: float = 0.3):
self._ocr_model = ocr_model
self._reason_model = reasoning_model
self._multi_model = multilingual_model
self._speech_model = speech_model
self._hf_key = hf_api_key
self._fl_key = featherless_api_key
self._max_tokens = max_tokens
self._temperature = temperature
def understand(self, prompt: str, image_b64: str = "") -> str:
"""MiniCPM-V via HuggingFace β€” OCR, diagram reading, concept extraction."""
if image_b64:
return hf_provider.vision_generate(
self._ocr_model, image_b64, prompt,
self._hf_key, self._max_tokens)
return hf_provider.generate(
self._ocr_model, prompt, api_key=self._hf_key,
max_tokens=self._max_tokens, temperature=self._temperature)
def reason(self, prompt: str, system: str = "") -> str:
"""Nemotron 3 Nano 4B via Featherless β€” quests, questions, tutor."""
return featherless_provider.generate(
self._reason_model, prompt, system,
self._fl_key, self._max_tokens, self._temperature)
def translate(self, prompt: str, system: str = "") -> str:
"""Tiny Aya 3.3B via HuggingFace β€” multilingual explanations."""
return hf_provider.generate(
self._multi_model, prompt, system,
self._hf_key, self._max_tokens, self._temperature)
def transcribe(self, audio_bytes: bytes) -> str:
"""Whisper via HuggingFace β€” speech to text."""
return hf_provider.transcribe(self._speech_model, audio_bytes, self._hf_key)