h2o-llmstudio
140 строк · 3.0 Кб
1import glob
2import re
3from dataclasses import dataclass
4from typing import Dict
5
6CLEANR = re.compile("<[^<]+?>")
7tooltip_files = glob.glob("documentation/docs/tooltips/**/*.mdx", recursive=True)
8
9
10def read_tooltip_file(path: str) -> str:
11"""
12Reads all lines of a text file.
13
14Args:
15filename: path to the file
16
17Returns:
18str: the text of the file
19"""
20
21with open(path) as f:
22lines = f.readlines()
23return "".join(lines)
24
25
26def cleanhtml(raw_html: str) -> str:
27"""
28Removes html tags from a string.
29
30Args:
31raw_html: the string to clean
32
33Returns:
34str: the cleaned string
35"""
36
37cleantext = re.sub(CLEANR, "", raw_html)
38return cleantext
39
40
41def clean_docusaurus_tags(text: str) -> str:
42"""
43Removes docusaurus tags from a string.
44
45Args:
46text: the string to clean
47
48Returns:
49str: the cleaned string
50"""
51
52text = text.replace(":::info note", "")
53text = text.replace(":::info Note", "")
54text = text.replace(":::tip tip", "")
55text = text.replace(":::", "")
56return text
57
58
59def clean_md_links(text: str) -> str:
60"""
61Removes markdown links from a string.
62
63Args:
64text: the string to clean
65
66Returns:
67str: the cleaned string
68"""
69
70text = re.sub(r"\[(.*?)\]\(.*?\)", r"\1", text)
71return text
72
73
74@dataclass
75class Tooltip:
76"""
77A tooltip.
78
79Returns:
80str: the text of the tooltip
81"""
82
83name: str
84text: str
85
86def __repr__(self):
87return f"{self.name}: {self.text}"
88
89
90class Tooltips:
91"""
92A collection of tooltips.
93
94During initialization, all tooltips are read from the tooltip files.
95
96Usage:
97tooltips = Tooltips()
98a tooltip can be accessed by its name:
99tooltips["name"] returns the tooltip with the name "name"
100"""
101
102def __init__(self):
103self.tooltips: Dict[str, Tooltip] = {}
104for filename in tooltip_files:
105name = filename.split("/")[-1].split(".")[0]
106name = name.replace("-", "_")
107name = name[1:] # remove leading underscore
108section = filename.split("/")[3]
109text = read_tooltip_file(filename)
110text = cleanhtml(text)
111text = clean_docusaurus_tags(text)
112text = clean_md_links(text)
113if name in self.tooltips.keys():
114raise ValueError
115self.add_tooltip(Tooltip(f"{section}_{name}", text))
116
117def add_tooltip(self, tooltip):
118self.tooltips[tooltip.name] = tooltip
119
120def __getitem__(self, name: str) -> str:
121try:
122text = self.tooltips[name].text
123except KeyError:
124text = None
125return text
126
127def __len__(self):
128return len(self.tooltips)
129
130def __repr__(self):
131return f"{self.tooltips}"
132
133def get(self, name: str, default=None):
134if name in self.tooltips.keys():
135return self.tooltips[name].text
136else:
137return default
138
139
140tooltips = Tooltips()
141