next-token-explorer / README.md
Pixelkram's picture
Upload 7 files
f408b78 verified
|
Raw History Blame Contribute Delete
4.05 kB
---
title: Next Token Explorer
emoji: 🎲
colorFrom: blue
colorTo: yellow
sdk: gradio
sdk_version: 6.29.0
python_version: "3.12"
app_file: app.py
pinned: false
license: apache-2.0
short_description: Live next-token probabilities, tokens and temperature
models:
- Qwen/Qwen2.5-1.5B-Instruct
preload_from_hub:
- Qwen/Qwen2.5-1.5B-Instruct
---
# Next Token Explorer
Teaching demo for the LLM seminar of the MA Design for Digital Futures at TH Nürnberg.
Type any text and see, live:
- **the tokens** the model reads, updated as you type (in assistant mode including the hidden chat template),
- **the 20 most likely next tokens** with their probabilities,
- **temperature**: every probability raised to the power 1/T and rescaled, with the model's own probability as a dashed outline,
- **sampling**: a dice strip of the whole distribution. A pin in the long tail returns the exact token at that point out of all 151,936 output slots.
Two modes:
- **Continue the text**: the model continues raw text, like a base model (same as Parts 2 and 3 of the week 1 notebook).
- **Answer as assistant**: the text is wrapped in Qwen's chat template as a user message, and the model writes the assistant's answer. The token view shows the whole template.
In assistant mode, the **system prompt** is editable, so you can change the assistant's personality and watch the effect on the next-token probabilities:
- Preset personas: Qwen default, Pip (the persona from the week 1 notebook), Butler, Grumpy flatmate, For children, and Secret keeper (a system prompt with a door code it must not reveal, for prompt-injection experiments, together with the example "The door code").
- An empty box is not "no system prompt": Qwen's chat template then inserts its own, "You are Qwen, created by Alibaba Cloud. You are a helpful assistant." The page says so and shows that text.
- The system prompt tokens are coloured violet in the token view, with their count, so you can see that the persona is just text the model reads before every message.
- Plain text mode has no system prompt. A persona there would have to be part of the text itself.
## Using it in class
- Open the direct app address (`https://<user>-<space>.hf.space`) full screen on the projector. Keyboard shortcuts work once you have clicked into the page.
- Shortcuts (cursor outside the text box, press Esc): Space roll the dice, F take the favourite, A write on its own, Backspace undo, C clear the answer, N next example, ↑ ↓ temperature.
- On **ZeroGPU**, GPU time counts against the daily quota of whoever is using the page. Be logged in to Hugging Face in the projector browser so it counts against your account. Anonymous visitors share a small IP-based quota, which a whole class behind one university address would use up quickly.
- On a **free CPU**, the same code runs without changes: about one to two seconds for the first step of a new text, a fraction of a second for each further token.
## How it works
The page is a single `gr.HTML` component (`ui/template.html`, `ui/style.css`, `ui/app.js`).
- Tokenizing while you type is a Gradio server function: CPU only, no queue, no GPU. It also returns where the system prompt sits in the token sequence.
- Every model step is a normal queued Gradio event, so ZeroGPU attributes the GPU time to the visitor. The page writes the request into the component's value and fires `submit`, the reply comes back the same way.
- One GPU call can take several steps: "Write on its own" asks for 8 tokens at once and replays them with the dice animation.
- The model sees exactly the generated token numbers, never re-tokenized text. Sampling is exact over the whole output layer at the chosen temperature.
Settings (Space variables): `MODEL_ID` (default `Qwen/Qwen2.5-1.5B-Instruct`), `DTYPE` (`float32` or `bfloat16`), `MAX_INPUT_TOKENS` (default 1024, system prompt and message together).
## Privacy
Text typed into the page is sent to this Space to be processed and is not stored or logged by the app. Do not type personal data.