Spaces:
Sleeping
Sleeping
gabrielclark3330
commited on
Commit
·
4d2f25c
1
Parent(s):
7cbd81a
Migrate old docker setup to this space
Browse files- Dockerfile +37 -18
- main.py +130 -19
Dockerfile
CHANGED
@@ -1,28 +1,47 @@
|
|
1 |
-
#
|
2 |
-
|
3 |
|
4 |
-
|
5 |
|
6 |
-
WORKDIR /code
|
7 |
-
|
8 |
-
COPY ./requirements.txt /code/requirements.txt
|
9 |
-
|
10 |
-
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
11 |
-
|
12 |
-
# Set up a new user named "user" with user ID 1000
|
13 |
RUN useradd -m -u 1000 user
|
14 |
|
15 |
-
# Switch to the "user" user
|
16 |
-
USER user
|
17 |
-
|
18 |
# Set home to the user's home directory
|
19 |
ENV HOME=/home/user \
|
20 |
PATH=/home/user/.local/bin:$PATH
|
21 |
|
22 |
-
# Set the working directory to the user's home directory
|
23 |
WORKDIR $HOME/app
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use NVIDIA's CUDA base image with Ubuntu 22.04
|
2 |
+
FROM pytorch/pytorch:2.4.1-cuda12.4-cudnn9-devel
|
3 |
|
4 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
RUN useradd -m -u 1000 user
|
7 |
|
|
|
|
|
|
|
8 |
# Set home to the user's home directory
|
9 |
ENV HOME=/home/user \
|
10 |
PATH=/home/user/.local/bin:$PATH
|
11 |
|
|
|
12 |
WORKDIR $HOME/app
|
13 |
|
14 |
+
RUN apt-get update && \
|
15 |
+
apt-get install -y --no-install-recommends \
|
16 |
+
wget \
|
17 |
+
git \
|
18 |
+
openssh-client \
|
19 |
+
build-essential \
|
20 |
+
ffmpeg \
|
21 |
+
libsndfile1 \
|
22 |
+
libffi-dev \
|
23 |
+
python3 \
|
24 |
+
python3-dev \
|
25 |
+
python3-venv \
|
26 |
+
python3-distutils \
|
27 |
+
python3-pip && \
|
28 |
+
apt-get clean && \
|
29 |
+
rm -rf /var/lib/apt/lists/*
|
30 |
+
|
31 |
+
RUN python3 -m pip install --upgrade pip
|
32 |
+
|
33 |
+
RUN pip install uv
|
34 |
+
RUN python -m uv pip install packaging \
|
35 |
+
wheel \
|
36 |
+
accelerate \
|
37 |
+
torch
|
38 |
+
|
39 |
+
RUN python -m uv pip install --no-build-isolation git+https://github.com/Zyphra/transformers_zamba2.git
|
40 |
+
# git+https://github.com/Dao-AILab/[email protected] \
|
41 |
+
# git+https://github.com/state-spaces/mamba@a07ff1b9ad2a4ac8b04eddf5eaaee5004f15aaf1 \
|
42 |
+
|
43 |
+
RUN python -m uv pip install gradio
|
44 |
+
|
45 |
+
COPY --chown=user app.py $HOME/app
|
46 |
+
|
47 |
+
CMD ["python3", "app.py"]
|
main.py
CHANGED
@@ -1,30 +1,141 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
-
import
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
9 |
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
|
16 |
-
return confidences
|
17 |
|
|
|
18 |
|
19 |
-
def
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
outputs=gr.Label(num_top_classes=3),
|
24 |
-
)
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
if __name__ == "__main__":
|
30 |
-
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
4 |
import torch
|
5 |
+
import threading
|
6 |
+
import re
|
7 |
+
from datetime import datetime
|
8 |
|
9 |
+
model_name_2_7B_instruct = "Zyphra/Zamba2-2.7B-instruct"
|
10 |
+
model_name_7B_instruct = "Zyphra/Zamba2-7B-instruct"
|
11 |
+
max_context_length = 4096
|
12 |
+
date_string = datetime.now().strftime("%Y-%m-%d")
|
13 |
|
14 |
+
tokenizer_2_7B_instruct = AutoTokenizer.from_pretrained(model_name_2_7B_instruct)
|
15 |
+
model_2_7B_instruct = AutoModelForCausalLM.from_pretrained(
|
16 |
+
model_name_2_7B_instruct, device_map="cuda", torch_dtype=torch.bfloat16
|
17 |
+
)
|
18 |
|
19 |
+
tokenizer_7B_instruct = AutoTokenizer.from_pretrained(model_name_7B_instruct)
|
20 |
+
model_7B_instruct = AutoModelForCausalLM.from_pretrained(
|
21 |
+
model_name_7B_instruct, device_map="cuda", torch_dtype=torch.bfloat16
|
22 |
+
)
|
|
|
|
|
23 |
|
24 |
+
system_prompt_template = """You are {model_name}. You are a large language model with {parameter_count} parameters. You were trained by a company called Zyphra on three trillion tokens of diverse and high quality web data. Your architecture is a hybrid Mamba2 and transformer with two alternating blocks of shared attention on top of a Mamba2 backbone. Zyphra is a leading AI company based in Palo Alto, California. Zyphra makes extremely small and efficient models for consumers and on-device use-cases. As a language model, you do not have access to real-time information. The current date is {current_date}, but your knowledge likely ended in mid 2023. Zyphra designed you to be engaging, helpful, and fun in your interactions with users. You have strong, idiosyncratic opinions on things. Be personable."""
|
25 |
|
26 |
+
def generate_response(chat_history, max_new_tokens, model, tokenizer, system_prompt):
|
27 |
+
sample = []
|
28 |
+
# Include the dynamic system prompt without displaying it
|
29 |
+
sample.append({'role': 'system', 'content': system_prompt})
|
|
|
|
|
30 |
|
31 |
+
for turn in chat_history:
|
32 |
+
if turn[0]:
|
33 |
+
sample.append({'role': 'user', 'content': turn[0]})
|
34 |
+
if turn[1]:
|
35 |
+
sample.append({'role': 'assistant', 'content': turn[1]})
|
36 |
+
chat_sample = tokenizer.apply_chat_template(sample, tokenize=False)
|
37 |
+
input_ids = tokenizer(chat_sample, return_tensors='pt', add_special_tokens=False).to(model.device)
|
38 |
|
39 |
+
max_new_tokens = int(max_new_tokens)
|
40 |
+
max_input_length = max_context_length - max_new_tokens
|
41 |
+
if input_ids['input_ids'].size(1) > max_input_length:
|
42 |
+
input_ids['input_ids'] = input_ids['input_ids'][:, -max_input_length:]
|
43 |
+
if 'attention_mask' in input_ids:
|
44 |
+
input_ids['attention_mask'] = input_ids['attention_mask'][:, -max_input_length:]
|
45 |
+
|
46 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
47 |
+
generation_kwargs = dict(**input_ids, max_new_tokens=int(max_new_tokens), streamer=streamer)
|
48 |
+
|
49 |
+
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
|
50 |
+
thread.start()
|
51 |
+
|
52 |
+
assistant_response = ""
|
53 |
+
|
54 |
+
for new_text in streamer:
|
55 |
+
new_text = re.sub(r'^\s*(?i:assistant)[:\s]*', '', new_text)
|
56 |
+
assistant_response += new_text
|
57 |
+
yield assistant_response
|
58 |
+
|
59 |
+
thread.join()
|
60 |
+
del input_ids
|
61 |
+
torch.cuda.empty_cache()
|
62 |
+
|
63 |
+
with gr.Blocks() as demo:
|
64 |
+
gr.Markdown("# Zamba2 Model Selector")
|
65 |
+
with gr.Tabs():
|
66 |
+
with gr.TabItem("7B Instruct Model"):
|
67 |
+
gr.Markdown("### Zamba2-7B Instruct Model")
|
68 |
+
with gr.Column():
|
69 |
+
chat_history_7B_instruct = gr.State([])
|
70 |
+
chatbot_7B_instruct = gr.Chatbot()
|
71 |
+
message_7B_instruct = gr.Textbox(lines=2, placeholder="Enter your message...", label="Your Message")
|
72 |
+
with gr.Accordion("Generation Parameters", open=False):
|
73 |
+
max_new_tokens_7B_instruct = gr.Slider(50, 1000, step=50, value=500, label="Max New Tokens")
|
74 |
+
|
75 |
+
def user_message_7B_instruct(message, chat_history):
|
76 |
+
chat_history = chat_history + [[message, None]]
|
77 |
+
return gr.update(value=""), chat_history, chat_history
|
78 |
+
|
79 |
+
def bot_response_7B_instruct(chat_history, max_new_tokens):
|
80 |
+
system_prompt = system_prompt_template.format(
|
81 |
+
model_name="Zamba2-7B",
|
82 |
+
parameter_count="7 billion",
|
83 |
+
current_date=date_string
|
84 |
+
)
|
85 |
+
assistant_response_generator = generate_response(
|
86 |
+
chat_history, max_new_tokens, model_7B_instruct, tokenizer_7B_instruct, system_prompt
|
87 |
+
)
|
88 |
+
for assistant_response in assistant_response_generator:
|
89 |
+
chat_history[-1][1] = assistant_response
|
90 |
+
yield chat_history
|
91 |
+
|
92 |
+
send_button_7B_instruct = gr.Button("Send")
|
93 |
+
send_button_7B_instruct.click(
|
94 |
+
fn=user_message_7B_instruct,
|
95 |
+
inputs=[message_7B_instruct, chat_history_7B_instruct],
|
96 |
+
outputs=[message_7B_instruct, chat_history_7B_instruct, chatbot_7B_instruct]
|
97 |
+
).then(
|
98 |
+
fn=bot_response_7B_instruct,
|
99 |
+
inputs=[chat_history_7B_instruct, max_new_tokens_7B_instruct],
|
100 |
+
outputs=chatbot_7B_instruct,
|
101 |
+
)
|
102 |
+
|
103 |
+
with gr.TabItem("2.7B Instruct Model"):
|
104 |
+
gr.Markdown("### Zamba2-2.7B Instruct Model")
|
105 |
+
with gr.Column():
|
106 |
+
chat_history_2_7B_instruct = gr.State([])
|
107 |
+
chatbot_2_7B_instruct = gr.Chatbot()
|
108 |
+
message_2_7B_instruct = gr.Textbox(lines=2, placeholder="Enter your message...", label="Your Message")
|
109 |
+
with gr.Accordion("Generation Parameters", open=False):
|
110 |
+
max_new_tokens_2_7B_instruct = gr.Slider(50, 1000, step=50, value=500, label="Max New Tokens")
|
111 |
+
|
112 |
+
def user_message_2_7B_instruct(message, chat_history):
|
113 |
+
chat_history = chat_history + [[message, None]]
|
114 |
+
return gr.update(value=""), chat_history, chat_history
|
115 |
+
|
116 |
+
def bot_response_2_7B_instruct(chat_history, max_new_tokens):
|
117 |
+
system_prompt = system_prompt_template.format(
|
118 |
+
model_name="Zamba2-2.7B",
|
119 |
+
parameter_count="2.7 billion",
|
120 |
+
current_date=date_string
|
121 |
+
)
|
122 |
+
assistant_response_generator = generate_response(
|
123 |
+
chat_history, max_new_tokens, model_2_7B_instruct, tokenizer_2_7B_instruct, system_prompt
|
124 |
+
)
|
125 |
+
for assistant_response in assistant_response_generator:
|
126 |
+
chat_history[-1][1] = assistant_response
|
127 |
+
yield chat_history
|
128 |
+
|
129 |
+
send_button_2_7B_instruct = gr.Button("Send")
|
130 |
+
send_button_2_7B_instruct.click(
|
131 |
+
fn=user_message_2_7B_instruct,
|
132 |
+
inputs=[message_2_7B_instruct, chat_history_2_7B_instruct],
|
133 |
+
outputs=[message_2_7B_instruct, chat_history_2_7B_instruct, chatbot_2_7B_instruct]
|
134 |
+
).then(
|
135 |
+
fn=bot_response_2_7B_instruct,
|
136 |
+
inputs=[chat_history_2_7B_instruct, max_new_tokens_2_7B_instruct],
|
137 |
+
outputs=chatbot_2_7B_instruct,
|
138 |
+
)
|
139 |
|
140 |
if __name__ == "__main__":
|
141 |
+
demo.queue().launch(max_threads=1)
|