Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,12 @@ import io
|
|
| 4 |
import random
|
| 5 |
import string
|
| 6 |
import base64
|
|
|
|
| 7 |
|
| 8 |
def random_key(length=16):
|
| 9 |
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
|
| 10 |
|
| 11 |
def xor_encrypt_decrypt(text, key):
|
| 12 |
-
# Simple XOR encryption/decryption
|
| 13 |
key_repeated = (key * (len(text) // len(key) + 1))[:len(text)]
|
| 14 |
return ''.join(chr(ord(a) ^ ord(b)) for a, b in zip(text, key_repeated))
|
| 15 |
|
|
@@ -17,7 +17,6 @@ def encode_image(image, input_text, key):
|
|
| 17 |
if image is None or not input_text or not key:
|
| 18 |
return None, gr.update(visible=True, value="Please provide image, input text, and key.")
|
| 19 |
try:
|
| 20 |
-
# Encrypt and base64-encode the message
|
| 21 |
encrypted = xor_encrypt_decrypt(input_text, key)
|
| 22 |
encrypted_b64 = base64.b64encode(encrypted.encode()).decode()
|
| 23 |
img = image.copy()
|
|
@@ -26,7 +25,12 @@ def encode_image(image, input_text, key):
|
|
| 26 |
buf = io.BytesIO()
|
| 27 |
img.save(buf, format="PNG", pnginfo=meta)
|
| 28 |
buf.seek(0)
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
return None, gr.update(visible=True, value=f"Error: {str(e)}")
|
| 32 |
|
|
@@ -72,7 +76,6 @@ with gr.Blocks() as demo:
|
|
| 72 |
|
| 73 |
def on_start(image, text, key):
|
| 74 |
result, status = encode_image(image, text, key)
|
| 75 |
-
# Only show download if result exists
|
| 76 |
return result, gr.update(visible=bool(result)), status
|
| 77 |
|
| 78 |
start_btn.click(
|
|
|
|
| 4 |
import random
|
| 5 |
import string
|
| 6 |
import base64
|
| 7 |
+
import tempfile
|
| 8 |
|
| 9 |
def random_key(length=16):
|
| 10 |
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
|
| 11 |
|
| 12 |
def xor_encrypt_decrypt(text, key):
|
|
|
|
| 13 |
key_repeated = (key * (len(text) // len(key) + 1))[:len(text)]
|
| 14 |
return ''.join(chr(ord(a) ^ ord(b)) for a, b in zip(text, key_repeated))
|
| 15 |
|
|
|
|
| 17 |
if image is None or not input_text or not key:
|
| 18 |
return None, gr.update(visible=True, value="Please provide image, input text, and key.")
|
| 19 |
try:
|
|
|
|
| 20 |
encrypted = xor_encrypt_decrypt(input_text, key)
|
| 21 |
encrypted_b64 = base64.b64encode(encrypted.encode()).decode()
|
| 22 |
img = image.copy()
|
|
|
|
| 25 |
buf = io.BytesIO()
|
| 26 |
img.save(buf, format="PNG", pnginfo=meta)
|
| 27 |
buf.seek(0)
|
| 28 |
+
# Write the BytesIO to a temp file and return the path
|
| 29 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 30 |
+
temp_file.write(buf.read())
|
| 31 |
+
temp_file.flush()
|
| 32 |
+
temp_file.close()
|
| 33 |
+
return temp_file.name, gr.update(visible=True, value="Success! Download your image below.")
|
| 34 |
except Exception as e:
|
| 35 |
return None, gr.update(visible=True, value=f"Error: {str(e)}")
|
| 36 |
|
|
|
|
| 76 |
|
| 77 |
def on_start(image, text, key):
|
| 78 |
result, status = encode_image(image, text, key)
|
|
|
|
| 79 |
return result, gr.update(visible=bool(result)), status
|
| 80 |
|
| 81 |
start_btn.click(
|