arudradey commited on
Commit
dde59de
Β·
verified Β·
1 Parent(s): 8543e30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -58
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 refresh_list():
12
- """Helper to update the file list in the UI"""
13
- files = os.listdir(SAVE_DIR)
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 it has a .txt extension if not provided
21
- if not filename.endswith('.txt'):
22
- filename += '.txt'
23
-
24
- full_path = os.path.join(SAVE_DIR, filename)
25
- try:
26
- with open(full_path, "w") as f:
27
- f.write(content)
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
- full_path = os.path.join(SAVE_DIR, filename)
 
37
  try:
38
- if os.path.exists(full_path):
39
- os.remove(full_path)
40
- return f"πŸ—‘οΈ Deleted '{filename}'", refresh_list()
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  else:
42
- return "❌ Error: File not found.", refresh_list()
 
43
  except Exception as e:
44
- return f"❌ Error: {str(e)}", refresh_list()
 
 
 
 
45
 
46
- # Building the UI
47
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
48
- gr.Markdown("# πŸ—„οΈ Bucket Manager (Persistent /data)")
49
 
50
- with gr.Tab("Write New File"):
 
51
  with gr.Row():
52
- file_name_input = gr.Textbox(label="Filename (e.g., logic_v1.txt)", placeholder="my_logic.txt")
53
- text_content_input = gr.TextArea(label="File Content", placeholder="Write your C logic or notes here...", lines=10)
54
- save_btn = gr.Button("πŸ’Ύ Save to Bucket", variant="primary")
55
- save_status = gr.Textbox(label="Status")
56
 
57
- with gr.Tab("Manage Bucket"):
58
- file_list_dropdown = gr.Dropdown(label="Files in /data", choices=os.listdir(SAVE_DIR))
59
- with gr.Row():
60
- refresh_btn = gr.Button("πŸ”„ Refresh List")
61
- delete_btn = gr.Button("πŸ—‘οΈ Delete Selected", variant="stop")
62
- delete_status = gr.Textbox(label="Status")
63
 
64
- # Wire up the logic
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()