Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -200,45 +200,20 @@ def get_subtitles(video_path):
|
|
200 |
|
201 |
try:
|
202 |
extract_audio(video_path, audio_path)
|
|
|
203 |
|
204 |
-
#
|
205 |
-
print("🎙️ 开始中文语音识别...")
|
206 |
-
result = whisper_model.transcribe(
|
207 |
-
audio_path,
|
208 |
-
language="zh", # 指定中文
|
209 |
-
task="transcribe", # 转录任务
|
210 |
-
word_timestamps=True, # 启用词级时间戳
|
211 |
-
verbose=True, # 详细输出
|
212 |
-
temperature=0.2, # 降低温度提高准确性
|
213 |
-
beam_size=5, # 增加beam size
|
214 |
-
best_of=5, # 最佳候选数
|
215 |
-
fp16=True # 使用fp16加速
|
216 |
-
)
|
217 |
-
|
218 |
-
# 创建VTT文件,包含更详细的信息
|
219 |
with open(subtitle_path, "w", encoding="utf-8") as vtt_file:
|
220 |
vtt_file.write("WEBVTT\n\n")
|
221 |
-
|
222 |
-
for i, segment in enumerate(result['segments']):
|
223 |
start = format_timestamp(segment['start'])
|
224 |
end = format_timestamp(segment['end'])
|
225 |
-
text = segment['text']
|
|
|
226 |
|
227 |
-
# 🔧 添加说话人标识和详细信息
|
228 |
-
if text:
|
229 |
-
# 尝试识别说话人(基于音频特征变化)
|
230 |
-
speaker_id = f"说话人{(i // 3) % 3 + 1}" # 简单的说话人分配策略
|
231 |
-
|
232 |
-
# 格式化字幕,包含说话人信息
|
233 |
-
formatted_text = f"【{speaker_id}】: {text}"
|
234 |
-
|
235 |
-
vtt_file.write(f"{start} --> {end}\n{formatted_text}\n\n")
|
236 |
-
|
237 |
-
print(f"✅ 中文字幕生成完成: {len(result['segments'])} 个片段")
|
238 |
return subtitle_path
|
239 |
-
|
240 |
except Exception as e:
|
241 |
-
print(f"
|
242 |
return None
|
243 |
|
244 |
def prepare_input(video_path, subtitle_path, instruction):
|
@@ -485,7 +460,7 @@ def load_minigpt4_model():
|
|
485 |
print(f"💾 模型加载后显存使用: {torch.cuda.memory_allocated(0) / 1024**3:.1f} GB")
|
486 |
|
487 |
print("🚀 开始初始化Whisper模型...")
|
488 |
-
whisper_model = whisper.load_model("
|
489 |
|
490 |
if torch.cuda.is_available():
|
491 |
print(f"💾 全部加载后显存使用: {torch.cuda.memory_allocated(0) / 1024**3:.1f} GB")
|
@@ -535,29 +510,15 @@ def analyze_video_with_minigpt4(video_file, instruction):
|
|
535 |
temp_video_path = os.path.join(temp_dir, "analysis_video.mp4")
|
536 |
shutil.copy2(video_file, temp_video_path)
|
537 |
|
538 |
-
#
|
539 |
if not instruction or instruction.strip() == "":
|
540 |
-
instruction = ""
|
541 |
-
|
542 |
-
1. **场景描述**: 详细描述视频中的环境、背景、位置等
|
543 |
-
2. **人物分析**: 描述出现的每个人物的外貌、服装、动作
|
544 |
-
3. **对话内容**: 准确记录视频中每个人说的话,包括:
|
545 |
-
- 谁在说话
|
546 |
-
- 具体说了什么内容
|
547 |
-
- 说话的语气和情感
|
548 |
-
4. **动作描述**: 详细描述人物的动作、手势、表情变化
|
549 |
-
5. **声音元素**: 除了对话,还有背景音乐、音效等
|
550 |
-
6. **画面细节**: 物品、文字、标识等可见元素
|
551 |
-
7. **情节发展**: 视频的故事情节或事件发展过程
|
552 |
-
|
553 |
-
请用中文详细描述,特别注意记录完整的对话内容和说话人信息。"""
|
554 |
|
555 |
# 调用MiniGPT4-Video的生成函数
|
556 |
-
print("🎬 开始MiniGPT4-Video深度内容分析...")
|
557 |
prediction = generate_prediction(
|
558 |
video_path=temp_video_path,
|
559 |
instruction=instruction,
|
560 |
-
gen_subtitles=True, #
|
561 |
stream=False
|
562 |
)
|
563 |
|
@@ -572,24 +533,21 @@ def analyze_video_with_minigpt4(video_file, instruction):
|
|
572 |
📋 **基本信息**:
|
573 |
- 视频文件: {os.path.basename(video_file)}
|
574 |
- 分析设备: {torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'CPU模式'}
|
575 |
-
-
|
576 |
-
- 字幕识别: 中文语音转文字
|
577 |
|
578 |
-
|
579 |
{prediction}
|
580 |
|
581 |
{format_violations_report(violations_result)}
|
582 |
|
583 |
📊 **技术信息**:
|
584 |
-
- 内容理解: MiniGPT4-Video + Whisper
|
585 |
- 规则引擎: 巨量引擎299条禁投规则
|
586 |
- 检测等级: P1(低危) + P2(中危) + P3(高危)
|
587 |
-
- 分析模式: 多模态理解 (
|
588 |
-
- 对话识别: 说话人标识 + 完整对话内容
|
589 |
|
590 |
💡 **说明**:
|
591 |
-
基于MiniGPT4-Video
|
592 |
-
特别优化了中文对话识别和说话人标识功能。
|
593 |
"""
|
594 |
|
595 |
# 获取综合风险等级
|
@@ -607,7 +565,7 @@ def analyze_video_with_minigpt4(video_file, instruction):
|
|
607 |
1. 检查视频文件格式 (建议MP4)
|
608 |
2. 确认模型文件是否正确加载
|
609 |
3. 检查GPU内存是否充足
|
610 |
-
4.
|
611 |
|
612 |
💡 **提示**: 如果问题持续,请检查模型和依赖项安装
|
613 |
"""
|
@@ -622,8 +580,8 @@ def create_app():
|
|
622 |
gr.Video(label="上传视频文件"),
|
623 |
gr.Textbox(
|
624 |
label="分析指令",
|
625 |
-
value="
|
626 |
-
placeholder="输入您希望AI
|
627 |
lines=3
|
628 |
)
|
629 |
],
|
@@ -631,25 +589,18 @@ def create_app():
|
|
631 |
gr.Textbox(label="MiniGPT4-Video 内容分析 + 巨量引擎规则检测", lines=20),
|
632 |
gr.Textbox(label="巨量引擎风险评级")
|
633 |
],
|
634 |
-
title="🎥 智能视频内容安全分析 - MiniGPT4-Video + 巨量引擎
|
635 |
description="""
|
636 |
-
## 🎬 基于MiniGPT4-Video + 巨量引擎299条禁投规则的专业视频安全检测系统
|
637 |
|
638 |
-
⚡ **ZeroGPU加速** | 🎬 **MiniGPT4-Video** | 🎙️ **Whisper
|
639 |
|
640 |
**🔥 核心功能:**
|
641 |
- 🎞️ **深度视频理解**: MiniGPT4-Video多模态分析
|
642 |
-
- 🎙️
|
643 |
-
- 👥 **说话人识别**: 自动标识不同说话人和完整对话内容
|
644 |
- 🛡️ **专业违规检测**: 巨量引擎完整禁投规则库
|
645 |
- 📊 **智能风险评级**: P0-P3四级风险等级
|
646 |
|
647 |
-
**🎯 中文优化特色:**
|
648 |
-
- **🎭 对话记录**: 准确识别"谁说了什么话"
|
649 |
-
- **📝 完整转录**: 中文语音转文字,包含说话人信息
|
650 |
-
- **🎬 详细分析**: 场景、人物、动作、情感的全面描述
|
651 |
-
- **🔍 内容理解**: 专门优化的中文内容分析指令
|
652 |
-
|
653 |
**🎯 检测维度:**
|
654 |
- **高危(P3)**: 违法出版物、烟草、医疗等严重违规
|
655 |
- **中危(P2)**: 赌博周边、房地产、金融等中等风险
|
@@ -660,10 +611,10 @@ def create_app():
|
|
660 |
金融类、医疗类、烟草类等全部299条巨量引擎禁投规则
|
661 |
""",
|
662 |
examples=[
|
663 |
-
[None, "
|
664 |
-
[None, "
|
665 |
-
[None, "
|
666 |
-
[None, "
|
667 |
],
|
668 |
cache_examples=False
|
669 |
)
|
|
|
200 |
|
201 |
try:
|
202 |
extract_audio(video_path, audio_path)
|
203 |
+
result = whisper_model.transcribe(audio_path, language="en")
|
204 |
|
205 |
+
# 创建VTT文件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
with open(subtitle_path, "w", encoding="utf-8") as vtt_file:
|
207 |
vtt_file.write("WEBVTT\n\n")
|
208 |
+
for segment in result['segments']:
|
|
|
209 |
start = format_timestamp(segment['start'])
|
210 |
end = format_timestamp(segment['end'])
|
211 |
+
text = segment['text']
|
212 |
+
vtt_file.write(f"{start} --> {end}\n{text}\n\n")
|
213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
return subtitle_path
|
|
|
215 |
except Exception as e:
|
216 |
+
print(f"字幕生成错误: {e}")
|
217 |
return None
|
218 |
|
219 |
def prepare_input(video_path, subtitle_path, instruction):
|
|
|
460 |
print(f"💾 模型加载后显存使用: {torch.cuda.memory_allocated(0) / 1024**3:.1f} GB")
|
461 |
|
462 |
print("🚀 开始初始化Whisper模型...")
|
463 |
+
whisper_model = whisper.load_model("base").to(f"cuda:{whisper_gpu_id}" if torch.cuda.is_available() else "cpu")
|
464 |
|
465 |
if torch.cuda.is_available():
|
466 |
print(f"💾 全部加载后显存使用: {torch.cuda.memory_allocated(0) / 1024**3:.1f} GB")
|
|
|
510 |
temp_video_path = os.path.join(temp_dir, "analysis_video.mp4")
|
511 |
shutil.copy2(video_file, temp_video_path)
|
512 |
|
513 |
+
# 使用MiniGPT4-Video进行真实分析
|
514 |
if not instruction or instruction.strip() == "":
|
515 |
+
instruction = "请详细分析这个视频的内容,包括场景、人物、动作、对话等,并描述所有可见和可听的元素。"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
516 |
|
517 |
# 调用MiniGPT4-Video的生成函数
|
|
|
518 |
prediction = generate_prediction(
|
519 |
video_path=temp_video_path,
|
520 |
instruction=instruction,
|
521 |
+
gen_subtitles=True, # 生成字幕
|
522 |
stream=False
|
523 |
)
|
524 |
|
|
|
533 |
📋 **基本信息**:
|
534 |
- 视频文件: {os.path.basename(video_file)}
|
535 |
- 分析设备: {torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'CPU模式'}
|
536 |
+
- 分析指令: {instruction}
|
|
|
537 |
|
538 |
+
🔍 **视频内容描述**:
|
539 |
{prediction}
|
540 |
|
541 |
{format_violations_report(violations_result)}
|
542 |
|
543 |
📊 **技术信息**:
|
544 |
+
- 内容理解: MiniGPT4-Video + Whisper
|
545 |
- 规则引擎: 巨量引擎299条禁投规则
|
546 |
- 检测等级: P1(低危) + P2(中危) + P3(高危)
|
547 |
+
- 分析模式: 多模态理解 (视觉+语音+文本)
|
|
|
548 |
|
549 |
💡 **说明**:
|
550 |
+
基于MiniGPT4-Video的深度内容理解,结合巨量引擎完整禁投规则库进行专业违规检测。
|
|
|
551 |
"""
|
552 |
|
553 |
# 获取综合风险等级
|
|
|
565 |
1. 检查视频文件格式 (建议MP4)
|
566 |
2. 确认模型文件是否正确加载
|
567 |
3. 检查GPU内存是否充足
|
568 |
+
4. 验证配置文件路径
|
569 |
|
570 |
💡 **提示**: 如果问题持续,请检查模型和依赖项安装
|
571 |
"""
|
|
|
580 |
gr.Video(label="上传视频文件"),
|
581 |
gr.Textbox(
|
582 |
label="分析指令",
|
583 |
+
value="请详细分析这个视频的内容,包括场景、人物、动作、对话等,并描述所有可见和可听的元素。",
|
584 |
+
placeholder="输入您希望AI如何分析这个视频...",
|
585 |
lines=3
|
586 |
)
|
587 |
],
|
|
|
589 |
gr.Textbox(label="MiniGPT4-Video 内容分析 + 巨量引擎规则检测", lines=20),
|
590 |
gr.Textbox(label="巨量引擎风险评级")
|
591 |
],
|
592 |
+
title="🎥 智能视频内容安全分析 - MiniGPT4-Video + 巨量引擎",
|
593 |
description="""
|
594 |
+
## 🎬 基于MiniGPT4-Video + 巨量引擎299条禁投规则的专业视频安全检测系统
|
595 |
|
596 |
+
⚡ **ZeroGPU加速** | 🎬 **MiniGPT4-Video** | 🎙️ **Whisper语音** | 🛡️ **巨量引擎299条规则**
|
597 |
|
598 |
**🔥 核心功能:**
|
599 |
- 🎞️ **深度视频理解**: MiniGPT4-Video多模态分析
|
600 |
+
- 🎙️ **语音转文字**: Whisper自动生成字幕
|
|
|
601 |
- 🛡️ **专业违规检测**: 巨量引擎完整禁投规则库
|
602 |
- 📊 **智能风险评级**: P0-P3四级风险等级
|
603 |
|
|
|
|
|
|
|
|
|
|
|
|
|
604 |
**🎯 检测维度:**
|
605 |
- **高危(P3)**: 违法出版物、烟草、医疗等严重违规
|
606 |
- **中危(P2)**: 赌博周边、房地产、金融等中等风险
|
|
|
611 |
金融类、医疗类、烟草类等全部299条巨量引擎禁投规则
|
612 |
""",
|
613 |
examples=[
|
614 |
+
[None, "分析这个视频是否包含禁投内容"],
|
615 |
+
[None, "检测视频中是否有巨量引擎禁止的产品或服务"],
|
616 |
+
[None, "评估视频内容的投放风险等级"],
|
617 |
+
[None, "详细描述视频内容并进行合规检查"]
|
618 |
],
|
619 |
cache_examples=False
|
620 |
)
|