Download Run.py from Zhaoming213/ChatBox6-base-finall: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/Zhaoming213/ChatBox6-base-finall/resolve/main/Run.py
- Command line
-
hf download hf://Zhaoming213/ChatBox6-base-finall/Run.py
-
curl -L -o Run.py https://huggingface.co/Zhaoming213/ChatBox6-base-finall/resolve/main/Run.py
1.21 kB
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer | |
| model_path = "model" | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) | |
| model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True).half().to(device) | |
| streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True) | |
| print("🤖 模型加载完成!请输入你的问题(输入 exit 退出):") | |
| while True: | |
| prompt = input("💬 用户: ") | |
| if prompt == "exit": | |
| break | |
| input_text = tokenizer.bos_token + prompt | |
| inputs = tokenizer(input_text, return_tensors="pt", truncation=True).to(device) | |
| # 5. 模型生成 | |
| print("助手: ", end="") | |
| with torch.no_grad(): | |
| model.generate( | |
| input_ids=inputs["input_ids"], | |
| attention_mask=inputs.get("attention_mask"), | |
| max_new_tokens=128, | |
| do_sample=True, | |
| top_p=0.95, | |
| temperature=0.9, | |
| streamer=streamer, | |
| pad_token_id=tokenizer.pad_token_id, | |
| eos_token_id=tokenizer.eos_token_id | |
| ) | |
| print("\n" + "-" * 30) |