JaynilJaiswal commited on
Commit
7290453
·
verified ·
1 Parent(s): 25c46f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -5
app.py CHANGED
@@ -17,6 +17,61 @@ import re
17
  import chromadb # ADDED for client check
18
  from typing import List, Dict, Any, Optional
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  # --- Load Structured Resume Data ---
21
  resume_filename = "resume_corrected.json" # Using the revamped JSON
22
  resume_data = {}
@@ -395,23 +450,23 @@ if not vectorstore:
395
 
396
  # --- Load Fine-tuned CTransformers model ---
397
  # (This part remains unchanged)
398
- model_path_gguf = "/data/zephyr-7b-beta.Q4_K_M.gguf" # MAKE SURE THIS PATH IS CORRECT
399
- print(f"Initializing Fine-Tuned CTransformers LLM from: {model_path_gguf}")
400
  config = {
401
  'max_new_tokens': 512, 'temperature': 0.1, 'context_length': 2048,
402
  'gpu_layers': 0, 'stream': False, 'threads': -1, 'top_k': 40,
403
  'top_p': 0.9, 'repetition_penalty': 1.1
404
  }
405
  llm = None
406
- if not os.path.exists(model_path_gguf):
407
  print(f"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
408
- print(f"ERROR: GGUF Model file not found at: {model_path_gguf}")
409
  print(f"Please download the model and place it at the correct path, or update model_path_gguf.")
410
  print(f"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
411
  print("LLM initialization skipped.")
412
  else:
413
  try:
414
- llm = CTransformers(model=model_path_gguf, model_type='llama', config=config)
415
  print("Fine-Tuned CTransformers LLM initialized.")
416
  except Exception as e:
417
  print(f"Error initializing CTransformers: {e}")
 
17
  import chromadb # ADDED for client check
18
  from typing import List, Dict, Any, Optional
19
 
20
+ # --- Constants ---
21
+ MODEL_REPO = "TheBloke/zephyr-7B-beta-GGUF"
22
+ MODEL_FILE = "zephyr-7b-beta.Q4_K_M.gguf"
23
+ # Define a path within the persistent storage for the model
24
+ # Using os.environ.get('HF_HOME', '/data') ensures it uses HF_HOME if set,
25
+ # otherwise defaults to /data. You might want a specific models subdir.
26
+ # Let's create a dedicated model path within /data:
27
+ MODEL_DIR = "/data/models" # Store models in /data/models
28
+ LOCAL_MODEL_PATH = os.path.join(MODEL_DIR, MODEL_FILE)
29
+
30
+ # --- Function to Download Model (Runtime Check) ---
31
+ def download_model_if_needed():
32
+ """Checks if model exists in persistent storage, downloads if not."""
33
+ print(f"Checking for model file at: {LOCAL_MODEL_PATH}")
34
+ if not os.path.exists(LOCAL_MODEL_PATH):
35
+ print(f"Model not found locally. Downloading from {MODEL_REPO}...")
36
+ try:
37
+ # Create the directory if it doesn't exist
38
+ os.makedirs(MODEL_DIR, exist_ok=True)
39
+ # Use hf_hub_download for robust downloading & caching (respects HF_HOME)
40
+ # We specify local_dir to force it into our /data structure,
41
+ # and local_dir_use_symlinks=False to avoid symlinks if that causes issues.
42
+ # If you set HF_HOME=/data in Dockerfile, it *should* cache there by default,
43
+ # but explicitly downloading to a specific path within /data is safer.
44
+ hf_hub_download(
45
+ repo_id=MODEL_REPO,
46
+ filename=MODEL_FILE,
47
+ local_dir=MODEL_DIR, # Download directly into this folder
48
+ local_dir_use_symlinks=False, # Avoid symlinks, copy directly
49
+ # cache_dir=os.environ.get('HF_HOME') # Optional: force cache dir if needed
50
+ )
51
+ # Verify download
52
+ if os.path.exists(LOCAL_MODEL_PATH):
53
+ print(f"Model downloaded successfully to {LOCAL_MODEL_PATH}")
54
+ else:
55
+ print(f"Download attempted but file still not found at {LOCAL_MODEL_PATH}. Check download path and permissions.")
56
+ # Consider raising an error or exiting if download fails critically
57
+ raise FileNotFoundError("Model download failed.")
58
+
59
+ except Exception as e:
60
+ print(f"Error downloading model: {e}")
61
+ # Handle error appropriately - maybe exit or try fallback
62
+ raise # Re-raise the exception to stop execution if model is critical
63
+ else:
64
+ print("Model file already exists locally.")
65
+
66
+ # --- Call the download function at the start ---
67
+ try:
68
+ download_model_if_needed()
69
+ except Exception as e:
70
+ print(f"Failed to ensure model availability: {e}")
71
+ exit() # Exit if model download fails and is required
72
+
73
+
74
+
75
  # --- Load Structured Resume Data ---
76
  resume_filename = "resume_corrected.json" # Using the revamped JSON
77
  resume_data = {}
 
450
 
451
  # --- Load Fine-tuned CTransformers model ---
452
  # (This part remains unchanged)
453
+ # model_path_gguf = "/data/zephyr-7b-beta.Q4_K_M.gguf" # MAKE SURE THIS PATH IS CORRECT
454
+ print(f"Initializing Fine-Tuned CTransformers LLM from: {LOCAL_MODEL_PATH}")
455
  config = {
456
  'max_new_tokens': 512, 'temperature': 0.1, 'context_length': 2048,
457
  'gpu_layers': 0, 'stream': False, 'threads': -1, 'top_k': 40,
458
  'top_p': 0.9, 'repetition_penalty': 1.1
459
  }
460
  llm = None
461
+ if not os.path.exists(LOCAL_MODEL_PATH):
462
  print(f"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
463
+ print(f"ERROR: GGUF Model file not found at: {LOCAL_MODEL_PATH}")
464
  print(f"Please download the model and place it at the correct path, or update model_path_gguf.")
465
  print(f"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
466
  print("LLM initialization skipped.")
467
  else:
468
  try:
469
+ llm = CTransformers(model=LOCAL_MODEL_PATH, model_type='llama', config=config)
470
  print("Fine-Tuned CTransformers LLM initialized.")
471
  except Exception as e:
472
  print(f"Error initializing CTransformers: {e}")