Spaces:
Paused
Paused
Download app.py from abcs-noob/Uploadfiles: direct link, hf CLI and curl.
- Browser
- Download file 2.77 kB
-
https://huggingface.co/spaces/abcs-noob/Uploadfiles/resolve/main/app.py
- Command line
-
hf download hf://spaces/abcs-noob/Uploadfiles/app.py
-
curl -L -o app.py https://huggingface.co/spaces/abcs-noob/Uploadfiles/resolve/main/app.py
2.77 kB
| import os | |
| from fastapi import FastAPI, HTTPException, Header, Security | |
| from fastapi.security import APIKeyHeader | |
| from pydantic import BaseModel | |
| from typing import List, Optional, Union | |
| from huggingface_hub import InferenceClient | |
| app = FastAPI(title="Multi-Model AI Router API") | |
| # Lấy Hugging Face Access Token từ Secret Settings của Space | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| # Danh sách Route ánh xạ tên Model ngắn gọn sang Model ID thực tế trên HF | |
| MODEL_MAP = { | |
| "gemma-4-31b": "google/gemma-4-31B-it", | |
| "gemma-4-26b": "google/gemma-4-26B-A4B-it", | |
| "kimi-k2.7-code": "moonshotai/Kimi-K2.7-Code", | |
| "qwen-3.6-27b": "Qwen/Qwen3.6-27B" | |
| } | |
| # --- Schemas chuẩn theo OpenAI Format --- | |
| class Message(BaseModel): | |
| role: str | |
| content: str | |
| class ChatCompletionRequest(BaseModel): | |
| model: str | |
| messages: List[Message] | |
| temperature: Optional[float] = 0.7 | |
| max_tokens: Optional[int] = 512 | |
| async def chat_completions(request: ChatCompletionRequest): | |
| # 1. Kiểm tra Model user yêu cầu có trong Router không | |
| target_model = MODEL_MAP.get(request.model) | |
| if not target_model: | |
| raise HTTPException( | |
| status_code=400, | |
| detail=f"Model '{request.model}' không hỗ trợ. Danh sách model khả dụng: {list(MODEL_MAP.keys())}" | |
| ) | |
| try: | |
| # 2. Khởi tạo Client gọi đến Provider tương ứng | |
| client = InferenceClient(model=target_model, token=HF_TOKEN) | |
| # Format lại prompt từ danh sách messages | |
| prompt_history = "" | |
| for msg in request.messages: | |
| prompt_history += f"<|im_start|>{msg.role}\n{msg.content}<|im_end|>\n" | |
| prompt_history += "<|im_start|>assistant\n" | |
| # 3. Forward request đến Serverless GPU | |
| response_text = client.text_generation( | |
| prompt_history, | |
| max_new_tokens=request.max_tokens, | |
| temperature=request.temperature | |
| ) | |
| # 4. Trả về Response chuẩn định dạng OpenAI | |
| return { | |
| "id": "chatcmpl-router", | |
| "object": "chat.completion", | |
| "model": request.model, | |
| "choices": [{ | |
| "index": 0, | |
| "message": { | |
| "role": "assistant", | |
| "content": response_text.strip() | |
| }, | |
| "finish_reason": "stop" | |
| }] | |
| } | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Lỗi Routing/Inference: {str(e)}") | |
| async def list_models(): | |
| """Endpoint cho user kiểm tra các model đang hỗ trợ""" | |
| return {"data": [{"id": key, "object": "model"} for key in MODEL_MAP.keys()]} |