Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,73 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Define the persistent directory
|
| 5 |
SAVE_DIR = "/data"
|
| 6 |
-
|
| 7 |
-
# Ensure the directory exists
|
| 8 |
if not os.path.exists(SAVE_DIR):
|
| 9 |
os.makedirs(SAVE_DIR, exist_ok=True)
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
return gr.Dropdown(choices=files, value=None if not files else files[0])
|
| 15 |
-
|
| 16 |
-
def save_text_file(filename, content):
|
| 17 |
-
if not filename:
|
| 18 |
-
return "β Error: Please provide a filename.", refresh_list()
|
| 19 |
|
| 20 |
-
# Ensure
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return f"β
Saved '{filename}' to /data", refresh_list()
|
| 29 |
-
except Exception as e:
|
| 30 |
-
return f"β Error: {str(e)}", refresh_list()
|
| 31 |
-
|
| 32 |
-
def delete_file(filename):
|
| 33 |
-
if not filename:
|
| 34 |
-
return "β Error: No file selected.", refresh_list()
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
else:
|
| 42 |
-
return "β Error:
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
-
return f"β Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
gr.Markdown("# ποΈ Bucket Manager (Persistent /data)")
|
| 49 |
|
| 50 |
-
with gr.Tab("
|
|
|
|
| 51 |
with gr.Row():
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
with gr.Tab("
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
delete_status = gr.Textbox(label="Status")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
save_btn.click(fn=save_text_file,
|
| 66 |
-
inputs=[file_name_input, text_content_input],
|
| 67 |
-
outputs=[save_status, file_list_dropdown])
|
| 68 |
-
|
| 69 |
-
delete_btn.click(fn=delete_file,
|
| 70 |
-
inputs=file_list_dropdown,
|
| 71 |
-
outputs=[delete_status, file_list_dropdown])
|
| 72 |
-
|
| 73 |
-
refresh_btn.click(fn=refresh_list,
|
| 74 |
-
outputs=file_list_dropdown)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
-
demo.launch()
|
| 78 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
import shutil
|
| 5 |
|
| 6 |
# Define the persistent directory
|
| 7 |
SAVE_DIR = "/data"
|
|
|
|
|
|
|
| 8 |
if not os.path.exists(SAVE_DIR):
|
| 9 |
os.makedirs(SAVE_DIR, exist_ok=True)
|
| 10 |
|
| 11 |
+
def compile_c_logic(c_code, filename):
|
| 12 |
+
if not filename or not c_code:
|
| 13 |
+
return "β Error: Filename and C code are required."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Ensure filename doesn't have extension for the command
|
| 16 |
+
base_name = filename.split('.')[0]
|
| 17 |
+
c_file_path = os.path.join("/tmp", f"{base_name}.c")
|
| 18 |
+
wasm_output_path = os.path.join(SAVE_DIR, f"{base_name}.wasm")
|
| 19 |
+
|
| 20 |
+
# 1. Write the C code to a temp file
|
| 21 |
+
with open(c_file_path, "w") as f:
|
| 22 |
+
f.write(c_code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# 2. Run Emscripten (emcc)
|
| 25 |
+
# Note: Hugging Face runs on Linux, so we call emcc directly
|
| 26 |
try:
|
| 27 |
+
# -s WASM=1 tells it to produce a .wasm file
|
| 28 |
+
# -o name.js is required by emcc to generate the .wasm
|
| 29 |
+
temp_js = os.path.join("/tmp", f"{base_name}.js")
|
| 30 |
+
result = subprocess.run(
|
| 31 |
+
["emcc", c_file_path, "-s", "WASM=1", "-o", temp_js],
|
| 32 |
+
capture_output=True, text=True
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
if result.returncode != 0:
|
| 36 |
+
return f"β Compilation Error:\n{result.stderr}"
|
| 37 |
+
|
| 38 |
+
# 3. Move the generated .wasm from /tmp to persistent /data
|
| 39 |
+
temp_wasm = os.path.join("/tmp", f"{base_name}.wasm")
|
| 40 |
+
if os.path.exists(temp_wasm):
|
| 41 |
+
shutil.move(temp_wasm, wasm_output_path)
|
| 42 |
+
return f"β
Success! Compiled to: {wasm_output_path}\n\nConsole:\n{result.stdout}"
|
| 43 |
else:
|
| 44 |
+
return "β Error: .wasm file was not generated."
|
| 45 |
+
|
| 46 |
except Exception as e:
|
| 47 |
+
return f"β System Error: {str(e)}"
|
| 48 |
+
|
| 49 |
+
# --- Reuse your previous file manager logic here ---
|
| 50 |
+
def list_files():
|
| 51 |
+
return gr.Dropdown(choices=os.listdir(SAVE_DIR))
|
| 52 |
|
| 53 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 54 |
+
gr.Markdown("# π οΈ LCPU: C-to-WASM & Bucket Manager")
|
|
|
|
| 55 |
|
| 56 |
+
with gr.Tab("C-to-WASM Compiler"):
|
| 57 |
+
gr.Markdown("### Write C logic and compile to Binary Pixels")
|
| 58 |
with gr.Row():
|
| 59 |
+
wasm_filename = gr.Textbox(label="Output Name", placeholder="adder_logic")
|
| 60 |
+
c_editor = gr.TextArea(label="C Source Code", placeholder="int main() { return 5 + 5; }", lines=10)
|
| 61 |
+
compile_btn = gr.Button("π Compile to WASM", variant="primary")
|
| 62 |
+
compile_status = gr.Textbox(label="Compiler Output")
|
| 63 |
|
| 64 |
+
with gr.Tab("Bucket Management"):
|
| 65 |
+
# (Insert your previous Save/Delete/Refresh logic here)
|
| 66 |
+
file_list = gr.Dropdown(label="Files in /data", choices=os.listdir(SAVE_DIR))
|
| 67 |
+
refresh_btn = gr.Button("π Refresh")
|
| 68 |
+
refresh_btn.click(fn=list_files, outputs=file_list)
|
|
|
|
| 69 |
|
| 70 |
+
compile_btn.click(fn=compile_c_logic, inputs=[c_editor, wasm_filename], outputs=compile_status)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
+
demo.launch()
|
|
|