import gradio as gr import base64 import pyperclip b64_state = None # simpan sementara di server def buffer_to_base64(binary_data: bytes): global b64_state # Encode bytes ke Base64 string b64_state = base64.b64encode(binary_data).decode("utf-8") return b64_state, f"[Buka hasil di tab baru](data:text/plain;base64,{b64_state})" def copy_to_server(): if b64_state: try: pyperclip.copy(b64_state) return "Base64 berhasil disalin ke clipboard **server**." except Exception as e: return f"Gagal menyalin ke clipboard server: {e}" return "Belum ada data yang dikonversi." with gr.Blocks() as demo: gr.Markdown("# ESP32 Buffer to Base64 API (No JS)") with gr.Row(): inp = gr.File(label="Upload Raw Buffer", type="binary") with gr.Row(): out = gr.Textbox(label="Base64 Output", lines=6, interactive=True) link_out = gr.Markdown("") # link buka tab baru with gr.Row(): btn_convert = gr.Button("Convert") btn_copy = gr.Button("Copy (Server Clipboard)") # Tombol Convert → tampilkan Base64 & link tab baru btn_convert.click(buffer_to_base64, inputs=inp, outputs=[out, link_out]) # Tombol Copy (server side) btn_copy.click(copy_to_server, outputs=out) if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=7860)