Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions backend/algorithm/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ def _setup_environment(video_path: str):

return main_output_path, video_name, timestamp

def _run_asr_and_process(video_path: str, video_name: str, main_output_path: str):
def _run_asr_and_process(video_path: str, video_name: str, main_output_path: str, asr_engine=None):
"""Runs ASR on the video and processes the result."""
logging.info("--- 步骤 0 & 1: 语音识别与数据处理 ---")
asr_engine = VideoDevourASRParaformerV2()
if asr_engine is None:
asr_engine = VideoDevourASRParaformerV2()
asr_result = asr_engine.devour_video(video_path)
asr_result_path = os.path.join(main_output_path, f"{video_name}_asr_result.json")
with open(asr_result_path, 'w', encoding='utf-8') as f:
Expand Down Expand Up @@ -119,12 +120,12 @@ def _generate_and_match_outline(processed_dialogue: list, main_output_path: str)
logging.info("--- 文本块匹配完成 ---")
return matched_data, headings_with_level, headings, outline

def run_full_pipeline(video_path: str):
def run_full_pipeline(video_path: str, asr_engine=None):
"""Orchestrates the full video processing pipeline."""
try:
main_output_path, video_name, timestamp = _setup_environment(video_path)

processed_dialogue = _run_asr_and_process(video_path, video_name, main_output_path)
processed_dialogue = _run_asr_and_process(video_path, video_name, main_output_path, asr_engine)
if not processed_dialogue:
raise ValueError("ASR处理后对话为空,流程中止。")

Expand Down
2 changes: 1 addition & 1 deletion backend/algorithm/vlm_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import List, Tuple, Union
from PIL import Image
import io
import backend.algorithm.config as config
import config


class VLMHandler:
Expand Down
24 changes: 23 additions & 1 deletion backend/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import shutil
import asyncio
import json
import logging
from pathlib import Path
from typing import Dict, List, Optional
from datetime import datetime
Expand All @@ -25,13 +26,18 @@
sys.path.insert(0, str(PROJECT_ROOT))

from backend.algorithm.pipeline import run_full_pipeline
from backend.devour.asr_engine_paraformer_v2 import VideoDevourASRParaformerV2

# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# 创建FastAPI应用
app = FastAPI(
title="VideoDevour API",
description="视频处理和分析服务",
version="1.0.0"
)
asr_engine = None

# 配置CORS
app.add_middleware(
Expand Down Expand Up @@ -80,6 +86,21 @@ def save_tasks():
# 启动时加载任务数据
load_tasks()

@app.on_event("startup")
async def startup_event():
"""
预加载 ASR 模型
"""
global asr_engine
try:
logging.info("正在预加载 ASR 模型...")
asr_engine = VideoDevourASRParaformerV2()
_ = asr_engine.asr_model
logging.info("ASR 模型预加载完成")
except Exception as e:
logging.error(f"ASR 模型预加载失败: {str(e)}")
asr_engine = None

# 数据模型
class TaskStatus(BaseModel):
task_id: str
Expand Down Expand Up @@ -718,7 +739,8 @@ def update_progress(progress: int, message: str, stage: str = None):
# 使用线程池执行器运行同步函数
with concurrent.futures.ThreadPoolExecutor() as executor:
# 在执行过程中定期更新进度
future = executor.submit(run_full_pipeline, video_path)
# 预加载模型
future = executor.submit(run_full_pipeline, video_path, asr_engine)

# 模拟进度更新
progress_steps = [
Expand Down