Spaces:
Paused
Paused
Update backend_models.py
Browse files- backend_models.py +19 -22
backend_models.py
CHANGED
|
@@ -1,31 +1,28 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Standalone model inference - Optimized for Free Tier (Serverless Inference API)
|
| 3 |
-
"""
|
| 4 |
import os
|
| 5 |
from openai import OpenAI
|
| 6 |
|
| 7 |
def get_inference_client(model_id: str, provider: str = "auto"):
|
| 8 |
"""
|
| 9 |
-
|
| 10 |
-
Không còn tham số trả phí (X-HF-Bill-To).
|
| 11 |
"""
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def get_real_model_id(model_id: str) -> str:
|
| 19 |
-
"""
|
| 20 |
-
Trả về ID model gốc, bỏ qua các hậu tố trả phí (novita, cerebras).
|
| 21 |
-
"""
|
| 22 |
-
# Loại bỏ hậu tố provider nếu có (ví dụ: model:provider -> model)
|
| 23 |
-
if ":" in model_id:
|
| 24 |
-
return model_id.split(":")[0]
|
| 25 |
return model_id
|
| 26 |
-
|
| 27 |
-
def is_native_sdk_model(model_id: str) -> bool:
|
| 28 |
-
return False
|
| 29 |
-
|
| 30 |
-
def is_mistral_model(model_id: str) -> bool:
|
| 31 |
-
return "mistral" in model_id.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from openai import OpenAI
|
| 3 |
|
| 4 |
def get_inference_client(model_id: str, provider: str = "auto"):
|
| 5 |
"""
|
| 6 |
+
Tự động phân biệt giữa model miễn phí và model trả phí.
|
|
|
|
| 7 |
"""
|
| 8 |
+
# Danh sách model miễn phí của Hugging Face
|
| 9 |
+
free_models = [
|
| 10 |
+
"meta-llama/Llama-3.1-8B-Instruct",
|
| 11 |
+
"google/gemma-2-9b-it",
|
| 12 |
+
"Qwen/Qwen2.5-7B-Instruct",
|
| 13 |
+
"mistralai/Mistral-7B-Instruct-v0.3"
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
if model_id in free_models:
|
| 17 |
+
# Dùng endpoint miễn phí
|
| 18 |
+
return OpenAI(
|
| 19 |
+
base_url="https://api-inference.huggingface.co/v1/",
|
| 20 |
+
api_key=os.getenv("HF_TOKEN")
|
| 21 |
+
)
|
| 22 |
+
else:
|
| 23 |
+
# Nếu dùng model trả phí mà đã hết quota, ta trả về client rỗng hoặc báo lỗi
|
| 24 |
+
# để hệ thống không bị crash đột ngột
|
| 25 |
+
raise Exception(f"Model {model_id} yêu cầu trả phí (Out of credits). Hãy chọn các model Llama hoặc Gemma trong menu.")
|
| 26 |
|
| 27 |
def get_real_model_id(model_id: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return model_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|