Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# file: app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from skidl import *
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def generate_circuit(skidl_code):
|
| 8 |
+
# Reset SKiDL trước mỗi lần build
|
| 9 |
+
reset()
|
| 10 |
+
try:
|
| 11 |
+
# Lưu code tạm thời
|
| 12 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.py') as tmp:
|
| 13 |
+
tmp.write(skidl_code.encode('utf-8'))
|
| 14 |
+
tmp_path = tmp.name
|
| 15 |
+
|
| 16 |
+
# Thực thi code
|
| 17 |
+
namespace = {}
|
| 18 |
+
exec(open(tmp_path).read(), namespace)
|
| 19 |
+
|
| 20 |
+
# Generate netlist
|
| 21 |
+
netlist_str = generate_netlist()
|
| 22 |
+
|
| 23 |
+
# Lưu netlist thành file cho phép tải
|
| 24 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.net', mode='w') as out_file:
|
| 25 |
+
out_file.write(netlist_str)
|
| 26 |
+
netlist_file_path = out_file.name
|
| 27 |
+
|
| 28 |
+
return "Netlist Generated Successfully!", netlist_file_path
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error: {e}", None
|
| 32 |
+
|
| 33 |
+
finally:
|
| 34 |
+
if os.path.exists(tmp_path):
|
| 35 |
+
os.remove(tmp_path)
|
| 36 |
+
|
| 37 |
+
# Gradio UI
|
| 38 |
+
with gr.Blocks() as demo:
|
| 39 |
+
gr.Markdown("# SKiDL Circuit Builder")
|
| 40 |
+
skidl_input = gr.Textbox(label="Enter SKiDL Python Code", lines=20)
|
| 41 |
+
output_message = gr.Textbox(label="Status / Error", interactive=False)
|
| 42 |
+
download_btn = gr.File(label="Download Netlist")
|
| 43 |
+
|
| 44 |
+
run_button = gr.Button("Build Circuit")
|
| 45 |
+
|
| 46 |
+
run_button.click(fn=generate_circuit, inputs=[skidl_input], outputs=[output_message, download_btn])
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch()
|