File size: 13,932 Bytes
424a951 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Intelligent Tokenizer v6.0 - Working Demo for Hugging Face Spaces
์ค์ ์๋ํ๋ ๋ฐ๋ชจ - ์๋ฎฌ๋ ์ด์
์์
"""
import gradio as gr
import torch
import sys
import io
from pathlib import Path
import json
import time
# UTF-8 ์ค์
if sys.stdout.encoding != 'utf-8':
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
# Add path
sys.path.append(str(Path(__file__).parent))
# Import actual modules
from core.boundary_aware_model import BoundaryAwareTokenizerModel
from src.core.byte_tokenizer_v6 import ByteTokenizerV6
# Device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class IntelligentTokenizerDemo:
def __init__(self):
"""Initialize the actual model"""
self.device = device
self.tokenizer = ByteTokenizerV6()
self.model = None
self.load_model()
def load_model(self):
"""Load the actual trained model"""
try:
# Try loading from pytorch_model.bin first (extracted weights)
model_path = Path("pytorch_model.bin")
if not model_path.exists():
# Fallback to checkpoint
model_path = Path("checkpoints/latest_checkpoint.pt")
if model_path.exists():
print(f"Loading model from {model_path}...")
checkpoint = torch.load(model_path, map_location=self.device, weights_only=False)
# Get model config
if 'model_config' in checkpoint:
model_config = checkpoint['model_config']
else:
# Load from config.json
with open("config.json", "r") as f:
config = json.load(f)
model_config = {
'vocab_size': config['vocab_size'],
'hidden_dim': config.get('decoder_hidden', 768),
'num_heads': config['num_heads'],
'num_encoder_layers': 5,
'num_decoder_layers': config['num_decoder_layers'],
'dropout': config['dropout']
}
# Initialize model
self.model = BoundaryAwareTokenizerModel(**model_config)
# Load weights
if 'model_state_dict' in checkpoint:
self.model.load_state_dict(checkpoint['model_state_dict'])
else:
self.model.load_state_dict(checkpoint)
self.model = self.model.to(self.device)
self.model.eval()
print("Model loaded successfully!")
else:
print("Warning: No model checkpoint found, using untrained model")
# Initialize untrained model for testing
model_config = {
'vocab_size': 260,
'hidden_dim': 768,
'num_heads': 8,
'num_encoder_layers': 5,
'num_decoder_layers': 6,
'dropout': 0.1
}
self.model = BoundaryAwareTokenizerModel(**model_config)
self.model = self.model.to(self.device)
self.model.eval()
except Exception as e:
print(f"Error loading model: {e}")
raise
def embed_text(self, text):
"""์ค์ ์๋ฒ ๋ฉ ์์ฑ"""
if not text:
return None, "Please enter text"
try:
# Encode text
encoded = self.tokenizer.encode(text)
byte_ids = encoded['input_ids']
# Truncate if too long
if len(byte_ids) > 256:
byte_ids = byte_ids[:256]
byte_ids[-1] = self.tokenizer.EOS
# Prepare tensors
input_ids = torch.tensor([byte_ids], device=self.device)
attention_mask = torch.tensor([encoded['attention_mask'][:len(byte_ids)]], device=self.device)
# Generate embeddings
with torch.no_grad():
encoder_outputs = self.model.encoder(input_ids, attention_mask)
embeddings = encoder_outputs['last_hidden_state']
# Statistics
original_bytes = len(text.encode('utf-8'))
compressed_tokens = embeddings.shape[1]
compression_ratio = original_bytes / compressed_tokens if compressed_tokens > 0 else 0
result = f"""โ
**Embedding Generated Successfully**
**Input Text:** {text[:100]}{'...' if len(text) > 100 else ''}
**Original Size:** {original_bytes} bytes
**Compressed Size:** {compressed_tokens} tokens
**Compression Ratio:** {compression_ratio:.2f}x
**Embedding Shape:** {list(embeddings.shape)}
**Device:** {self.device}
**First 10 values:** {embeddings[0, 0, :10].cpu().numpy().tolist()}
"""
return embeddings, result
except Exception as e:
return None, f"Error: {str(e)}"
def restore_text(self, text):
"""์ค์ ๋ณต์ ํ
์คํธ"""
if not text:
return "Please enter text"
try:
# Encode text
encoded = self.tokenizer.encode(text)
byte_ids = encoded['input_ids']
# Truncate if needed
if len(byte_ids) > 256:
byte_ids = byte_ids[:256]
byte_ids[-1] = self.tokenizer.EOS
truncated = True
else:
truncated = False
if len(byte_ids) <= 1:
return "Text too short for restoration test"
# Prepare tensors
input_ids = torch.tensor([byte_ids], device=self.device)
attention_mask = torch.tensor([encoded['attention_mask'][:len(byte_ids)]], device=self.device)
# Teacher forcing restoration
with torch.no_grad():
decoder_input = input_ids[:, :-1]
labels = input_ids[:, 1:]
outputs = self.model(
input_ids=input_ids,
attention_mask=attention_mask,
decoder_input_ids=decoder_input,
labels=labels,
use_cross_attention=True
)
# Get predictions
predictions = torch.argmax(outputs['logits'], dim=-1)
accuracy = (predictions == labels).float().mean().item()
# Decode predictions
pred_list = predictions[0].cpu().tolist()
full_sequence = [self.tokenizer.BOS] + pred_list
# Convert to text
filtered = [b for b in full_sequence if 0 <= b < 256]
if filtered:
restored_bytes = bytes(filtered)
restored_text = restored_bytes.decode('utf-8', errors='ignore')
else:
restored_text = "[Unable to restore]"
result = f"""โ
**Restoration Test Complete**
**Original Text:** {text[:100]}{'...' if len(text) > 100 else ''}
**Restored Text:** {restored_text[:100]}{'...' if len(restored_text) > 100 else ''}
**Accuracy:** {accuracy:.1%}
**Bytes Processed:** {len(byte_ids)}
{'**Note:** Text was truncated to 256 bytes' if truncated else ''}
**Status:** {'Perfect Match! โจ' if accuracy > 0.95 else 'Good Match' if accuracy > 0.8 else 'Partial Match'}
"""
return result
except Exception as e:
return f"Error: {str(e)}"
def compress_stats(self, text):
"""์์ถ ํต๊ณ ๋ถ์"""
if not text:
return "Please enter text"
try:
lines = text.strip().split('\n')
results = []
for line in lines[:10]: # Limit to 10 lines
if not line.strip():
continue
# Get compression stats
encoded = self.tokenizer.encode(line)
byte_ids = encoded['input_ids']
if len(byte_ids) > 256:
byte_ids = byte_ids[:256]
input_ids = torch.tensor([byte_ids], device=self.device)
attention_mask = torch.tensor([encoded['attention_mask'][:len(byte_ids)]], device=self.device)
with torch.no_grad():
encoder_outputs = self.model.encoder(input_ids, attention_mask)
compressed_size = encoder_outputs['last_hidden_state'].shape[1]
original_size = len(line.encode('utf-8'))
ratio = original_size / compressed_size if compressed_size > 0 else 0
results.append({
'text': line[:50] + '...' if len(line) > 50 else line,
'original': original_size,
'compressed': compressed_size,
'ratio': ratio
})
# Format results
output = "**Compression Analysis Results**\n\n"
output += "| Text | Original | Compressed | Ratio |\n"
output += "|------|----------|------------|-------|\n"
for r in results:
output += f"| {r['text']} | {r['original']} bytes | {r['compressed']} tokens | {r['ratio']:.2f}x |\n"
# Average stats
if results:
avg_ratio = sum(r['ratio'] for r in results) / len(results)
total_original = sum(r['original'] for r in results)
total_compressed = sum(r['compressed'] for r in results)
output += f"\n**Summary:**\n"
output += f"- Average Compression: {avg_ratio:.2f}x\n"
output += f"- Total Original: {total_original} bytes\n"
output += f"- Total Compressed: {total_compressed} tokens\n"
output += f"- Overall Ratio: {total_original/total_compressed if total_compressed > 0 else 0:.2f}x\n"
return output
except Exception as e:
return f"Error: {str(e)}"
# Initialize demo
print("Initializing Intelligent Tokenizer Demo...")
demo = IntelligentTokenizerDemo()
# Gradio Interface
with gr.Blocks(title="Intelligent Tokenizer v6.0", theme=gr.themes.Base()) as app:
gr.Markdown("""
# ๐ Intelligent Tokenizer v6.0 - Live Demo
**World's First Pure Learning-Based Byte-Level Tokenizer**
- No vocabulary files, no language rules - just intelligence!
- 260 fixed vocab (256 bytes + 4 special tokens)
- Works with ANY language/script/emoji
""")
with gr.Tab("๐ค Embedding"):
with gr.Row():
with gr.Column():
embed_input = gr.Textbox(
label="Input Text",
placeholder="Enter any text in any language...",
lines=3
)
embed_btn = gr.Button("Generate Embedding", variant="primary")
with gr.Column():
embed_output = gr.Markdown(label="Result")
embed_btn.click(
lambda x: demo.embed_text(x)[1],
inputs=embed_input,
outputs=embed_output
)
with gr.Tab("๐ Restoration"):
with gr.Row():
with gr.Column():
restore_input = gr.Textbox(
label="Input Text",
placeholder="Enter text to test restoration...",
lines=3
)
restore_btn = gr.Button("Test Restoration", variant="primary")
with gr.Column():
restore_output = gr.Markdown(label="Result")
restore_btn.click(
demo.restore_text,
inputs=restore_input,
outputs=restore_output
)
with gr.Tab("๐ Compression Analysis"):
with gr.Row():
with gr.Column():
compress_input = gr.Textbox(
label="Input Text (one item per line)",
placeholder="Enter multiple texts, one per line...",
lines=5
)
compress_btn = gr.Button("Analyze Compression", variant="primary")
with gr.Column():
compress_output = gr.Markdown(label="Analysis")
compress_btn.click(
demo.compress_stats,
inputs=compress_input,
outputs=compress_output
)
with gr.Tab("โน๏ธ About"):
gr.Markdown("""
## About Intelligent Tokenizer v6.0
### Key Features:
- **Pure Learning-Based**: No predefined rules or vocabularies
- **Universal Coverage**: Works with all 204+ languages equally
- **Compression**: 2-3x currently, targeting 5-10x
- **Real Model**: This demo uses the actual trained model (1.2GB)
### Architecture:
- Encoder: 5-layer transformer (512โ768 dims)
- Decoder: 6-layer transformer (768 hidden)
- Total: ~274M parameters
- Training: 23 epochs on multilingual data
### Development:
- Solo developer, 4 months development
- Trained on personal RTX 3060
- No prior AI experience
### Links:
- [GitHub Repository](https://github.com/ggunio/intelligent-tokenizer)
- [Hugging Face Model](https://huggingface.co/ggunio/intelligent-tokenizer-v6)
""")
if __name__ == "__main__":
print(f"Running on device: {device}")
print("Launching Gradio app...")
app.launch() |