MarianaCodebase commited on
Commit
2e1c2fe
·
verified ·
1 Parent(s): 2192c17

Upload model.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. model.py +13 -1
model.py CHANGED
@@ -18,6 +18,7 @@ _llm = None
18
  _using_mock = False
19
  _model_checked = False
20
  _gen_lock = threading.Lock()
 
21
 
22
  IM_START = "<|im_start|>"
23
  IM_END = "<|im_end|>"
@@ -72,10 +73,20 @@ def _load_model():
72
  if _model_checked:
73
  return
74
 
75
- _model_checked = True
 
 
 
 
 
 
 
 
 
76
  gguf = _find_gguf()
77
  if gguf is None:
78
  _using_mock = True
 
79
  return
80
 
81
  try:
@@ -107,6 +118,7 @@ def _load_model():
107
  print(f"[model] Could not load GGUF ({gguf}): {exc}")
108
  _llm = None
109
  _using_mock = True
 
110
 
111
 
112
  def _build_prompt(system_prompt: str, user_prompt: str) -> str:
 
18
  _using_mock = False
19
  _model_checked = False
20
  _gen_lock = threading.Lock()
21
+ _load_lock = threading.Lock()
22
 
23
  IM_START = "<|im_start|>"
24
  IM_END = "<|im_end|>"
 
73
  if _model_checked:
74
  return
75
 
76
+ # Double-checked lock: the startup warm-up thread and the first real request
77
+ # can both reach here at once; without this they would load the model twice.
78
+ with _load_lock:
79
+ if _model_checked:
80
+ return
81
+ _do_load_model()
82
+
83
+
84
+ def _do_load_model():
85
+ global _llm, _using_mock, _model_checked
86
  gguf = _find_gguf()
87
  if gguf is None:
88
  _using_mock = True
89
+ _model_checked = True
90
  return
91
 
92
  try:
 
118
  print(f"[model] Could not load GGUF ({gguf}): {exc}")
119
  _llm = None
120
  _using_mock = True
121
+ _model_checked = True
122
 
123
 
124
  def _build_prompt(system_prompt: str, user_prompt: str) -> str: