Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import traceback
|
|
|
|
| 4 |
from vajra.assembler import assemble, AssembleError
|
| 5 |
from vajra.linker import link, LinkerError
|
| 6 |
from vajra.cpu import VajraCPU
|
| 7 |
from vajra.utils import format_registers, format_log, load_example_program
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
def run_vajra(source: str, mode: str, max_steps: int):
|
|
|
|
| 10 |
try:
|
|
|
|
| 11 |
imem, data, directives, symbols = assemble(source)
|
|
|
|
|
|
|
| 12 |
data, symbols = link(directives, data, symbols)
|
|
|
|
|
|
|
| 13 |
cpu = VajraCPU()
|
| 14 |
cpu.load_program(imem)
|
| 15 |
cpu.load_data(data)
|
| 16 |
-
|
|
|
|
| 17 |
if mode == "Run":
|
| 18 |
result = cpu.run(max_steps=max_steps)
|
| 19 |
output = f"✅ Execution complete.\n\nFinal R0: {result:.6f}\nCycles: {cpu.cycle_count}\n\n"
|
| 20 |
output += format_registers(cpu.R, cpu.PC, cpu.SP)
|
| 21 |
output += f"\n\n--- Execution Log ---\n{format_log(cpu.log)}"
|
|
|
|
| 22 |
elif mode == "Step":
|
| 23 |
steps = min(max_steps, 50)
|
| 24 |
for _ in range(steps):
|
| 25 |
if cpu.halted:
|
| 26 |
break
|
| 27 |
cpu.step()
|
|
|
|
| 28 |
output = f"⏯ Stepped {cpu.cycle_count} instructions.\n\n"
|
| 29 |
output += format_registers(cpu.R, cpu.PC, cpu.SP)
|
| 30 |
output += f"\n\n--- Execution Log ---\n{format_log(cpu.log)}"
|
|
|
|
| 31 |
else: # Profile
|
| 32 |
cpu.run(max_steps=max_steps)
|
| 33 |
output = f"📊 Profile:\n"
|
|
@@ -35,19 +49,25 @@ def run_vajra(source: str, mode: str, max_steps: int):
|
|
| 35 |
output += f" Halted: {cpu.halted}\n"
|
| 36 |
output += f" Final PC: {cpu.PC}\n"
|
| 37 |
output += f" Final R0: {cpu.R[0]:.6f}\n"
|
|
|
|
| 38 |
return output
|
|
|
|
| 39 |
except (AssembleError, LinkerError) as e:
|
| 40 |
return f"❌ Error: {e}"
|
| 41 |
except Exception as e:
|
| 42 |
return f"❌ Runtime error: {e}\n\nDetails:\n{traceback.format_exc()}"
|
| 43 |
|
|
|
|
| 44 |
def load_example(example_name: str):
|
|
|
|
| 45 |
return load_example_program(example_name)
|
| 46 |
|
|
|
|
|
|
|
| 47 |
with gr.Blocks(title="Vajra-ISA Virtual Machine") as demo:
|
| 48 |
gr.Markdown("# 🕉️ Vajra-ISA (वज्र-ISA)")
|
| 49 |
gr.Markdown("Vedic-inspired Virtual Machine for Neuro-Symbolic AI")
|
| 50 |
-
|
| 51 |
with gr.Row():
|
| 52 |
with gr.Column(scale=2):
|
| 53 |
code_input = gr.Code(
|
|
@@ -56,6 +76,7 @@ with gr.Blocks(title="Vajra-ISA Virtual Machine") as demo:
|
|
| 56 |
value=load_example_program('addition'),
|
| 57 |
lines=20
|
| 58 |
)
|
|
|
|
| 59 |
with gr.Row():
|
| 60 |
example_dropdown = gr.Dropdown(
|
| 61 |
choices=['addition', 'multiply', 'perceptron', 'dhyana', 'memory'],
|
|
@@ -67,6 +88,7 @@ with gr.Blocks(title="Vajra-ISA Virtual Machine") as demo:
|
|
| 67 |
value='Run',
|
| 68 |
label="Execution Mode"
|
| 69 |
)
|
|
|
|
| 70 |
max_steps = gr.Slider(
|
| 71 |
minimum=10,
|
| 72 |
maximum=100000,
|
|
@@ -74,19 +96,23 @@ with gr.Blocks(title="Vajra-ISA Virtual Machine") as demo:
|
|
| 74 |
step=10,
|
| 75 |
label="Max Steps"
|
| 76 |
)
|
|
|
|
| 77 |
run_btn = gr.Button("▶ Execute", variant="primary")
|
|
|
|
| 78 |
with gr.Column(scale=2):
|
| 79 |
output = gr.Textbox(
|
| 80 |
label="Output & Register State",
|
| 81 |
lines=30,
|
| 82 |
interactive=False
|
| 83 |
)
|
| 84 |
-
|
|
|
|
| 85 |
example_dropdown.change(
|
| 86 |
fn=load_example,
|
| 87 |
inputs=example_dropdown,
|
| 88 |
outputs=code_input
|
| 89 |
)
|
|
|
|
| 90 |
run_btn.click(
|
| 91 |
fn=run_vajra,
|
| 92 |
inputs=[code_input, mode_dropdown, max_steps],
|
|
@@ -94,6 +120,7 @@ with gr.Blocks(title="Vajra-ISA Virtual Machine") as demo:
|
|
| 94 |
)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
|
|
|
| 97 |
demo.launch(
|
| 98 |
server_name="0.0.0.0",
|
| 99 |
server_port=int(os.environ.get("PORT", 7860)),
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import traceback
|
| 4 |
+
import spaces # <--- ही नवीन ओळ (ZeroGPU साठी)
|
| 5 |
from vajra.assembler import assemble, AssembleError
|
| 6 |
from vajra.linker import link, LinkerError
|
| 7 |
from vajra.cpu import VajraCPU
|
| 8 |
from vajra.utils import format_registers, format_log, load_example_program
|
| 9 |
|
| 10 |
+
|
| 11 |
+
# <--- हा डेकोरेटर (महत्त्वाचा!)
|
| 12 |
+
@spaces.GPU
|
| 13 |
def run_vajra(source: str, mode: str, max_steps: int):
|
| 14 |
+
"""Execute Vajra program and return results."""
|
| 15 |
try:
|
| 16 |
+
# 1. Assemble
|
| 17 |
imem, data, directives, symbols = assemble(source)
|
| 18 |
+
|
| 19 |
+
# 2. Link (load HF datasets if needed)
|
| 20 |
data, symbols = link(directives, data, symbols)
|
| 21 |
+
|
| 22 |
+
# 3. Create CPU and load program
|
| 23 |
cpu = VajraCPU()
|
| 24 |
cpu.load_program(imem)
|
| 25 |
cpu.load_data(data)
|
| 26 |
+
|
| 27 |
+
# 4. Execute
|
| 28 |
if mode == "Run":
|
| 29 |
result = cpu.run(max_steps=max_steps)
|
| 30 |
output = f"✅ Execution complete.\n\nFinal R0: {result:.6f}\nCycles: {cpu.cycle_count}\n\n"
|
| 31 |
output += format_registers(cpu.R, cpu.PC, cpu.SP)
|
| 32 |
output += f"\n\n--- Execution Log ---\n{format_log(cpu.log)}"
|
| 33 |
+
|
| 34 |
elif mode == "Step":
|
| 35 |
steps = min(max_steps, 50)
|
| 36 |
for _ in range(steps):
|
| 37 |
if cpu.halted:
|
| 38 |
break
|
| 39 |
cpu.step()
|
| 40 |
+
|
| 41 |
output = f"⏯ Stepped {cpu.cycle_count} instructions.\n\n"
|
| 42 |
output += format_registers(cpu.R, cpu.PC, cpu.SP)
|
| 43 |
output += f"\n\n--- Execution Log ---\n{format_log(cpu.log)}"
|
| 44 |
+
|
| 45 |
else: # Profile
|
| 46 |
cpu.run(max_steps=max_steps)
|
| 47 |
output = f"📊 Profile:\n"
|
|
|
|
| 49 |
output += f" Halted: {cpu.halted}\n"
|
| 50 |
output += f" Final PC: {cpu.PC}\n"
|
| 51 |
output += f" Final R0: {cpu.R[0]:.6f}\n"
|
| 52 |
+
|
| 53 |
return output
|
| 54 |
+
|
| 55 |
except (AssembleError, LinkerError) as e:
|
| 56 |
return f"❌ Error: {e}"
|
| 57 |
except Exception as e:
|
| 58 |
return f"❌ Runtime error: {e}\n\nDetails:\n{traceback.format_exc()}"
|
| 59 |
|
| 60 |
+
|
| 61 |
def load_example(example_name: str):
|
| 62 |
+
"""Load example program."""
|
| 63 |
return load_example_program(example_name)
|
| 64 |
|
| 65 |
+
|
| 66 |
+
# Gradio Interface
|
| 67 |
with gr.Blocks(title="Vajra-ISA Virtual Machine") as demo:
|
| 68 |
gr.Markdown("# 🕉️ Vajra-ISA (वज्र-ISA)")
|
| 69 |
gr.Markdown("Vedic-inspired Virtual Machine for Neuro-Symbolic AI")
|
| 70 |
+
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column(scale=2):
|
| 73 |
code_input = gr.Code(
|
|
|
|
| 76 |
value=load_example_program('addition'),
|
| 77 |
lines=20
|
| 78 |
)
|
| 79 |
+
|
| 80 |
with gr.Row():
|
| 81 |
example_dropdown = gr.Dropdown(
|
| 82 |
choices=['addition', 'multiply', 'perceptron', 'dhyana', 'memory'],
|
|
|
|
| 88 |
value='Run',
|
| 89 |
label="Execution Mode"
|
| 90 |
)
|
| 91 |
+
|
| 92 |
max_steps = gr.Slider(
|
| 93 |
minimum=10,
|
| 94 |
maximum=100000,
|
|
|
|
| 96 |
step=10,
|
| 97 |
label="Max Steps"
|
| 98 |
)
|
| 99 |
+
|
| 100 |
run_btn = gr.Button("▶ Execute", variant="primary")
|
| 101 |
+
|
| 102 |
with gr.Column(scale=2):
|
| 103 |
output = gr.Textbox(
|
| 104 |
label="Output & Register State",
|
| 105 |
lines=30,
|
| 106 |
interactive=False
|
| 107 |
)
|
| 108 |
+
|
| 109 |
+
# Event handlers
|
| 110 |
example_dropdown.change(
|
| 111 |
fn=load_example,
|
| 112 |
inputs=example_dropdown,
|
| 113 |
outputs=code_input
|
| 114 |
)
|
| 115 |
+
|
| 116 |
run_btn.click(
|
| 117 |
fn=run_vajra,
|
| 118 |
inputs=[code_input, mode_dropdown, max_steps],
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
+
# Gradio 6.0 compatible launch
|
| 124 |
demo.launch(
|
| 125 |
server_name="0.0.0.0",
|
| 126 |
server_port=int(os.environ.get("PORT", 7860)),
|