Spaces:
Runtime error
Runtime error
Duplicate from mikeee/baichuan-13b-chat-try
Browse filesCo-authored-by: mikeee <mikeee@users.noreply.huggingface.co>
- .gitattributes +35 -0
- README.md +13 -0
- app.py +82 -0
- requirements.txt +13 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Baichuan 13b Chat Try
|
3 |
+
emoji: 🌍
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: blue
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.43.2
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: mikeee/baichuan-13b-chat-try
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# pylint: disable=invalid-name, line-too-long, missing-module-docstring
|
2 |
+
import gc
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
|
6 |
+
import gradio
|
7 |
+
import rich
|
8 |
+
import torch
|
9 |
+
from huggingface_hub import snapshot_download
|
10 |
+
from loguru import logger
|
11 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
12 |
+
from transformers.generation.utils import GenerationConfig
|
13 |
+
|
14 |
+
model_name = "baichuan-inc/Baichuan2-13B-Chat-4bits"
|
15 |
+
if not torch.cuda.is_available():
|
16 |
+
gradio.Error(f"No cuda, cant run {model_name}")
|
17 |
+
raise SystemError(f"No cuda, cant run {model_name}")
|
18 |
+
|
19 |
+
# snapshot_download?
|
20 |
+
loc = snapshot_download(repo_id=model_name, local_dir="model")
|
21 |
+
|
22 |
+
# fix timezone in Linux
|
23 |
+
os.environ["TZ"] = "Asia/Shanghai"
|
24 |
+
try:
|
25 |
+
time.tzset() # type: ignore # pylint: disable=no-member
|
26 |
+
except Exception: # pylint: disable=broad-except
|
27 |
+
# Windows
|
28 |
+
logger.warning("Windows, cant run time.tzset()")
|
29 |
+
|
30 |
+
model = None
|
31 |
+
gc.collect() # for interactive testing
|
32 |
+
|
33 |
+
logger.info("start")
|
34 |
+
has_cuda = torch.cuda.is_available()
|
35 |
+
|
36 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False, trust_remote_code=True)
|
37 |
+
|
38 |
+
if has_cuda:
|
39 |
+
model = AutoModelForCausalLM.from_pretrained(
|
40 |
+
"model", # loc
|
41 |
+
device_map="auto",
|
42 |
+
torch_dtype=torch.bfloat16, # pylint: disable=no-member
|
43 |
+
load_in_8bit=True,
|
44 |
+
trust_remote_code=True,
|
45 |
+
# use_ram_optimized_load=False,
|
46 |
+
# offload_folder="offload_folder",
|
47 |
+
) # .cuda()
|
48 |
+
else:
|
49 |
+
try:
|
50 |
+
# model = AutoModel.from_pretrained(model_name, trust_remote_code=True).float()
|
51 |
+
model = AutoModelForCausalLM.from_pretrained(
|
52 |
+
# model_name, trust_remote_code=True
|
53 |
+
"model",
|
54 |
+
trust_remote_code=True,
|
55 |
+
) # .float() not supported
|
56 |
+
except Exception as exc:
|
57 |
+
logger.error(exc)
|
58 |
+
logger.warning("Doesnt seem to load for CPU...")
|
59 |
+
raise SystemExit(1) from exc
|
60 |
+
|
61 |
+
model = model.eval()
|
62 |
+
|
63 |
+
rich.print(f"{model=}")
|
64 |
+
|
65 |
+
logger.info("done")
|
66 |
+
|
67 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
68 |
+
"baichuan-inc/Baichuan2-13B-Chat-4bits", use_fast=False, trust_remote_code=True
|
69 |
+
)
|
70 |
+
|
71 |
+
# model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-13B-Chat-4bits", device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)
|
72 |
+
|
73 |
+
model.generation_config = GenerationConfig.from_pretrained(
|
74 |
+
"baichuan-inc/Baichuan2-13B-Chat-4bits"
|
75 |
+
)
|
76 |
+
messages = []
|
77 |
+
messages.append({"role": "user", "content": "解释一下“温故而知新”"})
|
78 |
+
response = model.chat(tokenizer, messages)
|
79 |
+
|
80 |
+
rich.print(response)
|
81 |
+
|
82 |
+
logger.info(f"{response=}")
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
install
|
2 |
+
|
3 |
+
torch
|
4 |
+
transformers
|
5 |
+
sentencepiece
|
6 |
+
accelerate
|
7 |
+
bitsandbytes
|
8 |
+
xformers
|
9 |
+
scipy
|
10 |
+
|
11 |
+
rich
|
12 |
+
loguru
|
13 |
+
huggingface_hub
|