Spaces:
Sleeping
Sleeping
Aditya DN
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import base64
|
| 3 |
+
|
| 4 |
+
def buffer_to_base64(file_obj):
|
| 5 |
+
"""
|
| 6 |
+
file_obj: gr.File komponen menerima binary stream
|
| 7 |
+
"""
|
| 8 |
+
# Baca data binary
|
| 9 |
+
binary_data = file_obj.read()
|
| 10 |
+
# Encode ke Base64 string
|
| 11 |
+
b64_str = base64.b64encode(binary_data).decode("utf-8")
|
| 12 |
+
return b64_str
|
| 13 |
+
|
| 14 |
+
# Interface Gradio
|
| 15 |
+
with gr.Blocks() as demo:
|
| 16 |
+
gr.Markdown("# ESP32 Buffer to Base64 API")
|
| 17 |
+
with gr.Row():
|
| 18 |
+
inp = gr.File(label="Upload Raw Buffer", type="binary")
|
| 19 |
+
out = gr.Textbox(label="Base64 Output")
|
| 20 |
+
btn = gr.Button("Convert")
|
| 21 |
+
btn.click(buffer_to_base64, inputs=inp, outputs=out)
|
| 22 |
+
|
| 23 |
+
# Jalankan sebagai REST API
|
| 24 |
+
if __name__ == "__main__":
|
| 25 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|