Create space_app
Browse files
space_app
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr, torch, json
|
| 2 |
+
from tokenizers import Tokenizer
|
| 3 |
+
from model.tiny_gpt2 import TinyGPT2, GPTConfig
|
| 4 |
+
|
| 5 |
+
TOK_PATH = "out/tokenizer.json"
|
| 6 |
+
CFG_PATH = "out/pretrain/gpt_config.json"
|
| 7 |
+
CKPT_PATH = "out/sft/model_sft.pt"
|
| 8 |
+
|
| 9 |
+
tok = Tokenizer.from_file(TOK_PATH)
|
| 10 |
+
cfg = GPTConfig(**json.load(open(CFG_PATH)))
|
| 11 |
+
model = TinyGPT2(cfg)
|
| 12 |
+
model.load_state_dict(torch.load(CKPT_PATH, map_location="cpu"))
|
| 13 |
+
model.eval()
|
| 14 |
+
|
| 15 |
+
def generate_reply(prompt, max_new_tokens=96, top_k=40, top_p=0.9, temperature=0.8):
|
| 16 |
+
ids = tok.encode("[BOS] " + prompt).ids
|
| 17 |
+
x = torch.tensor([ids], dtype=torch.long)
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
y = model.generate(x, max_new_tokens=int(max_new_tokens), top_k=int(top_k),
|
| 20 |
+
top_p=float(top_p), temperature=float(temperature))
|
| 21 |
+
text = tok.decode(y[0].tolist())
|
| 22 |
+
return text.replace("[BOS]", "").split("[EOS]")[0].strip()
|
| 23 |
+
|
| 24 |
+
demo = gr.Interface(
|
| 25 |
+
fn=generate_reply,
|
| 26 |
+
inputs=[
|
| 27 |
+
gr.Textbox(label="Ask an IB Physics HL question"),
|
| 28 |
+
gr.Slider(16, 256, value=96, step=1, label="Max new tokens"),
|
| 29 |
+
gr.Slider(0, 100, value=40, step=1, label="top_k"),
|
| 30 |
+
gr.Slider(0.5, 1.0, value=0.9, step=0.01, label="top_p"),
|
| 31 |
+
gr.Slider(0.2, 1.5, value=0.8, step=0.05, label="temperature"),
|
| 32 |
+
],
|
| 33 |
+
outputs=gr.Textbox(label="MiniGPT Answer"),
|
| 34 |
+
title="IB-Physics-Mini-GPT (from scratch)",
|
| 35 |
+
description="Tiny educational LLM trained from scratch + light SFT on physics definitions & short Q&A."
|
| 36 |
+
)
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
demo.launch()
|