Wisdom Chen commited on
Commit
bd14e47
·
unverified ·
1 Parent(s): 21b3792

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +17 -12
model.py CHANGED
@@ -53,12 +53,18 @@ def initialize_models() -> bool:
53
  try:
54
  print(f"Initializing models on device: {device}")
55
 
56
- # Add explicit Hugging Face login
57
- from huggingface_hub import login
58
- hf_token = st.secrets["HUGGINGFACE_TOKEN"]
59
- if not hf_token:
60
- raise ValueError("HUGGINGFACE_TOKEN not found in Streamlit secrets")
61
- login(token=hf_token)
 
 
 
 
 
 
62
 
63
  # Initialize CLIP model with error handling
64
  try:
@@ -85,20 +91,18 @@ def initialize_models() -> bool:
85
  # Initialize tokenizer with specific version requirements
86
  llm_tokenizer = AutoTokenizer.from_pretrained(
87
  model_name,
88
- token=hf_token,
89
- trust_remote_code=True,
90
- use_auth_token=True # Add this line
91
  )
92
  llm_tokenizer.pad_token = llm_tokenizer.eos_token
93
 
94
  llm_model = AutoModelForCausalLM.from_pretrained(
95
  model_name,
96
- token=hf_token,
97
  quantization_config=quantization_config,
98
  device_map="auto",
99
  torch_dtype=torch.float16,
100
- trust_remote_code=True,
101
- use_auth_token=True # Add this line
102
  )
103
  llm_model.eval()
104
  print("LLM initialized successfully")
@@ -110,6 +114,7 @@ def initialize_models() -> bool:
110
  except Exception as e:
111
  raise RuntimeError(f"Model initialization failed: {str(e)}")
112
 
 
113
  # Data loading
114
  def load_data() -> bool:
115
  """
 
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:
 
91
  # Initialize tokenizer with specific version requirements
92
  llm_tokenizer = AutoTokenizer.from_pretrained(
93
  model_name,
94
+ use_auth_token=hf_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,
102
  quantization_config=quantization_config,
103
  device_map="auto",
104
  torch_dtype=torch.float16,
105
+ trust_remote_code=True
 
106
  )
107
  llm_model.eval()
108
  print("LLM initialized successfully")
 
114
  except Exception as e:
115
  raise RuntimeError(f"Model initialization failed: {str(e)}")
116
 
117
+
118
  # Data loading
119
  def load_data() -> bool:
120
  """