#!/usr/bin/env python3 """ 🎥 Video Content Safety Analysis 适配ZeroGPU的视频内容安全分析应用 """ import os import tempfile import gradio as gr import torch import numpy as np from typing import Optional, Tuple import logging # 设置中国镜像(如果在中国网络环境) os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com' # ZeroGPU装饰器 try: import spaces GPU_AVAILABLE = True print("✅ ZeroGPU spaces 可用") except ImportError: print("⚠️ ZeroGPU spaces 不可用,使用CPU模式") GPU_AVAILABLE = False # 创建空装饰器 class spaces: @staticmethod def GPU(func): return func # 全局变量 model = None processor = None def load_model(): """加载模型(延迟加载)""" global model, processor if model is not None: return model, processor try: print("🔄 正在加载模型...") # 这里需要根据实际情况导入和加载您的模型 # 暂时返回模拟的模型 print("✅ 模型加载成功(模拟)") # 实际应该是: # from minigpt4_video_demo import init_model # model, processor, _, _, _ = init_model(args) model = "simulation_model" processor = "simulation_processor" return model, processor except Exception as e: print(f"❌ 模型加载失败: {e}") return None, None @spaces.GPU if GPU_AVAILABLE else lambda f: f def analyze_video_content(video_path: str, instruction: str = "请分析这个视频的内容") -> Tuple[str, str]: """ 分析视频内容 Args: video_path: 视频文件路径 instruction: 分析指令 Returns: Tuple[str, str]: (分析结果, 安全评级) """ try: # 加载模型 model, processor = load_model() if model is None: return "❌ 模型加载失败", "无法评估" print(f"🔄 正在分析视频: {video_path}") print(f"📝 分析指令: {instruction}") # 模拟分析过程 # 在实际应用中,这里会调用您的视频分析模型 # 模拟分析结果 analysis_result = f""" 🎬 **视频内容分析结果** 📋 **基本信息**: - 视频路径: {video_path} - 分析指令: {instruction} 🔍 **内容分析**: - 检测到的对象: 人物、场景、文字等 - 音频内容: 语音转文字结果 - 情感分析: 积极/中性/消极 🛡️ **安全检测**: - 暴力内容: 未检测到 - 不当内容: 未检测到 - 版权问题: 未检测到 ✅ **总体评估**: 内容安全,符合平台规范 """ safety_rating = "✅ P3 (安全)" return analysis_result, safety_rating except Exception as e: error_msg = f"❌ 分析过程中出错: {str(e)}" return error_msg, "⚠️ 错误" def create_interface(): """创建Gradio界面""" with gr.Blocks( title="🎥 Video Content Safety Analysis", theme=gr.themes.Soft(), css=""" .container { max-width: 800px; margin: auto; } .header { text-align: center; padding: 20px; } .footer { text-align: center; padding: 10px; color: #666; } """ ) as app: # 标题 gr.Markdown(""" # 🎥 智能视频内容安全分析 基于MiniGPT4-Video的多模态视频理解与安全检测系统 ⚡ **ZeroGPU加速** | 🛡️ **智能安全检测** | 🌍 **中英双语支持** """, elem_classes=["header"]) with gr.Row(): with gr.Column(scale=1): # 输入区域 gr.Markdown("## 📤 上传视频") video_input = gr.Video( label="选择视频文件 (支持MP4, AVI, MOV等格式,建议小于50MB)" ) instruction_input = gr.Textbox( label="分析指令", placeholder="请输入分析指令,如:请分析这个视频的内容安全性", value="请分析这个视频的内容,重点关注是否存在违规内容", lines=2 ) analyze_btn = gr.Button( "🚀 开始分析", variant="primary", size="lg" ) with gr.Column(scale=1): # 输出区域 gr.Markdown("## 📊 分析结果") analysis_output = gr.Textbox( label="详细分析", lines=15, max_lines=20, show_copy_button=True ) safety_output = gr.Textbox( label="安全评级", lines=1 ) # 示例和说明 gr.Markdown(""" ## 💡 使用说明 1. **上传视频**: 选择要分析的视频文件 2. **输入指令**: 描述您希望如何分析视频内容 3. **开始分析**: 点击按钮开始智能分析 4. **查看结果**: 获得详细的内容分析和安全评级 ## ⚠️ 注意事项 - 🕐 ZeroGPU有60秒运行时间限制 - 📁 建议上传文件小于50MB - ⏱️ 首次加载模型需要1-2分钟 - 🔄 分析时间取决于视频长度和复杂度 ## 🏷️ 安全等级说明 - **🚨 P0 (高危)**: 严重违规,需立即处理 - **⚠️ P1 (中危)**: 中等风险,需要审核 - **⚡ P2 (低危)**: 轻微风险,建议关注 - **✅ P3 (安全)**: 内容安全,符合规范 """, elem_classes=["footer"]) # 绑定事件 analyze_btn.click( fn=analyze_video_content, inputs=[video_input, instruction_input], outputs=[analysis_output, safety_output], show_progress=True ) return app def main(): """主函数""" print("🚀 启动视频内容安全分析应用") # 检查GPU可用性 if torch.cuda.is_available(): print(f"✅ GPU可用: {torch.cuda.get_device_name(0)}") else: print("⚠️ 使用CPU模式") # 创建应用 app = create_interface() # 启动应用 if __name__ == "__main__": app.launch( server_name="0.0.0.0", server_port=7860, share=False, show_error=True, quiet=False ) if __name__ == "__main__": main()