Spaces:
Sleeping
Sleeping
Commit
·
8febf87
1
Parent(s):
a49d90d
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,9 @@
|
|
| 4 |
适配ZeroGPU的视频内容安全分析应用
|
| 5 |
"""
|
| 6 |
import os
|
| 7 |
-
import tempfile
|
| 8 |
import gradio as gr
|
| 9 |
import torch
|
| 10 |
-
|
| 11 |
-
from typing import Optional, Tuple
|
| 12 |
-
import logging
|
| 13 |
|
| 14 |
# 设置中国镜像(如果在中国网络环境)
|
| 15 |
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
|
|
@@ -41,15 +38,8 @@ def load_model():
|
|
| 41 |
|
| 42 |
try:
|
| 43 |
print("🔄 正在加载模型...")
|
| 44 |
-
|
| 45 |
-
# 这里需要根据实际情况导入和加载您的模型
|
| 46 |
-
# 暂时返回模拟的模型
|
| 47 |
print("✅ 模型加载成功(模拟)")
|
| 48 |
|
| 49 |
-
# 实际应该是:
|
| 50 |
-
# from minigpt4_video_demo import init_model
|
| 51 |
-
# model, processor, _, _, _ = init_model(args)
|
| 52 |
-
|
| 53 |
model = "simulation_model"
|
| 54 |
processor = "simulation_processor"
|
| 55 |
|
|
@@ -61,16 +51,7 @@ def load_model():
|
|
| 61 |
|
| 62 |
@spaces.GPU if GPU_AVAILABLE else lambda f: f
|
| 63 |
def analyze_video_content(video_path: str, instruction: str = "请分析这个视频的内容") -> Tuple[str, str]:
|
| 64 |
-
"""
|
| 65 |
-
分析视频内容
|
| 66 |
-
|
| 67 |
-
Args:
|
| 68 |
-
video_path: 视频文件路径
|
| 69 |
-
instruction: 分析指令
|
| 70 |
-
|
| 71 |
-
Returns:
|
| 72 |
-
Tuple[str, str]: (分析结果, 安全评级)
|
| 73 |
-
"""
|
| 74 |
try:
|
| 75 |
# 加载模型
|
| 76 |
model, processor = load_model()
|
|
@@ -80,28 +61,25 @@ def analyze_video_content(video_path: str, instruction: str = "请分析这个
|
|
| 80 |
print(f"🔄 正在分析视频: {video_path}")
|
| 81 |
print(f"📝 分析指令: {instruction}")
|
| 82 |
|
| 83 |
-
# 模拟分析过程
|
| 84 |
-
# 在实际应用中,这里会调用您的视频分析模型
|
| 85 |
-
|
| 86 |
# 模拟分析结果
|
| 87 |
analysis_result = f"""
|
| 88 |
-
🎬
|
| 89 |
|
| 90 |
-
📋
|
| 91 |
- 视频路径: {video_path}
|
| 92 |
- 分析指令: {instruction}
|
| 93 |
|
| 94 |
-
🔍
|
| 95 |
- 检测到的对象: 人物、场景、文字等
|
| 96 |
- 音频内容: 语音转文字结果
|
| 97 |
- 情感分析: 积极/中性/消极
|
| 98 |
|
| 99 |
-
🛡️
|
| 100 |
- 暴力内容: 未检测到
|
| 101 |
- 不当内容: 未检测到
|
| 102 |
- 版权问题: 未检测到
|
| 103 |
|
| 104 |
-
✅
|
| 105 |
"""
|
| 106 |
|
| 107 |
safety_rating = "✅ P3 (安全)"
|
|
@@ -113,58 +91,33 @@ def analyze_video_content(video_path: str, instruction: str = "请分析这个
|
|
| 113 |
return error_msg, "⚠️ 错误"
|
| 114 |
|
| 115 |
def create_interface():
|
| 116 |
-
"""
|
| 117 |
|
| 118 |
-
with gr.Blocks(
|
| 119 |
-
title="🎥 Video Content Safety Analysis",
|
| 120 |
-
theme=gr.themes.Soft(),
|
| 121 |
-
css="""
|
| 122 |
-
.container { max-width: 800px; margin: auto; }
|
| 123 |
-
.header { text-align: center; padding: 20px; }
|
| 124 |
-
.footer { text-align: center; padding: 10px; color: #666; }
|
| 125 |
-
"""
|
| 126 |
-
) as app:
|
| 127 |
-
|
| 128 |
-
# 标题
|
| 129 |
-
gr.Markdown("""
|
| 130 |
-
# 🎥 智能视频内容安全分析
|
| 131 |
-
|
| 132 |
-
基于MiniGPT4-Video的多模态视频理解与安全检测系统
|
| 133 |
|
| 134 |
-
|
| 135 |
-
""
|
| 136 |
|
| 137 |
with gr.Row():
|
| 138 |
-
with gr.Column(
|
| 139 |
-
# 输入区域
|
| 140 |
gr.Markdown("## 📤 上传视频")
|
| 141 |
|
| 142 |
-
video_input = gr.Video(
|
| 143 |
-
label="选择视频文件 (支持MP4, AVI, MOV等格式,建议小于50MB)"
|
| 144 |
-
)
|
| 145 |
|
| 146 |
instruction_input = gr.Textbox(
|
| 147 |
label="分析指令",
|
| 148 |
-
placeholder="请输入分析指令,如:请分析这个视频的内容安全性",
|
| 149 |
value="请分析这个视频的内容,重点关注是否存在违规内容",
|
| 150 |
lines=2
|
| 151 |
)
|
| 152 |
|
| 153 |
-
analyze_btn = gr.Button(
|
| 154 |
-
"🚀 开始分析",
|
| 155 |
-
variant="primary",
|
| 156 |
-
size="lg"
|
| 157 |
-
)
|
| 158 |
|
| 159 |
-
with gr.Column(
|
| 160 |
-
# 输出区域
|
| 161 |
gr.Markdown("## 📊 分析结果")
|
| 162 |
|
| 163 |
analysis_output = gr.Textbox(
|
| 164 |
label="详细分析",
|
| 165 |
-
lines=
|
| 166 |
-
max_lines=20,
|
| 167 |
-
show_copy_button=True
|
| 168 |
)
|
| 169 |
|
| 170 |
safety_output = gr.Textbox(
|
|
@@ -172,36 +125,26 @@ def create_interface():
|
|
| 172 |
lines=1
|
| 173 |
)
|
| 174 |
|
| 175 |
-
# 示例和说明
|
| 176 |
gr.Markdown("""
|
| 177 |
## 💡 使用说明
|
| 178 |
|
| 179 |
-
1.
|
| 180 |
-
2.
|
| 181 |
-
3.
|
| 182 |
-
4.
|
| 183 |
|
| 184 |
## ⚠️ 注意事项
|
| 185 |
|
| 186 |
-
-
|
| 187 |
-
-
|
| 188 |
-
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
## 🏷️ 安全等级说明
|
| 192 |
-
|
| 193 |
-
- **🚨 P0 (高危)**: 严重违规,需立即处理
|
| 194 |
-
- **⚠️ P1 (中危)**: 中等风险,需要审核
|
| 195 |
-
- **⚡ P2 (低危)**: 轻微风险,建议关注
|
| 196 |
-
- **✅ P3 (安全)**: 内容安全,符合规范
|
| 197 |
-
""", elem_classes=["footer"])
|
| 198 |
|
| 199 |
# 绑定事件
|
| 200 |
analyze_btn.click(
|
| 201 |
fn=analyze_video_content,
|
| 202 |
inputs=[video_input, instruction_input],
|
| 203 |
-
outputs=[analysis_output, safety_output]
|
| 204 |
-
show_progress=True
|
| 205 |
)
|
| 206 |
|
| 207 |
return app
|
|
@@ -219,15 +162,13 @@ def main():
|
|
| 219 |
# 创建应用
|
| 220 |
app = create_interface()
|
| 221 |
|
| 222 |
-
# 启动应用
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
quiet=False
|
| 230 |
-
)
|
| 231 |
|
| 232 |
if __name__ == "__main__":
|
| 233 |
main()
|
|
|
|
| 4 |
适配ZeroGPU的视频内容安全分析应用
|
| 5 |
"""
|
| 6 |
import os
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
import torch
|
| 9 |
+
from typing import Tuple
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# 设置中国镜像(如果在中国网络环境)
|
| 12 |
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
|
|
|
|
| 38 |
|
| 39 |
try:
|
| 40 |
print("🔄 正在加载模型...")
|
|
|
|
|
|
|
|
|
|
| 41 |
print("✅ 模型加载成功(模拟)")
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
model = "simulation_model"
|
| 44 |
processor = "simulation_processor"
|
| 45 |
|
|
|
|
| 51 |
|
| 52 |
@spaces.GPU if GPU_AVAILABLE else lambda f: f
|
| 53 |
def analyze_video_content(video_path: str, instruction: str = "请分析这个视频的内容") -> Tuple[str, str]:
|
| 54 |
+
"""分析视频内容"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
# 加载模型
|
| 57 |
model, processor = load_model()
|
|
|
|
| 61 |
print(f"🔄 正在分析视频: {video_path}")
|
| 62 |
print(f"📝 分析指令: {instruction}")
|
| 63 |
|
|
|
|
|
|
|
|
|
|
| 64 |
# 模拟分析结果
|
| 65 |
analysis_result = f"""
|
| 66 |
+
🎬 视频内容分析结果
|
| 67 |
|
| 68 |
+
📋 基本信息:
|
| 69 |
- 视频路径: {video_path}
|
| 70 |
- 分析指令: {instruction}
|
| 71 |
|
| 72 |
+
🔍 内容分析:
|
| 73 |
- 检测到的对象: 人物、场景、文字等
|
| 74 |
- 音频内容: 语音转文字结果
|
| 75 |
- 情感分析: 积极/中性/消极
|
| 76 |
|
| 77 |
+
🛡️ 安全检测:
|
| 78 |
- 暴力内容: 未检测到
|
| 79 |
- 不当内容: 未检测到
|
| 80 |
- 版权问题: 未检测到
|
| 81 |
|
| 82 |
+
✅ 总体评估: 内容安全,符合平台规范
|
| 83 |
"""
|
| 84 |
|
| 85 |
safety_rating = "✅ P3 (安全)"
|
|
|
|
| 91 |
return error_msg, "⚠️ 错误"
|
| 92 |
|
| 93 |
def create_interface():
|
| 94 |
+
"""创建简化的Gradio界面"""
|
| 95 |
|
| 96 |
+
with gr.Blocks(title="Video Content Safety Analysis") as app:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
gr.Markdown("# 🎥 智能视频内容安全分析")
|
| 99 |
+
gr.Markdown("基于MiniGPT4-Video的多模态视频理解与安全检测系统")
|
| 100 |
|
| 101 |
with gr.Row():
|
| 102 |
+
with gr.Column():
|
|
|
|
| 103 |
gr.Markdown("## 📤 上传视频")
|
| 104 |
|
| 105 |
+
video_input = gr.Video(label="选择视频文件")
|
|
|
|
|
|
|
| 106 |
|
| 107 |
instruction_input = gr.Textbox(
|
| 108 |
label="分析指令",
|
|
|
|
| 109 |
value="请分析这个视频的内容,重点关注是否存在违规内容",
|
| 110 |
lines=2
|
| 111 |
)
|
| 112 |
|
| 113 |
+
analyze_btn = gr.Button("🚀 开始分析")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
with gr.Column():
|
|
|
|
| 116 |
gr.Markdown("## 📊 分析结果")
|
| 117 |
|
| 118 |
analysis_output = gr.Textbox(
|
| 119 |
label="详细分析",
|
| 120 |
+
lines=10
|
|
|
|
|
|
|
| 121 |
)
|
| 122 |
|
| 123 |
safety_output = gr.Textbox(
|
|
|
|
| 125 |
lines=1
|
| 126 |
)
|
| 127 |
|
|
|
|
| 128 |
gr.Markdown("""
|
| 129 |
## 💡 使用说明
|
| 130 |
|
| 131 |
+
1. 上传视频文件
|
| 132 |
+
2. 输入分析指令
|
| 133 |
+
3. 点击开始分析
|
| 134 |
+
4. 查看分析结果
|
| 135 |
|
| 136 |
## ⚠️ 注意事项
|
| 137 |
|
| 138 |
+
- ZeroGPU有60秒运行时间限制
|
| 139 |
+
- 建议上传文件小于50MB
|
| 140 |
+
- 首次加载模型需要1-2分钟
|
| 141 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
# 绑定事件
|
| 144 |
analyze_btn.click(
|
| 145 |
fn=analyze_video_content,
|
| 146 |
inputs=[video_input, instruction_input],
|
| 147 |
+
outputs=[analysis_output, safety_output]
|
|
|
|
| 148 |
)
|
| 149 |
|
| 150 |
return app
|
|
|
|
| 162 |
# 创建应用
|
| 163 |
app = create_interface()
|
| 164 |
|
| 165 |
+
# 启动应用 - ZeroGPU环境配置
|
| 166 |
+
app.launch(
|
| 167 |
+
server_name="0.0.0.0",
|
| 168 |
+
server_port=7860,
|
| 169 |
+
share=True,
|
| 170 |
+
show_error=True
|
| 171 |
+
)
|
|
|
|
|
|
|
| 172 |
|
| 173 |
if __name__ == "__main__":
|
| 174 |
main()
|