Wendong-Fan commited on
Commit
a1520e7
·
1 Parent(s): ad9f534

update wendong

Browse files
Files changed (1) hide show
  1. owl/webapp_zh.py +134 -43
owl/webapp_zh.py CHANGED
@@ -2,21 +2,20 @@
2
  from owl.utils import run_society
3
  import os
4
  import gradio as gr
5
- import time
6
- import json
7
  from typing import Tuple, List, Dict, Any
8
  import importlib
9
 
10
  os.environ['PYTHONIOENCODING'] = 'utf-8'
 
11
  # Enhanced CSS with navigation bar and additional styling
12
  custom_css = """
13
  :root {
14
- --primary-color: #1e3c72;
15
- --secondary-color: #2a5298;
16
- --accent-color: #4776E6;
17
  --light-bg: #f8f9fa;
18
- --border-color: #dee2e6;
19
- --text-muted: #6c757d;
20
  }
21
 
22
  .container {
@@ -33,6 +32,7 @@ custom_css = """
33
  color: white;
34
  border-radius: 10px 10px 0 0;
35
  margin-bottom: 0;
 
36
  }
37
 
38
  .navbar-logo {
@@ -48,17 +48,7 @@ custom_css = """
48
  gap: 20px;
49
  }
50
 
51
- .navbar-menu a {
52
- color: white;
53
- text-decoration: none;
54
- padding: 5px 10px;
55
- border-radius: 5px;
56
- transition: background-color 0.3s;
57
- }
58
-
59
- .navbar-menu a:hover {
60
- background-color: rgba(255, 255, 255, 0.1);
61
- }
62
 
63
  .header {
64
  text-align: center;
@@ -67,7 +57,7 @@ custom_css = """
67
  color: white;
68
  padding: 40px 20px;
69
  border-radius: 0 0 10px 10px;
70
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
71
  }
72
 
73
  .module-info {
@@ -137,22 +127,25 @@ custom_css = """
137
  background-color: white;
138
  border-radius: 8px;
139
  padding: 20px;
140
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
141
  transition: transform 0.3s, box-shadow 0.3s;
142
  height: 100%;
143
  display: flex;
144
  flex-direction: column;
 
145
  }
146
 
147
  .feature-card:hover {
148
  transform: translateY(-5px);
149
- box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
 
150
  }
151
 
152
  .feature-icon {
153
  font-size: 2em;
154
  color: var(--primary-color);
155
  margin-bottom: 10px;
 
156
  }
157
 
158
  .feature-card h3 {
@@ -166,6 +159,22 @@ custom_css = """
166
  line-height: 1.5;
167
  }
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  /* Improved button and input styles */
170
  button.primary {
171
  background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
@@ -175,21 +184,20 @@ button.primary {
175
  button.primary:hover {
176
  background: linear-gradient(90deg, var(--secondary-color), var(--primary-color));
177
  transform: translateY(-2px);
178
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
179
  }
180
  """
181
 
182
  # Dictionary containing module descriptions
183
  MODULE_DESCRIPTIONS = {
184
- "run": "默认模式:使用默认的智能体协作模式,适合大多数任务。",
185
- "run_mini":"使用最小化配置处理任务",
186
  "run_deepseek_zh":"使用deepseek模型处理中文任务",
187
- "run_terminal_zh": "终端模式:可执行命令行操作,支持网络搜索、文件处理等功能。适合需要系统交互的任务。",
188
- "run_mini": "精简模式:轻量级智能体协作,适合快速回答和简单任务处理,响应速度更快。",
189
- "run_gaia_roleplaying":"GAIA基准测试实现,用于评估模型能力",
190
  "run_openai_compatiable_model":"使用openai兼容模型处理任务",
191
  "run_ollama":"使用本地ollama模型处理任务",
192
- "run_qwen_mini_zh":"使用qwen模型处理中文任务",
193
  "run_qwen_zh":"使用qwen模型处理任务",
194
 
195
 
@@ -219,6 +227,20 @@ def format_chat_history(chat_history: List[Dict[str, str]]) -> List[List[str]]:
219
 
220
  return formatted_history
221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  def run_owl(question: str, example_module: str) -> Tuple[str, List[List[str]], str, str]:
223
  """运行OWL系统并返回结果
224
 
@@ -229,29 +251,94 @@ def run_owl(question: str, example_module: str) -> Tuple[str, List[List[str]], s
229
  Returns:
230
  Tuple[...]: 回答、聊天历史、令牌计数、状态
231
  """
 
 
 
 
 
 
 
 
 
232
  try:
 
 
 
 
 
 
 
 
 
233
  # 动态导入目标模块
234
  module_path = f"owl.examples.{example_module}"
235
- module = importlib.import_module(module_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
 
237
  # 检查是否包含construct_society函数
238
  if not hasattr(module, "construct_society"):
239
- raise AttributeError(f"模块 {module_path} 中未找到 construct_society 函数")
 
 
 
 
 
240
 
241
  # 构建社会模拟
242
- society = module.construct_society(question)
 
 
 
 
 
 
 
 
243
 
244
- # 运行社会模拟(假设run_society兼容不同模块)
245
- answer, chat_history, token_info = run_society(society)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
 
247
- # 格式化和令牌计数(与原逻辑一致)
248
- formatted_chat_history = format_chat_history(chat_history)
249
- total_tokens = token_info["completion_token_count"] + token_info["prompt_token_count"]
 
 
 
 
250
 
251
  return (
252
  answer,
253
  formatted_chat_history,
254
- f"完成令牌: {token_info['completion_token_count']:,} | 提示令牌: {token_info['prompt_token_count']:,} | 总计: {total_tokens:,}",
255
  "✅ 成功完成"
256
  )
257
 
@@ -337,9 +424,9 @@ def create_ui():
337
  )
338
 
339
  # 增强版模块选择下拉菜单
 
340
  module_dropdown = gr.Dropdown(
341
- choices=["run", "run_mini","run_terminal_zh","run_gaia_roleplaying",
342
- "run_openai_compatiable_model","run_ollama","run_qwen_zh","run_qwen_mini_zh","run_deepseek_zh","run_terminal"],
343
  value="run_terminal_zh",
344
  label="选择功能模块",
345
  interactive=True
@@ -411,8 +498,7 @@ def create_ui():
411
  <h3>关于 OWL 多智能体协作系统</h3>
412
  <p>OWL 是一个基于CAMEL框架开发的先进多智能体协作系统,旨在通过智能体协作解决复杂问题。</p>
413
  <p>© 2025 CAMEL-AI.org. 基于Apache License 2.0开源协议</p>
414
- <p><a href="https://github.com/camel-ai/owl" target="_blank">GitHub</a>
415
-
416
  </div>
417
  """)
418
 
@@ -434,8 +520,13 @@ def create_ui():
434
 
435
  # 主函数
436
  def main():
437
- app = create_ui()
438
- app.launch(share=False)
 
 
 
 
 
439
 
440
  if __name__ == "__main__":
441
  main()
 
2
  from owl.utils import run_society
3
  import os
4
  import gradio as gr
 
 
5
  from typing import Tuple, List, Dict, Any
6
  import importlib
7
 
8
  os.environ['PYTHONIOENCODING'] = 'utf-8'
9
+
10
  # Enhanced CSS with navigation bar and additional styling
11
  custom_css = """
12
  :root {
13
+ --primary-color: #4a89dc;
14
+ --secondary-color: #5d9cec;
15
+ --accent-color: #7baaf7;
16
  --light-bg: #f8f9fa;
17
+ --border-color: #e4e9f0;
18
+ --text-muted: #8a9aae;
19
  }
20
 
21
  .container {
 
32
  color: white;
33
  border-radius: 10px 10px 0 0;
34
  margin-bottom: 0;
35
+ box-shadow: 0 2px 10px rgba(74, 137, 220, 0.15);
36
  }
37
 
38
  .navbar-logo {
 
48
  gap: 20px;
49
  }
50
 
51
+ /* Navbar styles moved to a more specific section below */
 
 
 
 
 
 
 
 
 
 
52
 
53
  .header {
54
  text-align: center;
 
57
  color: white;
58
  padding: 40px 20px;
59
  border-radius: 0 0 10px 10px;
60
+ box-shadow: 0 4px 6px rgba(93, 156, 236, 0.12);
61
  }
62
 
63
  .module-info {
 
127
  background-color: white;
128
  border-radius: 8px;
129
  padding: 20px;
130
+ box-shadow: 0 2px 8px rgba(74, 137, 220, 0.08);
131
  transition: transform 0.3s, box-shadow 0.3s;
132
  height: 100%;
133
  display: flex;
134
  flex-direction: column;
135
+ border: 1px solid rgba(228, 233, 240, 0.6);
136
  }
137
 
138
  .feature-card:hover {
139
  transform: translateY(-5px);
140
+ box-shadow: 0 5px 15px rgba(74, 137, 220, 0.15);
141
+ border-color: rgba(93, 156, 236, 0.3);
142
  }
143
 
144
  .feature-icon {
145
  font-size: 2em;
146
  color: var(--primary-color);
147
  margin-bottom: 10px;
148
+ text-shadow: 0 1px 2px rgba(74, 137, 220, 0.1);
149
  }
150
 
151
  .feature-card h3 {
 
159
  line-height: 1.5;
160
  }
161
 
162
+ /* Navbar link styles - ensuring consistent colors */
163
+ .navbar-menu a {
164
+ color: #ffffff !important;
165
+ text-decoration: none;
166
+ padding: 5px 10px;
167
+ border-radius: 5px;
168
+ transition: background-color 0.3s, color 0.3s;
169
+ font-weight: 500;
170
+ text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
171
+ }
172
+
173
+ .navbar-menu a:hover {
174
+ background-color: rgba(255, 255, 255, 0.15);
175
+ color: #ffffff !important;
176
+ }
177
+
178
  /* Improved button and input styles */
179
  button.primary {
180
  background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
 
184
  button.primary:hover {
185
  background: linear-gradient(90deg, var(--secondary-color), var(--primary-color));
186
  transform: translateY(-2px);
187
+ box-shadow: 0 4px 8px rgba(74, 137, 220, 0.2);
188
  }
189
  """
190
 
191
  # Dictionary containing module descriptions
192
  MODULE_DESCRIPTIONS = {
193
+ "run": "默认模式:使用OpenAI模型的默认的智能体协作模式,适合大多数任务。",
194
+ "run_mini":"使用使用OpenAI模型最小化配置处理任务",
195
  "run_deepseek_zh":"使用deepseek模型处理中文任务",
196
+ "run_terminal_zh": "终端模式:可执行命令行操作,支持网络搜索、文件处理等功能。适合需要系统交互的任务,使用OpenAI模型",
197
+ "run_gaia_roleplaying":"GAIA基准测试实现,用于评估Agent能力",
 
198
  "run_openai_compatiable_model":"使用openai兼容模型处理任务",
199
  "run_ollama":"使用本地ollama模型处理任务",
200
+ "run_qwen_mini_zh":"使用qwen模型最小化配置处理任务",
201
  "run_qwen_zh":"使用qwen模型处理任务",
202
 
203
 
 
227
 
228
  return formatted_history
229
 
230
+ def validate_input(question: str) -> bool:
231
+ """验证用户输入是否有效
232
+
233
+ Args:
234
+ question: 用户问题
235
+
236
+ Returns:
237
+ bool: 输入是否有效
238
+ """
239
+ # 检查输入是否为空或只包含空格
240
+ if not question or question.strip() == "":
241
+ return False
242
+ return True
243
+
244
  def run_owl(question: str, example_module: str) -> Tuple[str, List[List[str]], str, str]:
245
  """运行OWL系统并返回结果
246
 
 
251
  Returns:
252
  Tuple[...]: 回答、聊天历史、令牌计数、状态
253
  """
254
+ # 验证输入
255
+ if not validate_input(question):
256
+ return (
257
+ "请输入有效的问题",
258
+ [],
259
+ "0",
260
+ "❌ 错误: 输入无效"
261
+ )
262
+
263
  try:
264
+ # 检查模块是否在MODULE_DESCRIPTIONS中
265
+ if example_module not in MODULE_DESCRIPTIONS:
266
+ return (
267
+ f"所选模块 '{example_module}' 不受支持",
268
+ [],
269
+ "0",
270
+ f"❌ 错误: 不支持的模块"
271
+ )
272
+
273
  # 动态导入目标模块
274
  module_path = f"owl.examples.{example_module}"
275
+ try:
276
+ module = importlib.import_module(module_path)
277
+ except ImportError as ie:
278
+ return (
279
+ f"无法导入模块: {module_path}",
280
+ [],
281
+ "0",
282
+ f"❌ 错误: 模块 {example_module} 不存在或无法加载 - {str(ie)}"
283
+ )
284
+ except Exception as e:
285
+ return (
286
+ f"导入模块时发生错误: {module_path}",
287
+ [],
288
+ "0",
289
+ f"❌ 错误: {str(e)}"
290
+ )
291
 
292
  # 检查是否包含construct_society函数
293
  if not hasattr(module, "construct_society"):
294
+ return (
295
+ f"模块 {module_path} 中未找到 construct_society 函数",
296
+ [],
297
+ "0",
298
+ f"❌ 错误: 模块接口不兼容"
299
+ )
300
 
301
  # 构建社会模拟
302
+ try:
303
+ society = module.construct_society(question)
304
+ except Exception as e:
305
+ return (
306
+ f"构建社会模拟时发生错误: {str(e)}",
307
+ [],
308
+ "0",
309
+ f"❌ 错误: 构建失败 - {str(e)}"
310
+ )
311
 
312
+ # 运行社会模拟
313
+ try:
314
+ answer, chat_history, token_info = run_society(society)
315
+ except Exception as e:
316
+ return (
317
+ f"运行社会模拟时发生错误: {str(e)}",
318
+ [],
319
+ "0",
320
+ f"❌ 错误: 运行失败 - {str(e)}"
321
+ )
322
+
323
+ # 格式化聊天历史
324
+ try:
325
+ formatted_chat_history = format_chat_history(chat_history)
326
+ except Exception as e:
327
+ # 如果格式化失败,返回空历史记录但继续处理
328
+ formatted_chat_history = []
329
 
330
+ # 安全地获取令牌计数
331
+ if not isinstance(token_info, dict):
332
+ token_info = {}
333
+
334
+ completion_tokens = token_info.get("completion_token_count", 0)
335
+ prompt_tokens = token_info.get("prompt_token_count", 0)
336
+ total_tokens = completion_tokens + prompt_tokens
337
 
338
  return (
339
  answer,
340
  formatted_chat_history,
341
+ f"完成令牌: {completion_tokens:,} | 提示令牌: {prompt_tokens:,} | 总计: {total_tokens:,}",
342
  "✅ 成功完成"
343
  )
344
 
 
424
  )
425
 
426
  # 增强版模块选择下拉菜单
427
+ # 只包含MODULE_DESCRIPTIONS中定义的模块
428
  module_dropdown = gr.Dropdown(
429
+ choices=list(MODULE_DESCRIPTIONS.keys()),
 
430
  value="run_terminal_zh",
431
  label="选择功能模块",
432
  interactive=True
 
498
  <h3>关于 OWL 多智能体协作系统</h3>
499
  <p>OWL 是一个基于CAMEL框架开发的先进多智能体协作系统,旨在通过智能体协作解决复杂问题。</p>
500
  <p>© 2025 CAMEL-AI.org. 基于Apache License 2.0开源协议</p>
501
+ <p><a href="https://github.com/camel-ai/owl" target="_blank">GitHub</a></p>
 
502
  </div>
503
  """)
504
 
 
520
 
521
  # 主函数
522
  def main():
523
+ try:
524
+ app = create_ui()
525
+ app.launch(share=False)
526
+ except Exception as e:
527
+ print(f"启动应用程序时发生错误: {str(e)}")
528
+ import traceback
529
+ traceback.print_exc()
530
 
531
  if __name__ == "__main__":
532
  main()