h2o-llmstudio

Форк
0
140 строк · 3.0 Кб
1
import glob
2
import re
3
from dataclasses import dataclass
4
from typing import Dict
5

6
CLEANR = re.compile("<[^<]+?>")
7
tooltip_files = glob.glob("documentation/docs/tooltips/**/*.mdx", recursive=True)
8

9

10
def read_tooltip_file(path: str) -> str:
11
    """
12
    Reads all lines of a text file.
13

14
    Args:
15
        filename: path to the file
16

17
    Returns:
18
        str: the text of the file
19
    """
20

21
    with open(path) as f:
22
        lines = f.readlines()
23
    return "".join(lines)
24

25

26
def cleanhtml(raw_html: str) -> str:
27
    """
28
    Removes html tags from a string.
29

30
    Args:
31
        raw_html: the string to clean
32

33
    Returns:
34
        str: the cleaned string
35
    """
36

37
    cleantext = re.sub(CLEANR, "", raw_html)
38
    return cleantext
39

40

41
def clean_docusaurus_tags(text: str) -> str:
42
    """
43
    Removes docusaurus tags from a string.
44

45
    Args:
46
        text: the string to clean
47

48
    Returns:
49
        str: the cleaned string
50
    """
51

52
    text = text.replace(":::info note", "")
53
    text = text.replace(":::info Note", "")
54
    text = text.replace(":::tip tip", "")
55
    text = text.replace(":::", "")
56
    return text
57

58

59
def clean_md_links(text: str) -> str:
60
    """
61
    Removes markdown links from a string.
62

63
    Args:
64
        text: the string to clean
65

66
    Returns:
67
        str: the cleaned string
68
    """
69

70
    text = re.sub(r"\[(.*?)\]\(.*?\)", r"\1", text)
71
    return text
72

73

74
@dataclass
75
class Tooltip:
76
    """
77
    A tooltip.
78

79
    Returns:
80
        str: the text of the tooltip
81
    """
82

83
    name: str
84
    text: str
85

86
    def __repr__(self):
87
        return f"{self.name}: {self.text}"
88

89

90
class Tooltips:
91
    """
92
    A collection of tooltips.
93

94
    During initialization, all tooltips are read from the tooltip files.
95

96
    Usage:
97
        tooltips = Tooltips()
98
        a tooltip can be accessed by its name:
99
        tooltips["name"] returns the tooltip with the name "name"
100
    """
101

102
    def __init__(self):
103
        self.tooltips: Dict[str, Tooltip] = {}
104
        for filename in tooltip_files:
105
            name = filename.split("/")[-1].split(".")[0]
106
            name = name.replace("-", "_")
107
            name = name[1:]  # remove leading underscore
108
            section = filename.split("/")[3]
109
            text = read_tooltip_file(filename)
110
            text = cleanhtml(text)
111
            text = clean_docusaurus_tags(text)
112
            text = clean_md_links(text)
113
            if name in self.tooltips.keys():
114
                raise ValueError
115
            self.add_tooltip(Tooltip(f"{section}_{name}", text))
116

117
    def add_tooltip(self, tooltip):
118
        self.tooltips[tooltip.name] = tooltip
119

120
    def __getitem__(self, name: str) -> str:
121
        try:
122
            text = self.tooltips[name].text
123
        except KeyError:
124
            text = None
125
        return text
126

127
    def __len__(self):
128
        return len(self.tooltips)
129

130
    def __repr__(self):
131
        return f"{self.tooltips}"
132

133
    def get(self, name: str, default=None):
134
        if name in self.tooltips.keys():
135
            return self.tooltips[name].text
136
        else:
137
            return default
138

139

140
tooltips = Tooltips()
141

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.