eddddyy commited on
Commit
17ffe56
·
verified ·
1 Parent(s): 2e1fa69

Update model_loader.py

Browse files
Files changed (1) hide show
  1. model_loader.py +31 -17
model_loader.py CHANGED
@@ -3,20 +3,34 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  from config import HF_TOKEN, MODEL_ID
4
 
5
  def load_model():
6
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN, trust_remote_code=True)
7
- model = AutoModelForCausalLM.from_pretrained(
8
- MODEL_ID,
9
- token=HF_TOKEN,
10
- trust_remote_code=True,
11
- device_map="cpu",
12
- torch_dtype=torch.float32,
13
- low_cpu_mem_usage=True
14
- )
15
- return pipeline(
16
- "text-generation",
17
- model=model,
18
- tokenizer=tokenizer,
19
- max_new_tokens=100,
20
- do_sample=True,
21
- temperature=0.7,
22
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from config import HF_TOKEN, MODEL_ID
4
 
5
  def load_model():
6
+ try:
7
+ print(f"🔄 Loading tokenizer and model: {MODEL_ID}")
8
+ tokenizer = AutoTokenizer.from_pretrained(
9
+ MODEL_ID,
10
+ token=HF_TOKEN,
11
+ trust_remote_code=True
12
+ )
13
+
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ MODEL_ID,
16
+ token=HF_TOKEN,
17
+ trust_remote_code=True,
18
+ device_map="auto" if torch.cuda.is_available() else "cpu",
19
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
20
+ low_cpu_mem_usage=True
21
+ )
22
+
23
+ print("✅ Model loaded successfully.")
24
+ return pipeline(
25
+ "text-generation",
26
+ model=model,
27
+ tokenizer=tokenizer,
28
+ max_new_tokens=150,
29
+ do_sample=True,
30
+ temperature=0.7,
31
+ top_p=0.9
32
+ )
33
+
34
+ except Exception as e:
35
+ print(f"❌ Failed to load model: {e}")
36
+ raise RuntimeError(f"Model loading failed: {e}")