File size: 2,540 Bytes
13f6fc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17e82fc
13f6fc8
17e82fc
 
 
 
 
 
 
 
13f6fc8
 
 
 
 
 
17e82fc
13f6fc8
 
 
 
 
 
17e82fc
 
13f6fc8
 
 
 
 
 
17e82fc
13f6fc8
17e82fc
13f6fc8
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from panda_gpt import PandaGPT
import gradio as gr

from huggingface_hub import hf_hub_download

vicuna_path = hf_hub_download(repo_id="ningshanwutuobang/ggml-pandagpt-vicuna-merge", filename="ggml-pandagpt-vicuna-q4_1.bin")
panda_path = hf_hub_download(repo_id="openllmplayground/pandagpt_13b_max_len_400", filename="pytorch_model.pt")

a = PandaGPT((vicuna_path,))
a.load_projection(panda_path)


def add_text(history, text):
   history = history + [(text, None)]
   return history, gr.update(value="", interactive=False)

def add_file(history, file):
    history = history + [((file.name,), None)]
    return history


def bot(history):
    text = history[-1][0]
    image_paths = []
    audio_paths = []
    video_paths = []
    for i in history[:-1]:
        if i[1] is None:
            if i[0][:4] in [".png", "jpeg"]:
                image_paths += list(i[0])
            if i[0][:3] in ["mp3", "wav"]:
                audio_paths += list(i[0])
            if i[0][:3] in ["mp4", "avi", "mkv"]:
                video_paths += list(i[0])
        else:
            image_paths = []
            audio_paths = []
            video_paths = []
    if len(image_paths) == 0 and len(audio_paths) == 0 and len(video_paths) == 0:
        response = a.eval_with_image(None, text)
    else:
        response = a.eval_with_image({"image_paths": image_paths,"audio_paths": audio_paths, "video_paths": video_paths}, text)
    history[-1][1] = ""
    for i in a.generate():
        history[-1][1] += i
        yield history
    if history[-1][1].endswith("###"):
        history[-1][1] = history[-1][1][:-3]
    yield history


with gr.Blocks() as demo:
    chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)

    with gr.Row():
        with gr.Column(scale=0.65):
            txt = gr.Textbox(
                show_label=False,
                placeholder="Enter text and press enter, or upload an image",
            ).style(container=False)
        with gr.Column(scale=0.15, min_width=0):
            btn = gr.UploadButton("πŸ“", file_types=["image", "video", "audio"])
        with gr.Column(scale=0.15, min_width=0):
            btn2 = gr.Button("reset")

    txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
        bot, chatbot, chatbot
    )
    txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
    file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False)
    btn2.click(a.reset, None, chatbot, queue=False)

demo.queue()
demo.launch()


# a.chat_with