Spaces:
Running
Running
File size: 7,312 Bytes
e841ba5 a1809de e841ba5 f18a2c3 e841ba5 f18a2c3 e841ba5 f18a2c3 e841ba5 f18a2c3 d8c513f f18a2c3 d8c513f f18a2c3 d8c513f f18a2c3 e841ba5 f18a2c3 e841ba5 1969c22 e841ba5 f18a2c3 e841ba5 f18a2c3 a1809de e841ba5 a1809de e841ba5 |
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
import os
import re
import gradio as gr
import modelscope_studio.components.antd as antd
import modelscope_studio.components.base as ms
from .env import is_modelscope_studio
from .parse_markdown import parse_markdown
class Docs:
def __init__(self, file_path: str):
self.file_path = file_path
# default current directory
self.markdown_files = [
filename for filename in os.listdir(os.path.dirname(file_path))
if filename.endswith(".md")
]
self.demo_modules = self._get_demo_modules()
if is_modelscope_studio:
self.markdown_files = list(
filter(
lambda x: x.endswith("-zh_CN.md") or
(f"{'.'.join(x.split('.')[:-1])}-zh_CN.md" not in self.
markdown_files), self.markdown_files))
else:
self.markdown_files = list(
filter(lambda x: not x.endswith("-zh_CN.md"),
self.markdown_files))
def _get_filename(self, filename: str):
if is_modelscope_studio:
return f"{filename}-zh_CN.md"
return f"{filename}.md"
def _remove_formatter(self, markdown_text):
pattern = r"^ *---[\s\S]*?---"
replaced_text = re.sub(pattern, "", markdown_text)
return replaced_text
def _list_demos(self, dir_path: str, prefix=''):
result = []
if (not os.path.isdir(dir_path)):
return result
for name in os.listdir(dir_path):
path = os.path.join(dir_path, name)
if os.path.isfile(path):
result.append(prefix + name)
elif os.path.isdir(path):
sub_prefix = prefix + name + '/'
result.extend(self._list_demos(path, sub_prefix))
return result
def _get_demo_modules(self):
import importlib.util
demos = [
demo for demo in self._list_demos(
os.path.join(os.path.dirname(self.file_path), "demos"))
if demo.endswith(".py") and not demo.startswith("__")
]
demo_modules = {}
for demo in demos:
demo_name = demo.split(".")[0]
spec = importlib.util.spec_from_file_location(
"demo",
os.path.join(os.path.dirname(self.file_path), "demos", demo))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
demo_modules[demo_name] = module
return demo_modules
def _read_file(self, relative_path: str):
with open(os.path.join(os.path.dirname(self.file_path), relative_path),
"r") as f:
return f.read()
def _render_demo(self,
demo_name,
prefix='',
suffix='',
position="left",
collapsible=False,
fixed=False,
title=''):
content = self._read_file(f"./demos/{demo_name}.py")
module = self.demo_modules[demo_name]
with antd.Card(styles=dict(body=dict(padding=10)),
elem_style=dict(margin="8px 0")):
if title:
with ms.Slot("title"):
ms.Text(title)
with antd.Row(align="stretch", wrap=True, gutter=[8, 8]):
with antd.Col(sm=dict(span=24 if position == "bottom" else 10,
order=2 if position == "bottom" else 1),
xs=dict(span=24, order=2)):
def render_code():
with antd.Row(elem_style=dict(height='100%'),
gutter=[8, 8]):
with antd.Col(sm=0, xs=24):
antd.Divider(type="horizontal",
variant="dashed",
elem_style=dict(width='100%',
margin='8px 0 0'))
resolved_prefix = prefix + "\n" if prefix else ""
resolved_suffix = suffix + "\n" if suffix else ""
with antd.Col(sm=23, xs=24):
ms.Markdown(f"""{resolved_prefix}```python
{content}
```{resolved_suffix}""",
header_links=True)
with antd.Col(sm=1,
xs=0,
elem_style=dict(height="100%")):
with ms.Div(elem_style=dict(
display="flex",
justifyContent="center",
width="100%",
height="100%")):
antd.Divider(type="vertical",
variant="dashed",
elem_style=dict(height='100%',
margin=0))
if collapsible:
with antd.Collapse():
with antd.Collapse.Item(label="Show Code",
key="code"):
render_code()
else:
render_code()
with antd.Col(
sm=dict(span=24 if position == "bottom" else 14,
order=1 if position == "bottom" else 2),
xs=dict(span=24, order=1),
elem_style=dict(
width='100%',
transform="translate(0, 0)" if fixed else None)):
module.demo.render()
def _render_markdown(self, markdown_file):
items = parse_markdown(self._remove_formatter(
self._read_file(markdown_file)),
read_file=self._read_file)
for item in items:
if item["type"] == "text":
ms.Markdown(item["value"], header_links=True)
elif item["type"] == "demo":
self._render_demo(item["name"],
prefix=item["prefix"],
suffix=item["suffix"],
position=item["position"],
collapsible=item["collapsible"],
fixed=item["fixed"],
title=item["title"])
def get_css(self):
css = ""
for demo_name in self.demo_modules:
module = self.demo_modules[demo_name]
if hasattr(module, "css"):
css += module.css
return css
def render(self, module_name: str = None):
parsed_filename = self._get_filename(
module_name) if module_name else None
with gr.Blocks() as demo:
self._render_markdown(
parsed_filename if parsed_filename else self.markdown_files[0])
return demo
|