Spaces:
Sleeping
Sleeping
| # File: app.py | |
| import gradio as gr | |
| import zlib | |
| from hashlib import sha256 | |
| # --- Word lists for encoding --- | |
| ADJS = [ | |
| "ancient","silent","lonely","golden","restless","gentle","wandering","hidden", | |
| "stubborn","fickle","quiet","murmuring","distant","tired","brave","secret" | |
| ] | |
| NOUNS = [ | |
| "river","man","village","book","key","window","path","harbor", | |
| "garden","letter","lantern","clock","forest","child","storm","candle" | |
| ] | |
| # --- Encoding/decoding helpers --- | |
| def _bytes_to_pairs(data_bytes): | |
| pairs = [] | |
| for b in data_bytes: | |
| hi = b >> 4 | |
| lo = b & 0x0F | |
| pairs.append((ADJS[hi], NOUNS[lo])) | |
| return pairs | |
| def _pairs_to_bytes(pairs): | |
| out = bytearray() | |
| for adj, noun in pairs: | |
| hi = ADJS.index(adj) | |
| lo = NOUNS.index(noun) | |
| out.append((hi << 4) | lo) | |
| return bytes(out) | |
| def encode_to_story(code_str, compress_level=6, sentence_pairs=6): | |
| compressed = zlib.compress(code_str.encode('utf-8'), level=compress_level) | |
| pairs = _bytes_to_pairs(compressed) | |
| phrases = ["{} {}".format(adj, noun) for adj, noun in pairs] | |
| sentences = [] | |
| for i in range(0, len(phrases), sentence_pairs): | |
| chunk = phrases[i:i+sentence_pairs] | |
| if not chunk: | |
| continue | |
| sentence = ", ".join(chunk).capitalize() + "." | |
| sentences.append(sentence) | |
| story = " ".join(sentences) | |
| checksum = sha256(compressed).hexdigest()[:8] | |
| story += f"\n\nβ end of story ({checksum})" | |
| return story | |
| def decode_from_story(story_text): | |
| lines = story_text.strip().splitlines() | |
| if lines and lines[-1].lower().startswith("β end of story"): | |
| lines = lines[:-1] | |
| body = " ".join(lines) | |
| import re | |
| cleaned = re.sub(r"[^\w\s]", " ", body) | |
| tokens = cleaned.split() | |
| if len(tokens) % 2 != 0: | |
| return "Error: Story token count invalid. Possibly corrupted." | |
| pairs = [] | |
| for i in range(0, len(tokens), 2): | |
| adj = tokens[i].lower() | |
| noun = tokens[i+1].lower() | |
| if adj not in ADJS or noun not in NOUNS: | |
| return f"Error: Unexpected word pair: {adj} {noun}. Decoding failed." | |
| pairs.append((adj, noun)) | |
| compressed = _pairs_to_bytes(pairs) | |
| try: | |
| original = zlib.decompress(compressed).decode('utf-8') | |
| except Exception as e: | |
| return f"Error decompressing: {str(e)}" | |
| return original | |
| # --- Gradio interface --- | |
| def encode_interface(code_input): | |
| if not code_input.strip(): | |
| return "Please enter code to encode." | |
| return encode_to_story(code_input) | |
| def decode_interface(story_input): | |
| if not story_input.strip(): | |
| return "Please enter story text to decode." | |
| return decode_from_story(story_input) | |
| with gr.Blocks(title="Code β Story Encoder") as demo: | |
| gr.Markdown( | |
| "## π Code β Story Encoder\n" | |
| "Enter any code (Python, JavaScript, etc.) and convert it into a story-like text. " | |
| "You can also paste the story to get your original code back." | |
| ) | |
| with gr.Tab("Encode Code β Story"): | |
| code_box = gr.Textbox(label="Enter your code", placeholder="Paste code here...", lines=10) | |
| encode_btn = gr.Button("Encode to Story") | |
| story_output = gr.Textbox(label="Story Output", lines=10) | |
| encode_btn.click(encode_interface, inputs=code_box, outputs=story_output) | |
| with gr.Tab("Decode Story β Code"): | |
| story_box = gr.Textbox(label="Enter story text", placeholder="Paste story here...", lines=10) | |
| decode_btn = gr.Button("Decode to Code") | |
| code_output = gr.Textbox(label="Decoded Code", lines=10) | |
| decode_btn.click(decode_interface, inputs=story_box, outputs=code_output) | |
| demo.launch() | |