rapacious commited on
Commit
9a9f90b
·
verified ·
1 Parent(s): 08e700f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -17
app.py CHANGED
@@ -18,20 +18,16 @@ def write_log_to_file(message):
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"
@@ -41,25 +37,21 @@ def run_engine(input_param, log_state):
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
@@ -88,10 +80,8 @@ def clear_log(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():
@@ -99,20 +89,18 @@ with gr.Blocks(title="Engine Runner on Hugging Face Space") as demo:
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
 
18
 
19
  # Hàm chạy file engine và cập nhật log
20
  def run_engine(input_param, log_state):
 
21
  if log_state is None:
22
  log_state = read_log_from_file()
23
 
 
24
  engine_path = "./engine.bin"
25
 
 
26
  if not os.path.exists(engine_path):
27
  log_state += "Lỗi: File engine.bin không tồn tại!\n"
28
  write_log_to_file("Lỗi: File engine.bin không tồn tại!")
29
  return log_state, log_state
30
 
 
31
  try:
32
  os.chmod(engine_path, 0o755)
33
  log_state += "Đã cấp quyền thực thi cho engine.bin.\n"
 
37
  write_log_to_file(f"Lỗi khi cấp quyền thực thi: {str(e)}")
38
  return log_state, log_state
39
 
 
40
  start_message = f"Bắt đầu chạy engine với tham số: {input_param}\n"
41
  log_state += start_message
42
  write_log_to_file(start_message)
43
 
44
  try:
 
45
  process = subprocess.Popen([engine_path, input_param], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
46
 
 
47
  while process.poll() is None:
48
  output = process.stdout.readline()
49
  if output:
50
  log_state += output
51
  write_log_to_file(output.strip())
52
  yield log_state, log_state
53
+ time.sleep(0.1)
54
 
 
55
  stdout, stderr = process.communicate()
56
  if stdout:
57
  log_state += stdout
 
80
 
81
  # Tạo giao diện Gradio
82
  with gr.Blocks(title="Engine Runner on Hugging Face Space") as demo:
 
83
  log_state = gr.State(value=None)
84
 
 
85
  gr.Markdown("## Chạy Engine Binary và Hiển thị Log")
86
  input_param = gr.Textbox(label="Tham số đầu vào", placeholder="Nhập tham số cho engine")
87
  with gr.Row():
 
89
  clear_button = gr.Button("Xóa Log")
90
  log_output = gr.Textbox(label="Log", lines=10, interactive=False, value=read_log_from_file())
91
 
92
+ # Sự kiện khi nhấn nút "Chạy Engine" (xóa _js)
93
  run_button.click(
94
  fn=run_engine,
95
  inputs=[input_param, log_state],
96
+ outputs=[log_output, log_state]
 
97
  )
98
 
99
+ # Sự kiện khi nhấn nút "Xóa Log" (xóa _js)
100
  clear_button.click(
101
  fn=clear_log,
102
  inputs=[log_state],
103
+ outputs=[log_output, log_state]
 
104
  )
105
 
106
  # Chạy ứng dụng