|
import gradio as gr |
|
import subprocess |
|
import os |
|
import time |
|
|
|
|
|
def read_log_from_file(): |
|
log_file = "logger.txt" |
|
if os.path.exists(log_file): |
|
with open(log_file, "r", encoding="utf-8") as f: |
|
return f.read() |
|
return "Chưa có log nào trong file logger.txt.\n" |
|
|
|
|
|
def write_log_to_file(message): |
|
with open("logger.txt", "a", encoding="utf-8") as log_file: |
|
log_file.write(f"{message}\n") |
|
|
|
|
|
def run_engine(input_param, log_state): |
|
if log_state is None: |
|
log_state = read_log_from_file() |
|
|
|
engine_path = "./engine.bin" |
|
|
|
if not os.path.exists(engine_path): |
|
log_state += "Lỗi: File engine.bin không tồn tại!\n" |
|
write_log_to_file("Lỗi: File engine.bin không tồn tại!") |
|
return log_state, log_state |
|
|
|
try: |
|
os.chmod(engine_path, 0o755) |
|
log_state += "Đã cấp quyền thực thi cho engine.bin.\n" |
|
write_log_to_file("Đã cấp quyền thực thi cho engine.bin.") |
|
except Exception as e: |
|
log_state += f"Lỗi khi cấp quyền thực thi: {str(e)}\n" |
|
write_log_to_file(f"Lỗi khi cấp quyền thực thi: {str(e)}") |
|
return log_state, log_state |
|
|
|
start_message = f"Bắt đầu chạy engine với tham số: {input_param}\n" |
|
log_state += start_message |
|
write_log_to_file(start_message) |
|
|
|
try: |
|
process = subprocess.Popen([engine_path, input_param], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
|
|
|
while process.poll() is None: |
|
output = process.stdout.readline() |
|
if output: |
|
log_state += output |
|
write_log_to_file(output.strip()) |
|
yield log_state, log_state |
|
time.sleep(0.1) |
|
|
|
stdout, stderr = process.communicate() |
|
if stdout: |
|
log_state += stdout |
|
write_log_to_file(stdout.strip()) |
|
if stderr: |
|
log_state += "Lỗi: " + stderr |
|
write_log_to_file("Lỗi: " + stderr.strip()) |
|
|
|
log_state += "Hoàn tất quá trình chạy engine.\n" |
|
write_log_to_file("Hoàn tất quá trình chạy engine.") |
|
|
|
except Exception as e: |
|
error_message = f"Lỗi khi chạy engine: {str(e)}\n" |
|
log_state += error_message |
|
write_log_to_file(error_message) |
|
|
|
return log_state, log_state |
|
|
|
|
|
def clear_log(log_state): |
|
log_state = "Log đã được xóa.\n" |
|
if os.path.exists("logger.txt"): |
|
os.remove("logger.txt") |
|
write_log_to_file("Log đã được xóa.") |
|
return log_state, log_state |
|
|
|
|
|
with gr.Blocks(title="Engine Runner on Hugging Face Space") as demo: |
|
log_state = gr.State(value=None) |
|
|
|
gr.Markdown("## Chạy Engine Binary và Hiển thị Log") |
|
input_param = gr.Textbox(label="Tham số đầu vào", placeholder="Nhập tham số cho engine") |
|
with gr.Row(): |
|
run_button = gr.Button("Chạy Engine") |
|
clear_button = gr.Button("Xóa Log") |
|
log_output = gr.Textbox(label="Log", lines=10, interactive=False, value=read_log_from_file()) |
|
|
|
|
|
run_button.click( |
|
fn=run_engine, |
|
inputs=[input_param, log_state], |
|
outputs=[log_output, log_state] |
|
) |
|
|
|
|
|
clear_button.click( |
|
fn=clear_log, |
|
inputs=[log_state], |
|
outputs=[log_output, log_state] |
|
) |
|
|
|
|
|
demo.queue().launch() |