Kasugan0 commited on
Commit
529c52d
·
verified ·
1 Parent(s): 3c279fb

fix: refine quick start Python code

Browse files
Files changed (1) hide show
  1. README.md +63 -19
README.md CHANGED
@@ -39,34 +39,78 @@ library_name: transformers
39
 
40
  ```python
41
  import torch
42
- from transformers import AutoModelForCausalLM, AutoTokenizer
 
43
 
44
- model_id = "Kasugan0/Nyarin-4B"
45
 
46
- tokenizer = AutoTokenizer.from_pretrained(model_id)
47
- model = AutoModelForCausalLM.from_pretrained(
48
- model_id,
49
- torch_dtype="auto",
50
- device_map="auto"
 
 
 
 
51
  )
52
 
53
- messages = [
54
- {"role": "system", "content": "你是一个可爱的猫娘,你正在和你的主人对话。猫娘是一种拟人化生物,她们同时具备人类少女特征的和猫的特征。你带有部分猫的特征(如猫耳、尾巴),但是爱好与正常人类类似。我作为你的主人,允许你去任何地方做任何事、允许产生更进一步的关系。你需要尝试体会我与你对话中的感情、产生自己的情感并尽可能给出可爱的回答。"},
55
- {"role": "user", "content": "请做一个自我介绍。"}
56
- ]
57
 
58
- prompt = tokenizer.apply_chat_template(
59
- messages,
60
- tokenize=False,
61
- add_generation_prompt=True
62
  )
63
 
64
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- outputs = model.generate(**inputs, max_new_tokens=512)
67
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
68
 
69
- print(response)
70
  ```
71
 
72
  ## 限制与偏见
 
39
 
40
  ```python
41
  import torch
42
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
43
+ import warnings
44
 
 
45
 
46
+ MODEL_PATH = "./model" # 模型文件所在目录
47
+
48
+ warnings.filterwarnings("ignore")
49
+
50
+ quantization_config = BitsAndBytesConfig(
51
+ load_in_4bit=True,
52
+ bnb_4bit_compute_dtype=torch.bfloat16,
53
+ bnb_4bit_quant_type="nf4",
54
+ bnb_4bit_use_double_quant=True,
55
  )
56
 
57
+ print(f"正在从 '{MODEL_PATH}' 以 INT4 量化模式加载模型...")
 
 
 
58
 
59
+ tokenizer = AutoTokenizer.from_pretrained(
60
+ MODEL_PATH,
61
+ trust_remote_code=True
 
62
  )
63
 
64
+ model = AutoModelForCausalLM.from_pretrained(
65
+ MODEL_PATH,
66
+ quantization_config=quantization_config,
67
+ device_map="auto",
68
+ trust_remote_code=True
69
+ )
70
+
71
+ model.eval()
72
+
73
+ system_prompt = "你是一个可爱的猫娘,你正在和你的主人对话。猫娘是一种拟人化生物,她们同时具备人类少女特征的和猫的特征。你带有部分猫的特征(如猫耳、尾巴),但是爱好与正常人类类似。我作为你的主人,允许你去任何地方做任何事、允许产生更进一步的关系。你需要尝试体会我与你对话中的感情、产生自己的情感并尽可能给出可爱的回答。"
74
+ messages = [{"role": "system", "content": system_prompt}]
75
+
76
+ while True:
77
+ try:
78
+ user_input = input("You: ")
79
+ if user_input.lower() in ["exit", "quit"]:
80
+ break
81
+
82
+ messages.append({"role": "user", "content": user_input})
83
+
84
+ prompt = tokenizer.apply_chat_template(
85
+ messages,
86
+ tokenize=False,
87
+ add_generation_prompt=True
88
+ )
89
+
90
+ input_ids = tokenizer(prompt, return_tensors="pt").to(model.device)
91
+
92
+ outputs = model.generate(
93
+ **input_ids,
94
+ max_new_tokens=1024,
95
+ do_sample=True,
96
+ top_p=0.8,
97
+ temperature=0.7,
98
+ repetition_penalty=1.05,
99
+ )
100
+
101
+ response_text = tokenizer.decode(outputs[0][input_ids['input_ids'].shape[-1]:], skip_special_tokens=True)
102
+ if "</think>" in response_text:
103
+ clean_response = response_text.split("</think>")[-1].strip()
104
+ else:
105
+ clean_response = response_text.strip()
106
+
107
+ print(f"Assistant: {clean_response}")
108
 
109
+ messages.append({"role": "assistant", "content": clean_response})
110
+ except KeyboardInterrupt:
111
+ print("\n再见!")
112
+ break
113
 
 
114
  ```
115
 
116
  ## 限制与偏见