update wendong

This commit is contained in:
Wendong
2025-03-10 04:22:08 +08:00
parent 8f6534e7e7
commit 36dcd3b87b
3 changed files with 223 additions and 158 deletions

View File

@@ -6,7 +6,6 @@ import threading
import time
from datetime import datetime
import queue
import re
from pathlib import Path
import json
import signal
@@ -22,20 +21,20 @@ process_lock = threading.Lock()
# 脚本选项
SCRIPTS = {
"Qwen Mini (中文)": "run_qwen_mini_zh.py",
"Qwen": "run_qwen.py",
"Qwen (中文)": "run_qwen_zh.py",
"Mini": "run_mini.py",
"DeepSeek": "run_deepseek.py",
"默认": "run.py",
"DeepSeek (中文)": "run_deepseek_zh.py",
"Default": "run.py",
"GAIA Roleplaying": "run_gaia_roleplaying.py"
}
# 脚本描述
SCRIPT_DESCRIPTIONS = {
"Qwen Mini (中文)": "使用阿里云Qwen模型的中文版本适合中文问答和任务",
"Qwen": "使用阿里云Qwen模型支持多种工具和功能",
"Qwen (中文)": "使用阿里云Qwen模型支持多种工具和功能",
"Mini": "轻量级版本使用OpenAI GPT-4o模型",
"DeepSeek": "使用DeepSeek模型适合复杂推理任务",
"默认": "默认OWL实现使用OpenAI GPT-4o模型和全套工具",
"DeepSeek (中文)": "使用DeepSeek模型适合非多模态任务",
"Default": "默认OWL实现使用OpenAI GPT-4o模型和全套工具",
"GAIA Roleplaying": "GAIA基准测试实现用于评估模型能力"
}
@@ -46,7 +45,7 @@ ENV_GROUPS = {
"name": "OPENAI_API_KEY",
"label": "OpenAI API密钥",
"type": "password",
"required": True,
"required": False,
"help": "OpenAI API密钥用于访问GPT模型。获取方式https://platform.openai.com/api-keys"
},
{
@@ -176,6 +175,7 @@ def save_env_vars(env_vars):
for key, value in env_vars.items():
if value: # 只保存非空值
# 确保值是字符串形式,并用引号包裹
value = str(value) # 确保值是字符串
if not (value.startswith('"') and value.endswith('"')) and not (value.startswith("'") and value.endswith("'")):
value = f'"{value}"'
existing_content[key] = value
@@ -243,7 +243,9 @@ def run_script(script_dropdown, question, progress=gr.Progress()):
"""运行选定的脚本并返回输出"""
global current_process
script_name = SCRIPTS[script_dropdown]
script_name = SCRIPTS.get(script_dropdown)
if not script_name:
return "❌ 无效的脚本选择", "", "", "", None
if not question.strip():
return "请输入问题!", "", "", "", None
@@ -280,14 +282,17 @@ def run_script(script_dropdown, question, progress=gr.Progress()):
# 创建线程来读取输出
def read_output():
with open(log_file, "w", encoding="utf-8") as f:
for line in iter(current_process.stdout.readline, ""):
if line:
# 写入日志文件
f.write(line)
f.flush()
# 添加到队列
log_queue.put(line)
try:
with open(log_file, "w", encoding="utf-8") as f:
for line in iter(current_process.stdout.readline, ""):
if line:
# 写入日志文件
f.write(line)
f.flush()
# 添加到队列
log_queue.put(line)
except Exception as e:
log_queue.put(f"读取输出时出错: {str(e)}\n")
# 启动读取线程
threading.Thread(target=read_output, daemon=True).start()
@@ -305,7 +310,10 @@ def run_script(script_dropdown, question, progress=gr.Progress()):
if time.time() - start_time > timeout:
with process_lock:
if current_process.poll() is None:
current_process.terminate()
if os.name == 'nt':
current_process.send_signal(signal.CTRL_BREAK_EVENT)
else:
current_process.terminate()
log_queue.put("执行超时,已终止进程\n")
break
@@ -355,32 +363,41 @@ def extract_answer(logs):
def extract_chat_history(logs):
"""尝试从日志中提取聊天历史"""
try:
for i, log in enumerate(logs):
chat_json_str = ""
capture_json = False
for log in logs:
if "chat_history" in log:
# 尝试找到JSON格式的聊天历史
# 开始捕获JSON
start_idx = log.find("[")
if start_idx != -1:
# 尝试解析JSON
json_str = log[start_idx:].strip()
# 查找下一行中可能的结束括号
if json_str[-1] != "]" and i+1 < len(logs):
for j in range(i+1, min(i+10, len(logs))):
end_idx = logs[j].find("]")
if end_idx != -1:
json_str += logs[j][:end_idx+1]
break
try:
chat_data = json.loads(json_str)
# 格式化为Gradio聊天组件可用的格式
formatted_chat = []
for msg in chat_data:
if "role" in msg and "content" in msg:
role = "用户" if msg["role"] == "user" else "助手"
formatted_chat.append([role, msg["content"]])
return formatted_chat
except json.JSONDecodeError:
pass
capture_json = True
chat_json_str = log[start_idx:]
elif capture_json:
# 继续捕获JSON直到找到匹配的结束括号
chat_json_str += log
if "]" in log:
# 找到结束括号尝试解析JSON
end_idx = chat_json_str.rfind("]") + 1
if end_idx > 0:
try:
# 清理可能的额外文本
json_str = chat_json_str[:end_idx].strip()
chat_data = json.loads(json_str)
# 格式化为Gradio聊天组件可用的格式
formatted_chat = []
for msg in chat_data:
if "role" in msg and "content" in msg:
role = "用户" if msg["role"] == "user" else "助手"
formatted_chat.append([role, msg["content"]])
return formatted_chat
except json.JSONDecodeError:
# 如果解析失败,继续捕获
pass
except Exception:
# 其他错误,停止捕获
capture_json = False
except Exception:
pass
return None
@@ -400,17 +417,19 @@ def create_ui():
)
with gr.Tabs() as tabs:
with gr.TabItem("运行模"):
with gr.TabItem("运行模"):
with gr.Row():
with gr.Column(scale=1):
# 确保默认值是SCRIPTS中存在的键
default_script = list(SCRIPTS.keys())[0] if SCRIPTS else None
script_dropdown = gr.Dropdown(
choices=list(SCRIPTS.keys()),
value=list(SCRIPTS.keys())[0],
label="选择模"
value=default_script,
label="选择模"
)
script_info = gr.Textbox(
value=get_script_info(list(SCRIPTS.keys())[0]),
value=get_script_info(default_script) if default_script else "",
label="模型描述",
interactive=False
)
@@ -452,9 +471,9 @@ def create_ui():
# 示例问题
examples = [
["Qwen Mini (中文)", "打开小红书上浏览推荐栏目下的前三个笔记内容,不要登陆,之后给我一个总结报告"],
["Mini", "What was the volume in m^3 of the fish bag that was calculated in the University of Leicester paper `Can Hiccup Supply Enough Fish to Maintain a Dragon's Diet?`"],
["默认", "What is the current weather in New York?"]
["Qwen Mini (中文)", "浏览亚马逊并找出一款对程序员有吸引力的产品。请提供产品名称和价格"],
["DeepSeek (中文)", "请分析GitHub上CAMEL-AI项目的最新统计数据。找出该项目的星标数量、贡献者数量和最近的活跃度。然后创建一个简单的Excel表格来展示这些数据并生成一个柱状图来可视化这些指标。最后总结CAMEL项目的受欢迎程度和发展趋势。"],
["Default", "Navigate to Amazon.com and identify one product that is attractive to coders. Please provide me with the product name and price. No need to verify your answer."]
]
gr.Examples(
@@ -505,14 +524,14 @@ def create_ui():
if var["type"] == "password":
env_inputs[var["name"]] = gr.Textbox(
value=env_vars.get(var["name"], ""),
label=var["label"] + (" (必填)" if var.get("required", False) else ""),
label=var["label"],
placeholder=f"请输入{var['label']}",
type="password"
)
else:
env_inputs[var["name"]] = gr.Textbox(
value=env_vars.get(var["name"], ""),
label=var["label"] + (" (必填)" if var.get("required", False) else ""),
label=var["label"],
placeholder=f"请输入{var['label']}"
)

View File

@@ -7,11 +7,20 @@ import traceback
def load_module_from_path(module_name, file_path):
"""从文件路径加载Python模块"""
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
try:
spec = importlib.util.spec_from_file_location(module_name, file_path)
if spec is None:
print(f"错误: 无法从 {file_path} 创建模块规范")
return None
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
except Exception as e:
print(f"加载模块时出错: {e}")
traceback.print_exc()
return None
def run_script_with_env_question(script_name):
"""使用环境变量中的问题运行脚本"""
@@ -27,46 +36,55 @@ def run_script_with_env_question(script_name):
print(f"错误: 脚本 {script_path} 不存在")
sys.exit(1)
# 读取脚本内容
with open(script_path, "r", encoding="utf-8") as f:
content = f.read()
# 创建临时文件路径
temp_script_path = script_path.with_name(f"temp_{script_path.name}")
# 检查脚本是否有main函数
has_main = re.search(r'def\s+main\s*\(\s*\)\s*:', content) is not None
# 转义问题中的特殊字符
escaped_question = question.replace("\\", "\\\\").replace("\"", "\\\"").replace("'", "\\'")
# 查找脚本中所有的question赋值
question_assignments = re.findall(r'question\s*=\s*(?:["\'].*?["\']|\(.*?\))', content)
print(f"在脚本中找到 {len(question_assignments)} 个question赋值")
# 修改脚本内容替换所有的question赋值
modified_content = content
# 如果脚本中有question赋值替换所有的赋值
if question_assignments:
for assignment in question_assignments:
modified_content = modified_content.replace(
assignment,
f'question = "{escaped_question}"'
)
print(f"已替换脚本中的所有question赋值为: {question}")
else:
# 如果没有找到question赋值尝试在main函数前插入
if has_main:
main_match = re.search(r'def\s+main\s*\(\s*\)\s*:', content)
if main_match:
insert_pos = main_match.start()
modified_content = content[:insert_pos] + f'\n# 用户输入的问题\nquestion = "{escaped_question}"\n\n' + content[insert_pos:]
print(f"已在main函数前插入问题: {question}")
try:
# 读取脚本内容
try:
with open(script_path, "r", encoding="utf-8") as f:
content = f.read()
except Exception as e:
print(f"读取脚本文件时出错: {e}")
sys.exit(1)
# 检查脚本是否有main函数
has_main = re.search(r'def\s+main\s*\(\s*\)\s*:', content) is not None
# 转义问题中的特殊字符
escaped_question = question.replace("\\", "\\\\").replace("\"", "\\\"").replace("'", "\\'")
# 查找脚本中所有的question赋值 - 改进的正则表达式
# 匹配单行和多行字符串赋值
question_assignments = re.findall(r'question\s*=\s*(?:["\'].*?["\']|""".*?"""|\'\'\'.*?\'\'\'|\(.*?\))', content, re.DOTALL)
print(f"在脚本中找到 {len(question_assignments)} 个question赋值")
# 修改脚本内容,替换所有question赋值
modified_content = content
# 如果脚本中有question赋值替换所有的赋值
if question_assignments:
for assignment in question_assignments:
modified_content = modified_content.replace(
assignment,
f'question = "{escaped_question}"'
)
print(f"已替换脚本中的所有question赋值为: {question}")
else:
# 如果没有main函数在文件开头插入
modified_content = f'# 用户输入的问题\nquestion = "{escaped_question}"\n\n' + content
print(f"已在文件开头插入问题: {question}")
# 添加monkey patch代码确保construct_society函数使用用户的问题
monkey_patch_code = f'''
# 如果没有找到question赋值尝试在main函数前插入
if has_main:
main_match = re.search(r'def\s+main\s*\(\s*\)\s*:', content)
if main_match:
insert_pos = main_match.start()
modified_content = content[:insert_pos] + f'\n# 用户输入的问题\nquestion = "{escaped_question}"\n\n' + content[insert_pos:]
print(f"已在main函数前插入问题: {question}")
else:
# 如果没有main函数在文件开头插入
modified_content = f'# 用户输入的问题\nquestion = "{escaped_question}"\n\n' + content
print(f"已在文件开头插入问题: {question}")
# 添加monkey patch代码确保construct_society函数使用用户的问题
monkey_patch_code = f'''
# 确保construct_society函数使用用户的问题
original_construct_society = globals().get('construct_society')
if original_construct_society:
@@ -78,23 +96,23 @@ if original_construct_society:
globals()['construct_society'] = patched_construct_society
print("已修补construct_society函数确保使用用户问题")
'''
# 在文件末尾添加monkey patch代码
modified_content += monkey_patch_code
# 如果脚本没有调用main函数添加调用代码
if has_main and "__main__" not in content:
modified_content += '''
# 在文件末尾添加monkey patch代码
modified_content += monkey_patch_code
# 如果脚本没有调用main函数添加调用代码
if has_main and "__main__" not in content:
modified_content += '''
# 确保调用main函数
if __name__ == "__main__":
main()
'''
print("已添加main函数调用代码")
# 如果脚本没有construct_society调用添加调用代码
if "construct_society" in content and "run_society" in content and "Answer:" not in content:
modified_content += f'''
print("已添加main函数调用代码")
# 如果脚本没有construct_society调用添加调用代码
if "construct_society" in content and "run_society" in content and "Answer:" not in content:
modified_content += f'''
# 确保执行construct_society和run_society
if "construct_society" in globals() and "run_society" in globals():
@@ -108,70 +126,98 @@ if "construct_society" in globals() and "run_society" in globals():
import traceback
traceback.print_exc()
'''
print("已添加construct_society和run_society调用代码")
# 执行修改后的脚本
try:
# 将脚本目录添加到sys.path
script_dir = script_path.parent
if str(script_dir) not in sys.path:
sys.path.insert(0, str(script_dir))
# 创建临时文件
temp_script_path = script_path.with_name(f"temp_{script_path.name}")
with open(temp_script_path, "w", encoding="utf-8") as f:
f.write(modified_content)
print(f"已创建临时脚本文件: {temp_script_path}")
print("已添加construct_society和run_society调用代码")
# 执行修改后的脚本
try:
# 直接执行临时脚本
print(f"开始执行脚本...")
# 将脚本目录添加到sys.path
script_dir = script_path.parent
if str(script_dir) not in sys.path:
sys.path.insert(0, str(script_dir))
# 如果有main函数加载模块并调用main
if has_main:
# 加载临时模块
module_name = f"temp_{script_path.stem}"
module = load_module_from_path(module_name, temp_script_path)
# 创建临时文件
try:
with open(temp_script_path, "w", encoding="utf-8") as f:
f.write(modified_content)
print(f"已创建临时脚本文件: {temp_script_path}")
except Exception as e:
print(f"创建临时脚本文件时出错: {e}")
sys.exit(1)
try:
# 直接执行临时脚本
print(f"开始执行脚本...")
# 确保模块中有question变量并且值是用户输入的问题
setattr(module, "question", question)
# 如果模块中有construct_society函数修补它
if hasattr(module, "construct_society"):
original_func = module.construct_society
def patched_func(*args, **kwargs):
return original_func(question)
module.construct_society = patched_func
print("已在模块级别修补construct_society函数")
# 调用main函数
if hasattr(module, "main"):
print("调用main函数...")
module.main()
# 如果有main函数加载模块并调用main
if has_main:
# 加载临时模块
module_name = f"temp_{script_path.stem}"
module = load_module_from_path(module_name, temp_script_path)
if module is None:
print(f"错误: 无法加载模块 {module_name}")
sys.exit(1)
# 确保模块中有question变量并且值是用户输入的问题
setattr(module, "question", question)
# 如果模块中有construct_society函数修补它
if hasattr(module, "construct_society"):
original_func = module.construct_society
def patched_func(*args, **kwargs):
return original_func(question)
module.construct_society = patched_func
print("已在模块级别修补construct_society函数")
# 调用main函数
if hasattr(module, "main"):
print("调用main函数...")
module.main()
else:
print(f"错误: 脚本 {script_path} 中没有main函数")
sys.exit(1)
else:
print(f"错误: 脚本 {script_path} 中没有main函数")
sys.exit(1)
else:
# 如果没有main函数直接执行修改后的脚本
print("直接执行脚本内容...")
exec(modified_content, {"__file__": str(temp_script_path)})
# 如果没有main函数直接执行修改后的脚本
print("直接执行脚本内容...")
# 使用更安全的方式执行脚本
with open(temp_script_path, "r", encoding="utf-8") as f:
script_code = f.read()
# 创建一个安全的全局命名空间
safe_globals = {
"__file__": str(temp_script_path),
"__name__": "__main__"
}
# 添加内置函数
safe_globals.update({k: v for k, v in globals().items()
if k in ['__builtins__']})
# 执行脚本
exec(script_code, safe_globals)
except Exception as e:
print(f"执行脚本时出错: {e}")
traceback.print_exc()
sys.exit(1)
except Exception as e:
print(f"执行脚本时出错: {e}")
print(f"处理脚本时出错: {e}")
traceback.print_exc()
sys.exit(1)
finally:
# 删除临时文件
if temp_script_path.exists():
temp_script_path.unlink()
print(f"已删除临时脚本文件: {temp_script_path}")
except Exception as e:
print(f"处理脚本时出错: {e}")
traceback.print_exc()
sys.exit(1)
finally:
# 删除临时文件
if temp_script_path.exists():
try:
temp_script_path.unlink()
print(f"已删除临时脚本文件: {temp_script_path}")
except Exception as e:
print(f"删除临时脚本文件时出错: {e}")
if __name__ == "__main__":
# 检查命令行参数
@@ -180,4 +226,4 @@ if __name__ == "__main__":
sys.exit(1)
# 运行指定的脚本
run_script_with_env_question(sys.argv[1])
run_script_with_env_question(sys.argv[1])

View File

@@ -27,7 +27,7 @@ def main():
# 创建并启动应用
app = create_ui()
app.launch(share=False)
app.queue().launch(share=False)
except ImportError as e:
print(f"错误: 无法导入必要的模块。请确保已安装所有依赖项: {e}")