Spaces:
Sleeping
Sleeping
Update app.py
Browse files2 unmodified lines34567830 unmodified lines3940414243444563 unmodified lines1091101111121131141151163 unmodified lines1201211221231241251261271281291301311321331341351362 unmodified linesimport shutilimport importlib.utilfrom pathlib import Pathfrom fastapi import FastAPI, UploadFile, File, Form, HTTPExceptionfrom fastapi.responses import FileResponse, JSONResponse30 unmodified lines"endpoints": {"POST /encrypt": "Upload a cover image, get encrypted image + 2 keys","POST /embed": "Upload secret image + run_dir, get stego image + secret key","POST /retrieve": "Provide run_dir + 3 keys, get constructed and secret images","GET /download/{run_dir}/{filename}": "Download result files"},"docs": "/docs"63 unmodified linesasync def retrieve(run_dir: str = Form(...),aes_key: str = Form(...),aes_key_2: str = Form(...),secret_shuffle_key: str = Form(...)):workdir = (RUNS_DIR / run_dir).resolve()if not workdir.exists():3 unmodified linescwd = os.getcwd()try:os.chdir(workdir)ret.retreive_data("reembbedded_image.png", aes_key, aes_key_2, secret_shuffle_key)rmse = ret.accuracy("cover.png", "constructed_image.png")ent = ret.calentropy("encrypted_image.png")finally:os.chdir(cwd)return JSONResponse({"constructed_image": f"/download/{workdir.name}/constructed_image.png","retrieved_secret_image": f"/download/{workdir.name}/ret_secret_image.png","metrics": {"rmse_cover_vs_constructed": rmse, "entropy_encrypted_image": ent}})@app.get("/download/{run_dir}/{filename}")def download(run_dir: str, filename: str):2 unmodified lines345678930 unmodified lines4041424344454663 unmodified lines1101111121131141151161173 unmodified lines1211221231241251261271281291301311321331341351361371381391401411421431441451461471481492 unmodified linesimport shutilimport importlib.utilfrom pathlib import Pathfrom typing import Optionalfrom fastapi import FastAPI, UploadFile, File, Form, HTTPExceptionfrom fastapi.responses import FileResponse, JSONResponse30 unmodified lines"endpoints": {"POST /encrypt": "Upload a cover image, get encrypted image + 2 keys","POST /embed": "Upload secret image + run_dir, get stego image + secret key","POST /retrieve": "Provide run_dir + keys, get constructed (and secret if all keys given)","GET /download/{run_dir}/{filename}": "Download result files"},"docs": "/docs"63 unmodified linesasync def retrieve(run_dir: str = Form(...),aes_key: str = Form(...),aes_key_2: Optional[str] = Form(None),secret_shuffle_key: Optional[str] = Form(None)):workdir = (RUNS_DIR / run_dir).resolve()if not workdir.exists():3 unmodified linescwd = os.getcwd()try:os.chdir(workdir)if aes_key and not aes_key_2 and not secret_shuffle_key:# Mode 1: Only constructed imageret.retreive_data_constructed("reembbedded_image.png", aes_key)elif aes_key and aes_key_2 and secret_shuffle_key:# Mode 2: Full constructed + secretret.retreive_data("reembbedded_image.png", aes_key, aes_key_2, secret_shuffle_key)else:raise HTTPException(400, "Invalid key combination provided")rmse = ret.accuracy("cover.png", "constructed_image.png")ent = ret.calentropy("encrypted_image.png")finally:os.chdir(cwd)resp = {"constructed_image": f"/download/{workdir.name}/constructed_image.png","metrics": {"rmse_cover_vs_constructed": rmse, "entropy_encrypted_image": ent}}if (workdir / "ret_secret_image.png").exists():resp["retrieved_secret_image"] = f"/download/{workdir.name}/ret_secret_image.png"return JSONResponse(resp)@app.get("/download/{run_dir}/{filename}")def download(run_dir: str, filename: str):
app.py+20-7