"""
Fabrique centrale d'instanciation des moteurs d'inference de Mirza.
Fournit une couche d'abstraction unifiee pour creer les moteurs STT, LLM et TTS.
"""
from ..config import LlmConfig, TtsConfig
from .interfaces.baseLlm import BaseLlm
from .interfaces.baseStt import BaseStt
from .interfaces.baseTts import BaseTts
from .llm.mlxLmLlm import MlxLmLlm
from .llm.mlxQwenLlm import MlxQwenLlm
from .llm.mtplxLlm import MtplxLlm
from .llm.openRouterLlm import OpenRouterLlm
from .llm.optiqLlm import OptiqLlm
from .stt.mlxNemotronStt import MlxNemotronStt
from .stt.mlxWhisperStt import MlxWhisperStt
from .tts.edgeTts import EdgeTts
from .tts.kokoroTts import KokoroTts
from .tts.meloTts import MeloTts
[docs]
class EngineFactory:
"""
Fabrique centrale des moteurs d'inference STT, LLM et TTS.
Centralise la selection des backends selon la configuration materielle et
logicielle injectee.
"""
[docs]
@staticmethod
def createSttEngine(
provider: str = "whisper",
modelRepo: str = "mlx-community/whisper-large-v3-turbo-q4",
) -> BaseStt:
"""
Instancie le moteur de transcription vocale (STT / Speech-to-Text).
Supporte Whisper Turbo MLX et Nemotron 3.5 8-bit.
Parameters
----------
provider : str, optional
Nom du fournisseur ('whisper', 'mlx', 'nemotron'). Defaut : "whisper".
modelRepo : str, optional
Depot HuggingFace ou chemin local du modele quantifie. Defaut :
"mlx-community/whisper-large-v3-turbo-q4".
Returns
-------
BaseStt
Instance du moteur de transcription conforme au contrat BaseStt.
Raises
------
ValueError
Si le fournisseur specifie n'est pas reconnu.
"""
providerLower = provider.lower()
if "nemotron" in modelRepo.lower() or providerLower == "nemotron":
return MlxNemotronStt(modelRepo=modelRepo)
elif providerLower in ("mlx", "whisper"):
return MlxWhisperStt(modelRepo=modelRepo)
else:
raise ValueError(f"Fournisseur STT inconnu : {provider}. Utilisez 'mlx' ou 'nemotron'.")
[docs]
@staticmethod
def createLlmEngine(
modelConfig: LlmConfig,
provider: str = "mlx",
) -> BaseLlm:
"""
Instancie le moteur de generation de langage (LLM / Large Language Model).
Charge Qwen 3.5 9B, Qwen 3.8 Distill MTPLX, ou une conversion OptiQ,
avec acceleration Multi-Token Prediction et gestion de cache.
Parameters
----------
modelConfig : LlmConfig
Configuration complete des hyperparametres LLM et Metal.
provider : str, optional
Nom du backend d'execution ('mlx', 'mtplx', 'mlxlm' ou 'optiq').
Defaut : "mlx".
Returns
-------
BaseLlm
Instance du moteur de langage conforme au contrat BaseLlm.
Raises
------
ValueError
Si le fournisseur specifie n'est pas reconnu.
"""
providerLower = provider.lower()
if providerLower == "mlx":
return MlxQwenLlm(modelConfig=modelConfig)
elif providerLower in ("mtplx", "qwen3.8"):
return MtplxLlm(modelConfig=modelConfig)
elif providerLower in ("mlxlm", "mlx-lm"):
# Modeles de langage purs, charges par mlx-lm (conversions OptiQ).
return MlxLmLlm(modelConfig=modelConfig)
elif providerLower in ("optiq", "optiq-mtp"):
# Serveur OptiQ : seul runtime qui branche la tete MTP d'un sidecar.
return OptiqLlm(modelConfig=modelConfig)
elif providerLower in ("openrouter", "remote", "api"):
# Modele distant : aucun chargement, tout passe par l'API.
return OpenRouterLlm(modelConfig=modelConfig)
else:
raise ValueError(
f"Fournisseur LLM inconnu : {provider}. "
"Utilisez 'mtplx', 'mlx', 'mlxlm', 'optiq' ou 'openrouter'."
)
[docs]
@staticmethod
def createTtsEngine(
ttsConfig: TtsConfig,
provider: str = "edge_tts",
) -> BaseTts:
"""
Instancie le moteur de synthese vocale (TTS / Text-to-Speech).
Permet d'alterner entre Edge-TTS Neural (cloud) et Kokoro ONNX ou
MeloTTS (synthese locale, autonome et hors-ligne).
Parameters
----------
ttsConfig : TtsConfig
Configuration acoustique (voix, vitesse, hauteur, decoupe de clauses).
provider : str, optional
Fournisseur de synthese ('edge_tts', 'kokoro' ou 'melo').
Defaut : "edge_tts".
Returns
-------
BaseTts
Instance du moteur conforme au contrat BaseTts.
Raises
------
ValueError
Si le fournisseur TTS n'est pas reconnu.
"""
providerLower = provider.lower()
if providerLower == "edge_tts":
return EdgeTts(ttsConfig=ttsConfig)
elif providerLower == "kokoro":
return KokoroTts(ttsConfig=ttsConfig)
elif providerLower in ("melo", "melotts"):
return MeloTts(ttsConfig=ttsConfig)
else:
raise ValueError(f"Fournisseur TTS inconnu : {provider}. Utilisez 'edge_tts', 'kokoro' ou 'melo'.")