Tonic commited on
Commit
9d0240c
Β·
1 Parent(s): 384c439

attempts to add chat template

Browse files
Files changed (2) hide show
  1. app.py +99 -56
  2. requirements.txt +2 -2
app.py CHANGED
@@ -53,6 +53,70 @@ joinus = """
53
  4. Use advanced settings for fine-tuning
54
  """
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  def check_local_model():
57
  """Check if local int4 model files exist"""
58
  required_files = [
@@ -74,20 +138,42 @@ def check_local_model():
74
  def load_model():
75
  """Load the model and tokenizer"""
76
  global model, tokenizer
77
- # logger.info(f"Loading tokenizer from {LOCAL_MODEL_PATH}")
78
- tokenizer = AutoTokenizer.from_pretrained(MAIN_MODEL_ID, subfolder="int4")
79
- # logger.info(f"Loading int4 model from {LOCAL_MODEL_PATH}")
80
- model = AutoModelForCausalLM.from_pretrained(
81
- MAIN_MODEL_ID,
82
- subfolder="int4",
83
- device_map="auto" if DEVICE == "cuda" else "cpu",
84
- torch_dtype=torch.bfloat16,
85
- trust_remote_code=True
86
- )
87
- if tokenizer.pad_token_id is None:
88
- tokenizer.pad_token_id = tokenizer.eos_token_id
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  logger.info("Model loaded successfully")
90
  return True
 
 
 
 
91
 
92
 
93
  def create_prompt(system_message, user_message, enable_thinking=True):
@@ -283,49 +369,6 @@ with gr.Blocks() as demo:
283
  )
284
 
285
  if __name__ == "__main__":
286
- # # Advanced model download and verification
287
- # logger.info("Starting advanced model download and verification process...")
288
- # try:
289
- # from download_model import main as download_main, check_model_files, verify_model_integrity
290
-
291
- # # Check if model files already exist and are valid
292
- # if check_model_files():
293
- # logger.info("Model files found, verifying integrity...")
294
- # if verify_model_integrity():
295
- # logger.info("βœ… Model files verified successfully - no download needed")
296
- # else:
297
- # logger.warning("⚠️ Model files exist but failed integrity check, re-downloading...")
298
- # download_success = download_main()
299
- # if not download_success:
300
- # logger.error("❌ Model download failed")
301
- # sys.exit(1)
302
- # else:
303
- # logger.info("πŸ“₯ Model files not found, downloading...")
304
- # download_success = download_main()
305
- # if download_success:
306
- # logger.info("βœ… Model download and verification completed successfully")
307
- # else:
308
- # logger.error("❌ Model download failed")
309
- # sys.exit(1)
310
-
311
- # except ImportError as e:
312
- # logger.error(f"❌ Error importing download_model: {e}")
313
- # logger.info("πŸ”„ Continuing with direct model loading...")
314
- # except Exception as e:
315
- # logger.error(f"❌ Error during model download process: {e}")
316
- # logger.info("πŸ”„ Continuing with direct model loading...")
317
-
318
- # # Load model with enhanced error handling
319
- # logger.info("πŸ”„ Loading model...")
320
- # try:
321
- # if not load_model():
322
- # logger.error("❌ Failed to load model. Please check the logs above.")
323
- # sys.exit(1)
324
- # logger.info("βœ… Model loaded successfully")
325
- # except Exception as e:
326
- # logger.error(f"❌ Error loading model: {e}")
327
- # sys.exit(1)
328
-
329
- # logger.info("πŸš€ Starting Gradio application...")
330
  demo.queue()
331
  demo.launch(ssr_mode=False, mcp_server=True)
 
53
  4. Use advanced settings for fine-tuning
54
  """
55
 
56
+ def download_chat_template():
57
+ """Download the chat template from the main repository"""
58
+ try:
59
+ chat_template_url = f"https://huggingface.co/{MAIN_MODEL_ID}/raw/main/chat_template.jinja"
60
+ logger.info(f"Downloading chat template from {chat_template_url}")
61
+
62
+ response = requests.get(chat_template_url, timeout=30)
63
+ response.raise_for_status()
64
+
65
+ chat_template_content = response.text
66
+ logger.info("Chat template downloaded successfully")
67
+ return chat_template_content
68
+
69
+ except requests.exceptions.RequestException as e:
70
+ logger.error(f"Error downloading chat template: {e}")
71
+ return None
72
+ except Exception as e:
73
+ logger.error(f"Unexpected error downloading chat template: {e}")
74
+ return None
75
+
76
+ def get_fallback_chat_template():
77
+ """Return a fallback chat template if download fails"""
78
+ return """{# ───── defaults ───── #}
79
+ {%- if enable_thinking is not defined -%}
80
+ {%- set enable_thinking = true -%}
81
+ {%- endif -%}
82
+
83
+ {# ───── reasoning mode ───── #}
84
+ {%- if enable_thinking -%}
85
+ {%- set reasoning_mode = "/think" -%}
86
+ {%- else -%}
87
+ {%- set reasoning_mode = "/no_think" -%}
88
+ {%- endif -%}
89
+
90
+ {# ───── header (system message) ───── #}
91
+ {{- "<|im_start|>system\\n" -}}
92
+ {{- system_message | trim -}}
93
+ {{- "<|im_end|>\\n" -}}
94
+
95
+ {# ───── conversation history ───── #}
96
+ {%- for message in messages -%}
97
+ {%- set content = message.content | trim -%}
98
+ {%- if message.role == "user" -%}
99
+ {{ "<|im_start|>user\\n" + content + "<|im_end|>\\n" }}
100
+ {%- elif message.role == "assistant" -%}
101
+ {%- if content.startswith("<think>") and content.endswith("</think>") -%}
102
+ {{ "<|im_start|>assistant\\n" + content + "<|im_end|>\\n" }}
103
+ {%- else -%}
104
+ {{ "<|im_start|>assistant\\n" + "<think>\\n\\n</think>\\n" + content.lstrip("\\n") + "<|im_end|>\\n" }}
105
+ {%- endif -%}
106
+ {%- elif message.role == "tool" -%}
107
+ {{ "<|im_start|>" + "user\\n" + content + "<|im_end|>\\n" }}
108
+ {%- endif -%}
109
+ {%- endfor -%}
110
+
111
+ {# ───── generation prompt ───── #}
112
+ {%- if add_generation_prompt -%}
113
+ {%- if reasoning_mode == "/think" -%}
114
+ {{ "<|im_start|>assistant\\n" }}
115
+ {%- else -%}
116
+ {{ "<|im_start|>assistant\\n" + "<think>\\n\\n</think>\\n" }}
117
+ {%- endif -%}
118
+ {%- endif -%}"""
119
+
120
  def check_local_model():
121
  """Check if local int4 model files exist"""
122
  required_files = [
 
138
  def load_model():
139
  """Load the model and tokenizer"""
140
  global model, tokenizer
141
+
142
+ try:
143
+ # Load tokenizer from main repository to get the base configuration
144
+ logger.info(f"Loading tokenizer from {MAIN_MODEL_ID}")
145
+ tokenizer = AutoTokenizer.from_pretrained(MAIN_MODEL_ID, subfolder="int4")
146
+
147
+ # Download and set the chat template
148
+ chat_template = download_chat_template()
149
+ if chat_template:
150
+ tokenizer.chat_template = chat_template
151
+ logger.info("Chat template downloaded and set successfully")
152
+ else:
153
+ # Use fallback chat template
154
+ logger.warning("Failed to download chat template, using fallback")
155
+ tokenizer.chat_template = get_fallback_chat_template()
156
+ logger.info("Fallback chat template set successfully")
157
+
158
+ # Load the int4 model from local path
159
+ logger.info(f"Loading int4 model from {LOCAL_MODEL_PATH}")
160
+ model = AutoModelForCausalLM.from_pretrained(
161
+ LOCAL_MODEL_PATH,
162
+ subfolder="int4",
163
+ device_map="auto" if DEVICE == "cuda" else "cpu",
164
+ torch_dtype=torch.bfloat16,
165
+ trust_remote_code=True
166
+ )
167
+
168
+ if tokenizer.pad_token_id is None:
169
+ tokenizer.pad_token_id = tokenizer.eos_token_id
170
+
171
  logger.info("Model loaded successfully")
172
  return True
173
+
174
+ except Exception as e:
175
+ logger.error(f"Error loading model: {e}")
176
+ return False
177
 
178
 
179
  def create_prompt(system_message, user_message, enable_thinking=True):
 
369
  )
370
 
371
  if __name__ == "__main__":
372
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
  demo.queue()
374
  demo.launch(ssr_mode=False, mcp_server=True)
requirements.txt CHANGED
@@ -7,5 +7,5 @@ safetensors>=0.4.0
7
  tokenizers>=0.21.2
8
  pyyaml>=6.0
9
  psutil>=5.9.0
10
- huggingface_hub>=0.20.0
11
- tqdm>=4.64.0
 
7
  tokenizers>=0.21.2
8
  pyyaml>=6.0
9
  psutil>=5.9.0
10
+ tqdm>=4.64.0
11
+ requests>=2.31.0