atiwari751 commited on
Commit
c128a5f
·
1 Parent(s): e51d186

1.4M text 7.53X compression

Browse files
Files changed (5) hide show
  1. .gitignore +1 -0
  2. BPE.py +4 -4
  3. decoded_output.txt +3 -1
  4. encode_decode.py +4 -4
  5. encode_input.txt +3 -1
.gitignore CHANGED
@@ -1,2 +1,3 @@
1
  .venv
2
  __pycache__
 
 
1
  .venv
2
  __pycache__
3
+ test.csv
BPE.py CHANGED
@@ -1,4 +1,5 @@
1
  import pickle
 
2
 
3
  # Read text from a file
4
  with open('text_file.txt', 'r', encoding='utf-8') as file:
@@ -27,16 +28,16 @@ def merge(ids, pair, idx):
27
  return newids
28
 
29
  def perform_bpe():
30
- vocab_size = 1500 # the desired final vocabulary size
31
  num_merges = vocab_size - 256
32
  ids = list(tokens) # copy so we don't destroy the original list
33
 
34
  merges = {} # (int, int) -> int
35
- for i in range(num_merges):
 
36
  stats = get_stats(ids)
37
  pair = max(stats, key=stats.get)
38
  idx = 256 + i
39
- #print(f"merging {pair} into a new token {idx}")
40
  ids = merge(ids, pair, idx)
41
  merges[pair] = idx
42
 
@@ -50,7 +51,6 @@ if __name__ == "__main__":
50
  print('---')
51
  print("length of text:", len(text))
52
  print('---')
53
- #print(tokens)
54
  print("length of tokens:", len(tokens))
55
 
56
  # Run BPE and save results
 
1
  import pickle
2
+ from tqdm import tqdm # Import tqdm for progress bar
3
 
4
  # Read text from a file
5
  with open('text_file.txt', 'r', encoding='utf-8') as file:
 
28
  return newids
29
 
30
  def perform_bpe():
31
+ vocab_size = 3500 # the desired final vocabulary size
32
  num_merges = vocab_size - 256
33
  ids = list(tokens) # copy so we don't destroy the original list
34
 
35
  merges = {} # (int, int) -> int
36
+ # Use tqdm to add a progress bar
37
+ for i in tqdm(range(num_merges), desc="Performing BPE", unit="merge"):
38
  stats = get_stats(ids)
39
  pair = max(stats, key=stats.get)
40
  idx = 256 + i
 
41
  ids = merge(ids, pair, idx)
42
  merges[pair] = idx
43
 
 
51
  print('---')
52
  print("length of text:", len(text))
53
  print('---')
 
54
  print("length of tokens:", len(tokens))
55
 
56
  # Run BPE and save results
decoded_output.txt CHANGED
@@ -1 +1,3 @@
1
- ाव
 
 
 
1
+ " प ठ ान क ोट पहुंच े PM मोद ी, ए यर ब ेस का � � ाय ज ा ल े ब ॉर्ड र � �लाक ों क ा कर ेंग े ह वाई सर्व े "," प्र धानमंत्र ी नरेंद्र मोद ी प ठ ान क ोट ए यर ब ेस पहुंच गए हैं. � �े � �यर ब ेस में स ुरक्ष ा के � �ाल ात का � � ाय ज ा ल े रह े हैं � �र � � ाय ुस ेन ाक र्म ियों से � �िल रह े हैं . स ुबह करीब स व ा द स बजे � �्रधानमंत्र ी प ंजाब के प ठ ान क ोट के लिए रव ाना � �ुए . � �यर ब ेस का � � ाय ज ा ल े ने के बाद प्रधानमंत्र ी ब ॉर्ड र � �लाक ों का � � वाई सर्व ेक ्ष ण भ ी कर ेंगे. � � ठ ान क ोट ए यर ब ेस पर � �िछले � � फ्त े � �तंक ियों ने � �मल ा क िया था. � �ाकिस्तान से � �ए आतंक ियों के � �मल े को � �ि� � ल कर द िया गय ा थ ा. स भ ी 6 पाकिस्तान ी � �तंक ी म ारे � �ए थे . 7 सुरक्ष ाबल भी � �ह ीद हुए थे. � �ारत ने � �ाकिस्तान को स ब ूत स ौंप ते हुए द ोष ियों के � �िलाफ स ख्त कार ्रवाई करने क ो कह ा है.
2
+ ज ानकार ी के म ुताबिक , प्रधानमंत्र ी के साथ आ र्म ी और � � य रफ ो र्स के � � ीफ भी � �ौजूद रह सक ते हैं. � �यर ब ेस पर
3
+ प ाकिस्तान ी � �तंक ियों
encode_decode.py CHANGED
@@ -11,8 +11,8 @@ for (p0, p1), idx in merges.items():
11
 
12
  def decode(ids):
13
  # given ids (list of integers), return Python string
14
- tokens = b"".join(vocab[idx] for idx in ids)
15
- text = tokens.decode("utf-8", errors="replace")
16
 
17
  # Write the decoded text to a new file
18
  with open('decoded_output.txt', 'w', encoding='utf-8') as f:
@@ -21,8 +21,8 @@ def decode(ids):
21
  return text
22
 
23
  # Example: Decode a list of IDs
24
- set = [281, 939, 280, 494, 274, 1128, 499, 1011, 387, 296, 297, 150, 329, 830, 176, 270, 1135, 1031, 282, 264]
25
- decoded_text = decode([499]) # Adjust the ID list as needed for your test
26
  print(decoded_text)
27
 
28
  def encode():
 
11
 
12
  def decode(ids):
13
  # given ids (list of integers), return Python string
14
+ tokens = [vocab[idx].decode("utf-8", errors="replace") for idx in ids]
15
+ text = ' '.join(tokens) # Join tokens with a single space
16
 
17
  # Write the decoded text to a new file
18
  with open('decoded_output.txt', 'w', encoding='utf-8') as f:
 
21
  return text
22
 
23
  # Example: Decode a list of IDs
24
+ set_of_ids = [34, 293, 474, 298, 275, 575, 1271, 260, 778, 1298, 763, 611, 1921, 310, 424, 352, 156, 347, 318, 947, 1410, 1832, 276, 2984, 314, 262, 770, 639, 2516, 1020, 3054, 260, 795, 1072, 993, 2392, 499, 474, 298, 275, 575, 611, 1921, 310, 424, 1271, 854, 940, 1761, 3036, 310, 424, 932, 1060, 661, 918, 342, 352, 156, 347, 318, 947, 1453, 1483, 324, 181, 347, 863, 591, 412, 606, 2234, 789, 481, 751, 587, 2039, 1750, 289, 301, 565, 278, 2675, 1532, 499, 1898, 820, 474, 298, 275, 575, 410, 3428, 1195, 569, 295, 3036, 310, 424, 352, 156, 347, 318, 947, 260, 1033, 2697, 495, 1832, 276, 2984, 761, 185, 1020, 3054, 377, 401, 430, 471, 953, 2232, 170, 474, 298, 275, 575, 611, 1921, 310, 424, 419, 2029, 185, 869, 268, 1254, 1998, 1842, 317, 2214, 1630, 376, 1141, 1709, 1909, 1842, 1200, 514, 171, 281, 798, 904, 510, 1865, 418, 264, 890, 1877, 272, 1254, 485, 1069, 967, 2100, 1046, 55, 2355, 2555, 563, 3352, 654, 1477, 1622, 708, 405, 1630, 859, 310, 1130, 289, 2817, 762, 336, 979, 1909, 867, 289, 3316, 1205, 1811, 1266, 2718, 886, 318, 1303, 1861, 1029, 44, 2697, 2033, 390, 606, 686, 143, 311, 1537, 271, 806, 286, 154, 1106, 563, 2460, 481, 786, 1061, 3036, 310, 424, 810, 2250, 881, 272, 1254, 1135]
25
+ decoded_text = decode(set_of_ids) # Pass the list of IDs
26
  print(decoded_text)
27
 
28
  def encode():
encode_input.txt CHANGED
@@ -1 +1,3 @@
1
- लोकसभा चुनाव 24 तारीख को वे रोते हुए पूछती
 
 
 
1
+ "पठानकोट पहुंचे PM मोदी, एयरबेस का जायजा ले बॉर्डर इलाकों का करेंगे हवाई सर्वे","प्रधानमंत्री नरेंद्र मोदी पठानकोट एयरबेस पहुंच गए हैं. वे एयरबेस में सुरक्षा के हालात का जायजा ले रहे हैं और वायुसेनाकर्मियों से मिल रहे हैं. सुबह करीब सवा दस बजे प्रधानमंत्री पंजाब के पठानकोट के लिए रवाना हुए. एयरबेस का जायजा लेने के बाद प्रधानमंत्री बॉर्डर इलाकों का हवाई सर्वेक्षण भी करेंगे. पठानकोट एयरबेस पर पिछले हफ्ते आतंकियों ने हमला किया था. पाकिस्तान से आए आतंकियों के हमले को विफल कर दिया गया था. सभी 6 पाकिस्तानी आतंकी मारे गए थे. 7 सुरक्षाबल भी शहीद हुए थे. भारत ने पाकिस्तान को सबूत सौंपते हुए दोषियों के खिलाफ सख्त कार्रवाई करने को कहा है.
2
+ जानकारी के मुताबिक, प्रधानमंत्री के साथ आर्मी और एयरफोर्स के चीफ भी मौजूद रह सकते हैं. एयरबेस पर
3
+ पाकिस्तानी आतंकियों