Wisdom Chen commited on
Commit
9ace4d6
·
unverified ·
1 Parent(s): e35c922

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +9 -21
model.py CHANGED
@@ -53,19 +53,6 @@ def initialize_models() -> bool:
53
  try:
54
  print(f"Initializing models on device: {device}")
55
 
56
- # Add explicit Hugging Face login with better error handling
57
- try:
58
- hf_token = st.secrets.HUGGINGFACE_TOKEN
59
- if not isinstance(hf_token, str) or not hf_token.startswith('hf_'):
60
- raise ValueError("Invalid Hugging Face token format")
61
-
62
- # Validate token before proceeding
63
- login(token=hf_token, write_permission=False)
64
- print("Successfully authenticated with Hugging Face")
65
-
66
- except Exception as e:
67
- raise RuntimeError(f"Hugging Face authentication failed: {str(e)}")
68
-
69
  # Initialize CLIP model with error handling
70
  try:
71
  clip_model, _, clip_preprocess = open_clip.create_model_and_transforms(
@@ -88,22 +75,23 @@ def initialize_models() -> bool:
88
  bnb_4bit_quant_type="nf4"
89
  )
90
 
91
- # Initialize tokenizer with specific version requirements
 
 
92
  llm_tokenizer = AutoTokenizer.from_pretrained(
93
  model_name,
94
- use_auth_token=hf_token, # Changed from token to use_auth_token
95
- trust_remote_code=True
 
96
  )
97
  llm_tokenizer.pad_token = llm_tokenizer.eos_token
98
-
99
  llm_model = AutoModelForCausalLM.from_pretrained(
100
  model_name,
101
- use_auth_token=hf_token, # Changed from token to use_auth_token
102
  quantization_config=quantization_config,
103
- device_map='cpu', # Force CPU usage
104
  torch_dtype=torch.float16,
105
- low_cpu_mem_usage=True,
106
- trust_remote_code=True
107
  )
108
  llm_model.eval()
109
  print("LLM initialized successfully")
 
53
  try:
54
  print(f"Initializing models on device: {device}")
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  # Initialize CLIP model with error handling
57
  try:
58
  clip_model, _, clip_preprocess = open_clip.create_model_and_transforms(
 
75
  bnb_4bit_quant_type="nf4"
76
  )
77
 
78
+ # Get token from Streamlit secrets
79
+ hf_token = st.secrets["HUGGINGFACE_TOKEN"]
80
+
81
  llm_tokenizer = AutoTokenizer.from_pretrained(
82
  model_name,
83
+ padding_side="left",
84
+ truncation_side="left",
85
+ token=hf_token # Add token here
86
  )
87
  llm_tokenizer.pad_token = llm_tokenizer.eos_token
88
+
89
  llm_model = AutoModelForCausalLM.from_pretrained(
90
  model_name,
 
91
  quantization_config=quantization_config,
92
+ device_map="auto",
93
  torch_dtype=torch.float16,
94
+ token=hf_token # Add token here
 
95
  )
96
  llm_model.eval()
97
  print("LLM initialized successfully")