Spaces:
Runtime error
Runtime error
import gradio as gr | |
import subprocess | |
import os | |
logs = [] | |
inference_logs = [] | |
# تأكد من أن مجلد /data موجود وآمن للكتابة | |
DATA_DIR = os.path.join(os.getcwd(), "data") | |
os.makedirs(DATA_DIR, exist_ok=True) | |
def run_training(): | |
global logs | |
logs = [] | |
# تنفيذ سكربت التدريب | |
process = subprocess.Popen( | |
["python", "run.py"], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT, | |
text=True, | |
bufsize=1 | |
) | |
# عرض اللوجات بشكل مباشر | |
for line in process.stdout: | |
logs.append(line) | |
yield "\n".join(logs[:]) # عرض آخر 50 سطر فقط | |
def run_inference(): | |
global inference_logs | |
inference_logs = [] | |
output_lines = [] | |
# تنفيذ سكربت التنبؤ | |
process = subprocess.Popen( | |
["python", "inference.py"], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT, | |
text=True, | |
bufsize=1 | |
) | |
for line in process.stdout: | |
inference_logs.append(line) | |
output_lines.append(line) | |
# حفظ نتائج التنبؤ في ملف داخل /data | |
inference_output_path = os.path.join(DATA_DIR, "inference_results.txt") | |
with open(inference_output_path, "a") as f: | |
f.write("=== New Inference Run ===\n") | |
f.writelines(output_lines) | |
f.write("\n") | |
yield "\n".join(inference_logs[:]) # عرض آخر 50 سطر فقط | |
# واجهة Gradio | |
with gr.Blocks(title="VRP Transformer Training & Inference") as demo: | |
gr.Markdown("# 🚛 Vehicle Routing Problem Solver with Transformer + Reinforcement Learning") | |
with gr.Tab("🚀 Start Training"): | |
with gr.Row(): | |
start_btn = gr.Button("Start Training") | |
output = gr.Textbox(label="Training Logs", lines=25) | |
start_btn.click(fn=run_training, outputs=output) | |
with gr.Tab("🔍 Run Inference"): | |
with gr.Row(): | |
infer_btn = gr.Button("Run Inference on New Batch") | |
infer_output = gr.Textbox(label="Inference Results", lines=15) | |
infer_btn.click(fn=run_inference, outputs=infer_output) | |
demo.launch() | |