File size: 3,670 Bytes
147790a 9a9f90b 147790a 9a9f90b 147790a 9a9f90b 147790a 9a9f90b 147790a 9a9f90b 147790a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import gradio as gr
import subprocess
import os
import time
# Hàm đọc log từ file logger.txt
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"
# Hàm ghi log vào file logger.txt
def write_log_to_file(message):
with open("logger.txt", "a", encoding="utf-8") as log_file:
log_file.write(f"{message}\n")
# Hàm chạy file engine và cập nhật log
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
# Hàm xóa log
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
# Tạo giao diện Gradio
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())
# Sự kiện khi nhấn nút "Chạy Engine" (xóa _js)
run_button.click(
fn=run_engine,
inputs=[input_param, log_state],
outputs=[log_output, log_state]
)
# Sự kiện khi nhấn nút "Xóa Log" (xóa _js)
clear_button.click(
fn=clear_log,
inputs=[log_state],
outputs=[log_output, log_state]
)
# Chạy ứng dụng
demo.queue().launch() |