Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
sys.path.insert(0, os.path.dirname(__file__))
|
| 7 |
+
|
| 8 |
+
from src.model import Nexus
|
| 9 |
+
from src.config import NexusConfig
|
| 10 |
+
from tokenizers import Tokenizer
|
| 11 |
+
|
| 12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
+
print(f"Loading on {device}")
|
| 14 |
+
|
| 15 |
+
config = NexusConfig()
|
| 16 |
+
model = Nexus(config)
|
| 17 |
+
|
| 18 |
+
weights_path = os.path.join(os.path.dirname(__file__), "weights", "nexus_instruct.pt")
|
| 19 |
+
if not os.path.exists(weights_path):
|
| 20 |
+
weights_path = os.path.join(os.path.dirname(__file__), "weights", "nexus_final.pt")
|
| 21 |
+
|
| 22 |
+
checkpoint = torch.load(weights_path, map_location=device, weights_only=False)
|
| 23 |
+
model.load_state_dict(checkpoint["model_state_dict"])
|
| 24 |
+
model = model.to(device)
|
| 25 |
+
model.eval()
|
| 26 |
+
print("Model loaded")
|
| 27 |
+
|
| 28 |
+
tokenizer_path = os.path.join(os.path.dirname(__file__), "data", "tokenizer.json")
|
| 29 |
+
tokenizer = Tokenizer.from_file(tokenizer_path)
|
| 30 |
+
|
| 31 |
+
bos_id = tokenizer.token_to_id("<bos>") or 1
|
| 32 |
+
eos_id = tokenizer.token_to_id("<eos>") or 2
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def chat(message, history):
|
| 36 |
+
history_ids = [bos_id]
|
| 37 |
+
for user_msg, bot_msg in history:
|
| 38 |
+
if user_msg:
|
| 39 |
+
history_ids.extend(tokenizer.encode(f"User: {user_msg}\nAssistant:").ids)
|
| 40 |
+
if bot_msg:
|
| 41 |
+
history_ids.extend(tokenizer.encode(f" {bot_msg}").ids + [eos_id])
|
| 42 |
+
|
| 43 |
+
history_ids.extend(tokenizer.encode(f"User: {message}\nAssistant:").ids)
|
| 44 |
+
|
| 45 |
+
input_tensor = torch.tensor([history_ids[-config.max_seq_len:]], dtype=torch.long, device=device)
|
| 46 |
+
|
| 47 |
+
with torch.no_grad():
|
| 48 |
+
for _ in range(128):
|
| 49 |
+
seq_len = input_tensor.shape[1]
|
| 50 |
+
if seq_len > config.max_seq_len:
|
| 51 |
+
input_tensor = input_tensor[:, -config.max_seq_len:]
|
| 52 |
+
|
| 53 |
+
logits = model(input_tensor, 0)
|
| 54 |
+
logits = logits[:, -1, :] / 0.2
|
| 55 |
+
|
| 56 |
+
probs = torch.softmax(logits, dim=-1)
|
| 57 |
+
next_token = torch.multinomial(probs, num_samples=1)
|
| 58 |
+
|
| 59 |
+
input_tensor = torch.cat([input_tensor, next_token], dim=-1)
|
| 60 |
+
|
| 61 |
+
if next_token.item() == eos_id:
|
| 62 |
+
break
|
| 63 |
+
|
| 64 |
+
response_ids = input_tensor[0].tolist()
|
| 65 |
+
response_ids = response_ids[-(input_tensor.shape[1] - len(history_ids)):]
|
| 66 |
+
|
| 67 |
+
response = tokenizer.decode(response_ids)
|
| 68 |
+
response = response.split("<eos>")[0].split("User:")[0].replace("Assistant:", "").strip()
|
| 69 |
+
|
| 70 |
+
if len(response) < 2:
|
| 71 |
+
response = "[no response]"
|
| 72 |
+
|
| 73 |
+
return response
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
css = """
|
| 77 |
+
footer {visibility: hidden}
|
| 78 |
+
.message {font-size: 14px}
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
demo = gr.ChatInterface(
|
| 82 |
+
fn=chat,
|
| 83 |
+
title="Nexus SmAll v1",
|
| 84 |
+
description="A 89.8M parameter transformer trained from scratch. May produce incoherent outputs — it's a tiny model!",
|
| 85 |
+
css=css,
|
| 86 |
+
theme="soft",
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
demo.launch()
|