File size: 8,853 Bytes
078ce08 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | """
StegNet β Gradio web interface.
Entry point for HuggingFace Spaces (or any Gradio-compatible host).
Set the HF_API_KEY environment variable (Spaces secret) to enable
the "Generate Image" tab.
Run locally:
python app.py
"""
import os
import sys
# Make sure the package root is on the path when run directly
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import gradio as gr
from app.models.DEEP_STEGO.hide_image import hide_image
from app.models.DEEP_STEGO.reveal_image import reveal_image
from app.models.ESRGAN.upscale_image import upscale_image
from app.models.encryption import aes, blowfish
from app.models.StableDiffusionAPI.StableDiffusionV2 import generate
# ---------------------------------------------------------------------------
# Handler functions
# ---------------------------------------------------------------------------
def hide_fn(cover, secret):
if cover is None or secret is None:
return None, "Please upload both a cover image and a secret image."
try:
output_path = hide_image(cover, secret)
return output_path, "Secret image hidden successfully!"
except Exception as e:
return None, f"Error: {e}"
def reveal_fn(steg):
if steg is None:
return None, "Please upload a steg image."
try:
output_path = reveal_image(steg)
return output_path, "Secret image revealed successfully!"
except Exception as e:
return None, f"Error: {e}"
def encrypt_fn(image, key, algo):
if image is None:
return None, "Please upload an image."
if not key:
return None, "Please enter a secret key."
try:
if algo == "AES":
aes.encrypt(image, key)
else:
blowfish.encrypt(image, key)
enc_path = image + ".enc"
return enc_path, f"Image encrypted with {algo}. Download the file below."
except Exception as e:
return None, f"Error: {e}"
def decrypt_fn(enc_file, key, algo):
if enc_file is None:
return None, "Please upload an encrypted .enc file."
if not key:
return None, "Please enter the secret key."
try:
if algo == "AES":
code, filename = aes.decrypt(enc_file, key)
else:
code, filename = blowfish.decrypt(enc_file, key)
if code == -1:
return None, "Decryption failed β wrong key."
return filename, "Image decrypted successfully!"
except Exception as e:
return None, f"Error: {e}"
def upscale_fn(image):
if image is None:
return None, "Please upload a low-resolution image."
try:
output_path = upscale_image(image)
return output_path, "Image upscaled 4Γ successfully!"
except Exception as e:
return None, f"Error: {e}"
def generate_fn(prompt):
if not prompt or not prompt.strip():
return None, "Please enter a text prompt."
if not os.environ.get("HF_API_KEY"):
return None, "HF_API_KEY is not set. Add it as a Space secret to enable image generation."
try:
img = generate(prompt.strip())
return img, "Image generated successfully!"
except Exception as e:
return None, f"Error: {e}"
# ---------------------------------------------------------------------------
# Gradio UI
# ---------------------------------------------------------------------------
DESCRIPTION = """
# StegNet
### AI-Based Secure Image Steganography Platform
Hide secret images inside cover images using deep learning, encrypt the result,
reveal hidden images, enhance resolution with ESRGAN, or generate new images
with Stable Diffusion β all in one place.
"""
with gr.Blocks(title="StegNet", theme=gr.themes.Soft()) as demo:
gr.Markdown(DESCRIPTION)
# ββ Tab 1: Hide Image ββββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("π Hide Image"):
gr.Markdown("Upload a **cover image** and a **secret image**. The model embeds the secret inside the cover.")
with gr.Row():
cover_input = gr.Image(type="filepath", label="Cover Image")
secret_input = gr.Image(type="filepath", label="Secret Image")
hide_btn = gr.Button("Hide Secret", variant="primary")
with gr.Row():
steg_output = gr.Image(label="Steg Image (download me)")
hide_status = gr.Textbox(label="Status", interactive=False, lines=2)
hide_btn.click(hide_fn, inputs=[cover_input, secret_input], outputs=[steg_output, hide_status])
# ββ Tab 2: Reveal Image ββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("π Reveal Image"):
gr.Markdown("Upload a **steg image** to extract the hidden secret image.")
steg_input = gr.Image(type="filepath", label="Steg Image")
reveal_btn = gr.Button("Reveal Secret", variant="primary")
with gr.Row():
revealed_output = gr.Image(label="Revealed Secret Image")
reveal_status = gr.Textbox(label="Status", interactive=False, lines=2)
reveal_btn.click(reveal_fn, inputs=[steg_input], outputs=[revealed_output, reveal_status])
# ββ Tab 3: Encrypt Image βββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("π‘οΈ Encrypt Image"):
gr.Markdown("Encrypt any image file (e.g. your steg image) with AES or Blowfish.")
enc_image_input = gr.Image(type="filepath", label="Image to Encrypt")
with gr.Row():
enc_key_input = gr.Textbox(label="Secret Key", type="password", placeholder="Enter a strong keyβ¦")
enc_algo = gr.Radio(["AES", "Blowfish"], value="AES", label="Algorithm")
enc_btn = gr.Button("Encrypt", variant="primary")
with gr.Row():
enc_file_output = gr.File(label="Encrypted File (.enc) β Download")
enc_status = gr.Textbox(label="Status", interactive=False, lines=2)
enc_btn.click(encrypt_fn, inputs=[enc_image_input, enc_key_input, enc_algo], outputs=[enc_file_output, enc_status])
# ββ Tab 4: Decrypt Image βββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("π Decrypt Image"):
gr.Markdown("Upload an encrypted **.enc** file and the correct key to recover the image.")
dec_file_input = gr.File(label="Encrypted File (.enc)", type="filepath")
with gr.Row():
dec_key_input = gr.Textbox(label="Secret Key", type="password", placeholder="Enter the key used for encryptionβ¦")
dec_algo = gr.Radio(["AES", "Blowfish"], value="AES", label="Algorithm")
dec_btn = gr.Button("Decrypt", variant="primary")
with gr.Row():
dec_image_output = gr.Image(label="Decrypted Image")
dec_status = gr.Textbox(label="Status", interactive=False, lines=2)
dec_btn.click(decrypt_fn, inputs=[dec_file_input, dec_key_input, dec_algo], outputs=[dec_image_output, dec_status])
# ββ Tab 5: Super Resolution ββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("β¨ Super Resolution"):
gr.Markdown("Upscale any image **4Γ** using ESRGAN.")
lr_input = gr.Image(type="filepath", label="Low-Resolution Image")
upscale_btn = gr.Button("Upscale 4Γ", variant="primary")
with gr.Row():
hr_output = gr.Image(label="High-Resolution Output")
upscale_status = gr.Textbox(label="Status", interactive=False, lines=2)
upscale_btn.click(upscale_fn, inputs=[lr_input], outputs=[hr_output, upscale_status])
# ββ Tab 6: Generate Image ββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("π¨ Generate Image"):
gr.Markdown(
"Generate an image from a text prompt using Stable Diffusion 2.1. \n"
"*(Requires `HF_API_KEY` set as a Space secret.)*"
)
prompt_input = gr.Textbox(
label="Text Prompt",
placeholder="A futuristic city at sunset, digital artβ¦",
lines=2,
)
gen_btn = gr.Button("Generate", variant="primary")
with gr.Row():
gen_output = gr.Image(label="Generated Image")
gen_status = gr.Textbox(label="Status", interactive=False, lines=2)
gen_btn.click(generate_fn, inputs=[prompt_input], outputs=[gen_output, gen_status])
demo.launch()
|