#!/usr/bin/env python """04 -- file upload, inspection and download. Uploads land in GRADIO_TEMP_DIR (see gradio-offline.env). On a host with SELinux in enforcing mode, that directory must be writable by the service account running the app. """ import hashlib import os import pathlib import gradio as gr from _common import launch def inspect(path): if not path: return {}, None p = pathlib.Path(path) data = p.read_bytes() info = { "name": p.name, "bytes": len(data), "sha256": hashlib.sha256(data).hexdigest(), "temp_dir": os.environ.get("GRADIO_TEMP_DIR", "(default)"), } # Write a small report next to the upload and offer it back for download. report = p.with_suffix(p.suffix + ".sha256") report.write_text(f"{info['sha256']} {p.name}\n") return info, str(report) with gr.Blocks(title="File upload") as demo: gr.Markdown("## Upload a file, get its checksum") up = gr.File(label="File", type="filepath") meta = gr.JSON(label="Metadata") dl = gr.File(label="Checksum file (download)") up.change(inspect, up, [meta, dl], api_name="inspect") if __name__ == "__main__": launch(demo)