good version for web

This commit is contained in:
2026-02-18 09:36:18 +08:00
parent 62ea5d36a5
commit 72ab0c0b56
42 changed files with 1305 additions and 1515 deletions

View File

@@ -4,6 +4,10 @@ from sqlalchemy.orm import Session
import uuid
import requests
import time
import logging
import os
logger = logging.getLogger(__name__)
from app.models.models import Algorithm, AlgorithmVersion, AlgorithmCall
from app.schemas.algorithm import AlgorithmCreate, AlgorithmUpdate, AlgorithmVersionCreate, AlgorithmVersionUpdate, AlgorithmCallCreate
@@ -97,14 +101,39 @@ class AlgorithmService:
@staticmethod
def get_algorithms(db: Session, skip: int = 0, limit: int = 100, algorithm_type: Optional[str] = None) -> List[Algorithm]:
"""获取算法列表"""
"""获取算法列表,优先显示已注册的服务"""
from app.models.models import AlgorithmService as Service
# 获取所有已注册的服务
services = db.query(Service).all()
# 获取所有算法
query = db.query(Algorithm)
# 如果指定了算法类型,进行过滤
if algorithm_type:
query = query.filter(Algorithm.type == algorithm_type)
return query.offset(skip).limit(limit).all()
algorithms = query.all()
# 创建服务名称集合,用于快速查找
service_names = {service.name for service in services}
# 将算法分为两类:已注册的服务和普通算法
registered_algorithms = []
normal_algorithms = []
for algo in algorithms:
if algo.name in service_names:
registered_algorithms.append(algo)
else:
normal_algorithms.append(algo)
# 合并结果:已注册的服务在前,普通算法在后
merged_algorithms = registered_algorithms + normal_algorithms
# 应用分页
return merged_algorithms[skip:skip+limit]
@staticmethod
def update_algorithm(db: Session, algorithm_id: str, algorithm_update: AlgorithmUpdate) -> Optional[Algorithm]:
@@ -358,14 +387,37 @@ class AlgorithmCallService:
# 记录开始时间
start_time = time.time()
# 处理视频路径 - 如果是MinIO路径下载到本地
from app.utils.file import file_storage
video_path = processed_input_data.get('video', '')
if video_path and video_path.startswith('media/'):
# 从MinIO下载视频到本地
video_content = file_storage.get_object(video_path)
if video_content:
# 保存到临时文件
import tempfile
import uuid
suffix = '.' + video_path.split('.')[-1] if '.' in video_path else '.mp4'
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp_file:
tmp_file.write(video_content)
local_video_path = tmp_file.name
# 使用本地路径
processed_input_data['video'] = local_video_path
else:
db_call.status = "failed"
db_call.error_message = "无法下载视频文件"
db.commit()
return db_call
# 调用算法API
print(f"[DEBUG] 调用算法API: {version.url}, 输入: {processed_input_data}")
response = requests.post(
version.url,
json={
"input_data": processed_input_data,
"params": call.params
},
timeout=30
timeout=120
)
# 计算响应时间
@@ -374,7 +426,35 @@ class AlgorithmCallService:
# 处理响应
if response.status_code == 200:
output_data = response.json()
print(f"[DEBUG] 算法响应: {output_data}")
# 处理算法返回的视频文件
result = output_data.get('result', {})
if result and isinstance(result, dict):
# 如果返回了本地视频路径,需要处理
if 'video' in result and result['video'] and result['video'].startswith('/'):
# 这是本地路径需要转换为可访问的URL
local_video = result['video']
if os.path.exists(local_video):
# 上传到MinIO并替换路径
with open(local_video, 'rb') as f:
video_content = f.read()
video_filename = f"results/{user_id}/{uuid.uuid4().hex[:12]}.mp4"
from app.utils.file import file_storage
success = file_storage.upload_from_bytes(video_content, video_filename)
if success:
result['video'] = video_filename
result['video_url'] = f"/api/v1/data/media/{video_filename}"
# 删除临时文件
try:
os.remove(local_video)
except:
pass
db_call.status = "success"
print(f"[DEBUG] 保存output_data: {output_data}")
db_call.output_data = output_data
db_call.response_time = response_time
else: