vesakkivignesh commited on
Commit
c7cd6db
·
verified ·
1 Parent(s): 8fcfeb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py CHANGED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+
4
+ import bdh
5
+ from bdh import BDHConfig, BDH
6
+
7
+ # Device (Spaces usually run on CPU)
8
+ device = torch.device("cpu")
9
+
10
+ # Small config for live demo (important!)
11
+ config = BDHConfig(
12
+ n_layer=2,
13
+ n_embd=128,
14
+ n_head=4,
15
+ mlp_internal_dim_multiplier=32,
16
+ vocab_size=256,
17
+ )
18
+
19
+ model = BDH(config).to(device)
20
+ model.eval()
21
+
22
+ @torch.no_grad()
23
+ def generate_text(prompt: str, max_tokens: int = 50):
24
+ if len(prompt) == 0:
25
+ return "Please enter a prompt."
26
+
27
+ idx = torch.tensor(
28
+ bytearray(prompt, "utf-8"),
29
+ dtype=torch.long,
30
+ device=device
31
+ ).unsqueeze(0)
32
+
33
+ out = model.generate(
34
+ idx,
35
+ max_new_tokens=max_tokens,
36
+ top_k=5
37
+ )
38
+
39
+ return bytes(
40
+ out.squeeze(0).to(torch.uint8).cpu()
41
+ ).decode(errors="replace")
42
+
43
+
44
+ gr.Interface(
45
+ fn=generate_text,
46
+ inputs=[
47
+ gr.Textbox(label="Prompt"),
48
+ gr.Slider(10, 100, value=50, step=10, label="Max tokens")
49
+ ],
50
+ outputs=gr.Textbox(label="BDH Output"),
51
+ title="BDH – Beyond Transformer (Live Demo)",
52
+ description="Inference-only demo of the BDH architecture"
53
+ ).launch()