Add search functionality for Kaggle kernels
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
-
import os, json, pathlib, tempfile
|
3 |
-
from typing import List, Dict
|
4 |
|
5 |
from dotenv import load_dotenv
|
6 |
load_dotenv()
|
@@ -35,6 +35,39 @@ def download_file(dataset_slug: str, file_name: str):
|
|
35 |
zip_path = pathlib.Path(tmp_dir) / f"{file_name}.zip"
|
36 |
return str(zip_path)
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
search_iface = gr.Interface(
|
39 |
fn=search_datasets,
|
40 |
inputs=[
|
@@ -65,9 +98,21 @@ download_file_iface = gr.Interface(
|
|
65 |
description="Downloads one file from the dataset and returns it."
|
66 |
)
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
demo = gr.TabbedInterface(
|
69 |
-
[search_iface, list_files_iface, download_file_iface],
|
70 |
-
tab_names=["Search", "Files", "Download"]
|
71 |
)
|
72 |
|
73 |
def _bootstrap_kaggle_credentials():
|
|
|
1 |
import gradio as gr
|
2 |
+
import os, json, pathlib, tempfile, datetime
|
3 |
+
from typing import List, Dict, Optional
|
4 |
|
5 |
from dotenv import load_dotenv
|
6 |
load_dotenv()
|
|
|
35 |
zip_path = pathlib.Path(tmp_dir) / f"{file_name}.zip"
|
36 |
return str(zip_path)
|
37 |
|
38 |
+
def search_kernels(query: str, max_results: int = 20, language: str = "Python") -> List[Dict]:
|
39 |
+
language_filter : Optional[str] = None if language == "all" else language.lower()
|
40 |
+
|
41 |
+
kernels = api.kernels_list(
|
42 |
+
search=query,
|
43 |
+
language=language_filter,
|
44 |
+
page_size=min(max_results, 20),
|
45 |
+
sort_by="voteCount",
|
46 |
+
)
|
47 |
+
|
48 |
+
out = []
|
49 |
+
for k in kernels[:max_results]:
|
50 |
+
last_run_raw = getattr(k, "lastRunTime", None) or getattr(k, "updated", None)
|
51 |
+
try:
|
52 |
+
last_run = (
|
53 |
+
datetime.datetime.fromisoformat(last_run_raw.rstrip("z"))
|
54 |
+
.strftime("%Y-%m-%d %H:%M") if last_run_raw else None
|
55 |
+
)
|
56 |
+
except Exception:
|
57 |
+
last_run = last_run_raw
|
58 |
+
out.append(
|
59 |
+
{
|
60 |
+
"title": k.title,
|
61 |
+
"ref": k.ref,
|
62 |
+
"language": k.language,
|
63 |
+
"votes": k.totalVotes,
|
64 |
+
"status": k.kernelStatus,
|
65 |
+
"last_run": last_run,
|
66 |
+
}
|
67 |
+
)
|
68 |
+
return out
|
69 |
+
|
70 |
+
|
71 |
search_iface = gr.Interface(
|
72 |
fn=search_datasets,
|
73 |
inputs=[
|
|
|
98 |
description="Downloads one file from the dataset and returns it."
|
99 |
)
|
100 |
|
101 |
+
search_kernels_iface = gr.Interface(
|
102 |
+
fn = search_kernels,
|
103 |
+
inputs = [
|
104 |
+
gr.Textbox(label="search term", placeholder="e.g. computer vision"),
|
105 |
+
gr.Slider(1, 50, step=1, value=20, label="Max results"),
|
106 |
+
gr.Dropdown(["All", "Python", "R"], value="Python", label="Language"),
|
107 |
+
],
|
108 |
+
outputs=gr.JSON(label="kernels"),
|
109 |
+
title="Search kaggle kernels",
|
110 |
+
description="Find notebook or script kernels by keyword, language, etc."
|
111 |
+
)
|
112 |
+
|
113 |
demo = gr.TabbedInterface(
|
114 |
+
[search_iface, list_files_iface, download_file_iface, search_kernels_iface],
|
115 |
+
tab_names=["Search Datasets", "Files", "Download File", "Search Kernels"],
|
116 |
)
|
117 |
|
118 |
def _bootstrap_kaggle_credentials():
|