Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
import torch
|
5 |
-
from threading import Thread
|
6 |
-
from typing import Iterator
|
7 |
|
8 |
model_name = "rubenroy/Zurich-14B-GCv2-5m"
|
9 |
-
MAX_INPUT_TOKEN_LENGTH = 4096
|
10 |
-
|
11 |
model = AutoModelForCausalLM.from_pretrained(
|
12 |
model_name,
|
13 |
torch_dtype=torch.bfloat16,
|
@@ -16,43 +12,31 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
16 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
17 |
|
18 |
@spaces.GPU
|
19 |
-
def generate(message
|
20 |
messages = [
|
21 |
-
{"role": "system", "content": "You are a
|
|
|
22 |
]
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
messages.append({"role": "user", "content": message})
|
28 |
-
|
29 |
-
input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt")
|
30 |
-
|
31 |
-
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
32 |
-
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
33 |
-
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
34 |
-
|
35 |
-
input_ids = input_ids.to(model.device)
|
36 |
-
|
37 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
38 |
-
generate_kwargs = dict(
|
39 |
-
{"input_ids": input_ids},
|
40 |
-
streamer=streamer,
|
41 |
-
max_new_tokens=max_new_tokens,
|
42 |
-
do_sample=True if float(temperature) > 0 else False,
|
43 |
-
top_p=top_p,
|
44 |
-
top_k=top_k,
|
45 |
-
temperature=temperature,
|
46 |
-
repetition_penalty=repetition_penalty
|
47 |
)
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
TITLE_HTML = """
|
58 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
@@ -85,12 +69,85 @@ TITLE_HTML = """
|
|
85 |
backdrop-filter: blur(10px);
|
86 |
transition: all 0.3s ease;
|
87 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
</style>
|
89 |
|
90 |
<div style="background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%); padding: 1.5rem; border-radius: 1.5rem; text-align: center; margin: 1rem auto; max-width: 1200px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);">
|
91 |
<div style="margin-bottom: 1.5rem;">
|
92 |
-
<
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
</div>
|
95 |
</div>
|
96 |
"""
|
@@ -99,7 +156,7 @@ examples = [
|
|
99 |
["Explain quantum computing in simple terms"],
|
100 |
["Write a short story about a time traveler"],
|
101 |
["Explain the process of photosynthesis"],
|
102 |
-
["Tell me an
|
103 |
]
|
104 |
|
105 |
with gr.Blocks() as demo:
|
@@ -108,16 +165,62 @@ with gr.Blocks() as demo:
|
|
108 |
with gr.Accordion("Generation Settings", open=False):
|
109 |
with gr.Row():
|
110 |
with gr.Column():
|
111 |
-
temperature = gr.Slider(
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
with gr.Column():
|
115 |
-
max_new_tokens = gr.Slider(
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
chatbot = gr.ChatInterface(
|
119 |
fn=generate,
|
120 |
-
additional_inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
examples=examples
|
122 |
)
|
123 |
|
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
import torch
|
|
|
|
|
5 |
|
6 |
model_name = "rubenroy/Zurich-14B-GCv2-5m"
|
|
|
|
|
7 |
model = AutoModelForCausalLM.from_pretrained(
|
8 |
model_name,
|
9 |
torch_dtype=torch.bfloat16,
|
|
|
12 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
|
14 |
@spaces.GPU
|
15 |
+
def generate(message, chat_history, temperature=0.7, top_p=0.9, top_k=50, max_new_tokens=512, repetition_penalty=1.1):
|
16 |
messages = [
|
17 |
+
{"role": "system", "content": "You are a helpul assistant named Zurich, a 14 billion parameter Large Language model, you were fine-tuned and trained by Ruben Roy. You have been trained with the GammaCorpus v2 dataset, a dataset filled with structured and filtered multi-turn conversations, this was also made by Ruben Roy."}, # Attribution to Qwen is not included to prevent hallucinations.
|
18 |
+
{"role": "user", "content": message}
|
19 |
]
|
20 |
+
text = tokenizer.apply_chat_template(
|
21 |
+
messages,
|
22 |
+
tokenize=False,
|
23 |
+
add_generation_prompt=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
)
|
25 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
26 |
+
generated_ids = model.generate(
|
27 |
+
**model_inputs,
|
28 |
+
temperature=float(temperature),
|
29 |
+
top_p=float(top_p),
|
30 |
+
top_k=int(top_k),
|
31 |
+
max_new_tokens=int(max_new_tokens),
|
32 |
+
repetition_penalty=float(repetition_penalty),
|
33 |
+
do_sample=True if float(temperature) > 0 else False
|
34 |
+
)
|
35 |
+
generated_ids = [
|
36 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
37 |
+
]
|
38 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
39 |
+
return response
|
40 |
|
41 |
TITLE_HTML = """
|
42 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
|
|
69 |
backdrop-filter: blur(10px);
|
70 |
transition: all 0.3s ease;
|
71 |
}
|
72 |
+
.info-link {
|
73 |
+
color: #60a5fa;
|
74 |
+
text-decoration: none;
|
75 |
+
transition: color 0.2s ease;
|
76 |
+
}
|
77 |
+
.info-link:hover {
|
78 |
+
color: #93c5fd;
|
79 |
+
text-decoration: underline;
|
80 |
+
}
|
81 |
+
.info-section {
|
82 |
+
margin-top: 0.5rem;
|
83 |
+
font-size: 0.9rem;
|
84 |
+
color: #94a3b8;
|
85 |
+
}
|
86 |
+
.settings-section {
|
87 |
+
background: rgba(255, 255, 255, 0.05);
|
88 |
+
padding: 1.5rem;
|
89 |
+
border-radius: 1rem;
|
90 |
+
margin: 1.5rem auto;
|
91 |
+
border: 1px solid rgba(255, 255, 255, 0.1);
|
92 |
+
max-width: 800px;
|
93 |
+
}
|
94 |
+
.settings-title {
|
95 |
+
color: #e2e8f0;
|
96 |
+
font-size: 1.25rem;
|
97 |
+
font-weight: 600;
|
98 |
+
margin-bottom: 1rem;
|
99 |
+
display: flex;
|
100 |
+
align-items: center;
|
101 |
+
gap: 0.7rem;
|
102 |
+
}
|
103 |
+
.parameter-info {
|
104 |
+
color: #94a3b8;
|
105 |
+
font-size: 0.8rem;
|
106 |
+
margin-top: 0.25rem;
|
107 |
+
}
|
108 |
</style>
|
109 |
|
110 |
<div style="background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%); padding: 1.5rem; border-radius: 1.5rem; text-align: center; margin: 1rem auto; max-width: 1200px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);">
|
111 |
<div style="margin-bottom: 1.5rem;">
|
112 |
+
<div style="display: flex; align-items: center; justify-content: center; gap: 1rem;">
|
113 |
+
<h1 style="font-size: 2.5rem; font-weight: 800; margin: 0; background: linear-gradient(135deg, #60a5fa 0%, #93c5fd 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">Zurich</h1>
|
114 |
+
<div style="width: 2px; height: 2.5rem; background: linear-gradient(180deg, #3b82f6 0%, #60a5fa 100%);"></div>
|
115 |
+
<p style="font-size: 1.25rem; color: #94a3b8; margin: 0;">GammaCorpus v2-5m</p>
|
116 |
+
</div>
|
117 |
+
<div class="info-section">
|
118 |
+
<span>Fine-tuned from <a href="https://huggingface.co/Qwen/Qwen2.5-14B-Instruct" class="info-link">Qwen 2.5 14B Instruct</a> | Model: <a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-5m" class="info-link">Zurich-14B-GCv2-5m</a> | Training Dataset: <a href="https://huggingface.co/datasets/rubenroy/GammaCorpus-v2-5m" class="info-link">GammaCorpus v2 5m</a></span>
|
119 |
+
</div>
|
120 |
+
</div>
|
121 |
+
|
122 |
+
<div style="display: flex; gap: 1.5rem; justify-content: center;">
|
123 |
+
<div class="model-section">
|
124 |
+
<h2 style="font-size: 1.25rem; color: #e2e8f0; margin-bottom: 1.4rem; margin-top: 1px; font-weight: 600; display: flex; align-items: center; justify-content: center; gap: 0.7rem;">
|
125 |
+
<i class="fas fa-brain"></i>
|
126 |
+
7B Models
|
127 |
+
</h2>
|
128 |
+
<div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.75rem;">
|
129 |
+
<a href="https://huggingface.co/rubenroy/Zurich-7B-GCv2-5m" class="model-btn">Zurich 7B GCv2 5m</a>
|
130 |
+
<a href="https://huggingface.co/rubenroy/Zurich-7B-GCv2-1m" class="model-btn">Zurich 7B GCv2 1m</a>
|
131 |
+
<a href="https://huggingface.co/rubenroy/Zurich-7B-GCv2-500k" class="model-btn">Zurich 7B GCv2 500k</a>
|
132 |
+
<a href="https://huggingface.co/rubenroy/Zurich-7B-GCv2-100k" class="model-btn">Zurich 7B GCv2 100k</a>
|
133 |
+
<a href="https://huggingface.co/rubenroy/Zurich-7B-GCv2-50k" class="model-btn">Zurich 7B GCv2 50k</a>
|
134 |
+
<a href="https://huggingface.co/rubenroy/Zurich-7B-GCv2-10k" class="model-btn">Zurich 7B GCv2 10k</a>
|
135 |
+
</div>
|
136 |
+
</div>
|
137 |
+
<div class="model-section">
|
138 |
+
<h2 style="font-size: 1.25rem; color: #e2e8f0; margin-bottom: 1.4rem; margin-top: 1px; font-weight: 600; display: flex; align-items: center; justify-content: center; gap: 0.7rem;">
|
139 |
+
<i class="fas fa-rocket"></i>
|
140 |
+
14B Models
|
141 |
+
</h2>
|
142 |
+
<div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.75rem;">
|
143 |
+
<a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-5m" class="model-btn">Zurich 14B GCv2 5m</a>
|
144 |
+
<a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-1m" class="model-btn">Zurich 14B GCv2 1m</a>
|
145 |
+
<a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-500k" class="model-btn">Zurich 14B GCv2 500k</a>
|
146 |
+
<a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-100k" class="model-btn">Zurich 14B GCv2 100k</a>
|
147 |
+
<a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-50k" class="model-btn">Zurich 14B GCv2 50k</a>
|
148 |
+
<a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-10k" class="model-btn">Zurich 14B GCv2 10k</a>
|
149 |
+
</div>
|
150 |
+
</div>
|
151 |
</div>
|
152 |
</div>
|
153 |
"""
|
|
|
156 |
["Explain quantum computing in simple terms"],
|
157 |
["Write a short story about a time traveler"],
|
158 |
["Explain the process of photosynthesis"],
|
159 |
+
["Tell me an intersting fact about Palm trees"]
|
160 |
]
|
161 |
|
162 |
with gr.Blocks() as demo:
|
|
|
165 |
with gr.Accordion("Generation Settings", open=False):
|
166 |
with gr.Row():
|
167 |
with gr.Column():
|
168 |
+
temperature = gr.Slider(
|
169 |
+
minimum=0.0,
|
170 |
+
maximum=2.0,
|
171 |
+
value=0.7,
|
172 |
+
step=0.1,
|
173 |
+
label="Temperature",
|
174 |
+
info="Higher values make the output more random, lower values make it more deterministic",
|
175 |
+
interactive=True
|
176 |
+
)
|
177 |
+
top_p = gr.Slider(
|
178 |
+
minimum=0.0,
|
179 |
+
maximum=1.0,
|
180 |
+
value=0.9,
|
181 |
+
step=0.05,
|
182 |
+
label="Top P",
|
183 |
+
info="Controls the cumulative probability threshold for nucleus sampling",
|
184 |
+
interactive=True
|
185 |
+
)
|
186 |
+
top_k = gr.Slider(
|
187 |
+
minimum=1,
|
188 |
+
maximum=100,
|
189 |
+
value=50,
|
190 |
+
step=1,
|
191 |
+
label="Top K",
|
192 |
+
info="Limits the number of tokens to consider for each generation step",
|
193 |
+
interactive=True
|
194 |
+
)
|
195 |
with gr.Column():
|
196 |
+
max_new_tokens = gr.Slider(
|
197 |
+
minimum=1,
|
198 |
+
maximum=2048,
|
199 |
+
value=512,
|
200 |
+
step=1,
|
201 |
+
label="Max New Tokens",
|
202 |
+
info="Maximum number of tokens to generate in the response",
|
203 |
+
interactive=True
|
204 |
+
)
|
205 |
+
repetition_penalty = gr.Slider(
|
206 |
+
minimum=1.0,
|
207 |
+
maximum=2.0,
|
208 |
+
value=1.1,
|
209 |
+
step=0.1,
|
210 |
+
label="Repetition Penalty",
|
211 |
+
info="Higher values stop the model from repeating the same info",
|
212 |
+
interactive=True
|
213 |
+
)
|
214 |
+
|
215 |
chatbot = gr.ChatInterface(
|
216 |
fn=generate,
|
217 |
+
additional_inputs=[
|
218 |
+
temperature,
|
219 |
+
top_p,
|
220 |
+
top_k,
|
221 |
+
max_new_tokens,
|
222 |
+
repetition_penalty
|
223 |
+
],
|
224 |
examples=examples
|
225 |
)
|
226 |
|