Update app.py
Browse files
app.py
CHANGED
|
@@ -1,132 +1,172 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
MODEL_ID = "devoppro/Kimi-K3"
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 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 |
-
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
global model
|
| 34 |
global tokenizer
|
|
|
|
| 35 |
|
| 36 |
if model is not None:
|
| 37 |
-
return model
|
| 38 |
-
|
| 39 |
-
with model_lock:
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
"HF_TOKEN is missing. "
|
| 49 |
-
"Add it under Space Settings → Secrets."
|
| 50 |
-
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
repo_id=MODEL_ID,
|
| 67 |
-
local_dir=str(MODEL_DIR),
|
| 68 |
-
token=token,
|
| 69 |
-
resume_download=True,
|
| 70 |
-
)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
"Loading Kimi K3 with AirLLM...",
|
| 74 |
-
flush=True,
|
| 75 |
-
)
|
| 76 |
|
| 77 |
model = AutoModel.from_pretrained(
|
| 78 |
-
|
|
|
|
| 79 |
trust_remote_code=True,
|
| 80 |
)
|
| 81 |
|
| 82 |
tokenizer = model.tokenizer
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
| 87 |
)
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
history,
|
| 94 |
-
user_message,
|
| 95 |
-
):
|
| 96 |
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
continue
|
| 103 |
|
| 104 |
-
role = item.get("role")
|
| 105 |
-
content = item.get("content")
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
{
|
| 114 |
-
"role": role,
|
| 115 |
-
"content": content,
|
| 116 |
-
}
|
| 117 |
-
)
|
| 118 |
|
| 119 |
-
|
| 120 |
-
{
|
| 121 |
-
"role": "user",
|
| 122 |
-
"content": user_message,
|
| 123 |
-
}
|
| 124 |
-
)
|
| 125 |
|
| 126 |
-
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
@spaces.GPU(
|
|
|
|
| 130 |
size="xlarge",
|
| 131 |
)
|
| 132 |
def generate(
|
|
@@ -136,139 +176,225 @@ def generate(
|
|
| 136 |
temperature,
|
| 137 |
):
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
if not message or not message.strip():
|
| 140 |
|
| 141 |
-
return
|
| 142 |
|
| 143 |
-
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
prompt = tokenizer.apply_chat_template(
|
| 153 |
-
|
| 154 |
tokenize=False,
|
| 155 |
add_generation_prompt=True,
|
| 156 |
)
|
| 157 |
|
| 158 |
-
|
| 159 |
-
[prompt],
|
| 160 |
-
return_tensors="pt",
|
| 161 |
-
padding=True,
|
| 162 |
-
truncation=True,
|
| 163 |
-
)
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
].cuda()
|
| 168 |
|
| 169 |
-
|
| 170 |
-
"attention_mask"
|
| 171 |
-
)
|
| 172 |
|
| 173 |
-
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
|
|
|
| 177 |
)
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
|
| 192 |
|
| 193 |
-
|
| 194 |
-
"attention_mask"
|
| 195 |
-
] = attention_mask
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
input_ids,
|
|
|
|
| 199 |
**generation_kwargs,
|
| 200 |
)
|
| 201 |
|
| 202 |
-
|
| 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 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
"❌ Kimi K3 error\n\n"
|
| 232 |
-
f"`{type(e).__name__}: {e}`"
|
| 233 |
)
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
-
|
| 249 |
|
|
|
|
| 250 |
|
| 251 |
-
|
| 252 |
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
| 259 |
|
| 260 |
gr.Markdown(
|
| 261 |
"""
|
| 262 |
# 🤖 Kimi K3 + AirLLM
|
| 263 |
|
| 264 |
-
###
|
| 265 |
-
|
|
|
|
|
|
|
| 266 |
|
| 267 |
-
|
| 268 |
-
the model is initialized.
|
| 269 |
"""
|
| 270 |
)
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
chatbot = gr.Chatbot(
|
| 273 |
label="Kimi K3",
|
| 274 |
type="messages",
|
|
@@ -279,40 +405,42 @@ the model is initialized.
|
|
| 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=
|
| 298 |
maximum=512,
|
| 299 |
value=128,
|
| 300 |
-
step=
|
| 301 |
label="Max new tokens",
|
| 302 |
)
|
| 303 |
|
| 304 |
temperature = gr.Slider(
|
| 305 |
-
minimum=0,
|
| 306 |
maximum=1.5,
|
| 307 |
-
value=0.
|
| 308 |
-
step=0.
|
| 309 |
label="Temperature",
|
| 310 |
)
|
| 311 |
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
send.click(
|
| 315 |
-
generate,
|
| 316 |
inputs=[
|
| 317 |
message,
|
| 318 |
chatbot,
|
|
@@ -320,13 +448,13 @@ the model is initialized.
|
|
| 320 |
temperature,
|
| 321 |
],
|
| 322 |
outputs=[
|
| 323 |
-
chatbot,
|
| 324 |
message,
|
|
|
|
| 325 |
],
|
| 326 |
)
|
| 327 |
|
| 328 |
message.submit(
|
| 329 |
-
generate,
|
| 330 |
inputs=[
|
| 331 |
message,
|
| 332 |
chatbot,
|
|
@@ -334,20 +462,30 @@ the model is initialized.
|
|
| 334 |
temperature,
|
| 335 |
],
|
| 336 |
outputs=[
|
| 337 |
-
chatbot,
|
| 338 |
message,
|
|
|
|
| 339 |
],
|
| 340 |
)
|
| 341 |
|
| 342 |
clear.click(
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
)
|
| 349 |
|
| 350 |
|
| 351 |
-
|
|
|
|
|
|
|
| 352 |
|
| 353 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
+
import gc
|
| 5 |
+
import time
|
| 6 |
+
import traceback
|
| 7 |
+
|
| 8 |
+
# ---------------------------------------------------------
|
| 9 |
+
# Persistent storage
|
| 10 |
+
# ---------------------------------------------------------
|
| 11 |
+
|
| 12 |
+
DATA_DIR = "/data"
|
| 13 |
+
|
| 14 |
+
HF_HOME = os.path.join(DATA_DIR, "hf-cache")
|
| 15 |
+
TRANSFORMERS_CACHE = os.path.join(HF_HOME, "transformers")
|
| 16 |
+
HF_HUB_CACHE = os.path.join(HF_HOME, "hub")
|
| 17 |
+
|
| 18 |
+
AIRLLM_CACHE = os.path.join(DATA_DIR, "airllm-cache")
|
| 19 |
+
|
| 20 |
+
for directory in [
|
| 21 |
+
DATA_DIR,
|
| 22 |
+
HF_HOME,
|
| 23 |
+
TRANSFORMERS_CACHE,
|
| 24 |
+
HF_HUB_CACHE,
|
| 25 |
+
AIRLLM_CACHE,
|
| 26 |
+
]:
|
| 27 |
+
os.makedirs(directory, exist_ok=True)
|
| 28 |
+
|
| 29 |
+
# Set these BEFORE importing transformers / huggingface libraries
|
| 30 |
+
os.environ["HF_HOME"] = HF_HOME
|
| 31 |
+
os.environ["HF_HUB_CACHE"] = HF_HUB_CACHE
|
| 32 |
+
os.environ["TRANSFORMERS_CACHE"] = TRANSFORMERS_CACHE
|
| 33 |
+
|
| 34 |
+
# AirLLM cache location
|
| 35 |
+
os.environ["AIRLLM_CACHE_DIR"] = AIRLLM_CACHE
|
| 36 |
+
|
| 37 |
+
# Prevent excessive CPU thread usage
|
| 38 |
+
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
|
| 39 |
+
|
| 40 |
+
# ---------------------------------------------------------
|
| 41 |
+
# Imports
|
| 42 |
+
# ---------------------------------------------------------
|
| 43 |
|
| 44 |
import gradio as gr
|
| 45 |
+
import torch
|
| 46 |
+
|
| 47 |
+
from airllm import AutoModel
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# ---------------------------------------------------------
|
| 51 |
+
# Configuration
|
| 52 |
+
# ---------------------------------------------------------
|
| 53 |
|
| 54 |
MODEL_ID = "devoppro/Kimi-K3"
|
| 55 |
|
| 56 |
+
MAX_NEW_TOKENS = 128
|
| 57 |
+
MAX_LENGTH = 2048
|
| 58 |
|
| 59 |
+
|
| 60 |
+
# ---------------------------------------------------------
|
| 61 |
+
# Global model
|
| 62 |
+
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
model = None
|
| 65 |
tokenizer = None
|
| 66 |
+
|
| 67 |
+
model_status = "Not initialized"
|
| 68 |
|
| 69 |
|
| 70 |
+
# ---------------------------------------------------------
|
| 71 |
+
# Model loading
|
| 72 |
+
# ---------------------------------------------------------
|
| 73 |
+
|
| 74 |
+
def initialize_model():
|
| 75 |
|
| 76 |
global model
|
| 77 |
global tokenizer
|
| 78 |
+
global model_status
|
| 79 |
|
| 80 |
if model is not None:
|
| 81 |
+
return model
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
print("=" * 70)
|
| 84 |
+
print("Kimi K3 + AirLLM")
|
| 85 |
+
print("=" * 70)
|
| 86 |
|
| 87 |
+
print(f"Model: {MODEL_ID}")
|
| 88 |
+
print(f"HF_HOME: {HF_HOME}")
|
| 89 |
+
print(f"HF_HUB_CACHE: {HF_HUB_CACHE}")
|
| 90 |
+
print(f"AirLLM cache: {AIRLLM_CACHE}")
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
print()
|
| 93 |
+
print("Checking persistent storage...")
|
| 94 |
|
| 95 |
+
try:
|
| 96 |
+
total, used, free = torch.cuda.mem_get_info()
|
| 97 |
+
print(f"CUDA memory available: {free / 1024**3:.2f} GB")
|
| 98 |
+
except Exception:
|
| 99 |
+
print("CUDA memory information unavailable outside GPU worker.")
|
| 100 |
|
| 101 |
+
print()
|
| 102 |
+
print("Preparing Kimi K3...")
|
| 103 |
+
print("If this is the first startup, Hugging Face will download the")
|
| 104 |
+
print("large model checkpoint to the persistent /data volume.")
|
| 105 |
+
print()
|
| 106 |
|
| 107 |
+
start = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
try:
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
model = AutoModel.from_pretrained(
|
| 112 |
+
MODEL_ID,
|
| 113 |
+
compression="4B",
|
| 114 |
trust_remote_code=True,
|
| 115 |
)
|
| 116 |
|
| 117 |
tokenizer = model.tokenizer
|
| 118 |
|
| 119 |
+
elapsed = time.time() - start
|
| 120 |
+
|
| 121 |
+
model_status = (
|
| 122 |
+
f"Model ready — initialization took {elapsed:.1f} seconds"
|
| 123 |
)
|
| 124 |
|
| 125 |
+
print("=" * 70)
|
| 126 |
+
print(model_status)
|
| 127 |
+
print("=" * 70)
|
| 128 |
|
| 129 |
+
return model
|
| 130 |
|
| 131 |
+
except Exception as e:
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
model_status = f"Initialization failed: {e}"
|
| 134 |
|
| 135 |
+
print()
|
| 136 |
+
print("MODEL INITIALIZATION ERROR")
|
| 137 |
+
print(traceback.format_exc())
|
| 138 |
|
| 139 |
+
raise
|
|
|
|
| 140 |
|
|
|
|
|
|
|
| 141 |
|
| 142 |
+
# ---------------------------------------------------------
|
| 143 |
+
# Initialize at module level
|
| 144 |
+
# ---------------------------------------------------------
|
| 145 |
+
#
|
| 146 |
+
# ZeroGPU expects model registration/loading at module scope.
|
| 147 |
+
#
|
| 148 |
+
# The actual ZeroGPU backend handles the CUDA registration
|
| 149 |
+
# and disk offload mechanism.
|
| 150 |
+
# ---------------------------------------------------------
|
| 151 |
|
| 152 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
initialize_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
+
except Exception as e:
|
| 157 |
|
| 158 |
+
print()
|
| 159 |
+
print("WARNING:")
|
| 160 |
+
print("Kimi K3 was not initialized during startup.")
|
| 161 |
+
print(str(e))
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
# ---------------------------------------------------------
|
| 165 |
+
# Generation
|
| 166 |
+
# ---------------------------------------------------------
|
| 167 |
|
| 168 |
@spaces.GPU(
|
| 169 |
+
duration=180,
|
| 170 |
size="xlarge",
|
| 171 |
)
|
| 172 |
def generate(
|
|
|
|
| 176 |
temperature,
|
| 177 |
):
|
| 178 |
|
| 179 |
+
global model
|
| 180 |
+
global tokenizer
|
| 181 |
+
|
| 182 |
+
if model is None:
|
| 183 |
+
|
| 184 |
+
initialize_model()
|
| 185 |
+
|
| 186 |
if not message or not message.strip():
|
| 187 |
|
| 188 |
+
return "", history
|
| 189 |
|
| 190 |
+
# -----------------------------------------------------
|
| 191 |
+
# Build conversation
|
| 192 |
+
# -----------------------------------------------------
|
| 193 |
|
| 194 |
+
conversation = []
|
| 195 |
|
| 196 |
+
if history:
|
| 197 |
+
|
| 198 |
+
for item in history:
|
| 199 |
+
|
| 200 |
+
if isinstance(item, dict):
|
| 201 |
+
|
| 202 |
+
role = item.get("role")
|
| 203 |
+
content = item.get("content", "")
|
| 204 |
+
|
| 205 |
+
if role in ["user", "assistant"]:
|
| 206 |
+
|
| 207 |
+
conversation.append(
|
| 208 |
+
{
|
| 209 |
+
"role": role,
|
| 210 |
+
"content": content,
|
| 211 |
+
}
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
elif isinstance(item, (list, tuple)):
|
| 215 |
+
|
| 216 |
+
if len(item) == 2:
|
| 217 |
+
|
| 218 |
+
user_message, assistant_message = item
|
| 219 |
+
|
| 220 |
+
if user_message:
|
| 221 |
+
|
| 222 |
+
conversation.append(
|
| 223 |
+
{
|
| 224 |
+
"role": "user",
|
| 225 |
+
"content": user_message,
|
| 226 |
+
}
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
if assistant_message:
|
| 230 |
+
|
| 231 |
+
conversation.append(
|
| 232 |
+
{
|
| 233 |
+
"role": "assistant",
|
| 234 |
+
"content": assistant_message,
|
| 235 |
+
}
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
conversation.append(
|
| 239 |
+
{
|
| 240 |
+
"role": "user",
|
| 241 |
+
"content": message,
|
| 242 |
+
}
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
# -----------------------------------------------------
|
| 246 |
+
# Tokenize
|
| 247 |
+
# -----------------------------------------------------
|
| 248 |
+
|
| 249 |
+
try:
|
| 250 |
|
| 251 |
prompt = tokenizer.apply_chat_template(
|
| 252 |
+
conversation,
|
| 253 |
tokenize=False,
|
| 254 |
add_generation_prompt=True,
|
| 255 |
)
|
| 256 |
|
| 257 |
+
except Exception:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
+
# Fallback for processors/tokenizers without
|
| 260 |
+
# chat-template support.
|
|
|
|
| 261 |
|
| 262 |
+
prompt = ""
|
|
|
|
|
|
|
| 263 |
|
| 264 |
+
for item in conversation:
|
| 265 |
|
| 266 |
+
prompt += (
|
| 267 |
+
f"{item['role'].upper()}: "
|
| 268 |
+
f"{item['content']}\n"
|
| 269 |
)
|
| 270 |
|
| 271 |
+
prompt += "ASSISTANT:"
|
| 272 |
+
|
| 273 |
+
print()
|
| 274 |
+
print("Generating response...")
|
| 275 |
+
print(f"Prompt length: {len(prompt)} characters")
|
| 276 |
+
|
| 277 |
+
inputs = tokenizer(
|
| 278 |
+
[prompt],
|
| 279 |
+
return_tensors="pt",
|
| 280 |
+
padding=True,
|
| 281 |
+
truncation=True,
|
| 282 |
+
max_length=MAX_LENGTH,
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
input_ids = inputs["input_ids"].cuda()
|
| 286 |
|
| 287 |
+
attention_mask = None
|
| 288 |
|
| 289 |
+
if "attention_mask" in inputs:
|
|
|
|
|
|
|
| 290 |
|
| 291 |
+
attention_mask = inputs["attention_mask"].cuda()
|
| 292 |
+
|
| 293 |
+
# -----------------------------------------------------
|
| 294 |
+
# Generation
|
| 295 |
+
# -----------------------------------------------------
|
| 296 |
+
|
| 297 |
+
generation_kwargs = {
|
| 298 |
+
"max_new_tokens": int(max_new_tokens),
|
| 299 |
+
"do_sample": True,
|
| 300 |
+
"temperature": float(temperature),
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
if attention_mask is not None:
|
| 304 |
+
|
| 305 |
+
generation_output = model.generate(
|
| 306 |
input_ids,
|
| 307 |
+
attention_mask=attention_mask,
|
| 308 |
**generation_kwargs,
|
| 309 |
)
|
| 310 |
|
| 311 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
+
generation_output = model.generate(
|
| 314 |
+
input_ids,
|
| 315 |
+
**generation_kwargs,
|
|
|
|
|
|
|
| 316 |
)
|
| 317 |
|
| 318 |
+
# -----------------------------------------------------
|
| 319 |
+
# Decode
|
| 320 |
+
# -----------------------------------------------------
|
| 321 |
+
|
| 322 |
+
generated_tokens = generation_output[0][
|
| 323 |
+
input_ids.shape[-1]:
|
| 324 |
+
]
|
| 325 |
+
|
| 326 |
+
answer = tokenizer.decode(
|
| 327 |
+
generated_tokens,
|
| 328 |
+
skip_special_tokens=True,
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
answer = answer.strip()
|
| 332 |
+
|
| 333 |
+
# -----------------------------------------------------
|
| 334 |
+
# Cleanup
|
| 335 |
+
# -----------------------------------------------------
|
| 336 |
+
|
| 337 |
+
del input_ids
|
| 338 |
+
|
| 339 |
+
if attention_mask is not None:
|
| 340 |
|
| 341 |
+
del attention_mask
|
| 342 |
|
| 343 |
+
del generation_output
|
| 344 |
|
| 345 |
+
gc.collect()
|
| 346 |
|
| 347 |
+
try:
|
| 348 |
+
|
| 349 |
+
torch.cuda.empty_cache()
|
| 350 |
+
|
| 351 |
+
except Exception:
|
| 352 |
+
|
| 353 |
+
pass
|
| 354 |
+
|
| 355 |
+
new_history = list(history or [])
|
| 356 |
+
|
| 357 |
+
new_history.append(
|
| 358 |
+
{
|
| 359 |
+
"role": "user",
|
| 360 |
+
"content": message,
|
| 361 |
+
}
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
new_history.append(
|
| 365 |
+
{
|
| 366 |
+
"role": "assistant",
|
| 367 |
+
"content": answer,
|
| 368 |
+
}
|
| 369 |
+
)
|
| 370 |
+
|
| 371 |
+
return "", new_history
|
| 372 |
|
| 373 |
|
| 374 |
+
# ---------------------------------------------------------
|
| 375 |
+
# UI
|
| 376 |
+
# ---------------------------------------------------------
|
| 377 |
+
|
| 378 |
+
with gr.Blocks() as demo:
|
| 379 |
|
| 380 |
gr.Markdown(
|
| 381 |
"""
|
| 382 |
# 🤖 Kimi K3 + AirLLM
|
| 383 |
|
| 384 |
+
### Kimi K3 running through AirLLM
|
| 385 |
+
|
| 386 |
+
This demo uses the persistent `/data` storage volume for the
|
| 387 |
+
large model checkpoint.
|
| 388 |
|
| 389 |
+
**First startup may take a long time.**
|
|
|
|
| 390 |
"""
|
| 391 |
)
|
| 392 |
|
| 393 |
+
status = gr.Markdown(
|
| 394 |
+
"### Model status\n"
|
| 395 |
+
"The model is being prepared..."
|
| 396 |
+
)
|
| 397 |
+
|
| 398 |
chatbot = gr.Chatbot(
|
| 399 |
label="Kimi K3",
|
| 400 |
type="messages",
|
|
|
|
| 405 |
|
| 406 |
message = gr.Textbox(
|
| 407 |
label="Message",
|
| 408 |
+
placeholder="Ask Kimi K3 anything...",
|
|
|
|
|
|
|
|
|
|
| 409 |
scale=5,
|
| 410 |
)
|
| 411 |
|
| 412 |
send = gr.Button(
|
| 413 |
"Send",
|
| 414 |
variant="primary",
|
| 415 |
+
scale=1,
|
| 416 |
)
|
| 417 |
|
| 418 |
with gr.Row():
|
| 419 |
|
| 420 |
max_tokens = gr.Slider(
|
| 421 |
+
minimum=16,
|
| 422 |
maximum=512,
|
| 423 |
value=128,
|
| 424 |
+
step=16,
|
| 425 |
label="Max new tokens",
|
| 426 |
)
|
| 427 |
|
| 428 |
temperature = gr.Slider(
|
| 429 |
+
minimum=0.1,
|
| 430 |
maximum=1.5,
|
| 431 |
+
value=0.7,
|
| 432 |
+
step=0.1,
|
| 433 |
label="Temperature",
|
| 434 |
)
|
| 435 |
|
| 436 |
+
clear = gr.Button("Clear conversation")
|
| 437 |
+
|
| 438 |
+
# -----------------------------------------------------
|
| 439 |
+
# Events
|
| 440 |
+
# -----------------------------------------------------
|
| 441 |
|
| 442 |
send.click(
|
| 443 |
+
fn=generate,
|
| 444 |
inputs=[
|
| 445 |
message,
|
| 446 |
chatbot,
|
|
|
|
| 448 |
temperature,
|
| 449 |
],
|
| 450 |
outputs=[
|
|
|
|
| 451 |
message,
|
| 452 |
+
chatbot,
|
| 453 |
],
|
| 454 |
)
|
| 455 |
|
| 456 |
message.submit(
|
| 457 |
+
fn=generate,
|
| 458 |
inputs=[
|
| 459 |
message,
|
| 460 |
chatbot,
|
|
|
|
| 462 |
temperature,
|
| 463 |
],
|
| 464 |
outputs=[
|
|
|
|
| 465 |
message,
|
| 466 |
+
chatbot,
|
| 467 |
],
|
| 468 |
)
|
| 469 |
|
| 470 |
clear.click(
|
| 471 |
+
fn=lambda: [],
|
| 472 |
+
inputs=None,
|
| 473 |
+
outputs=chatbot,
|
| 474 |
+
)
|
| 475 |
+
|
| 476 |
+
# Update status after startup
|
| 477 |
+
status.value = (
|
| 478 |
+
f"### Model status\n"
|
| 479 |
+
f"`{model_status}`"
|
| 480 |
)
|
| 481 |
|
| 482 |
|
| 483 |
+
# ---------------------------------------------------------
|
| 484 |
+
# Launch
|
| 485 |
+
# ---------------------------------------------------------
|
| 486 |
|
| 487 |
+
demo.launch(
|
| 488 |
+
server_name="0.0.0.0",
|
| 489 |
+
server_port=7860,
|
| 490 |
+
ssr_mode=True,
|
| 491 |
+
)
|