statnlp PY007 commited on
Commit
9bcc72d
·
0 Parent(s):

Duplicate from PY007/SLM-Chatbox-Preview

Browse files

Co-authored-by: Zhang Peiyuan <[email protected]>

Files changed (4) hide show
  1. .gitattributes +35 -0
  2. README.md +13 -0
  3. app.py +139 -0
  4. requirements.txt +5 -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: SLM Chatbox Preview
3
+ emoji: 💻
4
+ colorFrom: indigo
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 3.39.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: PY007/SLM-Chatbox-Preview
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gc
3
+ from string import Template
4
+ from threading import Thread
5
+
6
+ import torch
7
+ import gradio as gr
8
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BatchEncoding, TextIteratorStreamer
9
+
10
+
11
+ tokenizer = AutoTokenizer.from_pretrained(
12
+ "PY007/LiteChat-Preview",
13
+ )
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ "PY007/LiteChat-Preview",
16
+ trust_remote_code=True,
17
+ device_map="auto",
18
+ torch_dtype=torch.float16
19
+ )
20
+ model.eval()
21
+
22
+
23
+ max_context_length = model.config.max_position_embeddings
24
+ max_new_tokens = 1024
25
+
26
+
27
+ prompt_template = Template("""\
28
+ ### Instruction: $human
29
+ ### Response: $bot\
30
+ """)
31
+
32
+
33
+ system_prompt = "Below is an instruction that describes a task. Write a response that appropriately completes the request."
34
+ system_prompt_tokens = tokenizer([f"{system_prompt}\n\n"], return_tensors="pt")
35
+ max_sys_tokens = system_prompt_tokens['input_ids'].size(-1)
36
+
37
+
38
+ def bot(history):
39
+ history = history or []
40
+
41
+ # Inject prompt formatting into the history
42
+ prompt_history = []
43
+ for human, bot in history:
44
+ if bot is not None:
45
+ bot = bot.replace("<br>", "\n")
46
+ bot = bot.rstrip()
47
+ prompt_history.append(
48
+ prompt_template.substitute(
49
+ human=human, bot=bot if bot is not None else "")
50
+ )
51
+
52
+ msg_tokens = tokenizer(
53
+ "\n\n".join(prompt_history).strip(),
54
+ return_tensors="pt",
55
+ add_special_tokens=False # Use <BOS> from the system prompt
56
+ )
57
+
58
+ # Take only the most recent context up to the max context length and prepend the
59
+ # system prompt with the messages
60
+ max_tokens = -max_context_length + max_new_tokens + max_sys_tokens
61
+ inputs = BatchEncoding({
62
+ k: torch.concat([system_prompt_tokens[k], msg_tokens[k][:, max_tokens:]], dim=-1)
63
+ for k in msg_tokens
64
+ }).to('cuda')
65
+ # inputs = BatchEncoding({
66
+ # k: torch.concat([system_prompt_tokens[k], msg_tokens[k][:, max_tokens:]], dim=-1)
67
+ # for k in msg_tokens
68
+ # })
69
+ # Remove `token_type_ids` b/c it's not yet supported for LLaMA `transformers` models
70
+ if inputs.get("token_type_ids", None) is not None:
71
+ inputs.pop("token_type_ids")
72
+
73
+ streamer = TextIteratorStreamer(
74
+ tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True
75
+ )
76
+ generate_kwargs = dict(
77
+ inputs,
78
+ streamer=streamer,
79
+ max_new_tokens=max_new_tokens,
80
+ do_sample=True,
81
+ top_p=0.95,
82
+ top_k=50,
83
+ temperature=0.7,
84
+ )
85
+ thread = Thread(target=model.generate, kwargs=generate_kwargs)
86
+ thread.start()
87
+
88
+ partial_text = ""
89
+ for new_text in streamer:
90
+ # Process out the prompt separator
91
+ new_text = new_text.replace("<br>", "\n")
92
+ if "###" in new_text:
93
+ new_text = new_text.split("###")[0]
94
+ partial_text += new_text.strip()
95
+ history[-1][1] = partial_text
96
+ break
97
+ else:
98
+ # Filter empty trailing new lines
99
+ if new_text == "\n":
100
+ new_text = new_text.strip()
101
+ partial_text += new_text
102
+ history[-1][1] = partial_text
103
+ yield history
104
+ return partial_text
105
+
106
+
107
+ def user(user_message, history):
108
+ return "", history + [[user_message, None]]
109
+
110
+
111
+ with gr.Blocks() as demo:
112
+ gr.Markdown("# SLM-Chat by Zhang Peiyuan, StatNLP")
113
+ gr.HTML("<a href='https://huggingface.co/PY007/SLM-Alpaca-Finetuned-Preview'><code>PY007/SLM-Alpaca-Finetuned-Preview</a>")
114
+
115
+ chatbot = gr.Chatbot([], elem_id="chatbot").style(height=500)
116
+ state = gr.State([])
117
+ with gr.Row():
118
+ with gr.Column():
119
+ msg = gr.Textbox(
120
+ label="Send a message",
121
+ placeholder="Send a message",
122
+ show_label=False
123
+ ).style(container=False)
124
+ with gr.Column():
125
+ with gr.Row():
126
+ submit = gr.Button("Send")
127
+ stop = gr.Button("Stop")
128
+ clear = gr.Button("Clear History")
129
+
130
+ submit_event = msg.submit(user, inputs=[msg, chatbot], outputs=[msg, chatbot], queue=False).then(
131
+ fn=bot, inputs=[chatbot], outputs=[chatbot], queue=True)
132
+ submit_click_event = submit.click(user, inputs=[msg, chatbot], outputs=[msg, chatbot], queue=False).then(
133
+ fn=bot, inputs=[chatbot], outputs=[chatbot], queue=True)
134
+
135
+ stop.click(fn=None, inputs=None, outputs=None, cancels=[submit_event, submit_click_event], queue=False)
136
+ clear.click(lambda: None, None, [chatbot], queue=True)
137
+
138
+ demo.queue(max_size=32)
139
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ accelerate
2
+ torch
3
+ bitsandbytes
4
+ transformers>=4.28.0,<4.29.0
5
+ scipy