zjrwtx commited on
Commit
f95eedb
·
1 Parent(s): 075a9b5

first web demo

Browse files
Files changed (4) hide show
  1. owl/app.py +302 -0
  2. owl/script_adapter.py +83 -0
  3. requirements.txt +5 -1
  4. run_app.py +43 -0
owl/app.py ADDED
@@ -0,0 +1,302 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import gradio as gr
4
+ import subprocess
5
+ import threading
6
+ import time
7
+ from datetime import datetime
8
+ import queue
9
+ import re
10
+ from pathlib import Path
11
+ import json
12
+
13
+ # 设置日志队列
14
+ log_queue = queue.Queue()
15
+
16
+ # 脚本选项
17
+ SCRIPTS = {
18
+ "Qwen Mini (中文)": "run_qwen_mini_zh.py",
19
+ "Qwen": "run_qwen.py",
20
+ "Mini": "run_mini.py",
21
+ "DeepSeek": "run_deepseek.py",
22
+ "默认": "run.py",
23
+ "GAIA Roleplaying": "run_gaia_roleplaying.py"
24
+ }
25
+
26
+ # 脚本描述
27
+ SCRIPT_DESCRIPTIONS = {
28
+ "Qwen Mini (中文)": "使用阿里云Qwen模型的中文版本,适合中文问答和任务",
29
+ "Qwen": "使用阿里云Qwen模型,支持多种工具和功能",
30
+ "Mini": "轻量级版本,使用OpenAI GPT-4o模型",
31
+ "DeepSeek": "使用DeepSeek模型,适合复杂推理任务",
32
+ "默认": "默认OWL实现,使用OpenAI GPT-4o模型和全套工具",
33
+ "GAIA Roleplaying": "GAIA基准测试实现,用于评估模型能力"
34
+ }
35
+
36
+ def get_script_info(script_name):
37
+ """获取脚本的详细信息"""
38
+ return SCRIPT_DESCRIPTIONS.get(script_name, "无描述信息")
39
+
40
+ def run_script(script_dropdown, question, progress=gr.Progress()):
41
+ """运行选定的脚本并返回输出"""
42
+ script_name = SCRIPTS[script_dropdown]
43
+
44
+ if not question.strip():
45
+ return "请输入问题!", "", "", "", None
46
+
47
+ # 清空日志队列
48
+ while not log_queue.empty():
49
+ log_queue.get()
50
+
51
+ # 创建日志目录
52
+ log_dir = Path("logs")
53
+ log_dir.mkdir(exist_ok=True)
54
+
55
+ # 创建带时间戳的日志文件
56
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
57
+ log_file = log_dir / f"{script_name.replace('.py', '')}_{timestamp}.log"
58
+
59
+ # 构建命令
60
+ cmd = [sys.executable, os.path.join("owl", "script_adapter.py"), os.path.join("owl", script_name)]
61
+
62
+ # 创建环境变量副本并添加问题
63
+ env = os.environ.copy()
64
+ env["OWL_QUESTION"] = question
65
+
66
+ # 启动进程
67
+ process = subprocess.Popen(
68
+ cmd,
69
+ stdout=subprocess.PIPE,
70
+ stderr=subprocess.STDOUT,
71
+ text=True,
72
+ bufsize=1,
73
+ env=env
74
+ )
75
+
76
+ # 创建线程来读取输出
77
+ def read_output():
78
+ with open(log_file, "w", encoding="utf-8") as f:
79
+ for line in iter(process.stdout.readline, ""):
80
+ if line:
81
+ # 写入日志文件
82
+ f.write(line)
83
+ f.flush()
84
+ # 添加到队列
85
+ log_queue.put(line)
86
+
87
+ # 启动读取线程
88
+ threading.Thread(target=read_output, daemon=True).start()
89
+
90
+ # 收集日志
91
+ logs = []
92
+ progress(0, desc="正在运行...")
93
+
94
+ # 等待进程完成或超时
95
+ start_time = time.time()
96
+ timeout = 1800 # 30分钟超时
97
+
98
+ while process.poll() is None:
99
+ # 检查是否超时
100
+ if time.time() - start_time > timeout:
101
+ process.terminate()
102
+ log_queue.put("执行超时,已终止进程\n")
103
+ break
104
+
105
+ # 从队列获取日志
106
+ while not log_queue.empty():
107
+ log = log_queue.get()
108
+ logs.append(log)
109
+
110
+ # 更新进度
111
+ elapsed = time.time() - start_time
112
+ progress(min(elapsed / 300, 0.99), desc="正在运行...")
113
+
114
+ # 短暂休眠以减少CPU使用
115
+ time.sleep(0.1)
116
+
117
+ # 每秒更新一次日志显示
118
+ yield status_message(process), extract_answer(logs), "".join(logs), str(log_file), None
119
+
120
+ # 获取剩余日志
121
+ while not log_queue.empty():
122
+ logs.append(log_queue.get())
123
+
124
+ # 提取聊天历史(如果有)
125
+ chat_history = extract_chat_history(logs)
126
+
127
+ # 返回最终状态和日志
128
+ return status_message(process), extract_answer(logs), "".join(logs), str(log_file), chat_history
129
+
130
+ def status_message(process):
131
+ """根据进程状态返回状态消息"""
132
+ if process.poll() is None:
133
+ return "⏳ 正在运行..."
134
+ elif process.returncode == 0:
135
+ return "✅ 执行成功"
136
+ else:
137
+ return f"❌ 执行失败 (返回码: {process.returncode})"
138
+
139
+ def extract_answer(logs):
140
+ """从日志中提取答案"""
141
+ answer = ""
142
+ for log in logs:
143
+ if "Answer:" in log:
144
+ answer = log.split("Answer:", 1)[1].strip()
145
+ break
146
+ return answer
147
+
148
+ def extract_chat_history(logs):
149
+ """尝试从日志中提取聊天历史"""
150
+ try:
151
+ for i, log in enumerate(logs):
152
+ if "chat_history" in log:
153
+ # 尝试找到JSON格式的聊天历史
154
+ start_idx = log.find("[")
155
+ if start_idx != -1:
156
+ # 尝试解析JSON
157
+ json_str = log[start_idx:].strip()
158
+ # 查找下一行中可能的结束括号
159
+ if json_str[-1] != "]" and i+1 < len(logs):
160
+ for j in range(i+1, min(i+10, len(logs))):
161
+ end_idx = logs[j].find("]")
162
+ if end_idx != -1:
163
+ json_str += logs[j][:end_idx+1]
164
+ break
165
+
166
+ try:
167
+ chat_data = json.loads(json_str)
168
+ # 格式化为Gradio聊天组件可用的格式
169
+ formatted_chat = []
170
+ for msg in chat_data:
171
+ if "role" in msg and "content" in msg:
172
+ role = "用户" if msg["role"] == "user" else "助手"
173
+ formatted_chat.append([role, msg["content"]])
174
+ return formatted_chat
175
+ except json.JSONDecodeError:
176
+ pass
177
+ except Exception:
178
+ pass
179
+ return None
180
+
181
+ def modify_script(script_name, question):
182
+ """修改脚本以使用提供的问题"""
183
+ script_path = os.path.join("owl", script_name)
184
+
185
+ with open(script_path, "r", encoding="utf-8") as f:
186
+ content = f.read()
187
+
188
+ # 查找并替换问题变量
189
+ if "question = " in content:
190
+ # 使用正则表达式替换问题字符串
191
+ modified_content = re.sub(
192
+ r'question\s*=\s*["\'].*?["\']',
193
+ f'question = "{question}"',
194
+ content
195
+ )
196
+
197
+ with open(script_path, "w", encoding="utf-8") as f:
198
+ f.write(modified_content)
199
+
200
+ return True
201
+
202
+ return False
203
+
204
+ def create_ui():
205
+ """创建Gradio界面"""
206
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as app:
207
+ gr.Markdown(
208
+ """
209
+ # 🦉 OWL 智能助手运行平台
210
+
211
+ 选择一个模型并输入您的问题,系统将运行相应的脚本并显示结果。
212
+ """
213
+ )
214
+
215
+ with gr.Row():
216
+ with gr.Column(scale=1):
217
+ script_dropdown = gr.Dropdown(
218
+ choices=list(SCRIPTS.keys()),
219
+ value=list(SCRIPTS.keys())[0],
220
+ label="选择模型"
221
+ )
222
+
223
+ script_info = gr.Textbox(
224
+ value=get_script_info(list(SCRIPTS.keys())[0]),
225
+ label="模型描述",
226
+ interactive=False
227
+ )
228
+
229
+ script_dropdown.change(
230
+ fn=lambda x: get_script_info(x),
231
+ inputs=script_dropdown,
232
+ outputs=script_info
233
+ )
234
+
235
+ question_input = gr.Textbox(
236
+ lines=5,
237
+ placeholder="请输入您的问题...",
238
+ label="问题"
239
+ )
240
+
241
+ run_button = gr.Button("运行", variant="primary")
242
+
243
+ with gr.Column(scale=2):
244
+ with gr.Tabs():
245
+ with gr.TabItem("结果"):
246
+ status_output = gr.Textbox(label="状态")
247
+ answer_output = gr.Textbox(label="回答", lines=10)
248
+ log_file_output = gr.Textbox(label="日志文件路径")
249
+
250
+ with gr.TabItem("运行日志"):
251
+ log_output = gr.Textbox(label="完整日志", lines=25)
252
+
253
+ with gr.TabItem("聊天历史"):
254
+ chat_output = gr.Chatbot(label="对话历史")
255
+
256
+ run_button.click(
257
+ fn=run_script,
258
+ inputs=[
259
+ script_dropdown,
260
+ question_input
261
+ ],
262
+ outputs=[status_output, answer_output, log_output, log_file_output, chat_output],
263
+ show_progress=True
264
+ )
265
+
266
+ # 示例问题
267
+ examples = [
268
+ ["Qwen Mini (中文)", "打开小红书上浏览推荐栏目下的前三个笔记内容,不要登陆,之后给我一个总结报告"],
269
+ ["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?`"],
270
+ ["默认", "What is the current weather in New York?"]
271
+ ]
272
+
273
+ gr.Examples(
274
+ examples=examples,
275
+ inputs=[script_dropdown, question_input]
276
+ )
277
+
278
+ # 添加页脚
279
+ gr.Markdown(
280
+ """
281
+ ### 📝 使用说明
282
+
283
+ - 选择一个模型并输入您的问题
284
+ - 点击"运行"按钮开始执行
285
+ - 在"结果"标签页查看执行状态和回答
286
+ - 在"运行日志"标签页查看完整日志
287
+ - 在"聊天历史"标签页查看对话历史(如果有)
288
+
289
+ ### ⚠️ 注意事项
290
+
291
+ - 运行某些模型可能需要API密钥,请确保在`.env`文件中设置了相应的环境变量
292
+ - 某些脚本可能需要较长时间运行,请耐心等待
293
+ - 如果运行超过30分钟,进程将自动终止
294
+ """
295
+ )
296
+
297
+ return app
298
+
299
+ if __name__ == "__main__":
300
+ # 创建并启动应用
301
+ app = create_ui()
302
+ app.queue().launch(share=True)
owl/script_adapter.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import importlib.util
4
+ import re
5
+ from pathlib import Path
6
+
7
+ def load_module_from_path(module_name, file_path):
8
+ """从文件路径加载Python模块"""
9
+ spec = importlib.util.spec_from_file_location(module_name, file_path)
10
+ module = importlib.util.module_from_spec(spec)
11
+ sys.modules[module_name] = module
12
+ spec.loader.exec_module(module)
13
+ return module
14
+
15
+ def run_script_with_env_question(script_name):
16
+ """使用环境变量中的问题运行脚本"""
17
+ # 获取环境变量中的问题
18
+ question = os.environ.get("OWL_QUESTION")
19
+ if not question:
20
+ print("错误: 未设置OWL_QUESTION环境变量")
21
+ sys.exit(1)
22
+
23
+ # 脚本路径
24
+ script_path = Path(script_name).resolve()
25
+ if not script_path.exists():
26
+ print(f"错误: 脚本 {script_path} 不存在")
27
+ sys.exit(1)
28
+
29
+ # 加载脚本模块
30
+ module_name = script_path.stem
31
+ try:
32
+ # 将脚本目录添加到sys.path
33
+ script_dir = script_path.parent
34
+ if str(script_dir) not in sys.path:
35
+ sys.path.insert(0, str(script_dir))
36
+
37
+ # 读取脚本内容
38
+ with open(script_path, "r", encoding="utf-8") as f:
39
+ content = f.read()
40
+
41
+ # 检查脚本是否有main函数
42
+ has_main = re.search(r'def\s+main\s*\(\s*\)\s*:', content) is not None
43
+
44
+ if has_main:
45
+ # 如果有main函数,加载模块并调用main
46
+ module = load_module_from_path(module_name, script_path)
47
+
48
+ # 修改模块中的question变量
49
+ if hasattr(module, "question"):
50
+ setattr(module, "question", question)
51
+
52
+ # 调用main函数
53
+ if hasattr(module, "main"):
54
+ module.main()
55
+ else:
56
+ print(f"错误: 脚本 {script_path} 中没有main函数")
57
+ sys.exit(1)
58
+ else:
59
+ # 如果没有main函数,直接执行脚本内容
60
+ # 替换question变量
61
+ modified_content = re.sub(
62
+ r'question\s*=\s*["\'].*?["\']',
63
+ f'question = "{question}"',
64
+ content
65
+ )
66
+
67
+ # 执行修改后的脚本
68
+ exec(modified_content, {"__file__": str(script_path)})
69
+
70
+ except Exception as e:
71
+ print(f"执行脚本时出错: {e}")
72
+ import traceback
73
+ traceback.print_exc()
74
+ sys.exit(1)
75
+
76
+ if __name__ == "__main__":
77
+ # 检查命令行参数
78
+ if len(sys.argv) < 2:
79
+ print("用法: python script_adapter.py <script_path>")
80
+ sys.exit(1)
81
+
82
+ # 运行指定的脚本
83
+ run_script_with_env_question(sys.argv[1])
requirements.txt CHANGED
@@ -129,7 +129,7 @@ ruff>=0.7.0
129
  mypy>=1.5.1
130
  toml>=0.10.2
131
  pre-commit>=3.0.0
132
- gradio>=3.0.0
133
 
134
  # Type stubs
135
  types-Pillow
@@ -141,3 +141,7 @@ types-tqdm
141
  types-colorama>=0.0.0
142
  types-requests>=2.0.0
143
  types-PyYAML>=6.0.0
 
 
 
 
 
129
  mypy>=1.5.1
130
  toml>=0.10.2
131
  pre-commit>=3.0.0
132
+ gradio>=4.0.0
133
 
134
  # Type stubs
135
  types-Pillow
 
141
  types-colorama>=0.0.0
142
  types-requests>=2.0.0
143
  types-PyYAML>=6.0.0
144
+
145
+ python-dotenv
146
+ tqdm
147
+ pathlib
run_app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ OWL 智能助手运行平台启动脚本
6
+ """
7
+
8
+ import os
9
+ import sys
10
+ from pathlib import Path
11
+
12
+ def main():
13
+ """主函数,启动OWL智能助手运行平台"""
14
+ # 确保当前目录是项目根目录
15
+ project_root = Path(__file__).resolve().parent
16
+ os.chdir(project_root)
17
+
18
+ # 创建日志目录
19
+ log_dir = project_root / "logs"
20
+ log_dir.mkdir(exist_ok=True)
21
+
22
+ # 导入并运行应用
23
+ sys.path.insert(0, str(project_root))
24
+
25
+ try:
26
+ from owl.app import create_ui
27
+
28
+ # 创建并启动应用
29
+ app = create_ui()
30
+ app.launch(share=False)
31
+
32
+ except ImportError as e:
33
+ print(f"错误: 无法导入必要的模块。请确保已安装所有依赖项: {e}")
34
+ print("提示: 运行 'pip install -r requirements.txt' 安装所有依赖项")
35
+ sys.exit(1)
36
+ except Exception as e:
37
+ print(f"启动应用程序时出错: {e}")
38
+ import traceback
39
+ traceback.print_exc()
40
+ sys.exit(1)
41
+
42
+ if __name__ == "__main__":
43
+ main()