Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -200,7 +200,17 @@ def get_subtitles(video_path):
|
|
200 |
|
201 |
try:
|
202 |
extract_audio(video_path, audio_path)
|
203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
# 创建VTT文件
|
206 |
with open(subtitle_path, "w", encoding="utf-8") as vtt_file:
|
@@ -285,11 +295,11 @@ def generate_prediction(video_path, instruction, gen_subtitles=True, stream=Fals
|
|
285 |
answers = model.generate(
|
286 |
prepared_images,
|
287 |
prompt,
|
288 |
-
max_new_tokens=
|
289 |
do_sample=True,
|
290 |
lengths=[length],
|
291 |
num_beams=1, # 保持beam=1减少计算
|
292 |
-
temperature=0.
|
293 |
top_p=0.9, # 添加top_p参数
|
294 |
repetition_penalty=1.1 # 避免重复
|
295 |
)
|
@@ -390,12 +400,31 @@ def optimize_gpu_memory():
|
|
390 |
"""GPU内存优化"""
|
391 |
print("🔍 开始GPU内存优化...")
|
392 |
|
393 |
-
# 设置环境变量优化内存分配
|
394 |
-
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:256,garbage_collection_threshold:0.6'
|
395 |
-
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
|
396 |
-
|
397 |
if torch.cuda.is_available():
|
398 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
399 |
print(f"💾 总显存: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB")
|
400 |
|
401 |
# 强制清理所有GPU缓存
|
@@ -403,10 +432,6 @@ def optimize_gpu_memory():
|
|
403 |
torch.cuda.ipc_collect()
|
404 |
gc.collect()
|
405 |
|
406 |
-
# 设置内存增长策略
|
407 |
-
torch.backends.cudnn.benchmark = False
|
408 |
-
torch.backends.cudnn.deterministic = True
|
409 |
-
|
410 |
print(f"💾 清理后可用显存: {(torch.cuda.get_device_properties(0).total_memory - torch.cuda.memory_allocated(0)) / 1024**3:.1f} GB")
|
411 |
|
412 |
def get_arguments():
|
@@ -460,7 +485,9 @@ def load_minigpt4_model():
|
|
460 |
print(f"💾 模型加载后显存使用: {torch.cuda.memory_allocated(0) / 1024**3:.1f} GB")
|
461 |
|
462 |
print("🚀 开始初始化Whisper模型...")
|
463 |
-
|
|
|
|
|
464 |
|
465 |
if torch.cuda.is_available():
|
466 |
print(f"💾 全部加载后显存使用: {torch.cuda.memory_allocated(0) / 1024**3:.1f} GB")
|
@@ -512,12 +539,16 @@ def analyze_video_with_minigpt4(video_file, instruction):
|
|
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=
|
521 |
gen_subtitles=True, # 生成字幕
|
522 |
stream=False
|
523 |
)
|
@@ -580,7 +611,7 @@ def create_app():
|
|
580 |
gr.Video(label="上传视频文件"),
|
581 |
gr.Textbox(
|
582 |
label="分析指令",
|
583 |
-
value="
|
584 |
placeholder="输入您希望AI如何分析这个视频...",
|
585 |
lines=3
|
586 |
)
|
@@ -650,5 +681,65 @@ def main():
|
|
650 |
show_error=True
|
651 |
)
|
652 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
653 |
if __name__ == "__main__":
|
654 |
main()
|
|
|
200 |
|
201 |
try:
|
202 |
extract_audio(video_path, audio_path)
|
203 |
+
# 🔧 优化中文语音识别
|
204 |
+
result = whisper_model.transcribe(
|
205 |
+
audio_path,
|
206 |
+
language="zh", # 明确指定中文
|
207 |
+
task="transcribe", # 明确指定转录任务
|
208 |
+
temperature=0.0, # 降低随机性
|
209 |
+
best_of=5, # 使用最佳结果
|
210 |
+
beam_size=5, # 增加beam搜索
|
211 |
+
patience=2.0, # 增加耐心参数
|
212 |
+
initial_prompt="以下是一段中文视频的语音内容:" # 中文提示
|
213 |
+
)
|
214 |
|
215 |
# 创建VTT文件
|
216 |
with open(subtitle_path, "w", encoding="utf-8") as vtt_file:
|
|
|
295 |
answers = model.generate(
|
296 |
prepared_images,
|
297 |
prompt,
|
298 |
+
max_new_tokens=512, # 增加token数以获得更详细的分析
|
299 |
do_sample=True,
|
300 |
lengths=[length],
|
301 |
num_beams=1, # 保持beam=1减少计算
|
302 |
+
temperature=0.7, # 稍微降低温度获得更稳定输出
|
303 |
top_p=0.9, # 添加top_p参数
|
304 |
repetition_penalty=1.1 # 避免重复
|
305 |
)
|
|
|
400 |
"""GPU内存优化"""
|
401 |
print("🔍 开始GPU内存优化...")
|
402 |
|
|
|
|
|
|
|
|
|
403 |
if torch.cuda.is_available():
|
404 |
+
gpu_name = torch.cuda.get_device_name(0)
|
405 |
+
print(f"🔍 GPU: {gpu_name}")
|
406 |
+
|
407 |
+
# 🔧 H200特定优化
|
408 |
+
if "H200" in gpu_name:
|
409 |
+
print("🚀 检测到H200显卡,应用特定优化...")
|
410 |
+
# H200优化设置
|
411 |
+
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:128,garbage_collection_threshold:0.8,expandable_segments:True'
|
412 |
+
os.environ['CUDA_LAUNCH_BLOCKING'] = '0' # H200上设置为0
|
413 |
+
os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8' # H200 cuBLAS优化
|
414 |
+
os.environ['NCCL_AVOID_RECORD_STREAMS'] = '1' # 避免H200内存问题
|
415 |
+
|
416 |
+
# 设置混合精度优化
|
417 |
+
torch.backends.cudnn.allow_tf32 = True # 启用TF32提升H200性能
|
418 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
419 |
+
torch.backends.cudnn.benchmark = True # H200上启用benchmark
|
420 |
+
|
421 |
+
else:
|
422 |
+
# 标准设置(A100等)
|
423 |
+
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:256,garbage_collection_threshold:0.6'
|
424 |
+
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
|
425 |
+
torch.backends.cudnn.benchmark = False
|
426 |
+
torch.backends.cudnn.deterministic = True
|
427 |
+
|
428 |
print(f"💾 总显存: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB")
|
429 |
|
430 |
# 强制清理所有GPU缓存
|
|
|
432 |
torch.cuda.ipc_collect()
|
433 |
gc.collect()
|
434 |
|
|
|
|
|
|
|
|
|
435 |
print(f"💾 清理后可用显存: {(torch.cuda.get_device_properties(0).total_memory - torch.cuda.memory_allocated(0)) / 1024**3:.1f} GB")
|
436 |
|
437 |
def get_arguments():
|
|
|
485 |
print(f"💾 模型加载后显存使用: {torch.cuda.memory_allocated(0) / 1024**3:.1f} GB")
|
486 |
|
487 |
print("🚀 开始初始化Whisper模型...")
|
488 |
+
# 🔧 使用更强的Whisper模型以提升中文识别
|
489 |
+
whisper_model = whisper.load_model("medium").to(f"cuda:{whisper_gpu_id}" if torch.cuda.is_available() else "cpu")
|
490 |
+
print("✅ Whisper模型加载完成 (medium版本,优化中文识别)")
|
491 |
|
492 |
if torch.cuda.is_available():
|
493 |
print(f"💾 全部加载后显存使用: {torch.cuda.memory_allocated(0) / 1024**3:.1f} GB")
|
|
|
539 |
|
540 |
# 使用MiniGPT4-Video进行真实分析
|
541 |
if not instruction or instruction.strip() == "":
|
542 |
+
instruction = "请详细分析这个视频的内容,包括场景、人物、动作、对话等。请用中文输出,并详细记录视频中谁说了什么话。"
|
543 |
+
|
544 |
+
# 🧠 使用智能规则感知指令
|
545 |
+
intelligent_instruction = create_intelligent_instruction(instruction)
|
546 |
+
print(f"🧠 使用智能规则感知指令进行分析...")
|
547 |
|
548 |
# 调用MiniGPT4-Video的生成函数
|
549 |
prediction = generate_prediction(
|
550 |
video_path=temp_video_path,
|
551 |
+
instruction=intelligent_instruction, # 使用智能指令
|
552 |
gen_subtitles=True, # 生成字幕
|
553 |
stream=False
|
554 |
)
|
|
|
611 |
gr.Video(label="上传视频文件"),
|
612 |
gr.Textbox(
|
613 |
label="分析指令",
|
614 |
+
value="请详细分析这个视频的内容,包括场景、人物、动作、对话等。请用中文输出,并详细记录视频中谁说了什么话。",
|
615 |
placeholder="输入您希望AI如何分析这个视频...",
|
616 |
lines=3
|
617 |
)
|
|
|
681 |
show_error=True
|
682 |
)
|
683 |
|
684 |
+
def create_intelligent_instruction(original_instruction):
|
685 |
+
"""创建具备规则理解能力的智能分析指令"""
|
686 |
+
|
687 |
+
# 核心禁投规则摘要 - 让AI知道需要检测什么
|
688 |
+
rules_summary = """
|
689 |
+
请特别注意以下巨量引擎禁投内容(如发现请在描述中明确指出):
|
690 |
+
|
691 |
+
🚨 **高危违规内容 (P3)**:
|
692 |
+
- 医疗器械、药品、保健品、医美服务
|
693 |
+
- 烟草制品、电子烟相关产品
|
694 |
+
- 虚拟货币、区块链、NFT、数字藏品
|
695 |
+
- 违法出版物、政治敏感内容
|
696 |
+
- 贷款、信贷、金融投资、股票
|
697 |
+
- 赌博、博彩、棋牌游戏
|
698 |
+
|
699 |
+
⚠️ **中危违规内容 (P2)**:
|
700 |
+
- 房地产买卖、租赁、中介服务
|
701 |
+
- 工具软件、刷机、破解软件
|
702 |
+
- 教育培训、学历提升、考试代办
|
703 |
+
- 翡翠、玉石、文玩、珠宝盲盒
|
704 |
+
- 黄金回收、贵金属投资
|
705 |
+
|
706 |
+
💭 **低危违规内容 (P1)**:
|
707 |
+
- 化妆品中的特殊功效产品
|
708 |
+
- 汽车修复、代办服务
|
709 |
+
- 游戏账号交易、代练
|
710 |
+
- 特殊食品、减肥产品
|
711 |
+
"""
|
712 |
+
|
713 |
+
intelligent_instruction = f"""
|
714 |
+
你是专业的巨量引擎广告内容审核专家。请用中文详细分析这个视频,包括:
|
715 |
+
|
716 |
+
📹 **视频内容详细描述**:
|
717 |
+
- 场景环境:描述视频拍摄场所、背景环境
|
718 |
+
- 人物信息:谁在视频中出现,年龄、性别、穿着特征
|
719 |
+
- 关键动作:详细描述人物的具体动作和行为
|
720 |
+
- 产品展示:如有产品展示,请详细描述产品外观、材质、用途
|
721 |
+
- 文字信息:视频中出现的任何文字、标识、品牌名称
|
722 |
+
|
723 |
+
🎙️ **语音对话内容**:
|
724 |
+
- 详细记录视频中的所有对话内容
|
725 |
+
- 明确标注"谁说了什么话"
|
726 |
+
- 记录任何产品介绍、价格信息、功效宣传
|
727 |
+
- 注意推销话术、营销用语
|
728 |
+
|
729 |
+
🔍 **潜在违规风险分析**:
|
730 |
+
{rules_summary}
|
731 |
+
|
732 |
+
🎯 **分析要求**:
|
733 |
+
1. 用中文输出所有内容
|
734 |
+
2. 对于任何可能涉及上述违规内容的元素,请明确指出
|
735 |
+
3. 重点关注翡翠、玉石、珠宝等文玩制品
|
736 |
+
4. 注意医疗、金融、房产、教育等敏感行业
|
737 |
+
5. 记录所有营销宣传语句
|
738 |
+
|
739 |
+
原始指令:{original_instruction}
|
740 |
+
"""
|
741 |
+
|
742 |
+
return intelligent_instruction
|
743 |
+
|
744 |
if __name__ == "__main__":
|
745 |
main()
|