Spaces:
Sleeping
Sleeping
Commit ·
c430d50
1
Parent(s): c424ad1
update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,93 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from nanochat.gpt import GPT, GPTConfig
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
n_kv_head=6,
|
| 30 |
-
n_embd=768,
|
| 31 |
-
sequence_len=2048,
|
| 32 |
-
)
|
| 33 |
|
| 34 |
model = GPT(config)
|
| 35 |
|
| 36 |
-
# 3. Load Weights
|
| 37 |
print("Loading weights...")
|
| 38 |
-
|
|
|
|
| 39 |
state_dict = {k.replace("_orig_mod.", ""): v for k, v in state_dict.items()}
|
|
|
|
| 40 |
model.load_state_dict(state_dict, strict=False)
|
| 41 |
-
model.to("cpu")
|
| 42 |
model.eval()
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
tokens
|
| 56 |
-
tokens
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
if __name__ == "__main__":
|
| 99 |
-
# Theme moved here to resolve UserWarning
|
| 100 |
-
demo.launch(
|
| 101 |
-
server_name="0.0.0.0",
|
| 102 |
-
server_port=7860,
|
| 103 |
-
theme=gr.themes.Soft(primary_hue="orange")
|
| 104 |
-
)
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import pickle
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
| 5 |
+
|
| 6 |
from nanochat.gpt import GPT, GPTConfig
|
| 7 |
+
|
| 8 |
+
print("🚀 Loading NanoChat...")
|
| 9 |
+
|
| 10 |
+
# -----------------------
|
| 11 |
+
# Load tokenizer
|
| 12 |
+
# -----------------------
|
| 13 |
+
|
| 14 |
+
with open("tokenizer.pkl", "rb") as f:
|
| 15 |
+
tokenizer = pickle.load(f)
|
| 16 |
+
|
| 17 |
+
print("Tokenizer loaded")
|
| 18 |
+
|
| 19 |
+
# -----------------------
|
| 20 |
+
# Load model config
|
| 21 |
+
# -----------------------
|
| 22 |
+
|
| 23 |
+
with open("meta_000971.json") as f:
|
| 24 |
+
meta = json.load(f)
|
| 25 |
+
|
| 26 |
+
config = GPTConfig(**meta)
|
| 27 |
+
|
| 28 |
+
# -----------------------
|
| 29 |
+
# Build model
|
| 30 |
+
# -----------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
model = GPT(config)
|
| 33 |
|
|
|
|
| 34 |
print("Loading weights...")
|
| 35 |
+
|
| 36 |
+
state_dict = torch.load("model_000971.pt", map_location="cpu")
|
| 37 |
state_dict = {k.replace("_orig_mod.", ""): v for k, v in state_dict.items()}
|
| 38 |
+
|
| 39 |
model.load_state_dict(state_dict, strict=False)
|
|
|
|
| 40 |
model.eval()
|
| 41 |
+
|
| 42 |
+
print("✅ NanoChat ready")
|
| 43 |
+
|
| 44 |
+
# -----------------------
|
| 45 |
+
# Chat function
|
| 46 |
+
# -----------------------
|
| 47 |
+
|
| 48 |
+
def generate_reply(message, history):
|
| 49 |
+
|
| 50 |
+
tokens = [tokenizer.bos_token_id]
|
| 51 |
+
|
| 52 |
+
for user, assistant in history:
|
| 53 |
+
tokens += [tokenizer.user_start_id] + tokenizer.encode(user) + [tokenizer.user_end_id]
|
| 54 |
+
tokens += [tokenizer.assistant_start_id] + tokenizer.encode(assistant) + [tokenizer.assistant_end_id]
|
| 55 |
+
|
| 56 |
+
tokens += [tokenizer.user_start_id] + tokenizer.encode(message) + [tokenizer.user_end_id]
|
| 57 |
+
tokens.append(tokenizer.assistant_start_id)
|
| 58 |
+
|
| 59 |
+
input_ids = torch.tensor([tokens])
|
| 60 |
+
|
| 61 |
+
with torch.no_grad():
|
| 62 |
+
output = model.generate(
|
| 63 |
+
input_ids,
|
| 64 |
+
max_tokens=256,
|
| 65 |
+
temperature=0.8,
|
| 66 |
+
top_k=40
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
new_tokens = output[0][input_ids.shape[1]:]
|
| 70 |
+
text = tokenizer.decode(new_tokens.tolist())
|
| 71 |
+
|
| 72 |
+
for tag in ["<|assistant_end|>", "<|end|>"]:
|
| 73 |
+
text = text.split(tag)[0]
|
| 74 |
+
|
| 75 |
+
return text.strip()
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# -----------------------
|
| 79 |
+
# UI
|
| 80 |
+
# -----------------------
|
| 81 |
+
|
| 82 |
+
demo = gr.ChatInterface(
|
| 83 |
+
fn=generate_reply,
|
| 84 |
+
title="🧸 NanoChat ClimbMix D12",
|
| 85 |
+
description="Small locally-trained NanoChat model running on HuggingFace Spaces",
|
| 86 |
+
examples=[
|
| 87 |
+
"Hi!",
|
| 88 |
+
"Explain UPI",
|
| 89 |
+
"Tell me a joke"
|
| 90 |
+
],
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|