tumuyan2 commited on
Commit
5e70c4c
·
1 Parent(s): d29dbb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -7
app.py CHANGED
@@ -20,7 +20,7 @@ task_counter = 0
20
  # 新增日志函数
21
  def print_log(task_id, filename, stage, status):
22
  if log_to_terminal:
23
- print(f"任务 ID: {task_id}, 文件名: {filename}, 状态: [{status}], 阶段: {stage}")
24
 
25
  # 修改 start_process 函数,处理新增输入
26
  def start_process(input1, input2, shape0_str, shape1_str, input_suffix=".pth"):
@@ -57,7 +57,7 @@ def start_process(input1, input2, shape0_str, shape1_str, input_suffix=".pth"):
57
  log += f"开始下载文件…\n"
58
  yield [], log
59
  # 生成唯一文件名
60
- file_name = str(uuid.uuid4()) + input_suffix
61
  file_path = os.path.join(os.getcwd(), file_name)
62
  if url.startswith('ftp://'):
63
  try:
@@ -199,7 +199,7 @@ def start_process(input1, input2, shape0_str, shape1_str, input_suffix=".pth"):
199
  if output == '' and process.poll() is not None:
200
  break
201
  if output:
202
- log += output.strip() + '\n'
203
  if log_to_terminal:
204
  print(output.strip())
205
  returncode = process.poll()
@@ -215,8 +215,8 @@ def start_process(input1, input2, shape0_str, shape1_str, input_suffix=".pth"):
215
  print_log(task_id, input2, "执行命令", "完成")
216
  yield output_files, log
217
  except Exception as e:
218
- log += f"发生错误: {str(e)}\n"
219
- print_log(task_id, input2,str(e) , f"失败")
220
  yield [], log
221
 
222
  # 创建 Gradio 界面
@@ -232,17 +232,27 @@ with gr.Blocks() as demo:
232
  # 修改为字符串输入控件
233
  shape0_str = gr.Textbox(label="shape0 (逗号分隔的整数)", value="1,3,128,128")
234
  shape1_str = gr.Textbox(label="shape1 (逗号分隔的整数)", value="0,0,0,0")
235
- start_button = gr.Button("开始")
 
 
 
236
  # 右侧列,包含输出组件和日志文本框
237
  with gr.Column():
238
  output = gr.File(label="输出文件", file_count="multiple")
239
  log_textbox = gr.Textbox(label="日志", lines=10, interactive=False)
240
 
241
  # 绑定事件,修改输入参数
242
- start_button.click(
243
  fn=start_process,
244
  inputs=[input1_file if input1_file.value else input1, input2, shape0_str, shape1_str],
245
  outputs=[output, log_textbox]
246
  )
 
 
 
 
 
 
 
247
 
248
  demo.launch()
 
20
  # 新增日志函数
21
  def print_log(task_id, filename, stage, status):
22
  if log_to_terminal:
23
+ print(f"任务{task_id}: {filename}, [{status}] {stage}")
24
 
25
  # 修改 start_process 函数,处理新增输入
26
  def start_process(input1, input2, shape0_str, shape1_str, input_suffix=".pth"):
 
57
  log += f"开始下载文件…\n"
58
  yield [], log
59
  # 生成唯一文件名
60
+ file_name = str(task_id) + input_suffix
61
  file_path = os.path.join(os.getcwd(), file_name)
62
  if url.startswith('ftp://'):
63
  try:
 
199
  if output == '' and process.poll() is not None:
200
  break
201
  if output:
202
+ # log += output.strip() + '\n'
203
  if log_to_terminal:
204
  print(output.strip())
205
  returncode = process.poll()
 
215
  print_log(task_id, input2, "执行命令", "完成")
216
  yield output_files, log
217
  except Exception as e:
218
+ log += f"发生错误: {e}\n"
219
+ print_log(task_id, input2, e , f"失败")
220
  yield [], log
221
 
222
  # 创建 Gradio 界面
 
232
  # 修改为字符串输入控件
233
  shape0_str = gr.Textbox(label="shape0 (逗号分隔的整数)", value="1,3,128,128")
234
  shape1_str = gr.Textbox(label="shape1 (逗号分隔的整数)", value="0,0,0,0")
235
+ with gr.Row():
236
+ start_button = gr.Button("开始")
237
+ # 添加取消按钮
238
+ cancel_button = gr.Button("取消")
239
  # 右侧列,包含输出组件和日志文本框
240
  with gr.Column():
241
  output = gr.File(label="输出文件", file_count="multiple")
242
  log_textbox = gr.Textbox(label="日志", lines=10, interactive=False)
243
 
244
  # 绑定事件,修改输入参数
245
+ process = start_button.click(
246
  fn=start_process,
247
  inputs=[input1_file if input1_file.value else input1, input2, shape0_str, shape1_str],
248
  outputs=[output, log_textbox]
249
  )
250
+ # 为取消按钮添加点击事件绑定,使用 cancels 属性取消 start_process 任务
251
+ cancel_button.click(
252
+ fn=None,
253
+ inputs=None,
254
+ outputs=None,
255
+ cancels=[process]
256
+ )
257
 
258
  demo.launch()