Spaces:
Sleeping
Sleeping
Commit
·
df0e34d
1
Parent(s):
5649c37
update app
Browse files
app.py
CHANGED
|
@@ -3,38 +3,18 @@ import torch
|
|
| 3 |
import os
|
| 4 |
from GPTLanguageModelClass import hyperparams
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
block_size = hyperparams.block_size
|
| 7 |
-
batch_size = hyperparams.batch_size
|
| 8 |
-
max_iters = hyperparams.max_iters
|
| 9 |
-
learning_rate = hyperparams.learning_rate
|
| 10 |
-
eval_every = hyperparams.eval_every
|
| 11 |
-
n_embd = hyperparams.n_embd
|
| 12 |
-
n_head = hyperparams.n_head
|
| 13 |
-
n_layer = hyperparams.n_layer
|
| 14 |
-
dropout = hyperparams.dropout
|
| 15 |
device = hyperparams.device
|
| 16 |
|
| 17 |
-
st.title("LLM from scratch Demo")
|
| 18 |
-
|
| 19 |
-
st.write(f"Using device: {device}")
|
| 20 |
-
|
| 21 |
if not os.path.exists("./vocab.txt"):
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
with open("./vocab.txt", "r", encoding="utf-8") as f:
|
| 25 |
-
|
| 26 |
-
chars = sorted(list(set(text)))
|
| 27 |
-
|
| 28 |
-
st.write(f"Vocab size: {len(chars)}")
|
| 29 |
-
st.write(f"Block size: {block_size}")
|
| 30 |
-
st.write(f"Batch size: {batch_size}")
|
| 31 |
-
st.write(f"Max iters: {max_iters}")
|
| 32 |
-
st.write(f"Learning rate: {learning_rate}")
|
| 33 |
-
st.write(f"Eval every: {eval_every}")
|
| 34 |
-
st.write(f"n_embd: {n_embd}")
|
| 35 |
-
st.write(f"n_head: {n_head}")
|
| 36 |
-
st.write(f"n_layer: {n_layer}")
|
| 37 |
-
st.write(f"dropout: {dropout}")
|
| 38 |
|
| 39 |
string_to_int = {ch: i for i, ch in enumerate(chars)}
|
| 40 |
int_to_string = {i: ch for i, ch in enumerate(chars)}
|
|
@@ -48,22 +28,100 @@ def decode(x):
|
|
| 48 |
return "".join([int_to_string[i] for i in x])
|
| 49 |
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
st.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
prompt = ""
|
| 59 |
prompt = st.text_area(
|
| 60 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
-
if len(prompt) != 0:
|
| 63 |
-
context = torch.tensor(encode(prompt), dtype=torch.long, device=device)
|
| 64 |
-
max_new_tokens = block_size - len(prompt)
|
| 65 |
-
generated_chars = decode(
|
| 66 |
-
model.generate(context.unsqueeze(0), max_new_tokens=max_new_tokens)[0].tolist()
|
| 67 |
-
)
|
| 68 |
-
st.write("Generated text:")
|
| 69 |
-
st.write(generated_chars)
|
|
|
|
| 3 |
import os
|
| 4 |
from GPTLanguageModelClass import hyperparams
|
| 5 |
|
| 6 |
+
st.set_page_config(page_title="LLM from Scratch Demo")
|
| 7 |
+
st.title("LLM from Scratch Demo")
|
| 8 |
+
|
| 9 |
block_size = hyperparams.block_size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
device = hyperparams.device
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
if not os.path.exists("./vocab.txt"):
|
| 13 |
+
st.error("Please run extract.py first")
|
| 14 |
+
st.stop()
|
| 15 |
+
|
| 16 |
with open("./vocab.txt", "r", encoding="utf-8") as f:
|
| 17 |
+
chars = sorted(list(set(f.read())))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
string_to_int = {ch: i for i, ch in enumerate(chars)}
|
| 20 |
int_to_string = {i: ch for i, ch in enumerate(chars)}
|
|
|
|
| 28 |
return "".join([int_to_string[i] for i in x])
|
| 29 |
|
| 30 |
|
| 31 |
+
@st.cache_resource
|
| 32 |
+
def load_model():
|
| 33 |
+
model_pickle_path = "./model.pt"
|
| 34 |
+
with open(model_pickle_path, "rb") as f:
|
| 35 |
+
model = torch.load(f, map_location=device, weights_only=False)
|
| 36 |
+
return model
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
model = load_model()
|
| 40 |
+
|
| 41 |
+
if "result" not in st.session_state:
|
| 42 |
+
st.session_state.result = None
|
| 43 |
+
|
| 44 |
+
if "prompt" not in st.session_state:
|
| 45 |
+
st.session_state.prompt = ""
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def clear_results():
|
| 49 |
+
st.session_state.result = None
|
| 50 |
+
st.session_state.prompt = ""
|
| 51 |
+
|
| 52 |
|
| 53 |
+
st.subheader("About")
|
| 54 |
+
|
| 55 |
+
st.markdown(
|
| 56 |
+
'This is a demo of a language model built from scratch using PyTorch. It generates text continuations based on a *character*-level GPT architecture trained on the [OpenWebText dataset](https://github.com/jcpeterson/openwebtext). What this means is that this model will "predict" the next character based on all previous characters. This model was built from scratch using PyTorch, following the [paper](https://arxiv.org/abs/1706.03762) "Attention is all you need". The goal of this project was to gain a deep familiarity with the underlying structure of an LLM. The model was trained on commodity hardware and utilized a comparatively small dataset size and model size.'
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
st.subheader("Model")
|
| 60 |
+
col1, col2, col3 = st.columns(3)
|
| 61 |
+
with col1:
|
| 62 |
+
st.write(f"**Device:** {device}")
|
| 63 |
+
st.write(f"**Vocab size:** {len(chars)}")
|
| 64 |
+
st.write(f"**Block size:** {block_size}")
|
| 65 |
+
st.write(f"**Batch size:** {hyperparams.batch_size}")
|
| 66 |
+
with col2:
|
| 67 |
+
st.write(f"**Max iters:** {hyperparams.max_iters}")
|
| 68 |
+
st.write(f"**Learning rate:** {hyperparams.learning_rate}")
|
| 69 |
+
st.write(f"**Eval every:** {hyperparams.eval_every}")
|
| 70 |
+
st.write(f"**n_embd:** {hyperparams.n_embd}")
|
| 71 |
+
with col3:
|
| 72 |
+
st.write(f"**n_head:** {hyperparams.n_head}")
|
| 73 |
+
st.write(f"**n_layer:** {hyperparams.n_layer}")
|
| 74 |
+
st.write(f"**Dropout:** {hyperparams.dropout}")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
st.subheader("Demo")
|
| 78 |
+
st.write(
|
| 79 |
+
"Enter some text (up to 127 characters) and click 'Generate' to see "
|
| 80 |
+
"the model's continuation"
|
| 81 |
+
)
|
| 82 |
|
|
|
|
| 83 |
prompt = st.text_area(
|
| 84 |
+
"Enter text to autocomplete:",
|
| 85 |
+
height=50,
|
| 86 |
+
max_chars=block_size - 1,
|
| 87 |
+
key="prompt",
|
| 88 |
+
placeholder="Type here...",
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
generate_clicked = st.button("Generate")
|
| 92 |
+
clear_clicked = st.button("Clear Results", on_click=clear_results)
|
| 93 |
+
|
| 94 |
+
if generate_clicked or len(prompt) != 0:
|
| 95 |
+
if prompt.strip():
|
| 96 |
+
context = torch.tensor(encode(prompt), dtype=torch.long, device=device)
|
| 97 |
+
max_new_tokens = block_size - len(prompt)
|
| 98 |
+
generated = model.generate(context.unsqueeze(0), max_new_tokens=max_new_tokens)[
|
| 99 |
+
0
|
| 100 |
+
]
|
| 101 |
+
full_text = decode(generated.tolist())
|
| 102 |
+
st.session_state.result = {
|
| 103 |
+
"input": prompt,
|
| 104 |
+
"continuation": full_text[len(prompt) :],
|
| 105 |
+
"full": full_text,
|
| 106 |
+
}
|
| 107 |
+
else:
|
| 108 |
+
st.warning("Please enter some text to autocomplete.")
|
| 109 |
+
st.session_state.result = None
|
| 110 |
+
|
| 111 |
+
if st.session_state.result:
|
| 112 |
+
st.subheader("Result")
|
| 113 |
+
st.write("**Your input:**")
|
| 114 |
+
st.write(st.session_state.result["input"])
|
| 115 |
+
st.write("**Generated continuation:**")
|
| 116 |
+
st.write(st.session_state.result["continuation"])
|
| 117 |
+
st.write("**Full text:**")
|
| 118 |
+
st.write(st.session_state.result["full"])
|
| 119 |
+
|
| 120 |
+
st.markdown("---")
|
| 121 |
+
st.markdown(
|
| 122 |
+
"Connect with me"
|
| 123 |
+
": [GitHub](https://github.com/ibrahimmkhalid/llm-from-scratch) "
|
| 124 |
+
"| [LinkedIn](https://linkedin.com/in/ibrahimmkhalid) "
|
| 125 |
+
"| [Website](https://ibrahimkhalid.me) "
|
| 126 |
+
"| [ibrahimmkhalid@gmail.com](mailto:ibrahimmkhalid@gmail.com)"
|
| 127 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|