153 lines
4.1 KiB
Python
153 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
运行系统测试 - 验证多模态检索系统功能
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
import logging
|
||
import traceback
|
||
from pathlib import Path
|
||
|
||
# 设置日志
|
||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||
logger = logging.getLogger(__name__)
|
||
|
||
def test_imports():
|
||
"""测试关键模块导入"""
|
||
logger.info("🔍 测试模块导入...")
|
||
|
||
try:
|
||
import torch
|
||
logger.info(f"✅ PyTorch {torch.__version__}")
|
||
|
||
import transformers
|
||
logger.info(f"✅ Transformers {transformers.__version__}")
|
||
|
||
import numpy as np
|
||
logger.info(f"✅ NumPy {np.__version__}")
|
||
|
||
from PIL import Image
|
||
logger.info("✅ Pillow")
|
||
|
||
import flask
|
||
logger.info(f"✅ Flask {flask.__version__}")
|
||
|
||
try:
|
||
import pymochow
|
||
logger.info("✅ PyMochow (百度VDB SDK)")
|
||
return True
|
||
except ImportError:
|
||
logger.warning("⚠️ PyMochow 未安装")
|
||
return False
|
||
|
||
except Exception as e:
|
||
logger.error(f"❌ 模块导入失败: {str(e)}")
|
||
return False
|
||
|
||
def test_baidu_vdb_connection():
|
||
"""测试百度VDB连接"""
|
||
logger.info("🔗 测试百度VDB连接...")
|
||
|
||
try:
|
||
import pymochow
|
||
from pymochow.configuration import Configuration
|
||
from pymochow.auth.bce_credentials import BceCredentials
|
||
|
||
# 连接配置
|
||
account = "root"
|
||
api_key = "vdb$yjr9ln3n0td"
|
||
endpoint = "http://180.76.96.191:5287"
|
||
|
||
config = Configuration(
|
||
credentials=BceCredentials(account, api_key),
|
||
endpoint=endpoint
|
||
)
|
||
|
||
client = pymochow.MochowClient(config)
|
||
|
||
# 测试连接
|
||
databases = client.list_databases()
|
||
logger.info(f"✅ VDB连接成功,发现 {len(databases)} 个数据库")
|
||
|
||
client.close()
|
||
return True
|
||
|
||
except Exception as e:
|
||
logger.error(f"❌ VDB连接失败: {str(e)}")
|
||
return False
|
||
|
||
def test_system_modules():
|
||
"""测试系统模块"""
|
||
logger.info("🔧 测试系统模块...")
|
||
|
||
try:
|
||
from multimodal_retrieval_vdb_only import MultimodalRetrievalVDBOnly
|
||
logger.info("✅ 多模态检索系统")
|
||
|
||
from baidu_vdb_production import BaiduVDBProduction
|
||
logger.info("✅ 百度VDB后端")
|
||
|
||
return True
|
||
|
||
except Exception as e:
|
||
logger.error(f"❌ 系统模块测试失败: {str(e)}")
|
||
return False
|
||
|
||
def create_directories():
|
||
"""创建必要目录"""
|
||
logger.info("📁 创建必要目录...")
|
||
|
||
directories = ["uploads", "sample_images", "text_data"]
|
||
|
||
for dir_name in directories:
|
||
dir_path = Path(dir_name)
|
||
dir_path.mkdir(exist_ok=True)
|
||
logger.info(f"✅ 目录: {dir_name}")
|
||
|
||
def main():
|
||
"""主测试函数"""
|
||
logger.info("🚀 开始系统测试...")
|
||
logger.info("=" * 50)
|
||
|
||
# 创建目录
|
||
create_directories()
|
||
|
||
# 运行测试
|
||
results = {}
|
||
results["imports"] = test_imports()
|
||
|
||
if results["imports"]:
|
||
results["vdb"] = test_baidu_vdb_connection()
|
||
results["modules"] = test_system_modules()
|
||
else:
|
||
logger.error("❌ 基础模块导入失败,跳过其他测试")
|
||
return False
|
||
|
||
# 输出结果
|
||
logger.info("\n" + "=" * 50)
|
||
logger.info("📊 测试结果:")
|
||
logger.info("=" * 50)
|
||
|
||
for test_name, result in results.items():
|
||
status = "✅ 通过" if result else "❌ 失败"
|
||
logger.info(f"{test_name}: {status}")
|
||
|
||
success_count = sum(results.values())
|
||
total_count = len(results)
|
||
success_rate = (success_count / total_count) * 100
|
||
|
||
logger.info(f"\n成功率: {success_count}/{total_count} ({success_rate:.1f}%)")
|
||
|
||
if success_rate >= 75:
|
||
logger.info("🎉 系统测试通过!")
|
||
return True
|
||
else:
|
||
logger.warning("⚠️ 系统存在问题")
|
||
return False
|
||
|
||
if __name__ == "__main__":
|
||
success = main()
|
||
sys.exit(0 if success else 1)
|