提问人:Thanh Nguyen 提问时间:11/11/2023 更新时间:11/11/2023 访问量:22
我在Gradio界面中的fn函数不断回调并变成循环
My fn Function in Gradio interface keep calling back and turn into a loop
问:
当我尝试在 Google Colab 上使用 gradio 界面运行我的代码时,界面中的 fn 函数不断回调自身,输出没有显示视频,但保持处理状态
我希望输出能够显示保存在 Colab 文件夹中的视频。我尝试运行它,它可以将视频保存到带有imageio.mimsave的colab文件夹,但它没有显示视频并再次调用fn函数。 这是我的代码:
import os
import random
import tempfile
import gradio as gr
import imageio
import numpy as np
import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
from direct2v_zeroshot import DirecT2VPipeline
model_id = "runwayml/stable-diffusion-v1-5"
pipe_ours = DirecT2VPipeline.from_pretrained(model_id, torch_dtype=torch.float16,variant='fp16').to('cuda')
pipe_ours.enable_model_cpu_offload()
pipe_ours.enable_vae_slicing()
def generate(prompts: str) -> str:
a = prompts
print(a)
pros = [
"A corgi is running on a grassy field, its ears flopping as it moves.",
"The corgi continues running, a second corgi starts to appear in the background.",
"The second corgi starts to run, playfully chasing the first corgi.",
"The first corgi maintains its pace, the second corgi getting closer.",
"Both corgis are running side by side, their short legs moving quickly.",
"The second corgi starts to take the lead, the first corgi following closely.",
"Both corgis continue running, their tails wagging happily as they race.",
"The first corgi begins to catch up, the two corgis running neck and neck.",
]
frames = pipe_ours(prompt=pros, generator=torch.Generator('cuda').manual_seed(0)).images
result = [(r * 255).astype("uint8") for r in frames]
return to_video(result, 4)
def to_video(frames: list[np.ndarray], fps: int) -> str:
out_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
writer = imageio.get_writer(out_file.name, format='FFMPEG', fps=fps)
for frame in frames:
writer.append_data(frame)
writer.close()
imageio.mimsave("video_ours.mp4", frames, fps=4)
return out_file.name
genai_app = gr.Interface(fn=generate,
inputs=[gr.Text()],
outputs=[gr.Video()],
title="Generate Video",
description=None,
)
if __name__ == "__main__":
genai_app.launch(share=True, debug=True)
答: 暂无答案
评论