Roobick commited on
Commit
bd4c7ae
·
1 Parent(s): 86ffe1f

Add upload functionality for Kaggle kernels and integrate new Gradio interface for kernel uploads

Browse files
Files changed (1) hide show
  1. app.py +42 -5
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
- import os, json, pathlib, tempfile, datetime, shutil
3
  from typing import List, Dict, Optional
4
-
5
  from dotenv import load_dotenv
6
  load_dotenv()
7
 
@@ -67,7 +67,7 @@ def search_kernels(query: str, max_results: int = 20) -> List[Dict]:
67
 
68
  def download_kernel_notebook(kernel_ref: str) -> str:
69
  tmp_dir = tempfile.mkdtemp()
70
- api.kernels_pull(kernel_ref, path=tmp_dir, quiet=False)
71
 
72
  zip_path = shutil.make_archive(
73
  base_name=os.path.join(tmp_dir, "kernel"),
@@ -76,6 +76,23 @@ def download_kernel_notebook(kernel_ref: str) -> str:
76
  )
77
  return zip_path
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  search_iface = gr.Interface(
80
  fn=search_datasets,
81
  inputs=[
@@ -128,9 +145,29 @@ search_kernels_iface = gr.Interface(
128
  description="Find notebook or script kernels by keyword."
129
  )
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  demo = gr.TabbedInterface(
132
- [search_iface, list_files_iface, download_dataset_iface, search_kernels_iface, download_kernel_iface],
133
- tab_names=["Search Datasets", "Files", "Download dataset", "Search Kernels", "Download kernels"],
 
 
134
  )
135
 
136
  def _bootstrap_kaggle_credentials():
 
1
  import gradio as gr
2
+ import os, json, pathlib, tempfile, datetime, shutil, io
3
  from typing import List, Dict, Optional
4
+ from contextlib import redirect_stdout, redirect_stderr
5
  from dotenv import load_dotenv
6
  load_dotenv()
7
 
 
67
 
68
  def download_kernel_notebook(kernel_ref: str) -> str:
69
  tmp_dir = tempfile.mkdtemp()
70
+ api.kernels_pull(kernel_ref, path=tmp_dir, metadata=True, quiet=False)
71
 
72
  zip_path = shutil.make_archive(
73
  base_name=os.path.join(tmp_dir, "kernel"),
 
76
  )
77
  return zip_path
78
 
79
+ def upload_kernel_notebook(kernel_zip: str) -> Dict:
80
+ tmp_dir = tempfile.mkdtemp()
81
+ shutil.unpack_archive(kernel_zip, tmp_dir)
82
+ stdout_buf, stderr_buf = io.StringIO(), io.StringIO()
83
+ try:
84
+ with redirect_stderr(stdout_buf), redirect_stderr(stderr_buf):
85
+ api.kernels_push(folder=tmp_dir)
86
+ result_text = stdout_buf.getvalue().strip() or "kernel pushed"
87
+ except Exception as exc:
88
+ return{
89
+ "status": "error",
90
+ "detail": f"{exc}",
91
+ "stderr": stderr_buf.getvalue().strip(),
92
+ }
93
+ finally:
94
+ shutil.rmtree(tmp_dir, ignore_errors=True)
95
+
96
  search_iface = gr.Interface(
97
  fn=search_datasets,
98
  inputs=[
 
145
  description="Find notebook or script kernels by keyword."
146
  )
147
 
148
+ upload_kernel_iface = gr.Interface(
149
+ fn = upload_kernel_notebook,
150
+ inputs=gr.File(
151
+ label="Kernel ZIP",
152
+ file_types=[".zip"],
153
+ type="filepath",
154
+ show_label=True,
155
+ ),
156
+ outputs=gr.JSON(label="kaggle response"),
157
+ title="Push kaggle kernel",
158
+ description=(
159
+ "Upload a kernel folder to kaggle\n"
160
+ "Prepare a ZIP that contains **kernel-metadata.json** and the notebook "
161
+ "or script files.\n"
162
+ ),
163
+ )
164
+
165
+
166
  demo = gr.TabbedInterface(
167
+ [search_iface, list_files_iface, download_dataset_iface,
168
+ search_kernels_iface, download_kernel_iface, upload_kernel_iface],
169
+ tab_names=["Search Datasets", "Files", "Download dataset",
170
+ "Search Kernels", "Download kernels", "Upload kernel zip"],
171
  )
172
 
173
  def _bootstrap_kaggle_credentials():