Spaces:
Runtime error
Runtime error
Benjamin Bossan
commited on
Commit
·
94e602d
1
Parent(s):
f4f6aba
Blacken code
Browse files
edit.py
CHANGED
|
@@ -36,13 +36,20 @@ from skops import card
|
|
| 36 |
from skops.card._model_card import PlotSection, split_subsection_names
|
| 37 |
|
| 38 |
from utils import iterate_key_section_content, process_card_for_rendering
|
| 39 |
-
from tasks import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
arepr = reprlib.Repr()
|
| 43 |
arepr.maxstring = 24
|
| 44 |
tmp_path = Path(mkdtemp(prefix="skops-")) # temporary files
|
| 45 |
-
hf_path = Path(mkdtemp(prefix="skops-"))
|
| 46 |
|
| 47 |
|
| 48 |
def load_model_card_from_repo(repo_id: str) -> card.Card:
|
|
@@ -53,7 +60,11 @@ def load_model_card_from_repo(repo_id: str) -> card.Card:
|
|
| 53 |
|
| 54 |
|
| 55 |
def _update_model_card(
|
| 56 |
-
model_card: card.Card,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
) -> None:
|
| 58 |
# This is a very roundabout way to update the model card but it's necessary
|
| 59 |
# because of how streamlit handles session state. Basically, there have to
|
|
@@ -109,7 +120,9 @@ def _update_model_card(
|
|
| 109 |
|
| 110 |
def _add_section(model_card: card.Card, key: str) -> None:
|
| 111 |
section_name = f"{key}/Untitled"
|
| 112 |
-
task = AddSectionTask(
|
|
|
|
|
|
|
| 113 |
st.session_state.task_state.add(task)
|
| 114 |
|
| 115 |
|
|
@@ -157,7 +170,11 @@ def _add_fig_form(
|
|
| 157 |
|
| 158 |
|
| 159 |
def create_form_from_section(
|
| 160 |
-
model_card: card.Card,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
) -> None:
|
| 162 |
split_sections = split_subsection_names(section_name)
|
| 163 |
old_title = split_sections[-1]
|
|
@@ -203,7 +220,9 @@ def create_form_from_section(
|
|
| 203 |
|
| 204 |
|
| 205 |
def display_sections(model_card: card.Card) -> None:
|
| 206 |
-
for key, section_name, content, is_fig in iterate_key_section_content(
|
|
|
|
|
|
|
| 207 |
create_form_from_section(model_card, key, section_name, content, is_fig)
|
| 208 |
|
| 209 |
|
|
@@ -248,9 +267,7 @@ def add_download_model_card_button():
|
|
| 248 |
model_card = st.session_state.get("model_card")
|
| 249 |
download_disabled = not bool(model_card)
|
| 250 |
data = model_card.render()
|
| 251 |
-
st.download_button(
|
| 252 |
-
"Save (md)", data=data, disabled=download_disabled
|
| 253 |
-
)
|
| 254 |
|
| 255 |
|
| 256 |
def edit_input_form():
|
|
|
|
| 36 |
from skops.card._model_card import PlotSection, split_subsection_names
|
| 37 |
|
| 38 |
from utils import iterate_key_section_content, process_card_for_rendering
|
| 39 |
+
from tasks import (
|
| 40 |
+
AddSectionTask,
|
| 41 |
+
AddFigureTask,
|
| 42 |
+
DeleteSectionTask,
|
| 43 |
+
TaskState,
|
| 44 |
+
UpdateFigureTask,
|
| 45 |
+
UpdateSectionTask,
|
| 46 |
+
)
|
| 47 |
|
| 48 |
|
| 49 |
arepr = reprlib.Repr()
|
| 50 |
arepr.maxstring = 24
|
| 51 |
tmp_path = Path(mkdtemp(prefix="skops-")) # temporary files
|
| 52 |
+
hf_path = Path(mkdtemp(prefix="skops-")) # hf repo
|
| 53 |
|
| 54 |
|
| 55 |
def load_model_card_from_repo(repo_id: str) -> card.Card:
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
def _update_model_card(
|
| 63 |
+
model_card: card.Card,
|
| 64 |
+
key: str,
|
| 65 |
+
section_name: str,
|
| 66 |
+
content: str,
|
| 67 |
+
is_fig: bool,
|
| 68 |
) -> None:
|
| 69 |
# This is a very roundabout way to update the model card but it's necessary
|
| 70 |
# because of how streamlit handles session state. Basically, there have to
|
|
|
|
| 120 |
|
| 121 |
def _add_section(model_card: card.Card, key: str) -> None:
|
| 122 |
section_name = f"{key}/Untitled"
|
| 123 |
+
task = AddSectionTask(
|
| 124 |
+
model_card, title=section_name, content="[More Information Needed]"
|
| 125 |
+
)
|
| 126 |
st.session_state.task_state.add(task)
|
| 127 |
|
| 128 |
|
|
|
|
| 170 |
|
| 171 |
|
| 172 |
def create_form_from_section(
|
| 173 |
+
model_card: card.Card,
|
| 174 |
+
key: str,
|
| 175 |
+
section_name: str,
|
| 176 |
+
content: str,
|
| 177 |
+
is_fig: bool = False,
|
| 178 |
) -> None:
|
| 179 |
split_sections = split_subsection_names(section_name)
|
| 180 |
old_title = split_sections[-1]
|
|
|
|
| 220 |
|
| 221 |
|
| 222 |
def display_sections(model_card: card.Card) -> None:
|
| 223 |
+
for key, section_name, content, is_fig in iterate_key_section_content(
|
| 224 |
+
model_card._data
|
| 225 |
+
):
|
| 226 |
create_form_from_section(model_card, key, section_name, content, is_fig)
|
| 227 |
|
| 228 |
|
|
|
|
| 267 |
model_card = st.session_state.get("model_card")
|
| 268 |
download_disabled = not bool(model_card)
|
| 269 |
data = model_card.render()
|
| 270 |
+
st.download_button("Save (md)", data=data, disabled=download_disabled)
|
|
|
|
|
|
|
| 271 |
|
| 272 |
|
| 273 |
def edit_input_form():
|
start.py
CHANGED
|
@@ -29,7 +29,7 @@ import skops.io as sio
|
|
| 29 |
from skops import card, hub_utils
|
| 30 |
|
| 31 |
|
| 32 |
-
hf_path = Path(mkdtemp(prefix="skops-"))
|
| 33 |
tmp_path = Path(mkdtemp(prefix="skops-")) # temporary files
|
| 34 |
description = """Create an sklearn model card
|
| 35 |
|
|
@@ -112,7 +112,9 @@ def create_skops_model_card() -> None:
|
|
| 112 |
def create_empty_model_card() -> None:
|
| 113 |
init_repo(hf_path)
|
| 114 |
metadata = card.metadata_from_config(hf_path)
|
| 115 |
-
model_card = card.Card(
|
|
|
|
|
|
|
| 116 |
model_card.add(**{"Untitled": "[More Information Needed]"})
|
| 117 |
st.session_state.model_card = model_card
|
| 118 |
|
|
@@ -167,7 +169,7 @@ def start_input_form():
|
|
| 167 |
],
|
| 168 |
key="task",
|
| 169 |
on_change=init_repo,
|
| 170 |
-
args=(hf_path,)
|
| 171 |
)
|
| 172 |
st.markdown("---")
|
| 173 |
|
|
@@ -176,7 +178,7 @@ def start_input_form():
|
|
| 176 |
value=f"scikit-learn=={sklearn.__version__}\n",
|
| 177 |
key="requirements",
|
| 178 |
on_change=init_repo,
|
| 179 |
-
args=(hf_path,)
|
| 180 |
)
|
| 181 |
st.markdown("---")
|
| 182 |
|
|
|
|
| 29 |
from skops import card, hub_utils
|
| 30 |
|
| 31 |
|
| 32 |
+
hf_path = Path(mkdtemp(prefix="skops-")) # hf repo
|
| 33 |
tmp_path = Path(mkdtemp(prefix="skops-")) # temporary files
|
| 34 |
description = """Create an sklearn model card
|
| 35 |
|
|
|
|
| 112 |
def create_empty_model_card() -> None:
|
| 113 |
init_repo(hf_path)
|
| 114 |
metadata = card.metadata_from_config(hf_path)
|
| 115 |
+
model_card = card.Card(
|
| 116 |
+
model=st.session_state.model, metadata=metadata, template=None
|
| 117 |
+
)
|
| 118 |
model_card.add(**{"Untitled": "[More Information Needed]"})
|
| 119 |
st.session_state.model_card = model_card
|
| 120 |
|
|
|
|
| 169 |
],
|
| 170 |
key="task",
|
| 171 |
on_change=init_repo,
|
| 172 |
+
args=(hf_path,),
|
| 173 |
)
|
| 174 |
st.markdown("---")
|
| 175 |
|
|
|
|
| 178 |
value=f"scikit-learn=={sklearn.__version__}\n",
|
| 179 |
key="requirements",
|
| 180 |
on_change=init_repo,
|
| 181 |
+
args=(hf_path,),
|
| 182 |
)
|
| 183 |
st.markdown("---")
|
| 184 |
|
tasks.py
CHANGED
|
@@ -15,6 +15,7 @@ from streamlit.runtime.uploaded_file_manager import UploadedFile
|
|
| 15 |
|
| 16 |
class Task:
|
| 17 |
"""(Abstract) base class for tasks"""
|
|
|
|
| 18 |
def do(self) -> None:
|
| 19 |
raise NotImplementedError
|
| 20 |
|
|
@@ -24,6 +25,7 @@ class Task:
|
|
| 24 |
|
| 25 |
class TaskState:
|
| 26 |
"""Tracking the state of tasks"""
|
|
|
|
| 27 |
def __init__(self) -> None:
|
| 28 |
self.done_list: list[Task] = []
|
| 29 |
self.undone_list: list[Task] = []
|
|
@@ -56,6 +58,7 @@ class TaskState:
|
|
| 56 |
|
| 57 |
class AddSectionTask(Task):
|
| 58 |
"""Add a new text section"""
|
|
|
|
| 59 |
def __init__(
|
| 60 |
self,
|
| 61 |
model_card: card.Card,
|
|
@@ -78,6 +81,7 @@ class AddSectionTask(Task):
|
|
| 78 |
|
| 79 |
class AddFigureTask(Task):
|
| 80 |
"""Add a new figure section"""
|
|
|
|
| 81 |
def __init__(
|
| 82 |
self,
|
| 83 |
model_card: card.Card,
|
|
@@ -106,6 +110,7 @@ class DeleteSectionTask(Task):
|
|
| 106 |
but only turned invisible.
|
| 107 |
|
| 108 |
"""
|
|
|
|
| 109 |
def __init__(
|
| 110 |
self,
|
| 111 |
model_card: card.Card,
|
|
@@ -123,6 +128,7 @@ class DeleteSectionTask(Task):
|
|
| 123 |
|
| 124 |
class UpdateSectionTask(Task):
|
| 125 |
"""Change the title or content of a text section"""
|
|
|
|
| 126 |
def __init__(
|
| 127 |
self,
|
| 128 |
model_card: card.Card,
|
|
@@ -154,6 +160,7 @@ class UpdateSectionTask(Task):
|
|
| 154 |
|
| 155 |
class UpdateFigureTask(Task):
|
| 156 |
"""Change the title or image of a figure section"""
|
|
|
|
| 157 |
def __init__(
|
| 158 |
self,
|
| 159 |
model_card: card.Card,
|
|
|
|
| 15 |
|
| 16 |
class Task:
|
| 17 |
"""(Abstract) base class for tasks"""
|
| 18 |
+
|
| 19 |
def do(self) -> None:
|
| 20 |
raise NotImplementedError
|
| 21 |
|
|
|
|
| 25 |
|
| 26 |
class TaskState:
|
| 27 |
"""Tracking the state of tasks"""
|
| 28 |
+
|
| 29 |
def __init__(self) -> None:
|
| 30 |
self.done_list: list[Task] = []
|
| 31 |
self.undone_list: list[Task] = []
|
|
|
|
| 58 |
|
| 59 |
class AddSectionTask(Task):
|
| 60 |
"""Add a new text section"""
|
| 61 |
+
|
| 62 |
def __init__(
|
| 63 |
self,
|
| 64 |
model_card: card.Card,
|
|
|
|
| 81 |
|
| 82 |
class AddFigureTask(Task):
|
| 83 |
"""Add a new figure section"""
|
| 84 |
+
|
| 85 |
def __init__(
|
| 86 |
self,
|
| 87 |
model_card: card.Card,
|
|
|
|
| 110 |
but only turned invisible.
|
| 111 |
|
| 112 |
"""
|
| 113 |
+
|
| 114 |
def __init__(
|
| 115 |
self,
|
| 116 |
model_card: card.Card,
|
|
|
|
| 128 |
|
| 129 |
class UpdateSectionTask(Task):
|
| 130 |
"""Change the title or content of a text section"""
|
| 131 |
+
|
| 132 |
def __init__(
|
| 133 |
self,
|
| 134 |
model_card: card.Card,
|
|
|
|
| 160 |
|
| 161 |
class UpdateFigureTask(Task):
|
| 162 |
"""Change the title or image of a figure section"""
|
| 163 |
+
|
| 164 |
def __init__(
|
| 165 |
self,
|
| 166 |
model_card: card.Card,
|