HFswapnil commited on
Commit
430bae8
·
verified ·
1 Parent(s): d4eb41b

Update run_model.py

Browse files
Files changed (1) hide show
  1. run_model.py +13 -10
run_model.py CHANGED
@@ -1,4 +1,4 @@
1
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
2
  from huggingface_hub import hf_hub_download
3
  import os
4
  import torch
@@ -11,23 +11,26 @@ SYSTEM_PROMPT = """You are helpful AI assistant. You answer questions truthfully
11
 
12
  MODEL_NAME = "microsoft/Phi-3.5-mini-instruct"
13
 
 
 
14
  model = AutoModelForCausalLM.from_pretrained(
15
  MODEL_NAME,
16
  device_map="cpu",
17
- torch_dtype=torch.bfloat16,
 
18
  )
19
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
20
 
21
  # print(f"Microsoft Phi-3.5-mini-instruct Downloaded Successfully !")
22
 
23
 
24
- generation_args = {
25
- "max_new_tokens": 64,
26
- "return_full_text": False,
27
- "temperature": 0.1,
28
- "top_p": 1.0,
29
- "do_sample": False,
30
- }
31
 
32
  # pipe = pipeline(
33
  # "text-generation",
@@ -49,7 +52,7 @@ async def generate_response(prompt: str, context: str = "", history: list = [])
49
  with torch.no_grad():
50
  prompt_text = "\n".join([f"{m['role']}: {m['content']}" for m in message])
51
  inputs = tokenizer(prompt_text, return_tensors="pt").to(model.device)
52
- outputs = model.generate(**inputs, **generation_args)
53
  outputs = tokenizer.decode(outputs[0], skip_special_tokens=True)
54
 
55
  # return outputs[0]["generated_text"]
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
2
  from huggingface_hub import hf_hub_download
3
  import os
4
  import torch
 
11
 
12
  MODEL_NAME = "microsoft/Phi-3.5-mini-instruct"
13
 
14
+ quantization_config = BitsAndBytesConfig(load_in_4bit=True)
15
+
16
  model = AutoModelForCausalLM.from_pretrained(
17
  MODEL_NAME,
18
  device_map="cpu",
19
+ torch_dtype="auto",
20
+ quantization_config=quantization_config
21
  )
22
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
23
 
24
  # print(f"Microsoft Phi-3.5-mini-instruct Downloaded Successfully !")
25
 
26
 
27
+ # generation_args = {
28
+ # "max_new_tokens": 64,
29
+ # "return_full_text": False,
30
+ # "temperature": 0.1,
31
+ # "top_p": 1.0,
32
+ # "do_sample": True,
33
+ # }
34
 
35
  # pipe = pipeline(
36
  # "text-generation",
 
52
  with torch.no_grad():
53
  prompt_text = "\n".join([f"{m['role']}: {m['content']}" for m in message])
54
  inputs = tokenizer(prompt_text, return_tensors="pt").to(model.device)
55
+ outputs = model.generate(**inputs, temperature=0.1, do_resample=True)
56
  outputs = tokenizer.decode(outputs[0], skip_special_tokens=True)
57
 
58
  # return outputs[0]["generated_text"]