File size: 10,839 Bytes
a6b603e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6364fa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc19eac
 
 
6364fa2
 
47398eb
a6b603e
 
 
6364fa2
a6b603e
 
 
 
6364fa2
a6b603e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# -*- coding: utf-8 -*-
# Open Dataset Finder (HF / Zenodo / Kaggle) with Gradio MCP enabled

import os, io, re, html, time, csv, subprocess, string, typing as T, json
from dataclasses import dataclass
from datetime import datetime

import requests
import pandas as pd
from rapidfuzz import fuzz
from rank_bm25 import BM25Okapi
from huggingface_hub import list_datasets, HfApi
import gradio as gr

# -------------------- Common Utilities --------------------
def to_dt_str(x) -> str:
    """Safely convert datetime or string into YYYY-MM-DD."""
    if not x:
        return ""
    if isinstance(x, datetime):
        return x.strftime("%Y-%m-%d")
    s = str(x)
    for fmt in ("%Y-%m-%d", "%Y-%m-%dT%H:%M:%S%z", "%Y-%m-%dT%H:%M:%S", "%Y/%m/%d", "%d/%m/%Y"):
        try:
            return datetime.strptime(s.replace("Z",""), fmt).strftime("%Y-%m-%d")
        except:
            pass
    return s[:10]

def tokenize(s: str) -> T.List[str]:
    s = (s or "").lower()
    for ch in string.punctuation:
        s = s.replace(ch, " ")
    return [w for w in s.split() if w]

# -------------------- Standard Schema --------------------
@dataclass
class Row:
    source: str
    id: str
    title: str
    description: str
    updated: str
    url: str
    download_url: str
    formats: T.List[str]

# -------------------- Hugging Face (datasets) --------------------
def search_hf(q, limit=40):
    """Use list_datasets → optionally enrich with dataset_info."""
    out = []
    api = HfApi()
    try:
        ds_list = list_datasets(search=q, limit=limit)
    except Exception as e:
        print("HF list_datasets error:", e)
        return out

    for d in ds_list:
        ds_id = getattr(d, "id", None) or ""
        title = ds_id
        url   = f"https://huggingface.co/datasets/{ds_id}"
        updated = to_dt_str(getattr(d, "lastModified", None) or getattr(d, "updated_at", None))

        desc = ""
        fmts = []
        try:
            info = api.dataset_info(ds_id, timeout=15)
            card = getattr(info, "cardData", None) or {}
            desc = (card.get("description") if isinstance(card, dict) else "") or ""
            updated = to_dt_str(getattr(info, "lastModified", None) or getattr(info, "updated_at", None)) or updated
        except Exception:
            pass

        out.append(Row("huggingface", ds_id, title, desc, updated, url, "", fmts))
    return out

# -------------------- Zenodo --------------------
SAFE_TIMEOUT=20
UA={"User-Agent":"OpenDatasetFinder/mini/0.2 (+HF Space)"}

def safe_get(url, params=None, timeout=SAFE_TIMEOUT, retries=2):
    for i in range(retries+1):
        try:
            r = requests.get(url, params=params, headers=UA, timeout=timeout)
            r.raise_for_status()
            return r
        except Exception:
            if i==retries:
                raise
            time.sleep(1.2*(i+1))

def search_zenodo(q, limit=40):
    base="https://zenodo.org/api/records"
    r = safe_get(base, params={"q":q, "type":"dataset", "size":limit})
    hits = r.json().get("hits",{}).get("hits",[])
    out=[]
    for h in hits:
        md=h.get("metadata",{}) or {}
        title = md.get("title") or h.get("title") or ""
        desc  = re.sub(r"<[^>]+>"," ", html.unescape(md.get("description") or "")).strip()
        url   = (h.get("links",{}) or {}).get("html","")
        files = h.get("files") or []
        fmts  = list({(f.get("type") or f.get("mimetype") or "").split("/")[-1] for f in files if f})
        dl    = files[0].get("links",{}).get("self","") if files else ""
        upd   = to_dt_str(h.get("updated"))
        out.append(Row("zenodo", str(h.get("id") or ""), title, desc, upd, url, dl, [f for f in fmts if f]))
    return out

# -------------------- Kaggle (env creds auto) --------------------
def ensure_kaggle_credentials():
    """If env vars exist, create ~/.kaggle/kaggle.json with correct permissions."""
    path = os.path.expanduser("~/.kaggle/kaggle.json")
    if os.path.exists(path):
        return
    user = os.environ.get("KAGGLE_USERNAME")
    key  = os.environ.get("KAGGLE_KEY")
    if not (user and key):
        return
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "w") as f:
        json.dump({"username": user, "key": key}, f)
    os.chmod(path, 0o600)

def kaggle_available():
    cred_path = os.path.expanduser("~/.kaggle/kaggle.json")
    return bool(os.environ.get("KAGGLE_USERNAME") and os.environ.get("KAGGLE_KEY")) or os.path.exists(cred_path)

def search_kaggle(q, limit=40):
    """API first → fallback CLI if empty/failure."""
    rows=[]
    try:
        ensure_kaggle_credentials()
        from kaggle.api.kaggle_api_extended import KaggleApi
        api=KaggleApi(); api.authenticate()

        try:
            api_res = api.dataset_list(search=q, page=1)
        except TypeError:
            api_res = []

        if api_res:
            for d in api_res[:limit]:
                try:
                    m = api.dataset_view(d.ref)
                    desc=(getattr(m, "description", "") or "").strip()
                    upd = to_dt_str(getattr(m, "lastUpdated", None))
                except Exception:
                    desc, upd = "", ""
                fmts=[]
                try:
                    files=api.dataset_list_files(d.ref).files
                    for f in files:
                        ext=(f.name.split(".")[-1] if "." in f.name else "").lower()
                        if ext: fmts.append(ext)
                    fmts = sorted(set(fmts))
                except Exception:
                    pass
                url=f"https://www.kaggle.com/datasets/{d.ref}"
                rows.append(Row("kaggle", d.ref, d.title or d.ref, desc, upd, url, url, fmts))
            return rows
    except Exception:
        pass

    try:
        cli = subprocess.run(
            ["kaggle", "datasets", "list", "-s", q, "--csv", "-p", "1", "-r", str(max(20, min(100, limit)))],
            capture_output=True, text=True
        )
        if cli.returncode == 0 and cli.stdout.strip():
            f = io.StringIO(cli.stdout)
            reader = csv.DictReader(f)
            for i, r in enumerate(reader):
                if i >= limit:
                    break
                title = r.get("title") or ""
                url   = r.get("url") or ""
                ref = "/".join(url.rstrip("/").split("/")[-2:]) if "/datasets/" in url else url
                rows.append(Row(
                    "kaggle",
                    ref,
                    title,
                    (r.get("subtitle") or "").strip(),
                    (r.get("lastUpdated") or "")[:10],
                    url,
                    url,
                    []
                ))
    except Exception:
        pass

    return rows

# -------------------- Ranking --------------------
def rank(q: str, rows: T.List[Row]):
    if not rows:
        return pd.DataFrame(columns=["source","id","title","description","updated","url","download_url","formats","score"])
    docs=[tokenize(r.title+" "+r.description) for r in rows]
    bm25=BM25Okapi(docs)
    qtok=tokenize(q)
    bm=bm25.get_scores(qtok)
    mx=max(bm) if len(bm)>0 else 1.0
    scored=[]
    for i,r in enumerate(rows):
        fz=fuzz.token_set_ratio(q, r.title+" "+r.description)/100.0
        rec=0.0
        try:
            if r.updated:
                days=(datetime.utcnow()-datetime.strptime(r.updated,"%Y-%m-%d")).days
                rec=max(0.0, 1.0-min(days,365)/365.0)
        except:
            pass
        score=0.6*(bm[i]/(mx+1e-9))+0.35*fz+0.05*rec
        scored.append([r.source,r.id,r.title,r.description[:500],r.updated,r.url,r.download_url,", ".join(r.formats), round(float(score),4)])
    df=pd.DataFrame(scored, columns=["source","id","title","description","updated","url","download_url","formats","score"])
    return df.sort_values("score", ascending=False).reset_index(drop=True)

# -------------------- Gradio UI --------------------
with gr.Blocks(title="Open Dataset Finder (HF • Zenodo • Kaggle)") as demo:
    gr.Markdown(
        """
# 🔍 Open Dataset Finder

This app lets you search datasets from multiple open data sources.

- **Hugging Face Datasets**: Public machine learning datasets for NLP, computer vision, speech, and more.  
- **Zenodo**: Research datasets shared by scientists and institutions, often linked to academic publications.  
- **Kaggle**: Community datasets, competition datasets, and practice datasets shared on Kaggle.  

### Kaggle authentication
To enable Kaggle search, you need to add your Kaggle API credentials as **Repository secrets** in the Space settings:

- `KAGGLE_USERNAME`: your Kaggle username  
- `KAGGLE_KEY`: your Kaggle API token (found in the `kaggle.json` file you can download from your Kaggle account)  

Once the secrets are set, you can check the Kaggle box in the UI and search Kaggle datasets directly here.

### Source repository
- GitHub: https://github.com/hyeonseo2/dataset-search-mcp
        """
    )

    with gr.Row():
        q = gr.Textbox(label="Query / Idea", value="korean weather")
        k = gr.Slider(10, 200, value=40, step=10, label="Results per source")

    with gr.Row():
        use_hf   = gr.Checkbox(value=True,  label="Hugging Face")
        use_zen  = gr.Checkbox(value=True,  label="Zenodo")
        use_kg   = gr.Checkbox(value=False, label="Kaggle")

    btn = gr.Button("Search", variant="primary")
    out = gr.Dataframe(wrap=True)
    log = gr.Textbox(label="Logs", lines=8)

    def do_search(q_, k_, u_hf, u_zen, u_kg):
        logs=[]
        rows=[]
        try:
            if u_hf:
                logs.append("Searching Hugging Face…")
                rows+=search_hf(q_, int(k_))
        except Exception as e:
            logs.append(f"HF error: {e}")

        try:
            if u_zen:
                logs.append("Searching Zenodo…")
                rows+=search_zenodo(q_, int(k_))
        except Exception as e:
            logs.append(f"Zenodo error: {e}")

        if u_kg:
            if kaggle_available():
                try:
                    logs.append("Searching Kaggle…")
                    rows+=search_kaggle(q_, int(k_))
                except Exception as e:
                    logs.append(f"Kaggle error: {e}")
            else:
                logs.append("No Kaggle credentials found → skipped")

        df=rank(q_, rows)
        logs.append(f"Total {len(df)} results")
        return df, "\n".join(logs)

    btn.click(do_search, inputs=[q,k,use_hf,use_zen,use_kg], outputs=[out, log])

# -------------------- Run (Gradio + MCP) --------------------
if __name__ == "__main__":
    demo.queue().launch(
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True,
        debug=False,
        mcp_server=True,
    )