Tonic commited on
Commit
384c439
Β·
1 Parent(s): 5a6251e

use the subfolder param

Browse files
Files changed (3) hide show
  1. app.py +61 -66
  2. download_model.py +12 -4
  3. verify_spaces_config.py +89 -0
app.py CHANGED
@@ -74,35 +74,21 @@ def check_local_model():
74
  def load_model():
75
  """Load the model and tokenizer"""
76
  global model, tokenizer
77
- try:
78
- if check_local_model():
79
- logger.info(f"Loading tokenizer from {LOCAL_MODEL_PATH}")
80
- tokenizer = AutoTokenizer.from_pretrained(LOCAL_MODEL_PATH)
81
- logger.info(f"Loading int4 model from {LOCAL_MODEL_PATH}")
82
- model = AutoModelForCausalLM.from_pretrained(
83
- LOCAL_MODEL_PATH,
84
- device_map="auto" if DEVICE == "cuda" else "cpu",
85
- torch_dtype=torch.bfloat16,
86
- trust_remote_code=True
87
- )
88
- else:
89
- logger.info(f"Local model not found, loading from {MAIN_MODEL_ID}")
90
- tokenizer = AutoTokenizer.from_pretrained(MAIN_MODEL_ID)
91
- logger.info(f"Loading int4 model from {MAIN_MODEL_ID}/int4")
92
- model = AutoModelForCausalLM.from_pretrained(
93
  MAIN_MODEL_ID,
94
- subfolder="int4", # Use the int4 subfolder
95
  device_map="auto" if DEVICE == "cuda" else "cpu",
96
  torch_dtype=torch.bfloat16,
97
  trust_remote_code=True
98
  )
99
- if tokenizer.pad_token_id is None:
100
- tokenizer.pad_token_id = tokenizer.eos_token_id
101
  logger.info("Model loaded successfully")
102
  return True
103
- except Exception as e:
104
- logger.error(f"Error loading model: {e}")
105
- return False
106
 
107
  def create_prompt(system_message, user_message, enable_thinking=True):
108
  """Create prompt using the model's chat template"""
@@ -193,14 +179,23 @@ def generate_response(message, history, system_message, max_tokens, temperature,
193
 
194
  def user(user_message, history):
195
  """Add user message to history"""
196
- return "", history + [[user_message, None]]
 
 
197
 
198
  def bot(history, system_prompt, max_length, temperature, top_p, advanced_checkbox, enable_thinking):
199
  """Generate bot response"""
200
- user_message = history[-1][0]
 
 
 
 
 
201
  do_sample = advanced_checkbox
202
  bot_message = generate_response(user_message, history, system_prompt, max_length, temperature, top_p, do_sample, enable_thinking)
203
- history[-1][1] = bot_message
 
 
204
  return history
205
 
206
  # Load model on startup
@@ -268,7 +263,7 @@ with gr.Blocks() as demo:
268
  generate_button = gr.Button(value="πŸ€– Petite Elle L'Aime 3")
269
 
270
  with gr.Column(scale=2):
271
- chatbot = gr.Chatbot(label="πŸ€– Petite Elle L'Aime 3", type="messages")
272
 
273
  generate_button.click(
274
  user,
@@ -288,49 +283,49 @@ with gr.Blocks() as demo:
288
  )
289
 
290
  if __name__ == "__main__":
291
- # Advanced model download and verification
292
- logger.info("Starting advanced model download and verification process...")
293
- try:
294
- from download_model import main as download_main, check_model_files, verify_model_integrity
295
 
296
- # Check if model files already exist and are valid
297
- if check_model_files():
298
- logger.info("Model files found, verifying integrity...")
299
- if verify_model_integrity():
300
- logger.info("βœ… Model files verified successfully - no download needed")
301
- else:
302
- logger.warning("⚠️ Model files exist but failed integrity check, re-downloading...")
303
- download_success = download_main()
304
- if not download_success:
305
- logger.error("❌ Model download failed")
306
- sys.exit(1)
307
- else:
308
- logger.info("πŸ“₯ Model files not found, downloading...")
309
- download_success = download_main()
310
- if download_success:
311
- logger.info("βœ… Model download and verification completed successfully")
312
- else:
313
- logger.error("❌ Model download failed")
314
- sys.exit(1)
315
 
316
- except ImportError as e:
317
- logger.error(f"❌ Error importing download_model: {e}")
318
- logger.info("πŸ”„ Continuing with direct model loading...")
319
- except Exception as e:
320
- logger.error(f"❌ Error during model download process: {e}")
321
- logger.info("πŸ”„ Continuing with direct model loading...")
322
 
323
- # Load model with enhanced error handling
324
- logger.info("πŸ”„ Loading model...")
325
- try:
326
- if not load_model():
327
- logger.error("❌ Failed to load model. Please check the logs above.")
328
- sys.exit(1)
329
- logger.info("βœ… Model loaded successfully")
330
- except Exception as e:
331
- logger.error(f"❌ Error loading model: {e}")
332
- sys.exit(1)
333
 
334
- logger.info("πŸš€ Starting Gradio application...")
335
  demo.queue()
336
  demo.launch(ssr_mode=False, mcp_server=True)
 
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):
94
  """Create prompt using the model's chat template"""
 
179
 
180
  def user(user_message, history):
181
  """Add user message to history"""
182
+ if history is None:
183
+ history = []
184
+ return "", history + [{"role": "user", "content": user_message}]
185
 
186
  def bot(history, system_prompt, max_length, temperature, top_p, advanced_checkbox, enable_thinking):
187
  """Generate bot response"""
188
+ # Get the last user message
189
+ if not history:
190
+ return history
191
+
192
+ user_message = history[-1]["content"] if history else ""
193
+
194
  do_sample = advanced_checkbox
195
  bot_message = generate_response(user_message, history, system_prompt, max_length, temperature, top_p, do_sample, enable_thinking)
196
+
197
+ # Add assistant response to history
198
+ history.append({"role": "assistant", "content": bot_message})
199
  return history
200
 
201
  # Load model on startup
 
263
  generate_button = gr.Button(value="πŸ€– Petite Elle L'Aime 3")
264
 
265
  with gr.Column(scale=2):
266
+ chatbot = gr.Chatbot(label="πŸ€– Petite Elle L'Aime 3", type="messages", value=[])
267
 
268
  generate_button.click(
269
  user,
 
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)
download_model.py CHANGED
@@ -28,8 +28,12 @@ def download_model():
28
  # Use huggingface_hub to download the model files
29
  from huggingface_hub import hf_hub_download, list_repo_files
30
 
31
- # List files in the int4 subfolder
32
- files = list_repo_files(MAIN_MODEL_ID, subfolder="int4")
 
 
 
 
33
 
34
  # Download each required file
35
  required_files = [
@@ -41,19 +45,23 @@ def download_model():
41
  "generation_config.json"
42
  ]
43
 
 
44
  for file_name in required_files:
45
- if file_name in files:
 
46
  logger.info(f"Downloading {file_name}...")
47
  hf_hub_download(
48
  repo_id=MAIN_MODEL_ID,
49
- filename=f"int4/{file_name}",
50
  local_dir=LOCAL_MODEL_PATH,
51
  local_dir_use_symlinks=False
52
  )
53
  logger.info(f"Downloaded {file_name}")
 
54
  else:
55
  logger.warning(f"File {file_name} not found in int4 subfolder")
56
 
 
57
  logger.info(f"Model downloaded successfully to {LOCAL_MODEL_PATH}")
58
  return True
59
 
 
28
  # Use huggingface_hub to download the model files
29
  from huggingface_hub import hf_hub_download, list_repo_files
30
 
31
+ # List all files in the repository
32
+ all_files = list_repo_files(MAIN_MODEL_ID)
33
+
34
+ # Filter files that are in the int4 subfolder
35
+ int4_files = [f for f in all_files if f.startswith("int4/")]
36
+ logger.info(f"Found {len(int4_files)} files in int4 subfolder")
37
 
38
  # Download each required file
39
  required_files = [
 
45
  "generation_config.json"
46
  ]
47
 
48
+ downloaded_count = 0
49
  for file_name in required_files:
50
+ int4_file_path = f"int4/{file_name}"
51
+ if int4_file_path in all_files:
52
  logger.info(f"Downloading {file_name}...")
53
  hf_hub_download(
54
  repo_id=MAIN_MODEL_ID,
55
+ filename=int4_file_path,
56
  local_dir=LOCAL_MODEL_PATH,
57
  local_dir_use_symlinks=False
58
  )
59
  logger.info(f"Downloaded {file_name}")
60
+ downloaded_count += 1
61
  else:
62
  logger.warning(f"File {file_name} not found in int4 subfolder")
63
 
64
+ logger.info(f"Downloaded {downloaded_count} out of {len(required_files)} required files")
65
  logger.info(f"Model downloaded successfully to {LOCAL_MODEL_PATH}")
66
  return True
67
 
verify_spaces_config.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Script to verify Hugging Face Spaces configuration
4
+ """
5
+
6
+ import os
7
+ import sys
8
+ import logging
9
+
10
+ # Configure logging
11
+ logging.basicConfig(level=logging.INFO)
12
+ logger = logging.getLogger(__name__)
13
+
14
+ def check_required_files():
15
+ """Check if all required files for HF Spaces deployment exist"""
16
+ required_files = [
17
+ "app.py",
18
+ "requirements.txt",
19
+ "README.md",
20
+ "build.py",
21
+ "download_model.py",
22
+ ".gitignore"
23
+ ]
24
+
25
+ missing_files = []
26
+ for file in required_files:
27
+ if not os.path.exists(file):
28
+ missing_files.append(file)
29
+
30
+ if missing_files:
31
+ logger.error(f"Missing required files: {missing_files}")
32
+ return False
33
+
34
+ logger.info("βœ… All required files present")
35
+ return True
36
+
37
+ def check_app_imports():
38
+ """Check if app.py can be imported without errors"""
39
+ try:
40
+ # Test basic imports
41
+ import gradio as gr
42
+ import torch
43
+ from transformers import AutoModelForCausalLM, AutoTokenizer
44
+
45
+ logger.info("βœ… Basic imports successful")
46
+ return True
47
+ except Exception as e:
48
+ logger.error(f"❌ Import error: {e}")
49
+ return False
50
+
51
+ def check_download_script():
52
+ """Check if download script can be imported"""
53
+ try:
54
+ from download_model import main as download_main
55
+ logger.info("βœ… Download script import successful")
56
+ return True
57
+ except Exception as e:
58
+ logger.error(f"❌ Download script import error: {e}")
59
+ return False
60
+
61
+ def main():
62
+ """Main verification function"""
63
+ logger.info("πŸ” Verifying Hugging Face Spaces configuration...")
64
+
65
+ checks = [
66
+ ("Required Files", check_required_files),
67
+ ("App Imports", check_app_imports),
68
+ ("Download Script", check_download_script)
69
+ ]
70
+
71
+ all_passed = True
72
+ for check_name, check_func in checks:
73
+ logger.info(f"Checking {check_name}...")
74
+ if check_func():
75
+ logger.info(f"βœ… {check_name} passed")
76
+ else:
77
+ logger.error(f"❌ {check_name} failed")
78
+ all_passed = False
79
+
80
+ if all_passed:
81
+ logger.info("πŸŽ‰ All checks passed! Ready for Hugging Face Spaces deployment.")
82
+ else:
83
+ logger.error("❌ Some checks failed. Please fix the issues above.")
84
+
85
+ return all_passed
86
+
87
+ if __name__ == "__main__":
88
+ success = main()
89
+ sys.exit(0 if success else 1)