File size: 2,197 Bytes
3fdcc70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import sys

sys.path.append(os.getcwd())
import os.path as osp
from pathlib import Path
import json
from .base import DataType
from cllm.utils import get_real_path


# sys.path.insert(0, sys.path[0] + "/../")
FILE_EXT = {
    "image": ["png", "jpeg", "jpg", "gif", "bmp", "tiff", "webp"],
    "video": ["mp4", "mov", "avi", "mkv"],
    "audio": ["wav", "mp3"],
}


class Container:
    def __init__(self, name, rtype, value) -> None:
        self.name = name
        self.rtype = rtype
        self.value = value

    def to_chatbot(self):
        pass

    def __str__(self):
        pass

    def __repr__(self) -> str:
        return str(self)


class File(Container):
    def to_chatbot(self):
        return str(self.value)

    @property
    def filename(self):
        return os.path.basename(self.value)

    def __str__(self):
        return f"`{self.filename}`"


class HTML(File):
    def to_chatbot(self):
        return str(self.value)

    def __str__(self):
        return f"`{self.filename}`"


class Image(File):
    def __str__(self):
        return f"`{self.filename}`"


class Video(File):
    def __str__(self):
        return f"`{self.filename}`"


class Audio(File):
    def __str__(self):
        return f"`{self.filename}`"


class Text(Container):
    def to_chatbot(self):
        if isinstance(self.value, str):
            return self.value
        elif isinstance(self.value, (list, tuple, dict)):
            return json.dumps(self.value, indent=2)
        return self.value

    def __str__(self):
        if isinstance(self.value, (list, dict)):
            return json.dumps(self.value)
        elif isinstance(self.value, str):
            return self.value
        return str(self.value)


def auto_type(name, rtype, value):
    if value is None:
        return None
    if "image" in str(rtype):
        return Image(name, rtype, get_real_path(value))
    if DataType.VIDEO == rtype:
        return Video(name, rtype, get_real_path(value))
    if DataType.AUDIO == rtype:
        return Audio(name, rtype, get_real_path(value))
    if DataType.HTML == rtype:
        return HTML(name, rtype, get_real_path(value))
    return Text(name, rtype, value)