File size: 3,080 Bytes
15c46ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Space entry point, with native media components and explicit runtime status."""
import json
import os
import traceback
os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False")
import gradio as gr
from engine import ENGINE, runtime_status


def predict(prompt, image, video, audio, steps, frames, seconds, seed):
    try:
        result = ENGINE.run(prompt, image, video, audio, steps, frames, seconds, seed)
        return result["text"], result["images"], result["video"], result["audio"], result["files"], runtime_status()
    except Exception as exc:
        traceback.print_exc()
        raise gr.Error(str(exc))


with gr.Blocks(title="NExT-GPT 课程复现") as demo:
    gr.Markdown("# NExT-GPT 多模态复现\n基于官方 `7b_tiva_v0` 权重。上传图片、视频或音频,并用文字提出要求。")
    gr.Markdown("首次推理需要加载模型;只有成功执行推理后,状态中的 `inference_verified` 才会变为 true。")
    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(label="文字指令", lines=3,
                                placeholder="Describe this image and generate a matching sound.")
            with gr.Tab("图片"):
                image = gr.Image(type="filepath", label="输入图片")
            with gr.Tab("视频"):
                video = gr.Video(label="输入视频")
            with gr.Tab("音频"):
                audio = gr.Audio(type="filepath", label="输入音频")
            with gr.Accordion("生成参数", open=False):
                steps = gr.Slider(10, 50, value=30, step=1, label="扩散步数")
                frames = gr.Dropdown([16, 24, 32], value=16, label="视频帧数")
                seconds = gr.Slider(1, 9, value=5, step=1, label="音频秒数")
                seed = gr.Number(value=13, precision=0, label="随机种子")
            run = gr.Button("运行 NExT-GPT", variant="primary")
        with gr.Column():
            text = gr.Textbox(label="文字输出", lines=8)
            images = gr.Gallery(label="生成图片")
            out_video = gr.Video(label="生成视频")
            out_audio = gr.Audio(label="生成音频")
            files = gr.File(file_count="multiple", label="下载输出及运行记录")
    with gr.Accordion("运行状态", open=False):
        status = gr.JSON(value=runtime_status(), label="真实环境状态")
        refresh = gr.Button("刷新状态")
        refresh.click(runtime_status, outputs=status, queue=False)
    gr.Examples(examples=[
        ["Generate a picture of a cat sitting in a garden."],
        ["Generate an image, a video and an audio clip of ocean waves."],
    ], inputs=[prompt], cache_examples=False)
    run.click(predict, [prompt, image, video, audio, steps, frames, seconds, seed],
              [text, images, out_video, out_audio, files, status])

if __name__ == "__main__":
    demo.queue(concurrency_count=1, max_size=4).launch(
        server_name=os.getenv("NEXTGPT_HOST", "0.0.0.0"), server_port=int(os.getenv("PORT", "7860")),
        share=False, inbrowser=False)