Spaces:
Running on Zero
Running on Zero
Show only the encoder: it is framed by the project site, which already has the title, explanation, caveat and a decoder
Browse files
app.py
CHANGED
|
@@ -10,8 +10,9 @@ button (`gr.LoginButton` / `hf_oauth`) is a different mechanism: it would tell
|
|
| 10 |
*this app* who the user is without changing their quota at all. Having one
|
| 11 |
implied otherwise, so it was removed.
|
| 12 |
|
| 13 |
-
Encoding needs the model, so it runs inside @spaces.GPU.
|
| 14 |
-
text and the key, so
|
|
|
|
| 15 |
"""
|
| 16 |
import os
|
| 17 |
import time
|
|
@@ -137,50 +138,33 @@ def encode(message, topic, key, attempts):
|
|
| 137 |
except Exception:
|
| 138 |
ok = False
|
| 139 |
span = se.payload_span(text, len(bits), key)
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
| 150 |
return text.strip(), note
|
| 151 |
|
| 152 |
|
| 153 |
-
def decode(text, key):
|
| 154 |
-
"""No model, no GPU — just the text and the key."""
|
| 155 |
-
if not (text or "").strip():
|
| 156 |
-
return "Paste the cover text."
|
| 157 |
-
if not (key or "").strip():
|
| 158 |
-
return "A key is required."
|
| 159 |
-
try:
|
| 160 |
-
return pc.decompress_from_bits(se.extract(text, key))
|
| 161 |
-
except Exception as exc:
|
| 162 |
-
return f"Could not decode: {exc}\n\n(Wrong key, or the cover text was edited.)"
|
| 163 |
-
|
| 164 |
-
|
| 165 |
CSS = """
|
| 166 |
-
#gn-out
|
| 167 |
"""
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
with gr.Blocks(title="Garfield-Naruto Encoder") as demo:
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
-
with gr.Tab("Encode"):
|
| 177 |
-
gr.Markdown("## Encode a message")
|
| 178 |
-
gr.Markdown(
|
| 179 |
-
"Enter your secret message below and G-N Encoder will hide it in LLM "
|
| 180 |
-
"slop. Topic allows you to choose what the slop is about and Key is a "
|
| 181 |
-
"random seed for the encoding (think of it like a password required "
|
| 182 |
-
"for decoding)."
|
| 183 |
-
)
|
| 184 |
msg = gr.Textbox(label="Message",
|
| 185 |
placeholder="Enter your secret message here.")
|
| 186 |
topic = gr.Textbox(label="Topic",
|
|
@@ -190,50 +174,13 @@ with gr.Blocks(title="Garfield-Naruto Encoder") as demo:
|
|
| 190 |
attempts = gr.Number(label="Attempts", value=6, minimum=1, maximum=10,
|
| 191 |
precision=0)
|
| 192 |
go = gr.Button("Begin Encoding", variant="primary")
|
| 193 |
-
|
| 194 |
out = gr.Textbox(label="Output", elem_id="gn-out", lines=10,
|
| 195 |
placeholder="Output will appear here…")
|
| 196 |
note = gr.Markdown()
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
with gr.Tab("Decode"):
|
| 200 |
-
gr.Markdown("## Decode a message")
|
| 201 |
-
gr.Markdown("Decoding needs only the text and the key — no model and no "
|
| 202 |
-
"GPU, so this tab costs no quota.")
|
| 203 |
-
dtext = gr.Textbox(label="Encoded text", lines=8,
|
| 204 |
-
placeholder="Paste the encoded text here.")
|
| 205 |
-
dkey = gr.Textbox(label="Key", placeholder="Pick a secret key.",
|
| 206 |
-
type="password")
|
| 207 |
-
dgo = gr.Button("DECODE", variant="primary")
|
| 208 |
-
dout = gr.Textbox(label="Decoded message", elem_id="gn-decode-out",
|
| 209 |
-
lines=3,
|
| 210 |
-
placeholder="Decoded message will appear here…")
|
| 211 |
-
dgo.click(decode, [dtext, dkey], dout)
|
| 212 |
-
|
| 213 |
-
with gr.Tab("How it works"):
|
| 214 |
-
gr.Markdown(
|
| 215 |
-
"Every character of the output is an encoding slot. Based on the key, "
|
| 216 |
-
"the alphabet is partitioned into three sets at **each character "
|
| 217 |
-
"position**: one encodes a `0`, one encodes a `1`, and one is a skip "
|
| 218 |
-
"that carries nothing. The partition is balanced by English letter "
|
| 219 |
-
"frequency so all three are roughly equally likely in natural text.\n\n"
|
| 220 |
-
"At every step the model's next-token distribution is masked to the "
|
| 221 |
-
"tokens whose characters agree with the bits still to be sent, then "
|
| 222 |
-
"sampled. A sentence is generated, scored, and regenerated if it reads "
|
| 223 |
-
"badly — so the payload survives while the prose stays readable.\n\n"
|
| 224 |
-
"Decoding reverses only the character step: walk the text, apply the "
|
| 225 |
-
"same keyed partition per position, and read off the bits. **No model "
|
| 226 |
-
"is involved**, which is why the Decode tab needs no GPU and why a "
|
| 227 |
-
"browser can do it."
|
| 228 |
-
)
|
| 229 |
-
gr.Markdown(
|
| 230 |
-
f"Model: `{MODEL_ID}` · GPU time on ZeroGPU is charged to the "
|
| 231 |
-
"**visitor**, per account per day — not to the account hosting this "
|
| 232 |
-
"Space. You are identified by your huggingface.co session, so open "
|
| 233 |
-
"this Space in a browser where you are signed in to use your own "
|
| 234 |
-
"quota rather than the pool shared by IP address."
|
| 235 |
-
)
|
| 236 |
|
| 237 |
if __name__ == "__main__":
|
| 238 |
-
# css moved from the Blocks constructor to launch() in Gradio 6
|
| 239 |
-
|
|
|
|
|
|
|
|
|
| 10 |
*this app* who the user is without changing their quota at all. Having one
|
| 11 |
implied otherwise, so it was removed.
|
| 12 |
|
| 13 |
+
Encoding needs the model, so it runs inside @spaces.GPU. There is no decoder
|
| 14 |
+
here: decoding needs only the text and the key, so the project site does it in
|
| 15 |
+
the browser with no model and no GPU at all.
|
| 16 |
"""
|
| 17 |
import os
|
| 18 |
import time
|
|
|
|
| 138 |
except Exception:
|
| 139 |
ok = False
|
| 140 |
span = se.payload_span(text, len(bits), key)
|
| 141 |
+
# The diagnostics are for whoever runs the Space, not for the visitor, so
|
| 142 |
+
# they go to the logs and the page shows a single verdict.
|
| 143 |
+
print(f"[stego] {len(bits)} bits in {len(text)} chars "
|
| 144 |
+
f"({len(bits) / max(len(text), 1):.3f} b/ch"
|
| 145 |
+
+ (f", {len(bits) / span:.3f} over the payload span" if span else "")
|
| 146 |
+
+ f") | {stats['accepted']} accepted, {stats['rejected']} rejected, "
|
| 147 |
+
f"{stats['fallback']} fallback"
|
| 148 |
+
+ (f", {stats['rounds']} batched rounds of {BATCH}" if BATCH else "")
|
| 149 |
+
+ f" | {dt:.0f}s", flush=True)
|
| 150 |
+
note = ("Verified: this text decodes back to your message." if ok else
|
| 151 |
+
"This run did not verify. Try again, or raise Attempts.")
|
| 152 |
return text.strip(), note
|
| 153 |
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
CSS = """
|
| 156 |
+
#gn-out textarea { font-family: ui-monospace, Menlo, Consolas, monospace; }
|
| 157 |
"""
|
| 158 |
|
| 159 |
+
# Just the encoder. The Space is shown in an iframe on the project site, which
|
| 160 |
+
# already supplies the title, the explanation, the security caveat and a
|
| 161 |
+
# browser-side decoder — anything more here repeats the page around it. Opened
|
| 162 |
+
# directly, Hugging Face's own Space header still names it.
|
| 163 |
with gr.Blocks(title="Garfield-Naruto Encoder") as demo:
|
| 164 |
+
# The Column draws nothing, but it is load-bearing. With the components
|
| 165 |
+
# placed straight on the Blocks root, Gradio 6.27's textbox autosize measured
|
| 166 |
+
# every empty field as needing max_lines (20) and drew each one 412px tall.
|
| 167 |
+
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
msg = gr.Textbox(label="Message",
|
| 169 |
placeholder="Enter your secret message here.")
|
| 170 |
topic = gr.Textbox(label="Topic",
|
|
|
|
| 174 |
attempts = gr.Number(label="Attempts", value=6, minimum=1, maximum=10,
|
| 175 |
precision=0)
|
| 176 |
go = gr.Button("Begin Encoding", variant="primary")
|
|
|
|
| 177 |
out = gr.Textbox(label="Output", elem_id="gn-out", lines=10,
|
| 178 |
placeholder="Output will appear here…")
|
| 179 |
note = gr.Markdown()
|
| 180 |
+
go.click(encode, [msg, topic, key_in, attempts], [out, note])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
if __name__ == "__main__":
|
| 183 |
+
# css moved from the Blocks constructor to launch() in Gradio 6.
|
| 184 |
+
# footer_links=[] drops the "Use via API · Built with Gradio · Settings"
|
| 185 |
+
# footer, which inside the site's iframe is just more chrome.
|
| 186 |
+
demo.launch(css=CSS, footer_links=[])
|