Traceback (most recent call last):
File "text-to-video.py", line 17, in
video_path = output[OutputKeys.OUTPUT_VIDEO_PATH]
AttributeError: type object 'OutputKeys' has no attribute 'OUTPUT_VIDEO_PATH'
import subprocess
import sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "datasets==2.16.1", "-i", "https://mirrors.aliyun.com/pypi/simple"])
import os
from modelscope.pipelines import pipeline
from modelscope.outputs import OutputKeys
pipeline = pipeline("text-to-video-synthesis", "damo/text-to-video-synthesis", device="cpu")
prompts = [
"A cat walking in a garden under sunlight, 0-6 seconds",
"A cat walking in a garden under sunlight, 6-12 seconds",
"A cat walking in a garden under sunlight, 12-18 seconds",
"A cat walking in a garden under sunlight, 18-24 seconds",
"A cat walking in a garden under sunlight, 24-30 seconds"
]
for i, prompt in enumerate(prompts):
output = pipeline(prompt, num_frames=48, fps=8)
video_path = output[OutputKeys.OUTPUT_VIDEO_PATH]
os.rename(video_path, f"/app/output/video_{i}.mp4")
print(f"Video saved to /app/output/video_{i}.mp4")
AttributeError: type object 'OutputKeys' has no attribute 'OUTPUT_VIDEO_PATH'
以及之前的错误日志(如 PyAV 缺失、attn_mask 形状问题、CUDA 相关问题),可以确认当前问题出在 modelscope.outputs.OutputKeys.OUTPUT_VIDEO_PATH 不存在,导致代码在以下行报错:
python
CollapseWrapRun
Copy
video_path = output[OutputKeys.OUTPUT_VIDEO_PATH]
这表明你使用的 modelscope 版本中,OutputKeys 类没有定义 OUTPUT_VIDEO_PATH 属性,可能是因为:
以下是问题的详细分析和解决步骤,基于你的代码和上下文。
python
CollapseWrapRun
Copyoutput = pipeline(prompt, num_frames=48, fps=8) video_path = output[OutputKeys.OUTPUT_VIDEO_PATH]
打印 pipeline 的输出字典,确认实际键名。
python
CollapseWrapRun
Copyimport subprocess import sys subprocess.check_call([sys.executable, "-m", "pip", "install", "datasets==2.16.1", "-i", "https://mirrors.aliyun.com/pypi/simple"]) import os from modelscope.pipelines import pipeline from modelscope.outputs import OutputKeys pipeline = pipeline("text-to-video-synthesis", "damo/text-to-video-synthesis", device="cpu") prompts = [ "A cat walking in a garden under sunlight, 0-6 seconds", "A cat walking in a garden under sunlight, 6-12 seconds", "A cat walking in a garden under sunlight, 12-18 seconds", "A cat walking in a garden under sunlight, 18-24 seconds", "A cat walking in a garden under sunlight, 24-30 seconds" ] for i, prompt in enumerate(prompts): output = pipeline(prompt, num_frames=48, fps=8) print(f"Pipeline output: {output}") # 添加调试打印 video_path = output[OutputKeys.OUTPUT_VIDEO_PATH] os.rename(video_path, f"/app/output/video_{i}.mp4") print(f"Video saved to /app/output/video_{i}.mp4")
bash
CollapseWrapRun
Copypython text-to-video.py
text
CollapseWrap
CopyPipeline output: {'video_path': '/tmp/video.mp4'}
python
CollapseWrapRun
Copyvideo_path = output['video_path']
查看 OutputKeys 的可用属性,找到正确的键名。
bash
CollapseWrapRun
Copypython -c "from modelscope.outputs import OutputKeys; print(dir(OutputKeys))"
python
CollapseWrapRun
Copyvideo_path = output[OutputKeys.VIDEO_PATH]
直接使用字符串键名,假设常见的键名。
python
CollapseWrapRun
Copyvideo_path = output[OutputKeys.OUTPUT_VIDEO_PATH]
python
CollapseWrapRun
Copyvideo_path = output['output_video_path'] # 尝试小写
python
CollapseWrapRun
Copyvideo_path = output['video_path'] video_path = output['output_video']
python
CollapseWrapRun
Copyimport subprocess import sys subprocess.check_call([sys.executable, "-m", "pip", "install", "datasets==2.16.1", "-i", "https://mirrors.aliyun.com/pypi/simple"]) import os from modelscope.pipelines import pipeline from modelscope.outputs import OutputKeys pipeline = pipeline("text-to-video-synthesis", "damo/text-to-video-synthesis", device="cpu") prompts = [ "A cat walking in a garden under sunlight, 0-6 seconds", "A cat walking in a garden under sunlight, 6-12 seconds", "A cat walking in a garden under sunlight, 12-18 seconds", "A cat walking in a garden under sunlight, 18-24 seconds", "A cat walking in a garden under sunlight, 24-30 seconds" ] for i, prompt in enumerate(prompts): output = pipeline(prompt, num_frames=48, fps=8) video_path = output['output_video_path'] # 替换为假设键名 os.rename(video_path, f"/app/output/video_{i}.mp4") print(f"Video saved to /app/output/video_{i}.mp4")
bash
CollapseWrapRun
Copypython text-to-video.py
你的代码直接传递 prompt(字符串)到 pipeline,而 text-to-video-synthesis 管道可能期望字典输入 {"text": prompt}。这可能影响输出结构。
python
CollapseWrapRun
Copyoutput = pipeline(prompt, num_frames=48, fps=8)
python
CollapseWrapRun
Copyoutput = pipeline({"text": prompt}, num_frames=48, fps=8)
python
CollapseWrapRun
Copyimport subprocess import sys subprocess.check_call([sys.executable, "-m", "pip", "install", "datasets==2.16.1", "-i", "https://mirrors.aliyun.com/pypi/simple"]) import os from modelscope.pipelines import pipeline from modelscope.outputs import OutputKeys pipeline = pipeline("text-to-video-synthesis", "damo/text-to-video-synthesis", device="cpu") prompts = [ "A cat walking in a garden under sunlight, 0-6 seconds", "A cat walking in a garden under sunlight, 6-12 seconds", "A cat walking in a garden under sunlight, 12-18 seconds", "A cat walking in a garden under sunlight, 18-24 seconds", "A cat walking in a garden under sunlight, 24-30 seconds" ] for i, prompt in enumerate(prompts): output = pipeline({"text": prompt}, num_frames=48, fps=8) print(f"Pipeline output: {output}") # 调试 video_path = output['output_video_path'] # 替换为假设键名 os.rename(video_path, f"/app/output/video_{i}.mp4") print(f"Video saved to /app/output/video_{i}.mp4")
bash
CollapseWrapRun
Copypython text-to-video.py
尝试更新 modelscope 到最新版本,或安装支持 OUTPUT_VIDEO_PATH 的版本。
bash
CollapseWrapRun
Copypip install --upgrade modelscope -i https://mirrors.aliyun.com/pypi/simple --root-user-action=ignore
bash
CollapseWrapRun
Copypip show modelscope
bash
CollapseWrapRun
Copypython text-to-video.py
bash
CollapseWrapRun
Copypip install modelscope==1.7.0 -i https://mirrors.aliyun.com/pypi/simple --root-user-action=ignore
查看 text_to_video_synthesis_pipeline.py 的 postprocess 方法,确认输出键名。
bash
CollapseWrapRun
Copynano /usr/local/lib/python3.8/dist-packages/modelscope/pipelines/multi_modal/text_to_video_synthesis_pipeline.py
python
CollapseWrapRun
Copyreturn {'video_path': video_path}
dockerfile
CollapseWrap
CopyRUN pip install av modelscope -i https://mirrors.aliyun.com/pypi/simple --root-user-action=ignore RUN sed -i 's/OutputKeys.OUTPUT_VIDEO_PATH/"output_video_path"/' /app/text-to-video.py RUN sed -i 's/pipeline(prompt/pipeline({"text": prompt}/' /app/text-to-video.py
bash
CollapseWrapRun
Copydocker build -t your_image_name .
bash
CollapseWrapRun
Copypip show modelscope torch torchvision av
bash
CollapseWrapRun
Copysed -i 's/with amp.autocast(enabled=True):/with torch.amp.autocast(device_type='"'"'cpu'"'"', enabled=True):/' /usr/local/lib/python3.8/dist-packages/modelscope/models/multi_modal/video_synthesis/text_to_video_synthesis_model.py
请提供:
bash
CollapseWrapRun
Copypip show modelscope