asoria HF staff commited on
Commit
2d53b10
·
1 Parent(s): 734af25

Fix visibility after notebook creation and push

Browse files
Files changed (1) hide show
  1. app.py +26 -26
app.py CHANGED
@@ -6,8 +6,6 @@ from huggingface_hub import HfApi
6
 
7
  """
8
  TODOs:
9
- - Show auth and push button only after notebook creation
10
- - Improve the link to the result notebook
11
  - Handle erros
12
  - Add more commands to the notebook
13
  - Parametrize the commands
@@ -28,23 +26,22 @@ def create_notebook_file(cell_commands, notebook_name="generated_notebook.ipynb"
28
 
29
 
30
  def push_notebook(file_path, dataset_id, token):
 
31
  api = HfApi(token=token)
32
  api.upload_file(
33
  path_or_fileobj=file_path,
34
- path_in_repo="dataset_analysis.ipynb",
35
  repo_id=dataset_id,
36
  repo_type="dataset",
37
  )
38
- # TODO: Handle permission error
39
  print("Notebook uploaded to Huggingface Hub.")
40
- link = (
41
- f"https://huggingface.co/datasets/{dataset_id}/blob/main/dataset_analyst.ipynb"
42
- )
43
- return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">See notebook</a>'
44
 
45
 
46
  def generate_notebook(dataset_id):
47
- # TODO: Get first config and split? or generate a dataframe per each split maybe?
48
  commands = [
49
  "!pip install pandas",
50
  "import pandas as pd",
@@ -53,8 +50,7 @@ def generate_notebook(dataset_id):
53
  ]
54
  notebook_name = f"{dataset_id.replace('/', '-')}.ipynb"
55
  create_notebook_file(commands, notebook_name=notebook_name)
56
- print("Notebook uploaded to Huggingface Hub.")
57
- return notebook_name
58
 
59
 
60
  with gr.Blocks() as demo:
@@ -75,49 +71,53 @@ with gr.Blocks() as demo:
75
  src="https://huggingface.co/datasets/{name}/embed/viewer/default/train"
76
  frameborder="0"
77
  width="100%"
78
- height="600px"
79
  ></iframe>
80
- """
81
  return gr.HTML(value=html_code)
82
 
83
- generate_btn = gr.Button("Generate notebook", visible=True)
84
-
85
- download_link = gr.File(label="Download notebook")
86
- generate_btn.click(
87
- generate_notebook, inputs=[dataset_name], outputs=[download_link]
88
- )
89
- with gr.Row() as auth_page:
90
  with gr.Column():
91
  auth_title = gr.Markdown(
92
- "Enter your token ([settings](https://huggingface.co/settings/tokens)):"
93
  )
94
  token_box = gr.Textbox(
95
  "", label="token", placeholder="hf_xxx", type="password"
96
  )
97
  auth_error = gr.Markdown("", visible=False)
98
 
 
 
 
 
 
 
 
 
 
99
  def auth(token):
100
  if not token:
101
  return {
102
  auth_error: gr.Markdown(value="", visible=False),
103
- push_btn: gr.Row(visible=False),
104
  }
105
  return {
106
  auth_error: gr.Markdown(value="", visible=False),
107
- push_btn: gr.Row(visible=True),
108
  }
109
 
110
- push_btn = gr.Button("Push notebook to hub", visible=False)
111
  token_box.change(
112
  auth,
113
  inputs=token_box,
114
  outputs=[auth_error, push_btn],
115
  )
116
- output_lbl = gr.HTML(value="")
117
 
118
  push_btn.click(
119
  push_notebook,
120
  inputs=[download_link, dataset_name, token_box],
121
- outputs=[output_lbl],
122
  )
 
123
  demo.launch()
 
6
 
7
  """
8
  TODOs:
 
 
9
  - Handle erros
10
  - Add more commands to the notebook
11
  - Parametrize the commands
 
26
 
27
 
28
  def push_notebook(file_path, dataset_id, token):
29
+ notebook_name = "dataset_analysis.ipynb"
30
  api = HfApi(token=token)
31
  api.upload_file(
32
  path_or_fileobj=file_path,
33
+ path_in_repo=notebook_name,
34
  repo_id=dataset_id,
35
  repo_type="dataset",
36
  )
 
37
  print("Notebook uploaded to Huggingface Hub.")
38
+ link = f"https://huggingface.co/datasets/{dataset_id}/blob/main/{notebook_name}"
39
+ html = f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline; text-decoration-style: dotted;">See notebook</a>'
40
+
41
+ return gr.HTML(value=html, visible=True)
42
 
43
 
44
  def generate_notebook(dataset_id):
 
45
  commands = [
46
  "!pip install pandas",
47
  "import pandas as pd",
 
50
  ]
51
  notebook_name = f"{dataset_id.replace('/', '-')}.ipynb"
52
  create_notebook_file(commands, notebook_name=notebook_name)
53
+ return gr.File(value=notebook_name, visible=True), gr.Row.update(visible=True)
 
54
 
55
 
56
  with gr.Blocks() as demo:
 
71
  src="https://huggingface.co/datasets/{name}/embed/viewer/default/train"
72
  frameborder="0"
73
  width="100%"
74
+ height="350px"
75
  ></iframe>
76
+ """
77
  return gr.HTML(value=html_code)
78
 
79
+ generate_btn = gr.Button("Generate notebook")
80
+ download_link = gr.File(label="Download notebook", visible=False)
81
+ with gr.Row(visible=False) as auth_page:
 
 
 
 
82
  with gr.Column():
83
  auth_title = gr.Markdown(
84
+ "Want to push to hub? Enter your token ([settings](https://huggingface.co/settings/tokens)):"
85
  )
86
  token_box = gr.Textbox(
87
  "", label="token", placeholder="hf_xxx", type="password"
88
  )
89
  auth_error = gr.Markdown("", visible=False)
90
 
91
+ push_btn = gr.Button("Push notebook to hub", visible=False)
92
+ output_lbl = gr.HTML(value="", visible=False)
93
+
94
+ generate_btn.click(
95
+ generate_notebook,
96
+ inputs=[dataset_name],
97
+ outputs=[download_link, auth_page],
98
+ )
99
+
100
  def auth(token):
101
  if not token:
102
  return {
103
  auth_error: gr.Markdown(value="", visible=False),
104
+ push_btn: gr.Button(visible=False),
105
  }
106
  return {
107
  auth_error: gr.Markdown(value="", visible=False),
108
+ push_btn: gr.Button("Push notebook to hub", visible=True),
109
  }
110
 
 
111
  token_box.change(
112
  auth,
113
  inputs=token_box,
114
  outputs=[auth_error, push_btn],
115
  )
 
116
 
117
  push_btn.click(
118
  push_notebook,
119
  inputs=[download_link, dataset_name, token_box],
120
+ outputs=output_lbl,
121
  )
122
+
123
  demo.launch()