Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
|
6 |
+
# Hàm đọc log từ file logger.txt
|
7 |
+
def read_log_from_file():
|
8 |
+
log_file = "logger.txt"
|
9 |
+
if os.path.exists(log_file):
|
10 |
+
with open(log_file, "r", encoding="utf-8") as f:
|
11 |
+
return f.read()
|
12 |
+
return "Chưa có log nào trong file logger.txt.\n"
|
13 |
+
|
14 |
+
# Hàm ghi log vào file logger.txt
|
15 |
+
def write_log_to_file(message):
|
16 |
+
with open("logger.txt", "a", encoding="utf-8") as log_file:
|
17 |
+
log_file.write(f"{message}\n")
|
18 |
+
|
19 |
+
# Hàm chạy file engine và cập nhật log
|
20 |
+
def run_engine(input_param, log_state):
|
21 |
+
# Khởi tạo log nếu log_state là None, đọc từ file nếu có
|
22 |
+
if log_state is None:
|
23 |
+
log_state = read_log_from_file()
|
24 |
+
|
25 |
+
# Đường dẫn tới file engine
|
26 |
+
engine_path = "./engine.bin"
|
27 |
+
|
28 |
+
# Kiểm tra và cấp quyền thực thi cho file engine
|
29 |
+
if not os.path.exists(engine_path):
|
30 |
+
log_state += "Lỗi: File engine.bin không tồn tại!\n"
|
31 |
+
write_log_to_file("Lỗi: File engine.bin không tồn tại!")
|
32 |
+
return log_state, log_state
|
33 |
+
|
34 |
+
# Cấp quyền thực thi (chmod 755)
|
35 |
+
try:
|
36 |
+
os.chmod(engine_path, 0o755)
|
37 |
+
log_state += "Đã cấp quyền thực thi cho engine.bin.\n"
|
38 |
+
write_log_to_file("Đã cấp quyền thực thi cho engine.bin.")
|
39 |
+
except Exception as e:
|
40 |
+
log_state += f"Lỗi khi cấp quyền thực thi: {str(e)}\n"
|
41 |
+
write_log_to_file(f"Lỗi khi cấp quyền thực thi: {str(e)}")
|
42 |
+
return log_state, log_state
|
43 |
+
|
44 |
+
# Ghi log bắt đầu quá trình
|
45 |
+
start_message = f"Bắt đầu chạy engine với tham số: {input_param}\n"
|
46 |
+
log_state += start_message
|
47 |
+
write_log_to_file(start_message)
|
48 |
+
|
49 |
+
try:
|
50 |
+
# Chạy file engine với tham số đầu vào
|
51 |
+
process = subprocess.Popen([engine_path, input_param], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
52 |
+
|
53 |
+
# Đọc log theo thời gian thực từ stdout và stderr
|
54 |
+
while process.poll() is None:
|
55 |
+
output = process.stdout.readline()
|
56 |
+
if output:
|
57 |
+
log_state += output
|
58 |
+
write_log_to_file(output.strip())
|
59 |
+
yield log_state, log_state
|
60 |
+
time.sleep(0.1) # Giảm tải CPU
|
61 |
+
|
62 |
+
# Đọc phần còn lại của output sau khi process hoàn tất
|
63 |
+
stdout, stderr = process.communicate()
|
64 |
+
if stdout:
|
65 |
+
log_state += stdout
|
66 |
+
write_log_to_file(stdout.strip())
|
67 |
+
if stderr:
|
68 |
+
log_state += "Lỗi: " + stderr
|
69 |
+
write_log_to_file("Lỗi: " + stderr.strip())
|
70 |
+
|
71 |
+
log_state += "Hoàn tất quá trình chạy engine.\n"
|
72 |
+
write_log_to_file("Hoàn tất quá trình chạy engine.")
|
73 |
+
|
74 |
+
except Exception as e:
|
75 |
+
error_message = f"Lỗi khi chạy engine: {str(e)}\n"
|
76 |
+
log_state += error_message
|
77 |
+
write_log_to_file(error_message)
|
78 |
+
|
79 |
+
return log_state, log_state
|
80 |
+
|
81 |
+
# Hàm xóa log
|
82 |
+
def clear_log(log_state):
|
83 |
+
log_state = "Log đã được xóa.\n"
|
84 |
+
if os.path.exists("logger.txt"):
|
85 |
+
os.remove("logger.txt")
|
86 |
+
write_log_to_file("Log đã được xóa.")
|
87 |
+
return log_state, log_state
|
88 |
+
|
89 |
+
# Tạo giao diện Gradio
|
90 |
+
with gr.Blocks(title="Engine Runner on Hugging Face Space") as demo:
|
91 |
+
# State để lưu log
|
92 |
+
log_state = gr.State(value=None)
|
93 |
+
|
94 |
+
# Giao diện người dùng
|
95 |
+
gr.Markdown("## Chạy Engine Binary và Hiển thị Log")
|
96 |
+
input_param = gr.Textbox(label="Tham số đầu vào", placeholder="Nhập tham số cho engine")
|
97 |
+
with gr.Row():
|
98 |
+
run_button = gr.Button("Chạy Engine")
|
99 |
+
clear_button = gr.Button("Xóa Log")
|
100 |
+
log_output = gr.Textbox(label="Log", lines=10, interactive=False, value=read_log_from_file())
|
101 |
+
|
102 |
+
# Sự kiện khi nhấn nút "Chạy Engine"
|
103 |
+
run_button.click(
|
104 |
+
fn=run_engine,
|
105 |
+
inputs=[input_param, log_state],
|
106 |
+
outputs=[log_output, log_state],
|
107 |
+
_js=None
|
108 |
+
)
|
109 |
+
|
110 |
+
# Sự kiện khi nhấn nút "Xóa Log"
|
111 |
+
clear_button.click(
|
112 |
+
fn=clear_log,
|
113 |
+
inputs=[log_state],
|
114 |
+
outputs=[log_output, log_state],
|
115 |
+
_js=None
|
116 |
+
)
|
117 |
+
|
118 |
+
# Chạy ứng dụng
|
119 |
+
demo.queue().launch()
|