Update app.py
Browse files
app.py
CHANGED
|
@@ -3,85 +3,152 @@ import threading
|
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
MODEL_ID = "devoppro/Kimi-K3"
|
|
|
|
| 8 |
MODEL_DIR = Path("/data/models/devoppro-Kimi-K3")
|
| 9 |
|
| 10 |
os.environ.setdefault("HF_HOME", "/data/huggingface")
|
| 11 |
-
os.environ.setdefault(
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
def load_model():
|
| 21 |
-
global _model, _tokenizer
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return _model, _tokenizer
|
| 29 |
|
| 30 |
-
|
| 31 |
-
raise RuntimeError("HF_TOKEN is missing. Add it in Space Settings → Secrets.")
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
from huggingface_hub import snapshot_download
|
| 38 |
from airllm import AutoModel
|
| 39 |
|
| 40 |
-
MODEL_DIR.mkdir(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
print(f"Downloading/checking {MODEL_ID}...", flush=True)
|
| 43 |
snapshot_download(
|
| 44 |
repo_id=MODEL_ID,
|
| 45 |
local_dir=str(MODEL_DIR),
|
| 46 |
-
token=
|
| 47 |
resume_download=True,
|
| 48 |
)
|
| 49 |
|
| 50 |
-
print(
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
str(MODEL_DIR),
|
| 53 |
trust_remote_code=True,
|
| 54 |
)
|
| 55 |
-
_tokenizer = _model.tokenizer
|
| 56 |
-
print("Kimi K3 initialized.", flush=True)
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
def build_messages(history, user_text):
|
| 62 |
messages = []
|
|
|
|
| 63 |
for item in history or []:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
return messages
|
| 72 |
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
if not message or not message.strip():
|
| 78 |
-
|
| 79 |
-
return
|
| 80 |
|
| 81 |
try:
|
|
|
|
| 82 |
model, tokenizer = load_model()
|
| 83 |
|
| 84 |
-
messages = build_messages(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
prompt = tokenizer.apply_chat_template(
|
| 86 |
messages,
|
| 87 |
tokenize=False,
|
|
@@ -95,59 +162,111 @@ def generate_reply(message, history, max_new_tokens, temperature):
|
|
| 95 |
truncation=True,
|
| 96 |
)
|
| 97 |
|
| 98 |
-
input_ids = inputs[
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
if attention_mask is not None:
|
| 101 |
-
attention_mask = attention_mask.cuda()
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
}
|
| 108 |
|
| 109 |
if attention_mask is not None:
|
| 110 |
-
kwargs["attention_mask"] = attention_mask
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
generated = output[0][input_ids.shape[-1]:]
|
| 115 |
answer = tokenizer.decode(
|
| 116 |
-
|
| 117 |
skip_special_tokens=True,
|
| 118 |
).strip()
|
| 119 |
|
| 120 |
-
new_history =
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
]
|
| 124 |
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
except Exception as exc:
|
| 128 |
error = (
|
| 129 |
-
"
|
| 130 |
-
f"`{type(
|
| 131 |
-
"Check the Space logs. Initial K3 setup can take a very long time."
|
| 132 |
)
|
| 133 |
|
| 134 |
-
new_history =
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
]
|
| 138 |
-
|
|
|
|
| 139 |
|
| 140 |
|
| 141 |
def clear_chat():
|
|
|
|
| 142 |
return [], ""
|
| 143 |
|
| 144 |
|
| 145 |
-
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
| 146 |
gr.Markdown(
|
| 147 |
-
"
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
)
|
| 152 |
|
| 153 |
chatbot = gr.Chatbot(
|
|
@@ -157,35 +276,78 @@ with gr.Blocks(title="Kimi K3 — AirLLM") as demo:
|
|
| 157 |
)
|
| 158 |
|
| 159 |
with gr.Row():
|
|
|
|
| 160 |
message = gr.Textbox(
|
| 161 |
label="Message",
|
| 162 |
-
placeholder=
|
|
|
|
|
|
|
| 163 |
lines=3,
|
| 164 |
scale=5,
|
| 165 |
)
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
with gr.Row():
|
|
|
|
| 169 |
max_tokens = gr.Slider(
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
)
|
|
|
|
| 172 |
temperature = gr.Slider(
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
)
|
|
|
|
| 175 |
clear = gr.Button("Clear")
|
| 176 |
|
| 177 |
send.click(
|
| 178 |
-
|
| 179 |
-
inputs=[
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
)
|
|
|
|
| 182 |
message.submit(
|
| 183 |
-
|
| 184 |
-
inputs=[
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
)
|
| 187 |
-
clear.click(clear_chat, outputs=[chatbot, message])
|
| 188 |
|
| 189 |
|
| 190 |
if __name__ == "__main__":
|
| 191 |
-
|
|
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
+
import spaces
|
| 7 |
|
| 8 |
MODEL_ID = "devoppro/Kimi-K3"
|
| 9 |
+
|
| 10 |
MODEL_DIR = Path("/data/models/devoppro-Kimi-K3")
|
| 11 |
|
| 12 |
os.environ.setdefault("HF_HOME", "/data/huggingface")
|
| 13 |
+
os.environ.setdefault(
|
| 14 |
+
"HUGGINGFACE_HUB_CACHE",
|
| 15 |
+
"/data/huggingface/hub",
|
| 16 |
+
)
|
| 17 |
+
os.environ.setdefault(
|
| 18 |
+
"TRANSFORMERS_CACHE",
|
| 19 |
+
"/data/huggingface/transformers",
|
| 20 |
+
)
|
| 21 |
+
os.environ.setdefault(
|
| 22 |
+
"AIRLLM_CACHE_DIR",
|
| 23 |
+
"/data/airllm",
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
model = None
|
| 27 |
+
tokenizer = None
|
| 28 |
+
model_lock = threading.Lock()
|
| 29 |
|
| 30 |
|
| 31 |
def load_model():
|
|
|
|
| 32 |
|
| 33 |
+
global model
|
| 34 |
+
global tokenizer
|
| 35 |
+
|
| 36 |
+
if model is not None:
|
| 37 |
+
return model, tokenizer
|
| 38 |
+
|
| 39 |
+
with model_lock:
|
| 40 |
|
| 41 |
+
if model is not None:
|
| 42 |
+
return model, tokenizer
|
|
|
|
| 43 |
|
| 44 |
+
token = os.getenv("HF_TOKEN")
|
|
|
|
| 45 |
|
| 46 |
+
if not token:
|
| 47 |
+
raise RuntimeError(
|
| 48 |
+
"HF_TOKEN is missing. "
|
| 49 |
+
"Add it under Space Settings → Secrets."
|
| 50 |
+
)
|
| 51 |
|
| 52 |
from huggingface_hub import snapshot_download
|
| 53 |
from airllm import AutoModel
|
| 54 |
|
| 55 |
+
MODEL_DIR.mkdir(
|
| 56 |
+
parents=True,
|
| 57 |
+
exist_ok=True,
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
print(
|
| 61 |
+
f"Preparing {MODEL_ID}...",
|
| 62 |
+
flush=True,
|
| 63 |
+
)
|
| 64 |
|
|
|
|
| 65 |
snapshot_download(
|
| 66 |
repo_id=MODEL_ID,
|
| 67 |
local_dir=str(MODEL_DIR),
|
| 68 |
+
token=token,
|
| 69 |
resume_download=True,
|
| 70 |
)
|
| 71 |
|
| 72 |
+
print(
|
| 73 |
+
"Loading Kimi K3 with AirLLM...",
|
| 74 |
+
flush=True,
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
model = AutoModel.from_pretrained(
|
| 78 |
str(MODEL_DIR),
|
| 79 |
trust_remote_code=True,
|
| 80 |
)
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
tokenizer = model.tokenizer
|
| 83 |
+
|
| 84 |
+
print(
|
| 85 |
+
"Kimi K3 loaded.",
|
| 86 |
+
flush=True,
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
return model, tokenizer
|
| 90 |
+
|
| 91 |
|
| 92 |
+
def build_messages(
|
| 93 |
+
history,
|
| 94 |
+
user_message,
|
| 95 |
+
):
|
| 96 |
|
|
|
|
| 97 |
messages = []
|
| 98 |
+
|
| 99 |
for item in history or []:
|
| 100 |
+
|
| 101 |
+
if not isinstance(item, dict):
|
| 102 |
+
continue
|
| 103 |
+
|
| 104 |
+
role = item.get("role")
|
| 105 |
+
content = item.get("content")
|
| 106 |
+
|
| 107 |
+
if role in (
|
| 108 |
+
"user",
|
| 109 |
+
"assistant",
|
| 110 |
+
) and content:
|
| 111 |
+
|
| 112 |
+
messages.append(
|
| 113 |
+
{
|
| 114 |
+
"role": role,
|
| 115 |
+
"content": content,
|
| 116 |
+
}
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
messages.append(
|
| 120 |
+
{
|
| 121 |
+
"role": "user",
|
| 122 |
+
"content": user_message,
|
| 123 |
+
}
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
return messages
|
| 127 |
|
| 128 |
|
| 129 |
+
@spaces.GPU(
|
| 130 |
+
size="xlarge",
|
| 131 |
+
)
|
| 132 |
+
def generate(
|
| 133 |
+
message,
|
| 134 |
+
history,
|
| 135 |
+
max_new_tokens,
|
| 136 |
+
temperature,
|
| 137 |
+
):
|
| 138 |
|
| 139 |
if not message or not message.strip():
|
| 140 |
+
|
| 141 |
+
return history or [], ""
|
| 142 |
|
| 143 |
try:
|
| 144 |
+
|
| 145 |
model, tokenizer = load_model()
|
| 146 |
|
| 147 |
+
messages = build_messages(
|
| 148 |
+
history,
|
| 149 |
+
message,
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
prompt = tokenizer.apply_chat_template(
|
| 153 |
messages,
|
| 154 |
tokenize=False,
|
|
|
|
| 162 |
truncation=True,
|
| 163 |
)
|
| 164 |
|
| 165 |
+
input_ids = inputs[
|
| 166 |
+
"input_ids"
|
| 167 |
+
].cuda()
|
| 168 |
+
|
| 169 |
+
attention_mask = inputs.get(
|
| 170 |
+
"attention_mask"
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
if attention_mask is not None:
|
|
|
|
| 174 |
|
| 175 |
+
attention_mask = (
|
| 176 |
+
attention_mask.cuda()
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
generation_kwargs = {
|
| 180 |
+
"max_new_tokens": int(
|
| 181 |
+
max_new_tokens
|
| 182 |
+
),
|
| 183 |
+
"do_sample": (
|
| 184 |
+
float(temperature) > 0
|
| 185 |
+
),
|
| 186 |
+
"temperature": float(
|
| 187 |
+
temperature
|
| 188 |
+
),
|
| 189 |
}
|
| 190 |
|
| 191 |
if attention_mask is not None:
|
|
|
|
| 192 |
|
| 193 |
+
generation_kwargs[
|
| 194 |
+
"attention_mask"
|
| 195 |
+
] = attention_mask
|
| 196 |
+
|
| 197 |
+
output = model.generate(
|
| 198 |
+
input_ids,
|
| 199 |
+
**generation_kwargs,
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
generated_tokens = output[
|
| 203 |
+
0
|
| 204 |
+
][
|
| 205 |
+
input_ids.shape[-1]:
|
| 206 |
+
]
|
| 207 |
|
|
|
|
| 208 |
answer = tokenizer.decode(
|
| 209 |
+
generated_tokens,
|
| 210 |
skip_special_tokens=True,
|
| 211 |
).strip()
|
| 212 |
|
| 213 |
+
new_history = (
|
| 214 |
+
history or []
|
| 215 |
+
) + [
|
| 216 |
+
{
|
| 217 |
+
"role": "user",
|
| 218 |
+
"content": message,
|
| 219 |
+
},
|
| 220 |
+
{
|
| 221 |
+
"role": "assistant",
|
| 222 |
+
"content": answer,
|
| 223 |
+
},
|
| 224 |
]
|
| 225 |
|
| 226 |
+
return new_history, ""
|
| 227 |
+
|
| 228 |
+
except Exception as e:
|
| 229 |
|
|
|
|
| 230 |
error = (
|
| 231 |
+
"❌ Kimi K3 error\n\n"
|
| 232 |
+
f"`{type(e).__name__}: {e}`"
|
|
|
|
| 233 |
)
|
| 234 |
|
| 235 |
+
new_history = (
|
| 236 |
+
history or []
|
| 237 |
+
) + [
|
| 238 |
+
{
|
| 239 |
+
"role": "user",
|
| 240 |
+
"content": message,
|
| 241 |
+
},
|
| 242 |
+
{
|
| 243 |
+
"role": "assistant",
|
| 244 |
+
"content": error,
|
| 245 |
+
},
|
| 246 |
]
|
| 247 |
+
|
| 248 |
+
return new_history, ""
|
| 249 |
|
| 250 |
|
| 251 |
def clear_chat():
|
| 252 |
+
|
| 253 |
return [], ""
|
| 254 |
|
| 255 |
|
| 256 |
+
with gr.Blocks(
|
| 257 |
+
title="Kimi K3 — AirLLM"
|
| 258 |
+
) as demo:
|
| 259 |
+
|
| 260 |
gr.Markdown(
|
| 261 |
+
"""
|
| 262 |
+
# 🤖 Kimi K3 + AirLLM
|
| 263 |
+
|
| 264 |
+
### 2.8T parameter Kimi K3
|
| 265 |
+
### Expert-streamed with AirLLM
|
| 266 |
+
|
| 267 |
+
The first request may take a long time while
|
| 268 |
+
the model is initialized.
|
| 269 |
+
"""
|
| 270 |
)
|
| 271 |
|
| 272 |
chatbot = gr.Chatbot(
|
|
|
|
| 276 |
)
|
| 277 |
|
| 278 |
with gr.Row():
|
| 279 |
+
|
| 280 |
message = gr.Textbox(
|
| 281 |
label="Message",
|
| 282 |
+
placeholder=(
|
| 283 |
+
"Ask Kimi K3 anything..."
|
| 284 |
+
),
|
| 285 |
lines=3,
|
| 286 |
scale=5,
|
| 287 |
)
|
| 288 |
+
|
| 289 |
+
send = gr.Button(
|
| 290 |
+
"Send",
|
| 291 |
+
variant="primary",
|
| 292 |
+
)
|
| 293 |
|
| 294 |
with gr.Row():
|
| 295 |
+
|
| 296 |
max_tokens = gr.Slider(
|
| 297 |
+
minimum=1,
|
| 298 |
+
maximum=512,
|
| 299 |
+
value=128,
|
| 300 |
+
step=1,
|
| 301 |
+
label="Max new tokens",
|
| 302 |
)
|
| 303 |
+
|
| 304 |
temperature = gr.Slider(
|
| 305 |
+
minimum=0,
|
| 306 |
+
maximum=1.5,
|
| 307 |
+
value=0.6,
|
| 308 |
+
step=0.05,
|
| 309 |
+
label="Temperature",
|
| 310 |
)
|
| 311 |
+
|
| 312 |
clear = gr.Button("Clear")
|
| 313 |
|
| 314 |
send.click(
|
| 315 |
+
generate,
|
| 316 |
+
inputs=[
|
| 317 |
+
message,
|
| 318 |
+
chatbot,
|
| 319 |
+
max_tokens,
|
| 320 |
+
temperature,
|
| 321 |
+
],
|
| 322 |
+
outputs=[
|
| 323 |
+
chatbot,
|
| 324 |
+
message,
|
| 325 |
+
],
|
| 326 |
)
|
| 327 |
+
|
| 328 |
message.submit(
|
| 329 |
+
generate,
|
| 330 |
+
inputs=[
|
| 331 |
+
message,
|
| 332 |
+
chatbot,
|
| 333 |
+
max_tokens,
|
| 334 |
+
temperature,
|
| 335 |
+
],
|
| 336 |
+
outputs=[
|
| 337 |
+
chatbot,
|
| 338 |
+
message,
|
| 339 |
+
],
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
clear.click(
|
| 343 |
+
clear_chat,
|
| 344 |
+
outputs=[
|
| 345 |
+
chatbot,
|
| 346 |
+
message,
|
| 347 |
+
],
|
| 348 |
)
|
|
|
|
| 349 |
|
| 350 |
|
| 351 |
if __name__ == "__main__":
|
| 352 |
+
|
| 353 |
+
demo.launch()
|