muhsin commited on
Commit
8988399
·
1 Parent(s): 1b90c64

a working version using tiktoken

Browse files
Files changed (3) hide show
  1. app.py +81 -2
  2. dev_notebooks/01_out_of_box.ipynb +11 -10
  3. requirements.txt +2 -0
app.py CHANGED
@@ -1,4 +1,83 @@
1
  import streamlit as st
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import tiktoken
3
+ from utils.metrics import calculate_compression
4
 
5
+ st.set_page_config(
6
+ page_title="Malayalam Tokenizer",
7
+ page_icon="🔤",
8
+ layout="wide"
9
+ )
10
+
11
+ st.title("Malayalam Tokenizer 🔤")
12
+ st.markdown("""
13
+ A tool to analyze text tokenization using OpenAI's tiktoken tokenizer.
14
+ This helps understand how text is processed for language models.
15
+ """)
16
+
17
+
18
+ default_text = """ഇതൊരു Malayalam ടോക്കനൈസർ ആണ് 🪧.
19
+ വാചകത്തെ ടോക്കണുകൾ എന്ന് വിളിക്കുന്ന ചെറിയ കഷണങ്ങളായി വിഭജിക്കുന്ന ഒരു method aanu ടോക്കനൈസർ.
20
+ ഈ ടോക്കണുകൾ വാക്കുകളോ വാക്കുകളുടെ ഭാഗങ്ങളോ പ്രതീകങ്ങളോ ആകാം.
21
+ ടെക്‌സ്‌റ്റ് കൂടുതൽ കാര്യക്ഷമമായി മനസ്സിലാക്കാനും പ്രോസസ്സ് ചെയ്യാനും ഇത് language modelukale സഹായിക്കുന്നു.🎂"""
22
+
23
+ # Text input
24
+ text = st.text_area(
25
+ "Enter your text:",
26
+ value=default_text,
27
+ height=150,
28
+ help="Enter any text you want to analyze. The text will be tokenized using tiktoken's cl100k_base encoding."
29
+ )
30
+
31
+ if text:
32
+ col1, col2, col3 = st.columns(3)
33
+
34
+ unicode_bytes = text.encode("utf-8")
35
+ enc = tiktoken.get_encoding("cl100k_base")
36
+ tokens = enc.encode(text)
37
+
38
+ with col1:
39
+ st.metric("Raw Text Length", f"{len(text)} chars")
40
+
41
+ with col2:
42
+ st.metric("UTF-8 Encoded Length", f"{len(unicode_bytes)} bytes")
43
+
44
+ with col3:
45
+ st.metric("Token Count", f"{len(tokens)} tokens")
46
+
47
+ compression = calculate_compression(text, tokens)
48
+ st.markdown("### Compression Analysis")
49
+
50
+ comp_col1, comp_col2 = st.columns(2)
51
+ with comp_col1:
52
+ st.metric(
53
+ "Compression Ratio",
54
+ f"{compression['compression_ratio']:.2f}x",
55
+ help="Higher ratio means better compression. Shows how many characters are represented by each token on average."
56
+ )
57
+
58
+ with comp_col2:
59
+ st.metric(
60
+ "Space Saving",
61
+ f"{compression['space_saving_percentage']:.1f}%",
62
+ help="Percentage of space saved by using tokens instead of raw text."
63
+ )
64
+
65
+ st.markdown("### Token Details")
66
+ token_col1, token_col2 = st.columns(2)
67
+
68
+ with token_col1:
69
+ st.markdown("#### First 10 Token IDs")
70
+ st.code(str(tokens[:10]), language="python")
71
+
72
+ with token_col2:
73
+ st.markdown("#### First 10 UTF-8 Bytes")
74
+ st.code(str(list(unicode_bytes[:10])), language="python")
75
+
76
+ with st.expander("ℹ️ How to interpret these results"):
77
+ st.markdown("""
78
+ - **Raw Text Length**: Number of characters in your input text
79
+ - **UTF-8 Encoded Length**: Size of text when encoded in UTF-8 format
80
+ - **Token Count**: Number of tokens the text is broken into
81
+ - **Compression Ratio**: How many characters are represented by each token on average
82
+ - **Space Saving**: Percentage reduction in size when using tokens vs raw text
83
+ """)
dev_notebooks/01_out_of_box.ipynb CHANGED
@@ -44,9 +44,10 @@
44
  "metadata": {},
45
  "outputs": [],
46
  "source": [
47
- "text = \"\"\"This is a malayalam tokeniser. A tokenizer is a tool that breaks text into smaller pieces called tokens. These tokens can be words, parts of words, or even characters. It helps computers understand and process text more efficiently.\n",
48
- "\n",
49
- "Think of it like cutting a big cake into slices. so it's easier to eat in mouth 🎂 \"\"\""
 
50
  ]
51
  },
52
  {
@@ -58,7 +59,7 @@
58
  "name": "stdout",
59
  "output_type": "stream",
60
  "text": [
61
- "Length of the input text: 316\n"
62
  ]
63
  }
64
  ],
@@ -75,8 +76,8 @@
75
  "name": "stdout",
76
  "output_type": "stream",
77
  "text": [
78
- "Length of the encoded text: 319\n",
79
- "First few bytes: [84, 104, 105, 115, 32, 105, 115, 32, 97, 32]\n"
80
  ]
81
  }
82
  ],
@@ -122,8 +123,8 @@
122
  "name": "stdout",
123
  "output_type": "stream",
124
  "text": [
125
- "Length of the tokenized text: 70\n",
126
- "First few token ids: [2028, 374, 264, 8811, 352, 17243, 4037, 12329, 13, 362]\n"
127
  ]
128
  }
129
  ],
@@ -142,8 +143,8 @@
142
  "name": "stdout",
143
  "output_type": "stream",
144
  "text": [
145
- "Compression ratio: 4.514285714285714\n",
146
- "Space saving percentage: 77.84810126582279\n"
147
  ]
148
  }
149
  ],
 
44
  "metadata": {},
45
  "outputs": [],
46
  "source": [
47
+ "text = \"\"\"ഇതൊരു Malayalam ടോക്കനൈസർ ആണ് 🪧. \n",
48
+ "വാചകത്തെ ടോക്കണുകൾ എന്ന് വിളിക്കുന്ന ചെറിയ കഷണങ്ങളായി വിഭജിക്കുന്ന ഒരു method aanu ടോക്കനൈസർ. \n",
49
+ " ടോക്കണുകൾ വാക്കുകളോ വാക്കുകളുടെ ഭാഗങ്ങളോ പ്രതീകങ്ങളോ ആകാം. \n",
50
+ "ടെക്‌സ്‌റ്റ് കൂടുതൽ കാര്യക്ഷമമായി മനസ്സിലാക്കാനും പ്രോസസ്സ് ചെയ്യാനും ഇത് language modelukale സഹായിക്കുന്നു.🎂\"\"\""
51
  ]
52
  },
53
  {
 
59
  "name": "stdout",
60
  "output_type": "stream",
61
  "text": [
62
+ "Length of the input text: 300\n"
63
  ]
64
  }
65
  ],
 
76
  "name": "stdout",
77
  "output_type": "stream",
78
  "text": [
79
+ "Length of the encoded text: 750\n",
80
+ "First few bytes: [224, 180, 135, 224, 180, 164, 224, 181, 138, 224]\n"
81
  ]
82
  }
83
  ],
 
123
  "name": "stdout",
124
  "output_type": "stream",
125
  "text": [
126
+ "Length of the tokenized text: 428\n",
127
+ "First few token ids: [34839, 229, 34839, 97, 51211, 232, 34839, 108, 51211, 223]\n"
128
  ]
129
  }
130
  ],
 
143
  "name": "stdout",
144
  "output_type": "stream",
145
  "text": [
146
+ "Compression ratio: 0.7009345794392523\n",
147
+ "Space saving percentage: -42.66666666666667\n"
148
  ]
149
  }
150
  ],
requirements.txt CHANGED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit==1.42.0
2
+ tiktoken==0.6.0