|
|
from django.apps import AppConfig |
|
|
|
|
|
class AiApiConfig(AppConfig): |
|
|
default_auto_field = 'django.db.models.BigAutoField' |
|
|
name = 'ai_api' |
|
|
|
|
|
def ready(self): |
|
|
from . import globals |
|
|
from deepface import DeepFace |
|
|
from ai_api.library.devlab_image import DevLabImage |
|
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
import whisper |
|
|
import os |
|
|
from safetensors import safe_open |
|
|
import torch |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
|
|
globals.devlab_image = DevLabImage() |
|
|
|
|
|
|
|
|
save_path = os.path.join(os.path.dirname(__file__), "ddet_classification") |
|
|
print(f"Model path: {save_path}") |
|
|
globals.save_path = save_path |
|
|
|
|
|
|
|
|
try: |
|
|
globals.tokenizer = AutoTokenizer.from_pretrained(save_path,device=device) |
|
|
print("Tokenizer loaded ✅") |
|
|
except Exception as e: |
|
|
print(f"Failed to load tokenizer: {e}") |
|
|
globals.tokenizer = None |
|
|
|
|
|
|
|
|
try: |
|
|
safetensor_file = os.path.join(save_path, "model.safetensors") |
|
|
if os.path.exists(safetensor_file): |
|
|
with safe_open(safetensor_file, framework="pt") as f: |
|
|
print("Safetensors file checked ✅") |
|
|
|
|
|
globals.model = AutoModelForSequenceClassification.from_pretrained(save_path) |
|
|
globals.model.eval() |
|
|
print("Classification model loaded ✅") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Failed to load classification model: {e}") |
|
|
globals.model = None |
|
|
|
|
|
|
|
|
try: |
|
|
globals.whisper_model = whisper.load_model("large",device=device) |
|
|
print("Whisper model loaded ✅") |
|
|
except Exception as e: |
|
|
print(f"Failed to load Whisper model: {e}") |
|
|
globals.whisper_model = None |
|
|
|
|
|
|
|
|
try: |
|
|
globals.facenet_model = DeepFace.build_model("Facenet") |
|
|
print("Facenet model loaded ✅") |
|
|
except Exception as e: |
|
|
print(f"Failed to load FaceNet model: {e}") |
|
|
globals.facenet_model = None |
|
|
|