Yifeng Wang(正经人王同学) commited on
Commit
7b943cd
·
2 Parent(s): 7a01f51 9c5d400

fix webdemo question (#145)

Browse files
Files changed (2) hide show
  1. owl/app.py +35 -15
  2. owl/script_adapter.py +5 -1
owl/app.py CHANGED
@@ -245,21 +245,32 @@ def terminate_process():
245
 
246
  with process_lock:
247
  if current_process is not None and current_process.poll() is None:
248
- # 在Windows上使用CTRL_BREAK_EVENT,在Unix上使用SIGTERM
249
- if os.name == "nt":
250
- current_process.send_signal(signal.CTRL_BREAK_EVENT)
251
- else:
252
- current_process.terminate()
253
-
254
- # 等待进程终止
255
  try:
256
- current_process.wait(timeout=5)
257
- except subprocess.TimeoutExpired:
258
- # 如果进程没有在5秒内终止,强制终止
259
- current_process.kill()
260
-
261
- log_queue.put("进程已终止\n")
262
- return "✅ 进程已终止"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  else:
264
  return "❌ 没有正在运行的进程"
265
 
@@ -296,6 +307,10 @@ def run_script(script_dropdown, question, progress=gr.Progress()):
296
 
297
  # 创建环境变量副本并添加问题
298
  env = os.environ.copy()
 
 
 
 
299
  env["OWL_QUESTION"] = question
300
 
301
  # 启动进程
@@ -488,12 +503,17 @@ def create_ui():
488
  )
489
 
490
  question_input = gr.Textbox(
491
- lines=5, placeholder="请输入您的问题...", label="问题"
 
 
 
 
492
  )
493
 
494
  gr.Markdown(
495
  """
496
  > **注意**: 您输入的问题将替换脚本中的默认问题。系统会自动处理问题的替换,确保您的问题被正确使用。
 
497
  """
498
  )
499
 
 
245
 
246
  with process_lock:
247
  if current_process is not None and current_process.poll() is None:
 
 
 
 
 
 
 
248
  try:
249
+ # 在Windows上使用taskkill强制终止进程树
250
+ if os.name == "nt":
251
+ # 获取进程ID
252
+ pid = current_process.pid
253
+ # 使用taskkill命令终止进程及其子进程
254
+ subprocess.run(f"taskkill /F /T /PID {pid}", shell=True)
255
+ else:
256
+ # 在Unix上使用SIGTERM和SIGKILL
257
+ current_process.terminate()
258
+ try:
259
+ current_process.wait(timeout=3)
260
+ except subprocess.TimeoutExpired:
261
+ current_process.kill()
262
+
263
+ # 等待进程终止
264
+ try:
265
+ current_process.wait(timeout=2)
266
+ except subprocess.TimeoutExpired:
267
+ pass # 已经尝试强制终止,忽略超时
268
+
269
+ log_queue.put("进程已终止\n")
270
+ return "✅ 进程已终止"
271
+ except Exception as e:
272
+ log_queue.put(f"终止进程时出错: {str(e)}\n")
273
+ return f"❌ 终止进程时出错: {str(e)}"
274
  else:
275
  return "❌ 没有正在运行的进程"
276
 
 
307
 
308
  # 创建环境变量副本并添加问题
309
  env = os.environ.copy()
310
+ # 确保问题是字符串类型
311
+ if not isinstance(question, str):
312
+ question = str(question)
313
+ # 保留换行符,但确保是有效的字符串
314
  env["OWL_QUESTION"] = question
315
 
316
  # 启动进程
 
503
  )
504
 
505
  question_input = gr.Textbox(
506
+ lines=8,
507
+ placeholder="请输入您的问题...",
508
+ label="问题",
509
+ elem_id="question_input",
510
+ show_copy_button=True
511
  )
512
 
513
  gr.Markdown(
514
  """
515
  > **注意**: 您输入的问题将替换脚本中的默认问题。系统会自动处理问题的替换,确保您的问题被正确使用。
516
+ > 支持多行输入,换行将被保留。
517
  """
518
  )
519
 
owl/script_adapter.py CHANGED
@@ -68,7 +68,11 @@ def run_script_with_env_question(script_name):
68
 
69
  # 转义问题中的特殊字符
70
  escaped_question = (
71
- question.replace("\\", "\\\\").replace('"', '\\"').replace("'", "\\'")
 
 
 
 
72
  )
73
 
74
  # 查找脚本中所有的question赋值 - 改进的正则表达式
 
68
 
69
  # 转义问题中的特殊字符
70
  escaped_question = (
71
+ question.replace("\\", "\\\\")
72
+ .replace('"', '\\"')
73
+ .replace("'", "\\'")
74
+ .replace("\n", "\\n") # 转义换行符
75
+ .replace("\r", "\\r") # 转义回车符
76
  )
77
 
78
  # 查找脚本中所有的question赋值 - 改进的正则表达式