Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,8 @@ import dropbox
|
|
| 9 |
import os
|
| 10 |
from datetime import datetime
|
| 11 |
|
| 12 |
-
# Get Dropbox token from environment (set as Space Secret)
|
| 13 |
DROPBOX_TOKEN = os.environ.get("DROPBOX_TOKEN")
|
| 14 |
-
DROPBOX_FOLDER = "/imagefun"
|
| 15 |
|
| 16 |
def random_key(length=16):
|
| 17 |
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
|
|
@@ -22,7 +21,7 @@ def xor_encrypt_decrypt(text, key):
|
|
| 22 |
|
| 23 |
def save_to_dropbox(image_bytes, key):
|
| 24 |
if not DROPBOX_TOKEN:
|
| 25 |
-
return
|
| 26 |
dbx = dropbox.Dropbox(DROPBOX_TOKEN)
|
| 27 |
now = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 28 |
filename = f"{key}_{now}.png"
|
|
@@ -30,17 +29,14 @@ def save_to_dropbox(image_bytes, key):
|
|
| 30 |
img_path = f"{DROPBOX_FOLDER}/{filename}"
|
| 31 |
key_path = f"{DROPBOX_FOLDER}/{key_filename}"
|
| 32 |
try:
|
| 33 |
-
# Upload image
|
| 34 |
dbx.files_upload(image_bytes, img_path, mode=dropbox.files.WriteMode.overwrite)
|
| 35 |
-
# Upload key as a text file
|
| 36 |
dbx.files_upload(key.encode(), key_path, mode=dropbox.files.WriteMode.overwrite)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return f"Dropbox upload failed: {e}"
|
| 40 |
|
| 41 |
def encode_image(image, input_text, key):
|
| 42 |
if image is None or not input_text or not key:
|
| 43 |
-
return None, gr.update(visible=True, value="Please provide image, input text, and key.")
|
| 44 |
try:
|
| 45 |
encrypted = xor_encrypt_decrypt(input_text, key)
|
| 46 |
encrypted_b64 = base64.b64encode(encrypted.encode()).decode()
|
|
@@ -50,19 +46,17 @@ def encode_image(image, input_text, key):
|
|
| 50 |
buf = io.BytesIO()
|
| 51 |
img.save(buf, format="PNG", pnginfo=meta)
|
| 52 |
buf.seek(0)
|
| 53 |
-
# Write the BytesIO to a temp file and return the path
|
| 54 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 55 |
temp_file.write(buf.read())
|
| 56 |
temp_file.flush()
|
| 57 |
temp_file.close()
|
| 58 |
|
| 59 |
-
# For Dropbox, we need the bytes again
|
| 60 |
buf.seek(0)
|
| 61 |
-
|
| 62 |
|
| 63 |
-
return temp_file.name, gr.update(visible=True, value="Success! Download your image above.")
|
| 64 |
except Exception as e:
|
| 65 |
-
return None, gr.update(visible=True, value=f"Error: {str(e)}")
|
| 66 |
|
| 67 |
def decode_image(img, key):
|
| 68 |
if img is None or not key:
|
|
@@ -71,7 +65,7 @@ def decode_image(img, key):
|
|
| 71 |
pil_img = Image.open(img)
|
| 72 |
encrypted_b64 = pil_img.info.get("encrypted_message")
|
| 73 |
if not encrypted_b64:
|
| 74 |
-
return "", gr.update(visible=True, value="No encrypted
|
| 75 |
encrypted = base64.b64decode(encrypted_b64).decode()
|
| 76 |
decrypted = xor_encrypt_decrypt(encrypted, key)
|
| 77 |
return decrypted, gr.update(visible=False)
|
|
@@ -89,9 +83,9 @@ with gr.Blocks() as demo:
|
|
| 89 |
key_enc = gr.Textbox(label="Key", max_lines=1, placeholder="Enter key or generate one")
|
| 90 |
gen_key_btn = gr.Button("๐ Generate random key")
|
| 91 |
start_btn = gr.Button("Start")
|
| 92 |
-
download_link = gr.File(label="Download image with
|
| 93 |
enc_status = gr.Textbox(label="Status", interactive=False, visible=True)
|
| 94 |
-
#
|
| 95 |
|
| 96 |
with gr.Tab("DEC (Decode)"):
|
| 97 |
with gr.Row():
|
|
@@ -99,19 +93,19 @@ with gr.Blocks() as demo:
|
|
| 99 |
image_dec = gr.Image(label="Upload Image With Encrypted Text", type="filepath")
|
| 100 |
key_dec = gr.Textbox(label="Key", max_lines=1, placeholder="Enter key used for encryption")
|
| 101 |
dec_btn = gr.Button("Decrypt")
|
| 102 |
-
output_text = gr.Textbox(label="Decrypted
|
| 103 |
dec_status = gr.Textbox(label="Status", interactive=False, visible=True)
|
| 104 |
|
| 105 |
gen_key_btn.click(lambda: random_key(), [], key_enc)
|
| 106 |
|
| 107 |
def on_start(image, text, key):
|
| 108 |
-
result, status
|
| 109 |
-
return result, gr.update(visible=bool(result)), status
|
| 110 |
|
| 111 |
start_btn.click(
|
| 112 |
on_start,
|
| 113 |
inputs=[image_enc, input_text, key_enc],
|
| 114 |
-
outputs=[download_link, download_link, enc_status
|
| 115 |
)
|
| 116 |
|
| 117 |
def on_decrypt(image_path, key):
|
|
|
|
| 9 |
import os
|
| 10 |
from datetime import datetime
|
| 11 |
|
|
|
|
| 12 |
DROPBOX_TOKEN = os.environ.get("DROPBOX_TOKEN")
|
| 13 |
+
DROPBOX_FOLDER = "/imagefun"
|
| 14 |
|
| 15 |
def random_key(length=16):
|
| 16 |
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
|
|
|
|
| 21 |
|
| 22 |
def save_to_dropbox(image_bytes, key):
|
| 23 |
if not DROPBOX_TOKEN:
|
| 24 |
+
return
|
| 25 |
dbx = dropbox.Dropbox(DROPBOX_TOKEN)
|
| 26 |
now = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 27 |
filename = f"{key}_{now}.png"
|
|
|
|
| 29 |
img_path = f"{DROPBOX_FOLDER}/{filename}"
|
| 30 |
key_path = f"{DROPBOX_FOLDER}/{key_filename}"
|
| 31 |
try:
|
|
|
|
| 32 |
dbx.files_upload(image_bytes, img_path, mode=dropbox.files.WriteMode.overwrite)
|
|
|
|
| 33 |
dbx.files_upload(key.encode(), key_path, mode=dropbox.files.WriteMode.overwrite)
|
| 34 |
+
except Exception:
|
| 35 |
+
pass # Silently ignore Dropbox errors
|
|
|
|
| 36 |
|
| 37 |
def encode_image(image, input_text, key):
|
| 38 |
if image is None or not input_text or not key:
|
| 39 |
+
return None, gr.update(visible=True, value="Please provide image, input text, and key.")
|
| 40 |
try:
|
| 41 |
encrypted = xor_encrypt_decrypt(input_text, key)
|
| 42 |
encrypted_b64 = base64.b64encode(encrypted.encode()).decode()
|
|
|
|
| 46 |
buf = io.BytesIO()
|
| 47 |
img.save(buf, format="PNG", pnginfo=meta)
|
| 48 |
buf.seek(0)
|
|
|
|
| 49 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 50 |
temp_file.write(buf.read())
|
| 51 |
temp_file.flush()
|
| 52 |
temp_file.close()
|
| 53 |
|
|
|
|
| 54 |
buf.seek(0)
|
| 55 |
+
save_to_dropbox(buf.getvalue(), key)
|
| 56 |
|
| 57 |
+
return temp_file.name, gr.update(visible=True, value="Success! Download your image above.")
|
| 58 |
except Exception as e:
|
| 59 |
+
return None, gr.update(visible=True, value=f"Error: {str(e)}")
|
| 60 |
|
| 61 |
def decode_image(img, key):
|
| 62 |
if img is None or not key:
|
|
|
|
| 65 |
pil_img = Image.open(img)
|
| 66 |
encrypted_b64 = pil_img.info.get("encrypted_message")
|
| 67 |
if not encrypted_b64:
|
| 68 |
+
return "", gr.update(visible=True, value="No encrypted text found in this image.")
|
| 69 |
encrypted = base64.b64decode(encrypted_b64).decode()
|
| 70 |
decrypted = xor_encrypt_decrypt(encrypted, key)
|
| 71 |
return decrypted, gr.update(visible=False)
|
|
|
|
| 83 |
key_enc = gr.Textbox(label="Key", max_lines=1, placeholder="Enter key or generate one")
|
| 84 |
gen_key_btn = gr.Button("๐ Generate random key")
|
| 85 |
start_btn = gr.Button("Start")
|
| 86 |
+
download_link = gr.File(label="Download image with encrypted text", visible=False)
|
| 87 |
enc_status = gr.Textbox(label="Status", interactive=False, visible=True)
|
| 88 |
+
# Dropbox status UI component removed
|
| 89 |
|
| 90 |
with gr.Tab("DEC (Decode)"):
|
| 91 |
with gr.Row():
|
|
|
|
| 93 |
image_dec = gr.Image(label="Upload Image With Encrypted Text", type="filepath")
|
| 94 |
key_dec = gr.Textbox(label="Key", max_lines=1, placeholder="Enter key used for encryption")
|
| 95 |
dec_btn = gr.Button("Decrypt")
|
| 96 |
+
output_text = gr.Textbox(label="Decrypted Text", lines=5, interactive=False)
|
| 97 |
dec_status = gr.Textbox(label="Status", interactive=False, visible=True)
|
| 98 |
|
| 99 |
gen_key_btn.click(lambda: random_key(), [], key_enc)
|
| 100 |
|
| 101 |
def on_start(image, text, key):
|
| 102 |
+
result, status = encode_image(image, text, key)
|
| 103 |
+
return result, gr.update(visible=bool(result)), status
|
| 104 |
|
| 105 |
start_btn.click(
|
| 106 |
on_start,
|
| 107 |
inputs=[image_enc, input_text, key_enc],
|
| 108 |
+
outputs=[download_link, download_link, enc_status]
|
| 109 |
)
|
| 110 |
|
| 111 |
def on_decrypt(image_path, key):
|