3. 训练模型

使用fit方法训练模型。我们将观察训练过程中的损失和准确度变化。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/81fc1a612f9d5feb62c4a52e702b9797_53.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/81fc1a612f9d5feb62c4a52e702b9797_55.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/81fc1a612f9d5feb62c4a52e702b9797_57.png

history = model.fit(X_train, y_train, epochs=300, validation_split=0.2, verbose=0)

4. 分析学习曲线

训练完成后,绘制损失曲线和准确度曲线对于诊断模型至关重要。

import matplotlib.pyplot as plt

# 绘制损失曲线
plt.plot(history.history['loss'], label='训练损失')
plt.plot(history.history['val_loss'], label='验证损失')
plt.title('模型损失曲线')
plt.ylabel('损失')
plt.xlabel('训练轮次')
plt.legend()
plt.show()

在分析学习曲线时,我们关注以下几点:

  • 斜率:曲线下降表明模型正在学习。

  • 平滑度:曲线平滑通常意味着学习过程稳定。

  • 收敛:曲线趋于平缓表明学习可能已完成。

  • 泛化差距:训练损失和验证损失之间的差距。差距小说明模型泛化能力强;差距大可能意味着过拟合。

  • 过拟合:如果验证损失在训练后期开始上升,而训练损失持续下降,则表明模型正在记忆训练数据,即过拟合。


总结

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/81fc1a612f9d5feb62c4a52e702b9797_59.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/81fc1a612f9d5feb62c4a52e702b9797_61.png

本节课中我们一起学习了机器学习的基础知识。我们首先使用Scikit-learn库实践了监督式学习的标准流程:加载数据、分割数据集、选择模型、训练和评估。接着,我们使用Keras构建并训练了一个简单的人工神经网络,了解了神经网络的基本组件(如层、神经元、激活函数)以及如何通过分析学习曲线来诊断模型。这些基础概念和动手经验是进一步探索更复杂模型和生成式AI的重要基石。

73:视觉小说基础标签 🏷️

在本节课中,我们将学习如何为项目创建一个特定的“快照”或里程碑,以便在未来的开发中或与他人协作时,能够准确地回到这个状态。

上一节我们介绍了如何为视觉小说项目添加资源和动画。本节中,我们来看看如何通过Git的“标签”功能来标记项目的当前进度。

概述

在软件开发中,尤其是在教程系列里,经常需要在特定节点保存项目的完整状态。这确保了学习者可以从一个已知的、功能完整的起点继续,而无需手动复现所有步骤。本节课将演示如何使用Git标签来标记“视觉小说基础”版本。

项目进展与标签创建

可以看到,项目已经取得了相当大的进展。这些工作基本上是在直播开始前一小时内完成的,主要包括添加资源和清理一些细节。

显然,还做了一些额外的工作。例如,现在界面上有三个设置选项,虽然其中两个尚未关联功能,但有一个已经可以工作。

具体来说,音频音量被设置为0。这样设置有一个目的:它创建了一个断点。如果你不想亲自编写视觉小说的所有代码,可以直接从这个节点开始继续学习。

此外,将音频静音还有另一个原因:背景中可能被拾取的音频会在YouTube上被标记(并非不良内容)。如果未来想将内容整合到其他平台,不希望遇到类似问题。

重点是,我们到达了当前这个状态。虽然音频播放功能尚未完全实现,但项目的外观已经美观很多,并且拥有了一些不错的动画。实现这些并没有花费太多时间。

现在,希望将项目的这个特定状态“锁定”在当前的时间和空间里。这样,如果你想精确地从这一点开始,就可以找到对应的标签。

操作步骤

以下是创建Git标签的具体步骤:

  1. 创建标签:使用命令 git tag -a visual-novel-base 来创建一个名为“visual-novel-base”的带注释标签。

  2. 推送标签:使用命令 git push --tags 将创建好的标签推送到远程仓库。

完成上述操作后,仓库的这个时间点就被成功标记了。这样,在未来的视频中,当专注于集成音频服务或其他功能时,你就可以准确地从这个标记点开始。

总结

本节课中我们一起学习了Git标签的重要性和使用方法。通过为项目打上“visual-novel-base”标签,我们创建了一个清晰的、可回溯的里程碑。这为跳过部分内容或需要寻找一个可靠起点的学习者提供了极大的便利。现在,这个特定状态的项目已经为你准备就绪。

74:主要AI风险与常见部署模式 🛡️

在本节课中,我们将探讨在公有云上部署生成式AI应用时面临的主要风险以及常见的部署模式。理解这些内容对于保障应用安全和组织数据至关重要。

概述

生成式AI应用在带来巨大潜力的同时,也伴随着一系列风险。本节课程将首先分析数据暴露、模型误用、合规性及法律等主要风险,然后介绍常见的API集成与私有端点等部署模式,最后从技术层面探讨身份管理、加密和网络安全三大支柱。

主要AI风险

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/0542db2ca8aaf4a8b8f5081be89d3dcb_1.png

以下是企业在部署生成式AI时需重点关注的主要风险领域。

数据暴露与隐私风险

数据是驱动AI的燃料,因此数据暴露是最大的风险。无论您是企业还是组织内的个人用户,都需要警惕数据泄露。虽然存在可部署的保护工具,但像DeepSeek这样的LLM使得隐私问题变得更加重要。特别是如果您所在的组织不应与特定国家(例如DeepSeek背后的中国)共享数据时。您必须留意所使用LLM的隐私政策,无论是免费版、开源版还是付费版,都需要明确其隐私条款以及如何保护输入其中的数据。

核心建议:将所有敏感数据隔离,并尽可能保持LLM的“低权限”状态。这意味着只发送指令并接收指令反馈,但不共享数据。

模型误用风险

由于您可能正在使用ChatGPT、Claude或互联网上的其他模型,模型误用可能相当普遍。如果您使用从Hugging Face等平台下载的开源版本,或为了安全保护而使用本地版本,这可能会增加幻觉等问题的风险。如果您使用的不是经过训练以减少幻觉的流行模型(如ChatGPT或Claude),而是使用未基于最新模型训练的版本,那么产生意外输出的风险可能导致公司做出错误决策。

应对措施:可以实施防护栏,通过持续监控模型的任何变化,并关注在线公告中关于您所用模型可能存在的任何漏洞或安全缺陷,来防止模型被误用。

安全、合规与监管标准风险

这对于大型组织,尤其是处理私人数据(如PII或个人可识别信息)的组织至关重要。这些信息可能包括护照、驾照、家庭住址等,一旦泄露,可能导致严重的合规与隐私违规。幸运的是,大多数人都遵循合规标准。在处理LLM及其可能涉及的数据时,理解适用的法规(如HIPAA、GDPR或ISO标准)是您必须完成的第一步。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/0542db2ca8aaf4a8b8f5081be89d3dcb_3.png

在明确合规要求后,您可以开始配置用于构建LLM的服务。您很可能使用的是亚马逊、Azure或谷歌的AI云服务。您可以考虑加入一些防护栏和云合规性检查,以确保所使用的服务是合规的,并且为LLM模型存储或使用的数据处于正确的位置。

此外,您还需要在整个系统中维护审计日志,这用于在出现问题时识别是谁做的以及他们是如何操作的。某些监管标准可能要求您保留一些信息或数据,无论是LLM系统的输出,还是简单的输入数据记录及输入输出后的变更情况。您需要根据任何监管标准来落实这些要求。

法律与知识产权风险

许多人讨论的另一个风险是法律风险,即知识产权和许可风险。您可能已经看到许多针对LLM模型使用受版权保护材料进行训练的诉讼。如果模型在预训练时使用了受版权保护的数据,或者使用了本不应用于商业用途的数据,而现在却用于商业目的,则可能构成许可违约。如果您不遵守所使用的LLM提供商的条款,同样可能构成违约。

常见部署模式

在理解了上述风险后,我们来看看常见的部署模式。

API集成模式

一种常见的模式是API集成。在这种模式下,您可以将组织内长期使用的工具(如Gmail、Slack等)通过其API能力与LLM提供商连接。您向LLM提供者发送API请求作为输入,并获取输出反馈。这被称为API集成。

私有端点与混合模式

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/0542db2ca8aaf4a8b8f5081be89d3dcb_5.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/0542db2ca8aaf4a8b8f5081be89d3dcb_6.png

如果您在自己的应用中使用AI,并希望使其具备AI功能,您可能会在组织网络内部使用私有端点。这个端点可能与外部端点通信,或者您可能在本地数据中心或云环境中托管一个私有的、本地部署的LLM提供商,以确保隐私和合规性。显然,也存在混合模式。目前,非常常见的模式是使用API集成和私有端点来构建具备AI功能的应用程序。

技术风险支柱

在部署时,需要警惕三个技术风险。

  1. 身份与访问管理(IAM)

    需要明确谁有权访问您提供给LLM的数据并进行修改,以及在您的云提供商中,谁有责任更改模型、AI应用或用于AI的基础设施。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/0542db2ca8aaf4a8b8f5081be89d3dcb_8.png

  1. 加密

    加密是数据安全的重要组成部分。谁拥有密钥的访问权是一个在保护AI应用安全时至关重要的问题。

  2. 网络安全

    您拥有的AI应用将托管在您的数据中心或云环境中。因此,网络配置必须确保没有外部方可以访问它,并且您需要持续监控任何威胁,定期进行渗透测试,并确保没有可用的公共访问途径。

总结:从技术风险角度来看,身份管理、加密和网络安全是您必须关注的三大支柱。

总结

本节课我们一起学习了生成式AI应用部署中的主要风险,包括数据隐私、模型误用、合规法律问题,并探讨了API集成和私有端点等常见部署模式。最后,我们从技术层面强调了身份管理、加密和网络安全三大风险支柱的重要性。希望这些知识能帮助您构建更安全的AI应用。

75:WhisperX逐字转录 Part1 🎤

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_1.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_3.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_5.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_7.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_9.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_11.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_12.png

在本节课中,我们将学习如何使用Whisper和WhisperX进行自动语音识别,并尝试获取逐字时间戳。我们将从环境配置开始,逐步探索模型的使用、音频格式处理以及如何通过Docker容器解决依赖问题。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_14.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_16.png

环境配置与模型选择

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_18.png

上一节我们介绍了课程目标,本节中我们来看看如何搭建一个用于自动语音识别的开发环境。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_20.png

首先,我们需要创建一个新的项目目录并设置Python环境。

以下是创建和激活Conda环境的步骤:

conda create -n asr_task python=3.11
conda activate asr_task
conda install ipykernel

接下来,安装必要的Python库。

pip install transformers ipywidgets scipy torch torchaudio

Whisper模型有不同的大小,性能与资源消耗各异。我们将使用small模型,它在英语和日语识别上表现良好,且对显存要求适中。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_22.png

使用Hugging Face Whisper进行转录

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_24.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_26.png

环境配置好后,我们来看看如何使用Hugging Face的Transformers库加载Whisper模型并进行转录。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_28.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_30.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_32.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_34.png

核心步骤是创建语音识别管道并指定模型。

from transformers import pipeline
import torch

<https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_36.png>

<https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_38.png>

<https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_39.png>

# 检查并设置设备
device = "cuda:0" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")

# 创建语音识别管道
asr_pipeline = pipeline(
    "automatic-speech-recognition",
    model="openai/whisper-small",
    device=device
)

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_41.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_43.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_45.png

加载音频文件并执行转录。

file_name = "jp_sample.wav"
result = asr_pipeline(file_name, return_timestamps=True)
print(result)

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_47.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_49.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_51.png

有时音频文件格式可能导致问题。如果遇到错误,可以使用FFmpeg进行转换,确保其采样率等参数符合要求。

ffmpeg -i input.wav -ar 16000 -ac 1 -c:a pcm_s16le output.wav

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_53.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_54.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_56.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_57.png

探索逐字时间戳与WhisperX

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_59.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_61.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_63.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_65.png

基础的Whisper模型返回的是句子或段落级别的时间戳。为了获得逐字时间戳,我们需要使用WhisperX。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_67.png

然而,直接安装WhisperX可能会遇到CUDA版本不匹配等复杂的依赖问题。一个更简单的解决方案是使用Docker容器,它包含了所有预配置的环境。

以下是使用Docker运行WhisperX的命令:

docker run --gpus all -v $(pwd):/app ghcr.io/m-bain/whisperx:latest --model large-v3 --language ja --output_dir /app/output /app/wake_up_converted.wav

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_69.png

这个命令会下载WhisperX的Docker镜像,并将当前目录挂载到容器的/app路径,然后对指定的音频文件进行转录,结果将输出到本地目录。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_71.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_73.png

总结

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_75.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/6dc2d2b929dadb59921c8c2b37a474ba_77.png

本节课中我们一起学习了自动语音识别的基础流程。我们首先配置了Python环境并安装了Whisper模型,成功实现了音频转录。接着,我们遇到了获取逐字时间戳的需求,并探索了WhisperX这一解决方案。由于本地环境依赖复杂,我们最终采用了Docker容器来便捷地运行WhisperX,从而能够处理日语等语言并获取更精细的单词级时间戳信息。这个过程展示了在AI开发中灵活运用不同工具和方法来解决实际问题的思路。

76:WhisperX逐词转录第二部分 🎤

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_1.png

在本节课中,我们将继续探索WhisperX,目标是获取逐词的时间戳信息,并利用这些数据创建一个动态高亮显示字幕的视频。我们将解决输出格式问题,并最终实现一个基础的“卡拉OK”式字幕效果。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_3.png


运行转录与输出格式探索

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_5.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_7.png

上一节我们成功运行了WhisperX。现在,我们来看看转录结果。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_1.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_9.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_11.png

转录运行速度很快,并成功返回了结果。然而,我们需要的不是普通的字幕文件,而是包含逐词时间戳的详细信息。当前的输出似乎是SRT格式。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_13.png

为了获取逐词数据,我们需要指定正确的输出格式。WhisperX支持多种格式。通过查阅其代码,我们找到了可用的选项。

以下是WhisperX支持的输出格式:

  • TXT:纯文本。

  • VTT:Web视频文本轨道格式。

  • SRT:最常见的字幕格式。

  • TSV:制表符分隔值。

  • JSON:包含结构化数据,如逐词时间戳。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_13.png

我们的目标是获取每个单词的开始和结束时间,因此JSON格式是最佳选择。我们将修改命令,将输出格式从SRT改为JSON

# 修改输出格式参数
--output_format JSON

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_15.png


https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_17.png

生成并验证JSON输出

修改命令并重新运行后,我们成功获得了JSON格式的输出文件。该文件结构包含segments(段落),每个段落下又有words(单词)列表,每个单词都包含了text(文本)、start(开始时间)和end(结束时间)字段。

这证实了我们已成功获取逐词时间戳数据。接下来,我们将利用这些数据实现动态字幕高亮。


https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_19.png

实现动态字幕高亮(“卡拉OK”效果)

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_21.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_23.png

我们的业务用例是:播放音频时,根据JSON文件中的时间码,在屏幕上高亮显示对应的转录文本。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_25.png

技术实现思路是:使用FFmpeg生成一个视频,在视频上叠加文字,并依据时间码动态改变文字颜色以实现高亮效果。这本质上是一个“卡拉OK”歌词效果。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_27.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_29.png

我们创建了一个Python脚本(karaoke.py)来完成这个任务。脚本的核心逻辑是:

  1. 读取音频文件和对应的JSON转录文件。

  2. 使用FFmpeg命令,将音频与根据时间码生成的高亮字幕合成一个新视频。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_31.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_33.png

# 脚本核心部分:构建FFmpeg滤镜,根据时间码设置文字颜色
drawtext_filter = f"drawtext=text='{word_text}':x=(w-tw)/2:y=h-60:fontsize=40:fontcolor=white:enable='between(t,{start_time},{end_time})'"

首次运行脚本生成了视频,但字幕显示为方框。这是字体编码问题。WSL环境中默认字体可能不包含所需字符(本例中是日文字符)。

解决方案是安装支持相应字符的字体,例如“Noto Sans”字体族。安装并正确配置字体路径后,问题得以解决。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7923084fd2d2033_27.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_35.png


https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_37.png

处理逐词与逐字符的差异

成功运行后,我们发现高亮效果是基于字符而非单词。这是因为WhisperX对某些语言(如日语)的输出可能是按字符切分的。

为了实现更自然的单词级高亮,我们需要对原始的JSON数据进行后处理,将属于同一个单词的连续字符时间码合并。

我们通过提示调整,生成了一个新版本的JSON文件(wakeup_words.json),其中时间码对应的是完整的单词。

使用这个新的单词级JSON文件重新运行我们的“卡拉OK”脚本,最终得到了理想的动态高亮效果。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_39.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7923084fd2d2033_41.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_41.png

虽然在某些衔接处仍有微小延迟,但整体效果已经非常出色,成功实现了日语语音的逐词高亮显示。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_43.png


总结与展望

本节课中我们一起学习了如何利用WhisperX获取逐词转录的JSON数据,并利用FFmpeg和Python脚本实现了动态字幕高亮效果。我们解决了输出格式选择、字体渲染以及字符到单词的合并处理等问题。

这个成果非常令人兴奋,它为我们打开了多扇大门:

  • 教育应用:制作语言学习材料,高亮跟读。

  • 媒体增强:为视频创建交互式字幕。

  • 游戏开发:在游戏中实现实时对话字幕高亮。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9d664ef0e7899307d7922884fd2d2033_45.png

未来可以进一步优化,例如构建自动化管道来处理大量文件,或者集成翻译以创建双语高亮字幕。我们成功将强大的语音识别与灵活的媒体处理工具结合,实现了一个既实用又有趣的功能。

77:WhisperX匹配原始转录

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_1.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_2.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_4.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_6.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_7.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_9.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_11.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_13.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_15.png

概述

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_17.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_19.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_21.png

在本节课中,我们将探讨一个实际业务场景:如何将已有的、准确的视频字幕与WhisperX生成的逐字符时间戳进行匹配,从而获得逐词高亮功能。我们将学习如何获取原始字幕、处理音频、运行WhisperX,并最终使用大语言模型(LLM)将两者对齐。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_23.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_25.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_27.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_29.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_31.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_33.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_35.png

业务场景与挑战

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_37.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_38.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_40.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_42.png

上一节我们介绍了如何使用WhisperX生成逐字符的转录。本节中,我们来看看一个常见的业务需求。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_44.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_45.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_47.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_49.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_50.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_52.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_54.png

假设你运营一个语言学习平台,平台上的视频已经配备了准确的字幕。当用户观看视频时,你希望实现单词随播放高亮的效果。直接使用WhisperX等转录服务可能不够准确,但你已经拥有正确的转录文本。因此,核心挑战在于:如何将已有的准确字幕与WhisperX生成的、带有时间戳但不一定字符正确的转录进行匹配,从而获得逐词的时间信息。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_56.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_58.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_60.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_62.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_64.png

我们的思路是:利用LLM强大的理解和匹配能力,以准确字幕为“真相源”,从WhisperX的输出中提取对应的时间戳,最终生成一个结合了正确文本和精确时间码的新文件。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_66.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_68.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_70.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_72.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_74.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_76.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_78.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_80.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_82.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_84.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_86.png

第一步:准备数据源

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_88.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_90.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_92.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_94.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_96.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_98.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_100.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_102.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_104.png

为了测试这个流程,我们需要一个拥有准确字幕的视频源。以下是寻找和准备数据源的步骤。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_106.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_108.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_110.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_111.png

  1. 寻找可靠来源:我们选择了一个提供“可理解日语”教学视频的频道。这些视频通常配有精心制作的字幕,准确性高。

  2. 获取原始字幕:通过YouTube的字幕功能,我们可以下载视频的原始日文字幕文件(通常为.srt.vtt格式)。

    • 使用工具如 youtube-transcript-apiyt-dlp 可以自动化下载字幕。

    • 下载后,需要清理字幕文件中的时间码和序号,只保留纯文本。

    • 最终保存为 og_comic_learn.txt 作为我们的“准确转录源”。

  3. 下载并准备音频:为了运行WhisperX,我们需要视频的音频文件。

    • 使用 yt-dlp 下载最佳音质的音频,并转换为WAV格式。

    • 为确保WhisperX的最佳识别效果,建议使用特定的音频参数。根据经验,有效的配置如下:

      # 示例:使用 yt-dlp 和 ffmpeg 转换音频
      yt-dlp -x --audio-format wav --audio-quality 0 --postprocessor-args "-acodec pcm_s16le -ac 1 -ar 24000" -o "comic_learn.wav" <视频URL>
      
    • 关键参数解释:

      • -acodec pcm_s16le: 指定PCM 16位小端编码,这是WAV的标准格式。

      • -ac 1: 设置为单声道(Mono)。

      • -ar 24000: 设置采样率为24000 Hz。

    • 将处理好的音频保存为 comic_learn_standard.wav

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_113.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_115.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_117.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_119.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_121.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_123.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_124.png

第二步:使用WhisperX生成转录

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_126.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_128.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_130.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_132.png

现在,我们使用WhisperX来处理音频文件,生成带有逐字符时间戳的转录。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_134.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_136.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_138.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_140.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_142.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_144.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_146.png

  1. 运行WhisperX:通过Docker容器运行WhisperX,指定日语模型和大模型以获取更佳效果。

    docker run -it --gpus all -v $(pwd):/data whisperx/whisperx:latest --model large --language ja --output_format json --output_dir /data/output comic_learn_standard.wav
    
  2. 理解输出:WhisperX会生成一个JSON文件(例如 comic_learn_standard.json)。这个文件结构包含 segments(段落),每个段落下又有 words,而每个 word 实际上是由 chars(字符)数组构成,每个字符都带有 start(开始时间)和 end(结束时间)。这正是我们需要的“带有时间戳但不一定字符正确”的转录数据。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_148.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_150.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_151.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_153.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_154.png

注意:对于长音频文件,WhisperX内部会自动进行分块处理(例如每30秒一块)。我们的输出JSON包含了所有块的结果。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_156.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_158.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_160.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_162.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_164.png

第三步:使用LLM对齐转录

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_166.png

这是最核心的一步。我们将编写一个Python脚本,利用LLM(如OpenAI GPT-4或Claude)将准确的原始字幕与WhisperX的详细输出进行对齐。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_168.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_170.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_172.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_174.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_176.png

以下是实现对齐脚本的关键步骤和代码逻辑:

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_178.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_180.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_182.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_184.png

  1. 加载数据:读取原始字幕文件(og_comic_learn.txt)和WhisperX的JSON输出文件(comic_learn_standard.json)。

  2. 构建提示词(Prompt):设计一个清晰的系统提示词,指导LLM完成对齐任务。提示词需要阐明:

    • 角色:你是一个专业的日语语言对齐系统。

    • 输入

      • original_transcript: 准确的无时间码字幕文本。

      • whisperx_data: WhisperX生成的、包含逐字符时间戳的转录数据。

    • 任务:以 original_transcript 为真相源,从 whisperx_data 中找出每个词(或自然短语)对应的时间码(start, end)。专注于语音匹配,因为WhisperX可能发音正确但字符写错。

    • 输出格式:要求LLM严格输出指定格式的JSON,包含 words 列表,每个词有 text, start, end 字段。

    • 示例:提供一个简明的输入输出示例,帮助LLM理解任务。

  3. 调用LLM API:将构建好的提示词发送给LLM(例如OpenAI的ChatCompletion接口)。

  4. 解析与保存:解析LLM返回的JSON内容,并将其保存为最终的对齐文件(如 comic_learn_aligned.json)。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_186.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_188.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_190.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_192.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_194.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_196.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_198.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_200.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_202.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_204.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_206.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_208.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_209.png

以下是脚本的核心代码框架:

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_211.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_212.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_214.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_216.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_218.png

import json
import openai
from dotenv import load_dotenv
import os

load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")

def align_transcripts(original_path, whisperx_path, output_path):
    # 1. 加载数据
    with open(original_path, 'r', encoding='utf-8') as f:
        original_transcript = f.read()
    with open(whisperx_path, 'r', encoding='utf-8') as f:
        whisperx_data = json.load(f)

    # 2. 构建提示词
    system_prompt = """你是一个专业的日语语言对齐系统。你的任务是将准确的日语转录文本与WhisperX生成的时间戳进行逐词对齐。"""
    user_prompt = f"""
    原始准确转录(无时间码):
    ```text

    {original_transcript}

    ```

    WhisperX数据(包含字符级时间戳):
    ```json

    {json.dumps(whisperx_data, ensure_ascii=False)[:5000]} 

    ```

    请将原始转录中的每个词或自然短语,与WhisperX数据中的时间信息对齐。
    专注于语音匹配,因为字符可能不准确但发音正确。
    请输出一个JSON对象,包含一个“words”数组,每个元素有“text”(文本)、“start”(开始时间)、“end”(结束时间)字段。
    """

    # 3. 调用LLM
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt}
        ],
        temperature=0.1
    )

    # 4. 解析并保存结果
    aligned_content = response.choices[0].message.content
    # 尝试从返回内容中提取JSON
    aligned_json = json.loads(aligned_content)
    with open(output_path, 'w', encoding='utf-8') as f:
        json.dump(aligned_json, f, ensure_ascii=False, indent=2)
    print(f"对齐完成,结果已保存至:{output_path}")

if __name__ == "__main__":
    align_transcripts("og_comic_learn.txt", "comic_learn_standard.json", "comic_learn_aligned.json")

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_220.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_222.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_224.png

注意事项与优化

  • 上下文长度:如果音频很长,WhisperX的JSON数据可能非常大,会超出LLM的上下文窗口限制。解决方案包括:

    • 数据精简:删除JSON中不必要的字段(如score),缩写键名(如word->w, start->s),移除空格和换行。

    • 分块处理:将原始字幕和WhisperX数据都按段落分块,分别进行对齐,最后合并结果。

  • 提示词工程:提供清晰、具体的示例可以极大提高对齐质量。

  • 模型选择:不同的LLM(GPT-4, Claude, 本地模型)在成本和效果上各有权衡,可根据实际情况选择。

  • 错误处理:代码中应增加对LLM返回内容格式的校验和错误处理。

总结

本节课中,我们一起学习并实践了一个完整的解决方案,用于为已有准确字幕的视频生成逐词时间戳。

  1. 明确需求:语言学习平台需要将准确字幕与音频时间轴对齐,实现单词高亮。

  2. 数据准备:获取原始字幕,并下载、转换出适合WhisperX处理的音频文件。

  3. 语音识别:利用WhisperX生成带有精细时间戳(字符级)的转录文本。

  4. 智能对齐:通过精心设计的提示词,借助大语言模型(LLM)的理解与匹配能力,将准确字幕文本与WhisperX的时间信息进行对齐,最终输出一份结合了正确文本和精确时间码的JSON文件。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_226.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_228.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_229.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/1843eed7a517c2ec8e2624f004739853_231.png

这个过程展示了如何将传统的语音识别工具(WhisperX)与现代的大语言模型相结合,解决实际业务中“数据再加工”的难题。虽然在实际操作中可能会遇到上下文长度限制、提示词优化、成本控制等挑战,但整个技术路径是可行且强大的。

78:本地运行OmniGen的失败尝试 🚧

在本节课中,我们将跟随Andrew一起尝试在本地运行OmniGen模型,以实现对游戏角色图像的动态编辑。我们将学习如何设置环境、克隆项目、安装依赖,并最终尝试运行模型。虽然本次尝试因GPU内存不足而失败,但整个过程为我们提供了宝贵的实践经验。

大家好,我是Andrew,我们继续回到日语学习视觉小说游戏的开发中。

说实话,我在一天之内就做出了显著的改进。你可以去查看提交记录,但我不会展示所有代码。虽然记录整个过程会很有趣,但那可能会增加大约8个小时的工作量。我确实会坐下来,然后高效地编写代码。重点是,项目已经有了很多改进。

现在,当我点击进入这里,你会看到一个很酷的细菌开场动画。我在这里做了不少工作,但没有什么特别针对性的内容。再次播放时,当你点击播放按钮然后停止,它会执行相应操作。所以重点是,音频已经集成到这里了。我添加了那个漂亮的开场着色器,并优化了一些系统,使得编码和操作更加容易。这只是对这个应用程序进行的大量整理工作。

但我意识到,我们可以用这些图像做一些非常有趣的事情。之前我只打算在这里使用单张静态图像。然后我想到,对了,还有OmniGen。我听说过OmniGen,也稍微用过一点,但从未在本地运行过。我非常希望能在本地运行它,这样我们就不必设置ComfyUI,而是可以直接运行单个模型并输入内容,让它为我们生成输出。我想看看我们是否能成功运行它。我也希望探索其他一些选项。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_1.png

所以,我先把这个放到一边。

探索其他图像处理工具 🛠️

市面上还有另一个工具叫PokerFace。即使我们不使用OmniGen,也可以试试Hugging Face上的PokerFace。我来展示一下这个东西。

这个工具的功能,我们上传一张图片后,你立刻就能看到它的威力。我们就用我自己来举个例子吧。这是我之前制作缩略图时拍的照片,这显然是我。现在我的脸看起来有点奇怪。但如果你看这里,它把我的脸模糊了一点。实际上我可以点击这里,看,我可以移动我的头部。现在,它对我的脸做了一些有点奇怪的处理,因为这显然不完全是我。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_3.png

我们可以在这里进行调整,我想我们可以改变。后面的标记点,比如我们可以把嘴巴张得更大一点。操作有点棘手。我只是点击并移动。好了,我在这方面不是很擅长。你看这里,操作点击的方式不是很明确。但重点是,你显然可以做一些事情。哦,那个效果挺酷的。我到这里了。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_5.png

所以,我们肯定有办法移动嘴巴。这将是我们让角色活起来并呈现不同状态的一种方式。但这些并不是表情。这有点意思,我喜欢这个功能。

尝试本地运行OmniGen模型 💻

但我也想尝试本地运行这个模型。我从未在本地运行过它。使用这些模型的关键在于,它是在什么数据上训练的?这始终是个问题。但我要继续,克隆这个仓库,看看能否在本地让它运行起来。

我没有把它克隆到特定的项目里,也没有克隆到“Free GenAI Bootcamp”这里。我们只是把它克隆到外部目录。我们回到这里,执行 git clone 命令。我们试试看能否让它运行起来,因为我认为利用它会非常令人兴奋。现在有一些服务提供这个模型,但需要付费。因为我拥有GPU,所以我想尝试利用它们。既然我能在ComfyUI中生成图像,为什么不能利用它呢?当然,使用ComfyUI你会有更多控制权,那是一个允许你下载和使用模型的工作流工具,能让你对处理流程有更多控制。而这个模型声称,你只需要给它一张图片、几张图片和文本,它就应该能工作。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_7.png

现在我已经下载好了,我将在VS Code中打开它。我们回到上一级目录,然后进入“omnigen”文件夹。好的,我们给它一点时间。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_9.png

我信任这个项目。这里面应该有运行的方法。我们这里有这个文件。然后我们还有演示文件。我不确定为什么这个文件这么大,但我们还是打开它。上面写着:如果遇到内存不足或时间成本问题,可以使用 offload_model=True。好的,这里有一个演示。它在做什么?它在改变饮料,改变杯子。也许我应该先尝试运行这个,看看能否使用它。我应该为这个项目创建一个独立的环境,因为如果不使用独立环境,我们可能会遇到问题,我需要去创建一个。

我要去我的GitHub仓库,因为我记不清具体的指令了。让我们看看README文件。它有没有说明如何创建环境?没有,它不会提供那么基础的信息。

我要去我的“GenAI Essentials”项目。回到“ExamPro Code”目录下的“GenAI Essentials”。在这里,我们有如何设置本地开发环境的说明。我们进入这里,然后进入“conda”目录,在这里找到“setup.md”文件。我想要的是类似这样的内容。现在,我要选择一个版本,我一直用Python 3.11,这次我想用稍微新一点的,就用3.11吧。在这里,我想复制这个命令。这是为OmniGen准备的。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_11.png

好的,我们继续。我输入了命令,但可能没有安装那个叫“ipykernel”的东西。不过没关系,我们马上修复它。我已经创建了环境,现在激活它。我们需要安装ipykernel。我总是拼错。我们就用这种方式来安装。

这将会使用conda-forge来获取并安装。我们给它一点时间。它要求确认,我们输入“yes”。现在好了,我们回到VS Code这里。我回到演示文件,选择“无论如何打开”,我不在乎它文件很大。然后我们选择内核,选择Python环境。我在找“omnigen”,找到了。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_13.png

上面写着:如果遇到内存不足、时间或成本问题,可以使用 offload_model=True。唯一的问题是,我的GPU正在被这个视频录制共享。我几乎想停止录制视频,但我们看看会发生什么。我们继续运行这个单元格。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_15.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_17.png

我想看看会发生什么。我要看看如果我暂停视频,我的GPU使用率是否会下降。我马上回来,我可以直接点击暂停。

确实,视频编码的负载下降了。这里的GPU使用率也下来了一点,但不足以解决问题。我们回到这里看看。我们看到它正在运行第一行代码,但已经出错了。问题是什么?导入torch失败,没有torch模块。如果他们能提供完整的运行说明就好了。

这里有“setup.py”文件。为什么它没起作用?好吧,我们看看README文件,也许“快速开始”部分有说明。执行 pip install -e .。好的,我们执行这个命令。这将会安装“-e”所代表的当前本地环境。看起来它说也可以创建一个新环境,这正是我做的。哦,它本可以自动完成这一步。它使用的是Python 3.10,而我用的是3.11。希望这没问题。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_19.png

安装指定CUDA版本的PyTorch。要是我在创建环境前知道这个就好了,所以我们执行这个命令。我们可能需要安装特定版本的PyTorch和CUDA。我们看看会发生什么,也许这会失败,我们就删除这个环境。但如果有必要,这也不是什么大问题。我们等待安装完成。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_21.png

好的,它说那些依赖已经安装好了。我们可能还是需要使用非常特定的版本,甚至需要指定非常特定的PyTorch版本来确保一切正常。即使在这里,他们也是在这之前做的,也许我们也必须这样做。我喜欢他们在这里展示的实现方式。我们回到演示文件这里,我们已经选择了Python 3.11环境。我们继续再试一次,选择GPU来运行OmniGen。我想我们确实需要这样做。也许它会自动选择。我们可以看看发生了什么。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_23.png

目前还没有变化。但我想,它现在所做的就是下载模型。它正在下载模型。下载完成后,我们才能进行其他操作。我们趁它还在下载时,先看看后面的内容。这里,他们读取图像并描述它是什么。

好的,这里。她戴着精致的耳环……看起来他们像是在创建图像,但实际上他们是在描述图像的内容。哦,不,这实际上是生成了一张图像。所以这实际上是生成了这张图像。

我的意思是,图像有点平滑,但对于我们的游戏来说,如果能有这样的效果,那也很好了。我们往下看。现在,他们说:我们的模型可以同时执行多个编辑命令。这里,移除女性的耳环,将杯子替换为装满闪闪发光的冰可乐的透明玻璃杯。我们输入这个提示词。如果你想生成由OmniGen文本到图像生成的图像,如果种子不同,你必须使用种子。好的,我们到这里。然后,哦,这是输入图像。这是输出图像。看起来有点奇怪,杯子像是悬浮在桌子上,尽管它投下了阴影。所以,如果这位女士口渴了,她应该拿什么?在图像中找到并用蓝色高亮显示。

哦,那里发生了什么?哦,也许那是原始源图像。是的,他们使用的不是同一杯酒。那个有点变形,但它还在那里。并且用蓝色高亮显示了它,好的。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_25.png

接下来我们有什么?在这里检测人体骨架。我们得到了这个骨架,然后得到了那个骨架。使用以下图片作为条件生成一张新照片。

我们输入骨架,得到了这个,我认为这很酷。回到这里,看起来我们的GPU……我不知道。它一直很稳定,可能还在下载模型。

这里,遵循这张图像的姿势,生成一张年轻男孩坐在沙发上的新照片。

好的,它匹配了姿势,这很好。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_27.png

一位教授和一个男孩一起读书。这里展示了我们识别、描述多个人物主体并生成他们的能力。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_29.png

好的,我的意思是,这些看起来都是我们之前看到的例子。但我想看看我们能否让这个模型运行起来。

因为如果我们能让它运行,下一步就会顺利很多。下载这个模型花了很长时间。我不确定它下载了多少东西,但你必须等待相当长的时间。所以我要在这里暂停,直到它100%下载完成。

运行模型与遭遇内存错误 💥

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_31.png

模型下载完成了。我们来看看接下来能做什么。我们试试看能否运行它。我好奇的是,它是在使用CPU还是GPU。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_33.png

它最初在使用CPU。但现在我们看到它开始使用GPU了。它正在占用所有专用内存。它一直在占用,看起来……

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_35.png

它耗尽了我们的专用内存。我们看看这里。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_37.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_39.png

所以,这里显示:无法解包不可迭代的None类型对象。这是什么意思?我不知道这里想说什么,无法解包不可迭代的None类型对象。在图像这里。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_41.png

我们是不是忘了运行什么?我不这么认为。这肯定是一个类型错误。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_43.png

我不完全确定问题出在哪里,但我们会去查一下。也许可以去问问Claude,但我可能用完了所有额度。你们可能从没想过我会用完额度,但我确实用完了。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_45.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_47.png

我们继续运行这个。嗯。这可能与Python版本有关吗?或者CUDA?

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_49.png

当GPU内存耗尽时,操作有时会返回None。所以我认为这就是我们遇到的问题。我们看到专用内存现在达到了最大值。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_51.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_53.png

这里没有可用的内存了。我甚至不确定此时如何恢复内存。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_55.png

看这里,使用率飙升然后下降了。但我想,我可能不得不停止这个视频录制,因为我无法真正运行它。但至少你看到了我们可以尝试去做。也许如果我能解决这个问题,我可以做一个后续视频。但我想,在本地我们目前只能做到这里了。

我要停止这个,然后看看能否恢复我的内存。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_57.png

总结 📝

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/f93f82498a41258ade8590673127bc21_59.png

本节课中,我们一起尝试了在本地运行OmniGen模型来编辑游戏角色图像。我们首先探索了PokerFace等替代工具,然后详细介绍了为OmniGen设置独立Python环境、安装依赖(如特定版本的PyTorch和CUDA)以及克隆项目的过程。在尝试运行模型时,我们遇到了GPU内存不足的关键错误,导致模型无法成功执行。这次尝试虽然失败了,但它揭示了在本地运行大型生成式AI模型时可能遇到的实际资源限制问题,并为我们后续探索云端解决方案或优化本地配置提供了宝贵的经验教训。

79:在Lightning AI和Replicate上运行OmniGen 🚀

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_1.png

在本节课中,我们将学习如何在本地资源不足的情况下,利用云端平台(Lightning AI和Replicate)来运行OmniGen模型。我们将探索环境配置、依赖安装、模型运行以及处理常见错误的完整流程。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_3.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_4.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_6.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_7.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_9.png

概述

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_11.png

OmniGen是一个强大的生成式AI模型,但其运行对计算资源(尤其是GPU显存)要求较高。当本地计算机资源不足时,我们可以转向云端计算平台。本节将演示如何在Lightning AI Studio中设置环境并尝试运行OmniGen,同时也会介绍使用Replicate这一托管服务的备选方案。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_13.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_15.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_17.png

尝试在Lightning AI上运行

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_19.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_20.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_22.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_23.png

上一节我们遇到了本地运行OmniGen的显存限制。本节中,我们来看看如何利用云端GPU资源来绕过这个限制。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_25.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_27.png

我尝试重启电脑并清空显存,但发现关闭Visual Studio Code或某些浏览器能释放被占用的内存。查阅OmniGen的要求后,我发现它可能需要约40GB的显存,这超出了我本地显卡的能力范围。因此,我决定尝试使用Lightning AI平台。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_29.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_31.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_33.png

登录并创建工作室

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_34.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_36.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_37.png

首先,登录Lightning AI平台。平台界面会显示可用的计算资源。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_39.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_40.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_1.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_42.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_44.png

我搜索了OmniGen,发现平台上已有用户创建的相关模板,这简化了我们的启动过程。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_46.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_48.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_50.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_51.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_53.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_6.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_7.png

接下来,我创建了一个新的工作室,将其命名为“OmniGen-Test”,并选择了GPU实例。平台清晰地显示了每种实例的每小时成本。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_55.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_57.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_9.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_59.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_61.png

我选择了配备1个T4 GPU的最低配置,确认并启动了环境。启动过程需要一些时间。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_63.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_65.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_67.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_69.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_11.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_71.png

配置环境与安装依赖

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_73.png

环境启动后,我们需要配置正确的PyTorch版本。通过nvidia-smi命令,我查看到当前CUDA版本为12.2。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_75.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_77.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_19.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_79.png

根据OmniGen仓库的requirements.txt文件,我们需要安装特定版本的库。以下是关键步骤:

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_81.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_83.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_85.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_87.png

  1. 克隆仓库

    git clone <OmniGen仓库地址>
    
  2. 创建Conda环境(可选):平台已有默认环境,我们直接在其中操作。检查Python版本为3.10,符合要求。

  3. 安装依赖:根据requirements.txt安装所有包。注意,平台可能已预装部分包。

    pip install -r requirements.txt
    
  4. 特别注意Transformers版本:OmniGen可能需要transformers==4.45.2。如果已安装更高版本,需降级:

    pip install transformers==4.45.2
    

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_89.png

运行模型与遇到的错误

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_91.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_92.png

依赖安装完成后,我尝试运行OmniGen提供的示例代码来生成图像。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_94.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_95.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_46.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_97.png

模型开始下载并加载,但随后遇到了一个错误:unpacked non-iterable None type。这与之前在本地遇到的错误相同。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_99.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_53.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_101.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_102.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_104.png

检查系统资源监控,发现GPU显存使用量约为11GB,并未超出T4显卡的16GB限制,因此问题可能不在资源上。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_106.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_108.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_110.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_101.png

排查与解决尝试

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_112.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_114.png

根据社区讨论,这个错误可能与库版本冲突有关。以下是尝试的解决方案:

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_116.png

  1. 确保版本完全匹配:再次检查并强制安装requirements.txt中指定的所有版本,尤其是transformers

  2. 使用pip install -e .方式安装:进入克隆的仓库目录,执行此命令以“可编辑模式”安装,确保依赖解析正确。

    cd omnigen
    pip install -e .
    
  3. 重启内核并清理输出:在Jupyter或VS Code中重启内核,然后再次运行代码。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_118.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_119.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_120.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_122.png

尽管进行了多次尝试,错误依然存在。这表明在Lightning AI的这个特定环境或配置下运行OmniGen存在兼容性问题。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_124.png

使用Replicate托管服务

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_126.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_128.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_130.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_132.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_133.png

由于在Lightning AI上自行配置遇到障碍,我们转向更简单的方案——使用Replicate。Replicate提供了预配置的OmniGen模型,只需通过API或Web界面即可调用,无需管理环境。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_135.png

以下是使用Replicate的核心步骤:

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_137.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_139.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_141.png

  1. 访问OmniGen模型页面:在Replicate上找到OmniGen模型。

  2. 配置输入:上传源图像,并编写修改提示(prompt)。例如:

    • Remove the woman‘s earrings.

    • Change the white sweater to a black band T-shirt.

    • Add a nose piercing and neck tattoo.

    • Change the book for a Nintendo Switch.

  3. 运行并等待:提交任务后,Replicate会处理请求。首次运行(冷启动)可能较慢,后续运行(热启动)会更快。平台会显示预估时间和成本(本例中约为0.14美元)。

  4. 获取结果:任务完成后,可以直接下载生成的图像。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_143.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_144.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_146.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_118.png

通过Replicate界面提交生成任务

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_147.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_149.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_150.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_152.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_154.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_184.png

生成结果保存至指定目录

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_156.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_157.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_158.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_160.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_162.png

性能考量与硬件对比

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_164.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_166.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_168.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_170.png

在等待模型运行的过程中,我们对比了不同GPU的性能,这对于选择计算平台很重要:

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_172.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_174.png

  • 本地RTX 4060:拥有8GB显存,性能足够但显存可能成为运行大模型的瓶颈。

  • 云端T4(Lightning AI):拥有16GB显存,显存更大但计算性能可能略低于消费级显卡。

  • 云端L40S(Replicate):拥有48GB显存,适合需要大显存的模型,但单任务成本较高。

  • 云端H100:顶级计算卡,性能远超上述显卡,但获取成本和等待时间也最高。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_176.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_178.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_180.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_182.png

选择平台时,需要在成本可用性(等待时间)、显存大小计算速度之间做出权衡。对于实验和原型开发,Replicate这类按需付费的托管服务通常更便捷。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_184.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_186.png

总结

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_187.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_189.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_190.png

本节课中我们一起学习了当本地资源不足时运行OmniGen模型的两种云端方案。

我们首先尝试在Lightning AI上自行配置环境,经历了环境创建、依赖安装和版本调试的全过程,虽然最终因特定兼容性错误未能成功,但熟悉了在云端开发环境中操作的工作流。

随后,我们转向了更简单直接的Replicate托管服务。通过其Web界面,我们轻松上传图片、输入修改指令并支付少量费用后获得了生成结果,这体现了托管服务“开箱即用”的优势。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/9af27a1fc0d942b7f7eb6cb20e932c14_192.png

核心收获是:对于复杂的开源模型,自行部署能提供最大的灵活性和控制力,但会面临环境配置和调试的挑战;而使用托管服务则能极大降低入门门槛,快速验证想法,适合大多数应用场景和初学者。你可以根据项目需求、预算和技术能力来选择最合适的路径。

80:最终项目提交指南 📝

在本节课中,我们将详细介绍如何填写最终项目提交表单,以确保您获得期望的等级和徽章。正确填写此表单是获得高级别徽章(如金色或红色小队徽章)的必要步骤。

表单提交概述

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_1.png

首先,请注意表单的提交时间窗口。该窗口在GenAI训练营网站上有明确规定。您必须在规定时间内提交。如果您错过了截止日期,能否提交取决于我们是否开启新的训练营批次或提供训练营后支持。目前,我们无法对此做出任何承诺。因此,请务必核对日期。如果超出截止日期,您将无法提交,也可能无法获得期望的等级。

某些徽章无论您是否提交都会发放。但对于最高级别的徽章,如金色或红色小队徽章,您必须按时并尽可能完善地填写此最终提交表单。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_3.png

表单填写详解与注意事项

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_4.png

上一节我们强调了提交时间的重要性,本节中我们来看看表单的具体内容和填写要点。

您会注意到,最终提交表单位于其独立的章节中。这里有一个关键点:一旦提交,您将无法更改。如果您需要先保存草稿,请务必选择“草稿”状态。但请注意,不要忘记最终提交。有些人会忘记返回并提交,误以为草稿状态就是已提交。请确认状态显示为“已提交”。您可以返回页面,进行强制刷新以确保提交成功。

接下来,我将围绕表单的各个部分进行说明,以便您清楚了解我的评估标准。

1. GitHub仓库链接

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_6.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_7.png

以下是需要填写的第一个核心信息。

  • GitHub URL:请提供您项目仓库的链接。我每周都要求提交此链接,本周再次明确要求,是因为系统设计的原因,我只能看到当前表单中的信息。为了我能快速、方便地找到您的仓库,请务必在此处填写。

  • Discord用户名:如果您在Discord社区中活跃,请将您的Discord用户名也填写在此处。虽然系统其他地方可能已有记录,但为了便于我交叉参考您的社区活动(这可能会为您赢得加分,甚至帮助您进入红色小队等级),请在此填写。

2. 目标等级选择

请选择您希望获得的最终等级。这些等级与评分标准相对应。

我们没有像之前的训练营那样制定复杂的评分细则。原因在于,生成式AI技术是全新的领域,我很难建立一个统一的基线。因此,评分将主要基于整体情况(on a curve)。我已经批阅了足够多的周度提交(例如第四周或第五周),对大家的水平分布有了大致了解。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_9.png

请明确告知您的目标等级。如果您目标是金色小队但未达到红色小队标准,这将使我的评分工作更高效,我不需要过多斟酌。这主要是为了简化我的工作流程。请记住,如果您不提交此表单,您将无法获得金色或红色小队徽章。所以,请清晰说明您的目标。

3. 项目总结:未完成与已完成的部分

这部分是您展示学习过程和成果的关键。

首先,总结您未能完成的技术目标。 列出您计划实现但未能完成的技术事项。这没有关系。只要您有良好的文档记录,并展示了在此过程中学到的东西,就不会影响评分。

当我提到“学到的东西”时,不仅仅指泛泛的概念(例如“我学会了什么是数据库”或“什么是LLM”)。从公司视角看,这些价值有限。真正有价值的是技术上的不确定性——那些连公司内部或网上都难以找到明确答案的问题。例如,如果您使用一个冷门的古希腊语模型,并尝试对其进行微调,但相关文档匮乏,您在此过程中克服的困难就属于技术不确定性。反之,如果只是其他人都会而您暂时不会的常见问题,那更多是技能缺口,不属于这里需要强调的“不确定性”。在撰写时,请确保内容直击要点、简洁明了,方便我快速阅读和评估。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_11.png

其次,总结您成功实现的技术目标。 列出您克服了技术不确定性并最终实现的事项。同样,如果这些是大家都能轻松完成的常规任务,则无需报告。请重点报告那些对您的项目而言独特且有挑战性的成就。

有些学员可能没有专注于极具不确定性的研究任务,而是跟随课程构建一个完整的项目并思考其商业用例。这也是很好的方向。请告诉我您的侧重点是什么。例如,您可以思考:这个方案能扩展到实际规模吗?它真的能解决这个真实业务问题吗?如果您无法深入研发部分,可以侧重展示对业务层面的思考。

4. 其他说明与考虑因素

这是一个自由文本框,用于提供您希望我知悉的任何额外信息。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_13.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_14.png

  • 考虑因素与特殊情况:如果您有任何希望我考虑的情况,例如时间安排上的困难、遇到的特殊挑战等,请在此框中说明。

  • 内容长度建议:请尽力填写,但避免内容过长。文本框的大小暗示了信息量的预期。如果内容超过框体两倍以上,可能就过多了。请保持总结的简洁性。更详细的信息应该放在您的GitHub仓库中,并且要让我易于查找。

提交策略与反馈

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_16.png

关于何时提交此表单,这取决于您自己。我在Discord中提过,某些周次的作业我需要批阅数千份,而后面周次的则只有数百份。如果您能及时完成后续周次作业,应该会获得评分。如果您进度严重落后,则存在风险。请务必在截止日期前提交。

如果您想稍作等待,先获取我之前作业的反馈再决定,这由您自行决定。本次训练营难度极高,主要由我(Andrew Brown)和另一位Andrew负责批阅,我们正在尽力为每个人提供反馈。

重要承诺:任何提交了最终表单并完成了所有周次作业的学员,都将获得反馈——特别是视频反馈,而不仅仅是文字点评。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_18.png

课程总结

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_19.png

本节课中,我们一起学习了最终项目提交表单的详细填写指南。我们强调了提交时限、表单各部分的填写要点(如GitHub链接、目标等级、技术总结),以及提供简洁、有价值信息的重要性。请确保在截止日期前完整提交表单,并祝您好运。希望大家在训练营中度过了一段愉快且收获满满的时光。我期待看到每个人的最终成果!

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_21.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_22.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/exampro-genai-bc/img/fa647cf3258653a17c4f630fa7a98306_23.png

加油!🚀

Logo

AtomGit AI 社区提供模型库、数据集、Agent、Token等资源

更多推荐