Spaces:
Running
on
T4
Running
on
T4
Duplicate from Sentdex/StableBeluga-7B-Chat
Browse filesCo-authored-by: Harrison Kinsley <Sentdex@users.noreply.huggingface.co>
- .gitattributes +35 -0
- README.md +14 -0
- app.py +86 -0
- requirements.txt +8 -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,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: StableBeluga 7B Chat
|
3 |
+
emoji: 🦀
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: yellow
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.39.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
+
duplicated_from: Sentdex/StableBeluga-7B-Chat
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import transformers
|
3 |
+
from torch import bfloat16
|
4 |
+
# from dotenv import load_dotenv # if you wanted to adapt this for a repo that uses auth
|
5 |
+
from threading import Thread
|
6 |
+
|
7 |
+
|
8 |
+
#HF_AUTH = os.getenv('HF_AUTH')
|
9 |
+
model_id = "stabilityai/StableBeluga-7B"
|
10 |
+
|
11 |
+
bnb_config = transformers.BitsAndBytesConfig(
|
12 |
+
load_in_4bit=True,
|
13 |
+
bnb_4bit_quant_type='nf4',
|
14 |
+
bnb_4bit_use_double_quant=True,
|
15 |
+
bnb_4bit_compute_dtype=bfloat16
|
16 |
+
)
|
17 |
+
model_config = transformers.AutoConfig.from_pretrained(
|
18 |
+
model_id,
|
19 |
+
#use_auth_token=HF_AUTH
|
20 |
+
)
|
21 |
+
|
22 |
+
model = transformers.AutoModelForCausalLM.from_pretrained(
|
23 |
+
model_id,
|
24 |
+
trust_remote_code=True,
|
25 |
+
config=model_config,
|
26 |
+
quantization_config=bnb_config,
|
27 |
+
device_map='auto',
|
28 |
+
#use_auth_token=HF_AUTH
|
29 |
+
)
|
30 |
+
|
31 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(
|
32 |
+
model_id,
|
33 |
+
#use_auth_token=HF_AUTH
|
34 |
+
)
|
35 |
+
|
36 |
+
|
37 |
+
DESCRIPTION = """
|
38 |
+
# Stable Beluga 7B Chat
|
39 |
+
This is a streaming Chat Interface implementation of [StableBeluga-7B](https://huggingface.co/stabilityai/StableBeluga-7B)
|
40 |
+
You can modify the system prompt, which can be quite fun. For example, you can try something like "You are a mean AI. Phrase all replies as insults" for a good laugh.
|
41 |
+
|
42 |
+
Sometimes the model doesn't appropriately hit its stop token. Feel free to hit "stop" and "retry" if this happens to you. Or PR a fix to stop the stream if the tokens for User: get hit or something.
|
43 |
+
"""
|
44 |
+
|
45 |
+
|
46 |
+
def prompt_build(system_prompt, user_inp, hist):
|
47 |
+
prompt = f"""### System:\n{system_prompt}\n\n"""
|
48 |
+
|
49 |
+
for pair in hist:
|
50 |
+
prompt += f"""### User:\n{pair[0]}\n\n### Assistant:\n{pair[1]}\n\n"""
|
51 |
+
|
52 |
+
prompt += f"""### User:\n{user_inp}\n\n### Assistant:"""
|
53 |
+
return prompt
|
54 |
+
|
55 |
+
def chat(user_input, history, system_prompt):
|
56 |
+
|
57 |
+
prompt = prompt_build(system_prompt, user_input, history)
|
58 |
+
model_inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
|
59 |
+
|
60 |
+
streamer = transformers.TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
61 |
+
|
62 |
+
generate_kwargs = dict(
|
63 |
+
model_inputs,
|
64 |
+
streamer=streamer,
|
65 |
+
max_new_tokens=2048,
|
66 |
+
do_sample=True,
|
67 |
+
top_p=0.95,
|
68 |
+
temperature=0.8,
|
69 |
+
top_k=50
|
70 |
+
)
|
71 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
72 |
+
t.start()
|
73 |
+
|
74 |
+
model_output = ""
|
75 |
+
for new_text in streamer:
|
76 |
+
model_output += new_text
|
77 |
+
yield model_output
|
78 |
+
return model_output
|
79 |
+
|
80 |
+
|
81 |
+
with gr.Blocks() as demo:
|
82 |
+
gr.Markdown(DESCRIPTION)
|
83 |
+
system_prompt = gr.Textbox("You are helpful AI.", label="System Prompt")
|
84 |
+
chatbot = gr.ChatInterface(fn=chat, additional_inputs=[system_prompt])
|
85 |
+
|
86 |
+
demo.queue().launch()
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
gradio
|
3 |
+
torch
|
4 |
+
bitsandbytes
|
5 |
+
einops
|
6 |
+
accelerate
|
7 |
+
xformers
|
8 |
+
scipy
|