Benjamin Bossan commited on
Commit
433130f
1 Parent(s): 1b701be

Add possibility to load existing model card

Browse files
Files changed (2) hide show
  1. app.py +54 -30
  2. requirements.txt +1 -0
app.py CHANGED
@@ -15,6 +15,7 @@ from tempfile import mkdtemp
15
  import pandas as pd
16
  import sklearn
17
  import streamlit as st
 
18
  from sklearn.base import BaseEstimator
19
 
20
  import skops.io as sio
@@ -31,10 +32,10 @@ PLOT_PREFIX = "__plot__:"
31
  if "custom_sections" not in st.session_state:
32
  st.session_state.custom_sections = {}
33
 
34
- # the tmp_repo is used to upload the sklearn model to
35
- tmp_repo = Path(mkdtemp(prefix="skops-"))
36
- # the hf_repo is the actual repo used for init()
37
- hf_repo = Path(mkdtemp(prefix="skops-"))
38
 
39
  # a hacky way to "persist" custom sections
40
  CUSTOM_SECTIONS_CACHE_FILE = ".custom-sections.json"
@@ -69,16 +70,16 @@ def _write_plot(plot_name, plot_file):
69
 
70
 
71
  def init_repo():
72
- _clear_repo(hf_repo)
73
 
74
  try:
75
- file_name = tmp_repo / "model.skops"
76
  sio.dump(model, file_name)
77
 
78
  reqs = [r.strip().rstrip(",") for r in requirements.splitlines()]
79
  hub_utils.init(
80
  model=file_name,
81
- dst=hf_repo,
82
  task=task,
83
  data=data,
84
  requirements=reqs,
@@ -120,10 +121,18 @@ def _parse_metrics(metrics):
120
  return metrics_table
121
 
122
 
 
 
 
 
 
123
  def _create_model_card():
124
  init_repo()
125
- metadata = card.metadata_from_config(hf_repo)
126
- model_card = card.Card(model=model, metadata=metadata)
 
 
 
127
 
128
  if model_description:
129
  model_card.add(**{"Model description": model_description})
@@ -256,7 +265,7 @@ def add_custom_plot():
256
 
257
  # store plot in temp repo
258
  file_name = plot_file.name.replace(" ", "_")
259
- file_path = str(tmp_repo / file_name)
260
  with open(file_path, "wb") as f:
261
  f.write(plot_file.getvalue())
262
 
@@ -303,28 +312,43 @@ with st.sidebar:
303
  if model is not None and data is not None:
304
  init_repo()
305
 
306
- model_description = st.text_input("Model description", value=PLACEHOLDER)
307
- intended_uses = st.text_area(
308
- "Intended uses & limitations", height=2, value=PLACEHOLDER
309
- )
310
- metrics = st.text_area("Metrics (e.g. 'accuracy = 0.95'), one metric per line")
311
- authors = st.text_area(
312
- "Authors",
313
- value="This model card is written by following authors:\n\n" + PLACEHOLDER,
314
- )
315
- contact = st.text_area(
316
- "Contact",
317
- value="You can contact the model card authors through following channels:\n\n"
318
- + PLACEHOLDER,
319
- )
320
- citation = st.text_area(
321
- "Citation",
322
- value="Below you can find information related to citation.\n\nBibTex:\n\n```\n"
323
- + PLACEHOLDER
324
- + "\n```",
325
- height=5,
326
  )
327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
  # ADD A CUSTOM SECTIONS
329
  with st.form("custom-section", clear_on_submit=True):
330
  section_name = st.text_input(
 
15
  import pandas as pd
16
  import sklearn
17
  import streamlit as st
18
+ from huggingface_hub import hf_hub_download
19
  from sklearn.base import BaseEstimator
20
 
21
  import skops.io as sio
 
32
  if "custom_sections" not in st.session_state:
33
  st.session_state.custom_sections = {}
34
 
35
+ # the tmp_path is used to upload the sklearn model to
36
+ tmp_path = Path(mkdtemp(prefix="skops-"))
37
+ # the hf_path is the actual repo used for init()
38
+ hf_path = Path(mkdtemp(prefix="skops-"))
39
 
40
  # a hacky way to "persist" custom sections
41
  CUSTOM_SECTIONS_CACHE_FILE = ".custom-sections.json"
 
70
 
71
 
72
  def init_repo():
73
+ _clear_repo(hf_path)
74
 
75
  try:
76
+ file_name = tmp_path / "model.skops"
77
  sio.dump(model, file_name)
78
 
79
  reqs = [r.strip().rstrip(",") for r in requirements.splitlines()]
80
  hub_utils.init(
81
  model=file_name,
82
+ dst=hf_path,
83
  task=task,
84
  data=data,
85
  requirements=reqs,
 
121
  return metrics_table
122
 
123
 
124
+ def _load_model_card_from_repo(repo_id: str) -> Card:
125
+ path = hf_hub_download(repo_id, "README.md")
126
+ return card.parse_modelcard(path)
127
+
128
+
129
  def _create_model_card():
130
  init_repo()
131
+ if model_card_repo: # load existing model card
132
+ model_card = _load_model_card_from_repo(model_card_repo)
133
+ else: # create new model card
134
+ metadata = card.metadata_from_config(hf_path)
135
+ model_card = card.Card(model=model, metadata=metadata)
136
 
137
  if model_description:
138
  model_card.add(**{"Model description": model_description})
 
265
 
266
  # store plot in temp repo
267
  file_name = plot_file.name.replace(" ", "_")
268
+ file_path = str(tmp_path / file_name)
269
  with open(file_path, "wb") as f:
270
  f.write(plot_file.getvalue())
271
 
 
312
  if model is not None and data is not None:
313
  init_repo()
314
 
315
+ model_card_repo = st.text_input(
316
+ "Optional: HF repo to load model card from (e.g. 'gpt2'), "
317
+ "leave empty to use default skops template",
318
+ value="",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319
  )
320
 
321
+ # DEFAULT SKOPS SECTIONS
322
+ if not model_card_repo:
323
+ model_description = st.text_input("Model description", value=PLACEHOLDER)
324
+ intended_uses = st.text_area(
325
+ "Intended uses & limitations", height=2, value=PLACEHOLDER
326
+ )
327
+ metrics = st.text_area("Metrics (e.g. 'accuracy = 0.95'), one metric per line")
328
+ authors = st.text_area(
329
+ "Authors",
330
+ value="This model card is written by following authors:\n\n" + PLACEHOLDER,
331
+ )
332
+ contact = st.text_area(
333
+ "Contact",
334
+ value="You can contact the model card authors through following channels:\n\n"
335
+ + PLACEHOLDER,
336
+ )
337
+ citation = st.text_area(
338
+ "Citation",
339
+ value="Below you can find information related to citation.\n\nBibTex:\n\n```\n"
340
+ + PLACEHOLDER
341
+ + "\n```",
342
+ height=5,
343
+ )
344
+ else:
345
+ model_description = None
346
+ intended_uses = None
347
+ metrics = None
348
+ authors = None
349
+ contact = None
350
+ citation = None
351
+
352
  # ADD A CUSTOM SECTIONS
353
  with st.form("custom-section", clear_on_submit=True):
354
  section_name = st.text_input(
requirements.txt CHANGED
@@ -1,3 +1,4 @@
 
1
  pandas
2
  scikit-learn
3
  skops
 
1
+ huggingface_hub
2
  pandas
3
  scikit-learn
4
  skops