CoolFace
Apppublic

akhaliq/anycoder

sourceHugging Faceupdated 5mo agoView on Hugging Face
3.3klikes
backend_models.py173 linesDownload Raw Back to root
1"""2Standalone model inference and client management for AnyCoder Backend API.3No Gradio dependencies - works with FastAPI/backend only.4"""5import os6from typing import Optional7 8from openai import OpenAI9 10def get_inference_client(model_id: str, provider: str = "auto"):11    """12    Return an appropriate client based on model_id.13    14    Returns OpenAI-compatible client for all models or raises error if not configured.15    """16    if model_id == "MiniMaxAI/MiniMax-M2" or model_id == "MiniMaxAI/MiniMax-M2.1" or model_id == "MiniMaxAI/MiniMax-M2.5":17        # Use HuggingFace Router with Novita provider for MiniMax M2 models18        return OpenAI(19            base_url="https://router.huggingface.co/v1",20            api_key=os.getenv("HF_TOKEN"),21            default_headers={"X-HF-Bill-To": "huggingface"}22        )23    24    elif model_id == "moonshotai/Kimi-K2-Thinking":25        # Use HuggingFace Router with Novita provider26        return OpenAI(27            base_url="https://router.huggingface.co/v1",28            api_key=os.getenv("HF_TOKEN"),29            default_headers={"X-HF-Bill-To": "huggingface"}30        )31    32    elif model_id == "moonshotai/Kimi-K2-Instruct":33        # Use HuggingFace Router with Groq provider34        return OpenAI(35            base_url="https://router.huggingface.co/v1",36            api_key=os.getenv("HF_TOKEN"),37            default_headers={"X-HF-Bill-To": "huggingface"}38        )39    40    elif model_id.startswith("deepseek-ai/"):41        # DeepSeek models via HuggingFace Router with Novita provider42        return OpenAI(43            base_url="https://router.huggingface.co/v1",44            api_key=os.getenv("HF_TOKEN"),45            default_headers={"X-HF-Bill-To": "huggingface"}46        )47    48    elif model_id.startswith("zai-org/GLM"):49        # GLM models via HuggingFace Router50        return OpenAI(51            base_url="https://router.huggingface.co/v1",52            api_key=os.getenv("HF_TOKEN"),53            default_headers={"X-HF-Bill-To": "huggingface"}54        )55    56    elif model_id.startswith("moonshotai/Kimi-K2"):57        # Kimi K2 models via HuggingFace Router58        return OpenAI(59            base_url="https://router.huggingface.co/v1",60            api_key=os.getenv("HF_TOKEN"),61            default_headers={"X-HF-Bill-To": "huggingface"}62        )63    64    elif model_id.startswith("Qwen/Qwen3-Coder-Next"):65        # Qwen models via HuggingFace Router66        return OpenAI(67            base_url="https://router.huggingface.co/v1",68            api_key=os.getenv("HF_TOKEN"),69            default_headers={"X-HF-Bill-To": "huggingface"}70        )71    72    elif model_id.startswith("google/gemma"):73        # Gemma models via HuggingFace Router74        return OpenAI(75            base_url="https://router.huggingface.co/v1",76            api_key=os.getenv("HF_TOKEN"),77            default_headers={"X-HF-Bill-To": "huggingface"}78        )79    80    elif model_id.startswith("Qwen/Qwen3.5"):81        # Qwen 3.5 models via HuggingFace Router82        return OpenAI(83            base_url="https://router.huggingface.co/v1",84            api_key=os.getenv("HF_TOKEN"),85            default_headers={"X-HF-Bill-To": "huggingface"}86        )87    88    else:89        # Unknown model - try HuggingFace Inference API90        return OpenAI(91            base_url="https://api-inference.huggingface.co/v1",92            api_key=os.getenv("HF_TOKEN")93        )94 95 96def get_real_model_id(model_id: str) -> str:97    """Get the real model ID with provider suffixes if needed"""98    if model_id == "zai-org/GLM-4.6":99        # GLM-4.6 requires Cerebras provider suffix in model string for API calls100        return "zai-org/GLM-4.6:cerebras"101    102    elif model_id == "MiniMaxAI/MiniMax-M2" or model_id == "MiniMaxAI/MiniMax-M2.1":103        # MiniMax M2 and M2.1 need Novita provider suffix104        return f"{model_id}:novita"105    106    elif model_id == "MiniMaxAI/MiniMax-M2.5":107        # MiniMax M2.5 needs fastest provider suffix108        return "MiniMaxAI/MiniMax-M2.5:fastest"109    110    elif model_id == "moonshotai/Kimi-K2-Thinking":111        # Kimi K2 Thinking needs Together AI provider112        return "moonshotai/Kimi-K2-Thinking:together"113    114    elif model_id == "moonshotai/Kimi-K2-Instruct":115        # Kimi K2 Instruct needs Groq provider116        return "moonshotai/Kimi-K2-Instruct:groq"117    118    elif model_id.startswith("deepseek-ai/DeepSeek-V3") or model_id.startswith("deepseek-ai/DeepSeek-R1"):119        # DeepSeek V3 and R1 models need Novita provider120        return f"{model_id}:novita"121    122    elif model_id == "zai-org/GLM-4.5":123        # GLM-4.5 needs fireworks-ai provider124        return "zai-org/GLM-4.5:fireworks-ai"125    126    elif model_id == "zai-org/GLM-4.7":127        # GLM-4.7 needs cerebras provider suffix128        return "zai-org/GLM-4.7:cerebras"129    130    elif model_id == "zai-org/GLM-4.7-Flash":131        # GLM-4.7-Flash via HuggingFace Router with Novita provider132        return "zai-org/GLM-4.7-Flash:novita"133    134    elif model_id == "zai-org/GLM-5":135        # GLM-5 via HuggingFace Router with Novita provider136        return "zai-org/GLM-5:novita"137    138    elif model_id == "zai-org/GLM-5.1":139        # GLM-5.1 via HuggingFace Router with Novita provider140        return "zai-org/GLM-5.1:novita"141    142    elif model_id == "moonshotai/Kimi-K2.5":143        # Kimi K2.5 needs Novita provider144        return "moonshotai/Kimi-K2.5:novita"145    146    elif model_id == "moonshotai/Kimi-K2.6":147        # Kimi K2.6 needs Novita provider148        return "moonshotai/Kimi-K2.6:novita"149    150    elif model_id == "Qwen/Qwen3-Coder-Next":151        # Qwen3-Coder-Next needs Novita provider152        return "Qwen/Qwen3-Coder-Next:novita"153    154    elif model_id == "google/gemma-4-31B-it":155        return "google/gemma-4-31B-it:fastest"156    157    elif model_id == "Qwen/Qwen3.5-397B-A17B":158        # Qwen3.5-397B-A17B needs fastest provider159        return "Qwen/Qwen3.5-397B-A17B:fastest"160    161    return model_id162 163 164def is_native_sdk_model(model_id: str) -> bool:165    """Check if model uses native SDK (not OpenAI-compatible)"""166    return False167 168 169def is_mistral_model(model_id: str) -> bool:170    """Check if model uses Mistral SDK"""171    return False172 173