Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,120 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# Sidebar contents
|
| 5 |
with st.sidebar:
|
| 6 |
st.title("The School of AI Tokenization App")
|
|
@@ -22,10 +136,21 @@ question = st.text_input("Please enter Hindi text: ")
|
|
| 22 |
|
| 23 |
if question:
|
| 24 |
response = "Your text is " + question
|
| 25 |
-
st.header("
|
| 26 |
st.write(response)
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
|
| 4 |
+
|
| 5 |
+
from urllib.request import urlopen
|
| 6 |
+
from bs4 import BeautifulSoup
|
| 7 |
+
|
| 8 |
+
from urllib.request import urlopen
|
| 9 |
+
from bs4 import BeautifulSoup
|
| 10 |
+
|
| 11 |
+
url = "https://raw.githubusercontent.com/cltk/hindi_text_ltrc/master/tulasidaas/Raamacharita_maanasa/1/main.txt"
|
| 12 |
+
html = urlopen(url).read()
|
| 13 |
+
soup = BeautifulSoup(html, features="html.parser")
|
| 14 |
+
|
| 15 |
+
# kill all script and style elements
|
| 16 |
+
for script in soup(["script", "style"]):
|
| 17 |
+
script.extract() # rip it out
|
| 18 |
+
|
| 19 |
+
# get text
|
| 20 |
+
text = soup.get_text()
|
| 21 |
+
ramayana_text = text
|
| 22 |
+
print(type(text))
|
| 23 |
+
|
| 24 |
+
print(text[:1000])
|
| 25 |
+
|
| 26 |
+
# here are all the unique characters that occur in this text
|
| 27 |
+
chars = sorted(list(set(text)))
|
| 28 |
+
vocab_size = len(chars)
|
| 29 |
+
print(''.join(chars))
|
| 30 |
+
print(vocab_size)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def get_stats(ids):
|
| 34 |
+
counts = {}
|
| 35 |
+
for pair in zip(ids, ids[1:]):
|
| 36 |
+
counts[pair] = counts.get(pair, 0) + 1
|
| 37 |
+
return counts
|
| 38 |
+
|
| 39 |
+
def merge(ids, pair, idx):
|
| 40 |
+
newids = []
|
| 41 |
+
i = 0
|
| 42 |
+
while i < len(ids):
|
| 43 |
+
if i < len(ids) - 1 and ids[i] == pair[0] and ids[i+1] == pair[1]:
|
| 44 |
+
newids.append(idx)
|
| 45 |
+
i += 2
|
| 46 |
+
else:
|
| 47 |
+
newids.append(ids[i])
|
| 48 |
+
i += 1
|
| 49 |
+
return newids
|
| 50 |
+
|
| 51 |
+
# ---
|
| 52 |
+
#text = "नाम जीहँ जपि जागहिं जोगी। बिरति बिरंचि प्रपंच बियोगी॥"
|
| 53 |
+
tokens = text.encode("utf-8") # raw bytes
|
| 54 |
+
tokens = list(map(int, tokens)) # convert to a list of integers in range 0..255 for convenience
|
| 55 |
+
|
| 56 |
+
vocab_size = 1000 # the desired final vocabulary size
|
| 57 |
+
num_merges = vocab_size - 256
|
| 58 |
+
ids = list(tokens) # copy so we don't destroy the original list
|
| 59 |
+
|
| 60 |
+
merges = {} # (int, int) -> int
|
| 61 |
+
for i in range(num_merges):
|
| 62 |
+
stats = get_stats(ids)
|
| 63 |
+
pair = max(stats, key=stats.get)
|
| 64 |
+
idx = 256 + i
|
| 65 |
+
# print(f"merging {pair} into a new token {idx}")
|
| 66 |
+
ids = merge(ids, pair, idx)
|
| 67 |
+
merges[pair] = idx
|
| 68 |
+
|
| 69 |
+
print("tokens length:", len(tokens))
|
| 70 |
+
print("ids length:", len(ids))
|
| 71 |
+
print(f"compression ratio: {len(tokens) / len(ids):.2f}X")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
vocab = {idx: bytes([idx]) for idx in range(256)}
|
| 75 |
+
for (p0, p1), idx in merges.items():
|
| 76 |
+
vocab[idx] = vocab[p0] + vocab[p1]
|
| 77 |
+
|
| 78 |
+
def decode(ids):
|
| 79 |
+
# given ids (list of integers), return Python string
|
| 80 |
+
tokens = b"".join(vocab[idx] for idx in ids)
|
| 81 |
+
text = tokens.decode("utf-8", errors="replace")
|
| 82 |
+
return text
|
| 83 |
+
|
| 84 |
+
print(decode([261]))
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def encode(text):
|
| 88 |
+
# given a string, return list of integers (the tokens)
|
| 89 |
+
tokens = list(text.encode("utf-8"))
|
| 90 |
+
while len(tokens) >= 2:
|
| 91 |
+
stats = get_stats(tokens)
|
| 92 |
+
pair = min(stats, key=lambda p: merges.get(p, float("inf")))
|
| 93 |
+
if pair not in merges:
|
| 94 |
+
break # nothing else can be merged
|
| 95 |
+
idx = merges[pair]
|
| 96 |
+
tokens = merge(tokens, pair, idx)
|
| 97 |
+
return tokens
|
| 98 |
+
|
| 99 |
+
msg = "पुलिस की मानें तो ये वारदात सुलिभंजन इलाके की है"
|
| 100 |
+
tk = list(encode(msg))
|
| 101 |
+
|
| 102 |
+
print("tokens length:", len(tk))
|
| 103 |
+
print(decode(encode(msg)))
|
| 104 |
+
print(tk)
|
| 105 |
+
#print("Total length:", len(ids))
|
| 106 |
+
#print(f"compression ratio: {len(tokens) / len(ids):.2f}X")
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
# Sidebar contents
|
| 119 |
with st.sidebar:
|
| 120 |
st.title("The School of AI Tokenization App")
|
|
|
|
| 136 |
|
| 137 |
if question:
|
| 138 |
response = "Your text is " + question
|
| 139 |
+
st.header("Tokenization:")
|
| 140 |
st.write(response)
|
| 141 |
|
| 142 |
|
| 143 |
+
msg = "पुलिस की मानें तो ये वारदात सुलिभंजन इलाके की है"
|
| 144 |
+
tk = list(encode(question))
|
| 145 |
+
response = "Tokens length:", len(tk), decode(encode(msg))
|
| 146 |
+
print("tokens length:", len(tk))
|
| 147 |
+
print(decode(encode(msg)))
|
| 148 |
+
print(tk)
|
| 149 |
+
st.write(response)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
|
| 154 |
|
| 155 |
|
| 156 |
|