add filesupload

This commit is contained in:
zjrwtx 2025-03-21 15:23:24 +08:00
parent 106a169ee9
commit fa1fdaa67a
2 changed files with 43 additions and 7 deletions

View File

@ -19,11 +19,14 @@ from camel.toolkits import (
AudioAnalysisToolkit,
CodeExecutionToolkit,
ExcelToolkit,
HumanToolkit,
ImageAnalysisToolkit,
SearchToolkit,
VideoAnalysisToolkit,
BrowserToolkit,
FileWriteToolkit,
HumanToolkit,
TerminalToolkit,
)
from camel.types import ModelPlatformType, ModelType
from camel.logger import set_log_level
@ -89,6 +92,8 @@ def construct_society(question: str) -> RolePlaying:
# Configure toolkits
tools = [
*HumanToolkit().get_tools(),
*TerminalToolkit().get_tools(),
*BrowserToolkit(
headless=False, # Set to True for headless mode (e.g., on remote servers)
web_agent_model=models["browsing"],
@ -131,7 +136,7 @@ def construct_society(question: str) -> RolePlaying:
def main():
r"""Main function to run the OWL system with an example question."""
# Default research question
default_task = "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."
default_task = "你的每一步都要经过我的意见才进行下一步执行用plot画一个爱心图并保存到本地."
# Override default task if command line argument is provided
task = sys.argv[1] if len(sys.argv) > 1 else default_task

View File

@ -25,6 +25,7 @@ from dotenv import load_dotenv, set_key, find_dotenv, unset_key
import threading
import queue
import re # For regular expression operations
import tempfile # 添加此行,用于处理临时文件
os.environ["PYTHONIOENCODING"] = "utf-8"
@ -309,12 +310,13 @@ def validate_input(question: str) -> bool:
return True
def run_owl(question: str, example_module: str) -> Tuple[str, str, str]:
def run_owl(question: str, example_module: str, uploaded_file=None) -> Tuple[str, str, str]:
"""运行OWL系统并返回结果
Args:
question: 用户问题
example_module: 要导入的示例模块名 "run_terminal_zh" "run_deep"
uploaded_file: 上传的文件路径可选
Returns:
Tuple[...]: 回答令牌计数状态
@ -330,6 +332,19 @@ def run_owl(question: str, example_module: str) -> Tuple[str, str, str]:
# 确保环境变量已加载
load_dotenv(find_dotenv(), override=True)
logging.info(f"处理问题: '{question}', 使用模块: {example_module}")
# 处理上传的文件
if uploaded_file:
# 记录上传的文件信息
file_name = os.path.basename(uploaded_file)
file_path = uploaded_file
logging.info(f"处理上传的文件: {file_name}, 路径: {file_path}")
# 将文件路径添加到问题中
if "文件路径:" not in question and "file path:" not in question.lower():
question = f"{question}\n文件路径: {file_path}"
logging.info(f"更新后的问题: '{question}'")
# 检查模块是否在MODULE_DESCRIPTIONS中
if example_module not in MODULE_DESCRIPTIONS:
@ -769,8 +784,8 @@ def create_ui():
logging.error(f"清空日志文件时出错: {str(e)}")
return ""
# 创建一个实时日志更新函数
def process_with_live_logs(question, module_name):
# 修改process_with_live_logs函数以支持文件上传
def process_with_live_logs(question, module_name, uploaded_file=None):
"""处理问题并实时更新日志"""
global CURRENT_PROCESS
@ -782,7 +797,7 @@ def create_ui():
def process_in_background():
try:
result = run_owl(question, module_name)
result = run_owl(question, module_name, uploaded_file)
result_queue.put(result)
except Exception as e:
result_queue.put((f"发生错误: {str(e)}", "0", f"❌ 错误: {str(e)}"))
@ -1062,6 +1077,22 @@ def create_ui():
value="打开百度搜索总结一下camel-ai的camel框架的github star、fork数目等并把数字用plot包写成python文件保存到本地并运行生成的python文件。",
)
# 添加文件上传组件
file_upload = gr.File(
label="上传文件(可选)",
file_types=["pdf", "docx", "txt", "csv", "xlsx", "json", "py", "ipynb"],
file_count="single",
type="filepath",
)
# 添加文件上传说明
gr.Markdown("""
<div style="background-color: #e7f3fe; border-left: 6px solid #2196F3; padding: 10px; margin-bottom: 15px; border-radius: 4px;">
<strong>文件上传说明</strong> 上传文件后系统会自动将文件路径添加到您的问题中
您可以在问题中引用这个文件例如"分析这个文件的内容""总结这个文档的要点"
</div>
""")
# 增强版模块选择下拉菜单
# 只包含MODULE_DESCRIPTIONS中定义的模块
module_dropdown = gr.Dropdown(
@ -1205,10 +1236,10 @@ def create_ui():
refresh_button.click(fn=update_env_table, outputs=[env_table])
# 设置事件处理
# 修改run_button点击事件以包含文件上传
run_button.click(
fn=process_with_live_logs,
inputs=[question_input, module_dropdown],
inputs=[question_input, module_dropdown, file_upload],
outputs=[token_count_output, status_output, log_display2],
)