davidtran999 commited on
Commit
47b08c9
·
verified ·
1 Parent(s): 18be497

Upload backend/hue_portal/wsgi.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. backend/hue_portal/wsgi.py +53 -0
backend/hue_portal/wsgi.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from django.core.wsgi import get_wsgi_application
3
+ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hue_portal.hue_portal.settings")
4
+ application = get_wsgi_application()
5
+
6
+ # Preload models in worker process (Gunicorn workers are separate processes)
7
+ # This ensures models are loaded when worker starts, not on first request
8
+ print('[WSGI] 🔄 Preloading models in worker process...', flush=True)
9
+ try:
10
+ # 1. Preload Embedding Model (BGE-M3)
11
+ try:
12
+ print('[WSGI] 📦 Preloading embedding model (BGE-M3)...', flush=True)
13
+ from hue_portal.core.embeddings import get_embedding_model
14
+ embedding_model = get_embedding_model()
15
+ if embedding_model:
16
+ print('[WSGI] ✅ Embedding model preloaded successfully', flush=True)
17
+ else:
18
+ print('[WSGI] ⚠️ Embedding model not loaded', flush=True)
19
+ except Exception as e:
20
+ print(f'[WSGI] ⚠️ Embedding model preload failed: {e}', flush=True)
21
+
22
+ # 2. Preload LLM Model (llama.cpp)
23
+ llm_provider = os.environ.get('DEFAULT_LLM_PROVIDER') or os.environ.get('LLM_PROVIDER', '')
24
+ if llm_provider.lower() == 'llama_cpp':
25
+ try:
26
+ print('[WSGI] 📦 Preloading LLM model (llama.cpp)...', flush=True)
27
+ from hue_portal.chatbot.llm_integration import get_llm_generator
28
+ llm_gen = get_llm_generator()
29
+ if llm_gen and hasattr(llm_gen, 'llama_cpp') and llm_gen.llama_cpp:
30
+ print('[WSGI] ✅ LLM model preloaded successfully', flush=True)
31
+ else:
32
+ print('[WSGI] ⚠️ LLM model not loaded (may load on first request)', flush=True)
33
+ except Exception as e:
34
+ print(f'[WSGI] ⚠️ LLM model preload failed: {e} (will load on first request)', flush=True)
35
+ else:
36
+ print(f'[WSGI] ⏭️ Skipping LLM preload (provider is {llm_provider or "not set"}, not llama_cpp)', flush=True)
37
+
38
+ # 3. Preload Reranker Model
39
+ try:
40
+ print('[WSGI] 📦 Preloading reranker model...', flush=True)
41
+ from hue_portal.core.reranker import get_reranker
42
+ reranker = get_reranker()
43
+ if reranker:
44
+ print('[WSGI] ✅ Reranker model preloaded successfully', flush=True)
45
+ else:
46
+ print('[WSGI] ⚠️ Reranker model not loaded (may load on first request)', flush=True)
47
+ except Exception as e:
48
+ print(f'[WSGI] ⚠️ Reranker preload failed: {e} (will load on first request)', flush=True)
49
+
50
+ print('[WSGI] ✅ Model preload completed in worker process', flush=True)
51
+ except Exception as e:
52
+ print(f'[WSGI] ⚠️ Model preload error: {e} (models will load on first request)', flush=True)
53
+