Spaces:
Sleeping
Sleeping
File size: 1,383 Bytes
ebea39e 34e75f3 ebea39e b434367 34e75f3 faa2ea4 34e75f3 ebea39e 34e75f3 ebea39e 34e75f3 faa2ea4 34e75f3 faa2ea4 34e75f3 faa2ea4 34e75f3 ebea39e |
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 |
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) |