Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: cc-by-nc-4.0
|
| 3 |
+
language:
|
| 4 |
+
- fr
|
| 5 |
+
pipeline_tag: text-generation
|
| 6 |
+
---
|
| 7 |
+
```!pip -q install torch huggingface_hub gradio
|
| 8 |
+
|
| 9 |
+
import re, torch, gradio as gr
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
from huggingface_hub import hf_hub_download
|
| 12 |
+
|
| 13 |
+
REPO = "lea97338/LunaLMV2"
|
| 14 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
|
| 16 |
+
# Téléchargement public : aucun token
|
| 17 |
+
path = hf_hub_download(REPO, "luna_model.pth")
|
| 18 |
+
ckpt = torch.load(path, map_location="cpu", weights_only=False)
|
| 19 |
+
|
| 20 |
+
class LunaLM(nn.Module):
|
| 21 |
+
def __init__(self, vocab_size, d_model=128, max_len=64):
|
| 22 |
+
super().__init__()
|
| 23 |
+
self.max_len, self.vocab_size = max_len, vocab_size
|
| 24 |
+
self.wte = nn.Embedding(vocab_size, d_model)
|
| 25 |
+
self.wpe = nn.Embedding(max_len, d_model)
|
| 26 |
+
self.ln1, self.ln2, self.ln_f = (
|
| 27 |
+
nn.LayerNorm(d_model), nn.LayerNorm(d_model),
|
| 28 |
+
nn.LayerNorm(d_model))
|
| 29 |
+
self.q_proj, self.k_proj, self.v_proj, self.out_proj = [
|
| 30 |
+
nn.Linear(d_model, d_model) for _ in range(4)]
|
| 31 |
+
self.mlp = nn.Sequential(
|
| 32 |
+
nn.Linear(d_model, d_model*2), nn.GELU(),
|
| 33 |
+
nn.Linear(d_model*2, d_model))
|
| 34 |
+
self.lm_head = nn.Linear(d_model, vocab_size, bias=False)
|
| 35 |
+
|
| 36 |
+
def forward(self, idx):
|
| 37 |
+
_, T = idx.shape
|
| 38 |
+
x = self.wte(idx) + self.wpe(
|
| 39 |
+
torch.arange(T, device=idx.device).unsqueeze(0))
|
| 40 |
+
n = self.ln1(x)
|
| 41 |
+
q, k, v = self.q_proj(n), self.k_proj(n), self.v_proj(n)
|
| 42 |
+
a = q @ k.transpose(-2, -1) / (k.size(-1) ** 0.5)
|
| 43 |
+
mask = torch.tril(torch.ones(T, T, device=idx.device)).bool()
|
| 44 |
+
x = x + self.out_proj(
|
| 45 |
+
torch.softmax(a.masked_fill(~mask, float("-inf")), -1) @ v)
|
| 46 |
+
return self.lm_head(self.ln_f(x + self.mlp(self.ln2(x))))
|
| 47 |
+
|
| 48 |
+
@staticmethod
|
| 49 |
+
def clean_tok(text):
|
| 50 |
+
return re.findall(r"\w+|[^\w\s]", text)
|
| 51 |
+
|
| 52 |
+
model = LunaLM(len(ckpt["w2i"]))
|
| 53 |
+
model.load_state_dict(ckpt["model"])
|
| 54 |
+
model.w2i, model.i2w = ckpt["w2i"], ckpt["i2w"]
|
| 55 |
+
model = model.to(DEVICE).eval()
|
| 56 |
+
|
| 57 |
+
@torch.inference_mode()
|
| 58 |
+
def generate_stream(prompt):
|
| 59 |
+
ids = [model.w2i["<bos>"]] + [
|
| 60 |
+
model.w2i.get(w, 3)
|
| 61 |
+
for w in ["user"] + model.clean_tok(prompt) + ["luna"]]
|
| 62 |
+
ids = ids[:model.max_len]
|
| 63 |
+
result = ""
|
| 64 |
+
|
| 65 |
+
while len(ids) < model.max_len:
|
| 66 |
+
x = torch.tensor([ids], device=DEVICE)
|
| 67 |
+
token = int(model(x)[0, -1].argmax().item())
|
| 68 |
+
if token in (model.w2i["<eos>"], model.w2i["<pad>"]):
|
| 69 |
+
break
|
| 70 |
+
word = model.i2w.get(token, "")
|
| 71 |
+
result += ("" if word in ".,!?;:" else " ") + word
|
| 72 |
+
ids.append(token)
|
| 73 |
+
yield result.strip()
|
| 74 |
+
|
| 75 |
+
demo = gr.ChatInterface(
|
| 76 |
+
fn=generate_stream,
|
| 77 |
+
title="🌙 LunaLMV2",
|
| 78 |
+
description="Inférence seule — génération en streaming."
|
| 79 |
+
)
|
| 80 |
+
demo.queue().launch(share=True)
|
| 81 |
+
```
|