build-small-hackathon/code-shrink-token-decimator
1
1import sys2import types3import ast4import re5import json6 7# ๐จ DYNAMIC FIX: Python 3.13 Compatibility Core Patches8if 'audioop' not in sys.modules:9 dummy_audioop = types.ModuleType('audioop')10 dummy_audioop.error = Exception11 sys.modules['audioop'] = dummy_audioop12 13if 'pyaudioop' not in sys.modules:14 dummy_pyaudioop = types.ModuleType('pyaudioop')15 dummy_pyaudioop.error = Exception16 sys.modules['pyaudioop'] = dummy_pyaudioop17 18try:19 import huggingface_hub20except ImportError:21 huggingface_hub = types.ModuleType('huggingface_hub')22 sys.modules['huggingface_hub'] = huggingface_hub23 24if not hasattr(huggingface_hub, 'HfFolder'):25 class DummyHfFolder:26 @staticmethod27 def get_token(): return None28 @staticmethod29 def save_token(token): pass30 @staticmethod31 def delete_token(): pass32 huggingface_hub.HfFolder = DummyHfFolder33 34import gradio as gr35 36def estimate_tokens(text):37 """Ultra-fast local token estimator (Roughly 1 token = 4 chars for code/text setup)"""38 if not text:39 return 040 return max(1, len(text) // 4 + text.count(' ') // 2)41 42def shrink_python_code(source_code):43 """Parses and strips syntax trees to remove bloat tokens natively"""44 try:45 tree = ast.parse(source_code)46 for node in ast.walk(tree):47 if isinstance(node, (ast.FunctionDef, ast.ClassDef, ast.Module)):48 if node.body and isinstance(node.body[0], ast.Expr) and isinstance(node.body[0].value, ast.Constant) and isinstance(node.body[0].value.value, str):49 node.body.pop(0)50 51 clean_code = ast.unparse(tree)52 clean_code = re.sub(r'\n\s*\n', '\n', clean_code)53 return clean_code.strip()54 except Exception:55 code = re.sub(r'#.*', '', source_code)56 code = re.sub(r'\"\"\"[\s\S]*?\"\"\"', '', code)57 code = re.sub(r'\'\'\'[\s\S]*?\'\'\'', '', code)58 code = re.sub(r'\n\s*\n', '\n', code)59 return code.strip()60 61def shrink_generic_text(raw_data):62 """Minifies structured JSON/Data arrays and repetitive text strings"""63 try:64 parsed_json = json.loads(raw_data)65 return json.dumps(parsed_json, separators=(',', ':'))66 except Exception:67 text = re.sub(r'\s+', ' ', raw_data)68 text = text.replace(", ", ",").replace(": ", ":").replace("; ", ";")69 return text.strip()70 71def decimator_engine(input_payload, mode):72 if not input_payload.strip():73 return "", "### โ ๏ธ System Warning\nPlease input your code or data block first!", 0, "0%", "$0.00"74 75 initial_tokens = estimate_tokens(input_payload)76 77 if mode == "Python/R/SQL Code Matrix":78 decimated_output = shrink_python_code(input_payload)79 else:80 decimated_output = shrink_generic_text(input_payload)81 82 final_tokens = estimate_tokens(decimated_output)83 84 token_delta = initial_tokens - final_tokens85 reduction_percentage = 0 if initial_tokens == 0 else (token_delta / initial_tokens) * 10086 estimated_savings = (token_delta / 1000) * 0.01587 if estimated_savings < 0: 88 estimated_savings = 0.089 90 report_markdown = f"""91 ### ๐ Token Decimation Analytics Matrix92 - **Original Payload Footprint:** `{initial_tokens}` estimated tokens.93 - **Optimized Stream Footprint:** `{final_tokens}` estimated tokens.94 - **Tokens Destroyed Successfully:** `{token_delta}` tokens wiped from context.95 """96 97 pct_string = f"{reduction_percentage:.1f}%"98 savings_string = f"${estimated_savings:.4f}"99 100 return decimated_output, report_markdown, token_delta, pct_string, savings_string101 102# Custom Cyber Matrix Terminal UI Theme for Judges103# Extended .margin-top-class to substitute the invalid row inline style attribute parameter safely104custom_css = """105body, .gradio-container { background-color: #050811 !important; font-family: 'Courier New', monospace; color: #00ff66 !important; }106.decimate-btn { background: linear-gradient(135deg, #00ff66, #047857) !important; color: #050811 !important; font-weight: bold !important; border: 1px solid #00ff66 !important; border-radius: 4px !important; letter-spacing: 1px; }107.decimate-btn:hover { box-shadow: 0 0 20px rgba(0,255,102,0.6); color: white !important; }108.panel-border { border: 1px solid #1e293b !important; border-radius: 6px; padding: 15px; background: #090d1a !important; box-shadow: inset 0 0 10px rgba(0,255,102,0.05); }109.metric-box { background: #0d1527 !important; border: 1px solid #00ff66 !important; border-radius: 4px; padding: 10px; text-align: center; }110.margin-top-class { margin-top: 20px !important; }111textarea, input { background-color: #02040a !important; color: #38bdf8 !important; border: 1px solid #1e293b !important; font-family: 'Consolas', monospace !important; }112textarea:focus { border-color: #00ff66 !important; }113"""114 115# ๐ฅ FIXED: Removed css parameters from gr.Blocks initialization wrapper116with gr.Blocks(title="Code-Shrink v1.0") as demo:117 gr.HTML(118 """119 <div style="text-align: center; margin-bottom: 20px; padding: 20px; background: #090d1a; border-radius: 6px; border: 1px solid #00ff66; box-shadow: 0 0 15px rgba(0,255,102,0.1);">120 <h1 style='margin: 0; font-size: 28px; color: #00ff66; letter-spacing: 3px;'>โก CODE-SHRINK: TOKEN-DECIMATOR</h1>121 <p style='margin: 5px 0 0 0; color: #94a3b8; font-size: 13px;'>Algorithmic Context Optimization Matrix // Bypassing LLM Budget Inflation Natively</p>122 </div>123 """124 )125 126 with gr.Row():127 with gr.Column(scale=3, elem_classes="panel-border"):128 gr.Markdown("### ๐ฅ Raw Context Payload Injection")129 payload_input = gr.Textbox(130 placeholder="Paste your bloated Python code, SQL queries, or huge JSON dictionaries here...",131 label="Raw Context Input",132 lines=12133 )134 135 with gr.Row():136 mode_dropdown = gr.Dropdown(137 choices=["Python/R/SQL Code Matrix", "Structured JSON / Raw Text Array"],138 value="Python/R/SQL Code Matrix",139 label="Lexical Processing Mode"140 )141 142 gr.HTML("<br>")143 process_btn = gr.Button("โก DECIMATE CONTEXT TOKENS", elem_classes="decimate-btn")144 145 with gr.Column(scale=3, elem_classes="panel-border"):146 gr.Markdown("### ๐ค Optimized Micro-Stream Output")147 payload_output = gr.Textbox(148 label="Decimated Token Stream (Ready for LLM Prompt)",149 lines=12,150 interactive=False151 )152 153 gr.HTML("<br>")154 with gr.Row():155 with gr.Column(scale=1, elem_classes="metric-box"):156 gr.Markdown("<span style='color:#94a3b8; font-size:11px;'>COMPRESSION</span>")157 pct_output = gr.HTML("<b style='color:#00ff66; font-size:22px;'>0%</b>")158 with gr.Column(scale=1, elem_classes="metric-box"):159 gr.Markdown("<span style='color:#94a3b8; font-size:11px;'>TOKENS WIPED</span>")160 delta_output = gr.HTML("<b style='color:#38bdf8; font-size:22px;'>0</b>")161 with gr.Column(scale=1, elem_classes="metric-box"):162 gr.Markdown("<span style='color:#94a3b8; font-size:11px;'>EST. API SAVINGS</span>")163 savings_output = gr.HTML("<b style='color:#e879f9; font-size:22px;'>$0.00</b>")164 165 # ๐ฅ FIXED: Replaced inline style injection parameters with valid custom css mapping selectors166 with gr.Row(elem_classes=["panel-border", "margin-top-class"]):167 diagnostics_output = gr.Markdown("`System Engine: Standing by. Awaiting dynamic payload mapping signals...`")168 169 process_btn.click(170 fn=decimator_engine,171 inputs=[payload_input, mode_dropdown],172 outputs=[payload_output, diagnostics_output, delta_output, pct_output, savings_output]173 )174 175# ๐ฅ FIXED: Passed custom UI styling matrices explicitly inside launcher execution limits176demo.launch(css=custom_css)177 178 