File size: 2,290 Bytes
b49bf13
 
 
2ad74e3
 
b49bf13
 
 
 
 
 
 
 
 
 
2ad74e3
b49bf13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ad74e3
b49bf13
 
2ad74e3
b49bf13
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
"""Free ZeroGPU Gradio Space for the original NExT-GPT 7b_tiva_v0 model."""
from __future__ import annotations

import os

import gradio as gr
import spaces

from inference import infer, load_model

# ZeroGPU emulates CUDA at startup. Initializing here lets it persist model
# placement between requests instead of moving large weights every time.
model = load_model()


@spaces.GPU(duration=60)
def respond(prompt, image, audio, video, temperature, max_tokens):
    try:
        return infer(model, prompt, image, audio, video, temperature, max_tokens)
    except Exception as exc:
        raise gr.Error(f"模型推理失败:{type(exc).__name__}: {exc}") from exc


with gr.Blocks(title="NExT-GPT 7B v0 复现", analytics_enabled=False) as demo:
    gr.Markdown(
        "# NExT-GPT 7B v0 复现\n"
        "使用官方 `7b_tiva_v0` 模型和公开检查点,支持文本、图片、音频、视频输入。"
        "生成的媒体类型由模型决定。免费 ZeroGPU 有排队和每日使用配额。"
    )
    with gr.Row():
        prompt = gr.Textbox(label="问题或生成指令", lines=3)
        image = gr.Image(label="图片输入", type="filepath")
    with gr.Row():
        audio = gr.Audio(label="音频输入", sources=["upload"], type="filepath")
        video = gr.Video(label="视频输入", sources=["upload"])
    with gr.Row():
        temperature = gr.Slider(0.1, 1.5, value=0.4, step=0.1, label="Temperature")
        max_tokens = gr.Slider(32, 256, value=128, step=16, label="最大生成 token")
    submit = gr.Button("运行", variant="primary")
    answer = gr.Textbox(label="文本输出", lines=6)
    with gr.Row():
        image_output = gr.Image(label="图片输出")
        video_output = gr.Video(label="视频输出")
        audio_output = gr.Audio(label="音频输出")
    submit.click(
        respond,
        [prompt, image, audio, video, temperature, max_tokens],
        [answer, image_output, video_output, audio_output],
        api_name="generate",
    )

if __name__ == "__main__":
    space_host = os.environ.get("SPACE_HOST")
    demo.queue(default_concurrency_limit=1, max_size=8).launch(
        server_name="0.0.0.0", server_port=7860, show_error=True,
        root_path=f"https://{space_host}" if space_host else None,
    )