Skip to content

Instantly share code, notes, and snippets.

@roflsunriz
Created April 23, 2025 13:54
Show Gist options
  • Save roflsunriz/b174185ce0c6a3bde04fcceea4771658 to your computer and use it in GitHub Desktop.
Save roflsunriz/b174185ce0c6a3bde04fcceea4771658 to your computer and use it in GitHub Desktop.
LLM context compression GUI : This program provides a GUI interface for compressing text using LZMA2 compression. It displays compression ratios and provides the compressed result with decompression instructions for LLMs.
#!/usr/bin/env python3
"""
LLM context compression GUI
This program provides a GUI interface for compressing text using LZMA2 compression.
It displays compression ratios and provides the compressed result with decompression
instructions for LLMs.
"""
import lzma
import base64
import tkinter as tk
from tkinter import ttk, scrolledtext
import textwrap
def compress_text(text):
"""Compress text using LZMA2 and return compressed data with instructions."""
if not text.strip():
return "テキストを入力してください。", 0, 0, 0
# Encode the text to bytes
data = text.encode('utf-8')
original_size = len(data)
# Compress with LZMA2
compressed = lzma.compress(data, preset=9 | lzma.PRESET_EXTREME)
compressed_size = len(compressed)
# Calculate compression ratio
ratio = compressed_size / original_size * 100
# Encode the compressed data to base64 for easier handling
b64_data = base64.b64encode(compressed).decode('ascii')
# Create the instruction header
instruction = (
"このデータはLZMA2で圧縮されたテキストです。以下の手順で解凍できます:\n"
"1. base64デコードして圧縮バイナリデータを取得\n"
"2. LZMA2アルゴリズムで解凍\n"
"3. 解凍されたバイト列をUTF-8でデコード\n\n"
"Python解凍コード例:\n"
"```python\n"
"import lzma, base64\n"
"compressed = base64.b64decode(「圧縮データ」)\n"
"original = lzma.decompress(compressed).decode('utf-8')\n"
"print(original)\n"
"```\n\n"
"-----圧縮データここから-----\n"
f"{b64_data}\n"
"-----圧縮データここまで-----"
)
return instruction, original_size, compressed_size, ratio
class LLMCompressionGUI:
def __init__(self, root):
self.root = root
self.root.title("LZMA2テキスト圧縮ツール")
self.root.geometry("800x600")
# Create a main frame with padding
main_frame = ttk.Frame(root, padding="10")
main_frame.pack(fill=tk.BOTH, expand=True)
# Input text area
ttk.Label(main_frame, text="圧縮するテキスト:").pack(anchor="w", pady=(0, 5))
self.input_text = scrolledtext.ScrolledText(main_frame, width=80, height=10, wrap=tk.WORD)
self.input_text.pack(fill=tk.BOTH, expand=True, pady=(0, 10))
# Compress button
compress_button = ttk.Button(main_frame, text="圧縮する", command=self.compress_and_display)
compress_button.pack(pady=(0, 10))
# Stats frame
stats_frame = ttk.Frame(main_frame)
stats_frame.pack(fill=tk.X, pady=(0, 10))
# Labels for statistics
self.original_size_var = tk.StringVar(value="元のサイズ: --")
self.compressed_size_var = tk.StringVar(value="圧縮後サイズ: --")
self.ratio_var = tk.StringVar(value="圧縮率: --")
ttk.Label(stats_frame, textvariable=self.original_size_var).pack(side=tk.LEFT, padx=(0, 15))
ttk.Label(stats_frame, textvariable=self.compressed_size_var).pack(side=tk.LEFT, padx=(0, 15))
ttk.Label(stats_frame, textvariable=self.ratio_var).pack(side=tk.LEFT)
# Output text area
ttk.Label(main_frame, text="圧縮結果 (LLM解凍指示付き):").pack(anchor="w", pady=(0, 5))
self.output_text = scrolledtext.ScrolledText(main_frame, width=80, height=15, wrap=tk.WORD)
self.output_text.pack(fill=tk.BOTH, expand=True)
# Copy button
copy_button = ttk.Button(main_frame, text="結果をコピー", command=self.copy_to_clipboard)
copy_button.pack(pady=(10, 0))
def compress_and_display(self):
text = self.input_text.get("1.0", tk.END)
result, original_size, compressed_size, ratio = compress_text(text)
# Update statistics
self.original_size_var.set(f"元のサイズ: {original_size} バイト")
self.compressed_size_var.set(f"圧縮後サイズ: {compressed_size} バイト")
self.ratio_var.set(f"圧縮率: {ratio:.2f}%")
# Display result
self.output_text.delete("1.0", tk.END)
self.output_text.insert("1.0", result)
def copy_to_clipboard(self):
result = self.output_text.get("1.0", tk.END)
self.root.clipboard_clear()
self.root.clipboard_append(result)
self.root.update() # Required to finalize clipboard update
def main():
root = tk.Tk()
app = LLMCompressionGUI(root)
root.mainloop()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment