Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- .gitignore +6 -0
- README.md +66 -8
- app.py +387 -0
- requirements.txt +4 -0
.gitignore
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
.venv/
|
| 4 |
+
*.jsonl
|
| 5 |
+
flagged/
|
| 6 |
+
.DS_Store
|
README.md
CHANGED
|
@@ -1,15 +1,73 @@
|
|
| 1 |
---
|
| 2 |
title: Dataset Creator
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
---
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Dataset Creator
|
| 3 |
+
emoji: 🧩
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.9.1
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
hf_oauth: true
|
| 11 |
+
hf_oauth_scopes:
|
| 12 |
+
- write-repos
|
| 13 |
---
|
| 14 |
|
| 15 |
+
# Dataset Creator
|
| 16 |
+
|
| 17 |
+
Combine chat-format conversation data from multiple Hugging Face datasets into
|
| 18 |
+
one shuffled, ready-to-train dataset - no notebook required.
|
| 19 |
+
|
| 20 |
+
## What it does
|
| 21 |
+
|
| 22 |
+
- Add any number of HF datasets by repo id (dynamic list, no cap).
|
| 23 |
+
- Each one gets a schema check via the lightweight `datasets-server` API
|
| 24 |
+
(falls back to a short streaming pull for gated or script-based datasets).
|
| 25 |
+
Known ShareGPT/OpenAI-style conversation lists and a handful of common flat
|
| 26 |
+
field pairs (`prompt`/`chosen`, `query`/`answer`, etc.) are detected
|
| 27 |
+
automatically.
|
| 28 |
+
- Anything that doesn't match a known shape shows you the raw columns so you
|
| 29 |
+
can map them by hand - no silent guesses, no half-right auto-mapping.
|
| 30 |
+
- Per-dataset sample limit and an optional system-prompt override.
|
| 31 |
+
- Build the combined, shuffled dataset, then either push it straight to your
|
| 32 |
+
own HF account (private or public, via your own OAuth login) or download it
|
| 33 |
+
as JSONL.
|
| 34 |
+
|
| 35 |
+
## Auth
|
| 36 |
+
|
| 37 |
+
Sign in with the button at the top. Pushing to the Hub uses *your* account
|
| 38 |
+
through HF's OAuth flow - there's no token to paste in, and nothing is stored
|
| 39 |
+
server-side between sessions.
|
| 40 |
+
|
| 41 |
+
## Project layout
|
| 42 |
+
|
| 43 |
+
- `app.py` - Gradio UI and event wiring.
|
| 44 |
+
- `core/` - pure logic: schema detection (`schema_detect.py`) and row mapping
|
| 45 |
+
(`field_mapper.py`). No network calls in here, which makes it the easy part
|
| 46 |
+
to unit test on its own - see the inline examples in each module's tests
|
| 47 |
+
if you add a `tests/` folder later.
|
| 48 |
+
- `services/` - everything that talks to Hugging Face: peeking at a dataset's
|
| 49 |
+
shape (`hf_inspect.py`), pulling the real rows (`hf_dataset_loader.py`), and
|
| 50 |
+
pushing/exporting the result (`hf_publish.py`).
|
| 51 |
+
|
| 52 |
+
## Known limitations
|
| 53 |
+
|
| 54 |
+
- Auto-detection is intentionally conservative. It only auto-applies a
|
| 55 |
+
mapping when it recognizes both conversation-role tags (`human`/`gpt`,
|
| 56 |
+
`user`/`assistant`, `user`/`model`) or an exact known flat-field-name pair.
|
| 57 |
+
Anything else routes to manual mapping on purpose - a near-miss column name
|
| 58 |
+
(e.g. `question`/`chosen` instead of `prompt`/`chosen`) won't get silently
|
| 59 |
+
mis-mapped. The known-pairs list is a plain Python list at the top of
|
| 60 |
+
`core/schema_detect.py` if you want to extend it for patterns you hit a lot.
|
| 61 |
+
- Datasets that bundle extra per-row enrichment beyond a simple triplet (for
|
| 62 |
+
example, appending a vulnerability note to an answer, or synthesizing a
|
| 63 |
+
prompt from a code snippet plus a language tag) won't be replicated by the
|
| 64 |
+
generic mapper - it pulls the two fields you point it at, verbatim. If you
|
| 65 |
+
need that kind of enrichment again, it's a small addition to
|
| 66 |
+
`core/field_mapper.py`.
|
| 67 |
+
- Gated datasets need the signed-in user's token to actually have read
|
| 68 |
+
access; there's no separate "request access" flow built in here.
|
| 69 |
+
- The `gr.OAuthToken` / `gr.OAuthProfile` injection is wired to the documented
|
| 70 |
+
Gradio pattern (annotated trailing parameters, not listed in `inputs=`) but
|
| 71 |
+
could only be validated structurally in this environment - there was no
|
| 72 |
+
live Spaces OAuth session available to confirm end to end. Worth a quick
|
| 73 |
+
sign-in-and-push smoke test right after deploying, before relying on it.
|
app.py
ADDED
|
@@ -0,0 +1,387 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Dataset Creator - Gradio app.
|
| 2 |
+
|
| 3 |
+
Lets you list any number of Hugging Face datasets, auto-detects (or lets
|
| 4 |
+
you manually map) how each one's rows turn into a chat-format triplet,
|
| 5 |
+
then combines, shuffles, and either pushes the result to your own HF
|
| 6 |
+
account or hands you a JSONL download.
|
| 7 |
+
|
| 8 |
+
UI wiring conventions used throughout this file, so the OAuth injection
|
| 9 |
+
keeps working:
|
| 10 |
+
- Every per-entry callback takes `entries` (the gr.State list) and
|
| 11 |
+
`uid` (a `gr.State(uid)` constant created at render time) as its
|
| 12 |
+
first two parameters, matching the first two items in `inputs=`.
|
| 13 |
+
- `oauth_token` / `oauth_profile` are declared as trailing, annotated
|
| 14 |
+
parameters and are *never* included in `inputs=` - Gradio resolves
|
| 15 |
+
them automatically from the signed-in session. See the Gradio OAuth
|
| 16 |
+
guide; this is the one part of the app that needs a live Spaces
|
| 17 |
+
OAuth session to fully smoke-test.
|
| 18 |
+
"""
|
| 19 |
+
from __future__ import annotations
|
| 20 |
+
|
| 21 |
+
import os
|
| 22 |
+
import random
|
| 23 |
+
import tempfile
|
| 24 |
+
|
| 25 |
+
import gradio as gr
|
| 26 |
+
|
| 27 |
+
from core import field_mapper, schema_detect
|
| 28 |
+
from core.models import DatasetEntry, FieldMapping
|
| 29 |
+
from services import hf_dataset_loader, hf_inspect, hf_publish
|
| 30 |
+
|
| 31 |
+
_STATUS_LABELS = {
|
| 32 |
+
"empty": "Not detected yet.",
|
| 33 |
+
"detecting": "Checking schema...",
|
| 34 |
+
"needs_mapping": "Couldn't auto-detect the field layout - map it manually below.",
|
| 35 |
+
"ready": "Ready.",
|
| 36 |
+
"error": "Error.",
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _find(entries: list, uid: str) -> DatasetEntry:
|
| 41 |
+
for entry in entries:
|
| 42 |
+
if entry.uid == uid:
|
| 43 |
+
return entry
|
| 44 |
+
raise ValueError(f"No dataset entry with uid {uid!r} - it may have been removed.")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def _status_text(entry: DatasetEntry) -> str:
|
| 48 |
+
label = _STATUS_LABELS.get(entry.status, entry.status)
|
| 49 |
+
if entry.status == "error" and entry.error_message:
|
| 50 |
+
return f"**Status:** {label} {entry.error_message}"
|
| 51 |
+
if entry.status == "ready" and entry.mapping:
|
| 52 |
+
return f"**Status:** {label} (mapping type: `{entry.mapping.kind}`)"
|
| 53 |
+
return f"**Status:** {label}"
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# --- entry list mutation ----------------------------------------------------
|
| 57 |
+
|
| 58 |
+
def add_entry(entries: list) -> list:
|
| 59 |
+
return entries + [DatasetEntry()]
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def remove_entry(entries: list, uid: str) -> list:
|
| 63 |
+
return [e for e in entries if e.uid != uid]
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def update_field(entries: list, uid: str, field_name: str, value) -> list:
|
| 67 |
+
entry = _find(entries, uid)
|
| 68 |
+
setattr(entry, field_name, value)
|
| 69 |
+
return entries
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def update_limit(entries: list, uid: str, value) -> list:
|
| 73 |
+
entry = _find(entries, uid)
|
| 74 |
+
entry.limit = int(value) if value else 0
|
| 75 |
+
return entries
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# --- schema detection / mapping ---------------------------------------------
|
| 79 |
+
|
| 80 |
+
def detect_entry(entries: list, uid: str, oauth_token: gr.OAuthToken | None) -> list:
|
| 81 |
+
entry = _find(entries, uid)
|
| 82 |
+
if not entry.repo_id.strip():
|
| 83 |
+
entry.status = "error"
|
| 84 |
+
entry.error_message = "Enter a dataset repo id first."
|
| 85 |
+
return entries
|
| 86 |
+
|
| 87 |
+
entry.status = "detecting"
|
| 88 |
+
token = oauth_token.token if oauth_token else None
|
| 89 |
+
try:
|
| 90 |
+
rows = hf_inspect.peek_rows(
|
| 91 |
+
entry.repo_id.strip(),
|
| 92 |
+
entry.subset.strip(),
|
| 93 |
+
entry.split.strip() or "train",
|
| 94 |
+
sample_size=8,
|
| 95 |
+
token=token,
|
| 96 |
+
)
|
| 97 |
+
except hf_inspect.DatasetInspectError as exc:
|
| 98 |
+
entry.status = "error"
|
| 99 |
+
entry.error_message = str(exc)
|
| 100 |
+
return entries
|
| 101 |
+
|
| 102 |
+
entry.sample_rows = rows
|
| 103 |
+
entry.detected_columns = list(rows[0].keys())
|
| 104 |
+
entry.detected_list_info = schema_detect.detect_list_column(rows)
|
| 105 |
+
mapping = schema_detect.auto_detect(rows)
|
| 106 |
+
|
| 107 |
+
if mapping:
|
| 108 |
+
entry.mapping = mapping
|
| 109 |
+
entry.status = "ready"
|
| 110 |
+
entry.error_message = ""
|
| 111 |
+
else:
|
| 112 |
+
entry.mapping = None
|
| 113 |
+
entry.status = "needs_mapping"
|
| 114 |
+
entry.error_message = ""
|
| 115 |
+
return entries
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def apply_flat_mapping(entries: list, uid: str, user_field: str, assistant_field: str) -> list:
|
| 119 |
+
entry = _find(entries, uid)
|
| 120 |
+
if not user_field or not assistant_field:
|
| 121 |
+
entry.status = "error"
|
| 122 |
+
entry.error_message = "Pick both a user field and an assistant field."
|
| 123 |
+
return entries
|
| 124 |
+
entry.mapping = FieldMapping(
|
| 125 |
+
kind="flat_pair",
|
| 126 |
+
config={"user_field": user_field, "assistant_field": assistant_field},
|
| 127 |
+
)
|
| 128 |
+
entry.status = "ready"
|
| 129 |
+
entry.error_message = ""
|
| 130 |
+
return entries
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def apply_list_mapping(
|
| 134 |
+
entries: list,
|
| 135 |
+
uid: str,
|
| 136 |
+
list_field: str,
|
| 137 |
+
role_key: str,
|
| 138 |
+
content_key: str,
|
| 139 |
+
human_tag: str,
|
| 140 |
+
assistant_tag: str,
|
| 141 |
+
) -> list:
|
| 142 |
+
entry = _find(entries, uid)
|
| 143 |
+
if not all([list_field, role_key, content_key, human_tag, assistant_tag]):
|
| 144 |
+
entry.status = "error"
|
| 145 |
+
entry.error_message = "Fill in every field for the conversation-list mapping."
|
| 146 |
+
return entries
|
| 147 |
+
if human_tag == assistant_tag:
|
| 148 |
+
entry.status = "error"
|
| 149 |
+
entry.error_message = "Human and assistant tags can't be the same value."
|
| 150 |
+
return entries
|
| 151 |
+
entry.mapping = FieldMapping(
|
| 152 |
+
kind="conversation_list",
|
| 153 |
+
config={
|
| 154 |
+
"list_field": list_field,
|
| 155 |
+
"role_key": role_key,
|
| 156 |
+
"content_key": content_key,
|
| 157 |
+
"human_tag": human_tag,
|
| 158 |
+
"gpt_tag": assistant_tag,
|
| 159 |
+
},
|
| 160 |
+
)
|
| 161 |
+
entry.status = "ready"
|
| 162 |
+
entry.error_message = ""
|
| 163 |
+
return entries
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
# --- combine / export --------------------------------------------------------
|
| 167 |
+
|
| 168 |
+
def run_pipeline(
|
| 169 |
+
entries: list,
|
| 170 |
+
seed,
|
| 171 |
+
oauth_token: gr.OAuthToken | None,
|
| 172 |
+
progress=gr.Progress(),
|
| 173 |
+
):
|
| 174 |
+
ready_entries = [e for e in entries if e.status == "ready" and e.mapping is not None]
|
| 175 |
+
if not ready_entries:
|
| 176 |
+
raise gr.Error("No datasets are ready. Detect or manually map at least one dataset first.")
|
| 177 |
+
|
| 178 |
+
token = oauth_token.token if oauth_token else None
|
| 179 |
+
all_records = []
|
| 180 |
+
breakdown_lines = []
|
| 181 |
+
total = len(ready_entries)
|
| 182 |
+
|
| 183 |
+
for idx, entry in enumerate(ready_entries):
|
| 184 |
+
progress((idx, total), desc=f"Loading {entry.repo_id}")
|
| 185 |
+
try:
|
| 186 |
+
rows = hf_dataset_loader.load_limited(
|
| 187 |
+
entry.repo_id.strip(),
|
| 188 |
+
entry.subset.strip(),
|
| 189 |
+
entry.split.strip() or "train",
|
| 190 |
+
int(entry.limit),
|
| 191 |
+
token=token,
|
| 192 |
+
)
|
| 193 |
+
except Exception as exc:
|
| 194 |
+
raise gr.Error(f"Failed loading '{entry.repo_id}': {exc}")
|
| 195 |
+
|
| 196 |
+
kept = 0
|
| 197 |
+
for row in rows:
|
| 198 |
+
triplet = field_mapper.extract_triplet(row, entry.mapping, entry.system_prompt)
|
| 199 |
+
if triplet:
|
| 200 |
+
all_records.append(triplet)
|
| 201 |
+
kept += 1
|
| 202 |
+
breakdown_lines.append(f"- `{entry.repo_id}` ({entry.split}): {kept}/{len(rows)} rows usable")
|
| 203 |
+
|
| 204 |
+
progress((total, total), desc="Shuffling")
|
| 205 |
+
rng = random.Random(int(seed)) if seed not in (None, "") else random.Random()
|
| 206 |
+
rng.shuffle(all_records)
|
| 207 |
+
|
| 208 |
+
summary = f"**Combined {len(all_records)} records from {total} dataset(s):**\n\n" + "\n".join(breakdown_lines)
|
| 209 |
+
return all_records, summary
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
def do_push(
|
| 213 |
+
records: list,
|
| 214 |
+
repo_name: str,
|
| 215 |
+
private: bool,
|
| 216 |
+
oauth_token: gr.OAuthToken | None,
|
| 217 |
+
oauth_profile: gr.OAuthProfile | None,
|
| 218 |
+
):
|
| 219 |
+
if not records:
|
| 220 |
+
raise gr.Error("Build the combined dataset first.")
|
| 221 |
+
if not oauth_token or not oauth_profile:
|
| 222 |
+
raise gr.Error("Sign in with your Hugging Face account first.")
|
| 223 |
+
if not repo_name.strip():
|
| 224 |
+
raise gr.Error("Give the dataset a name.")
|
| 225 |
+
|
| 226 |
+
repo_id = f"{oauth_profile.username}/{repo_name.strip()}"
|
| 227 |
+
try:
|
| 228 |
+
url = hf_publish.push_dataset(records, repo_id, bool(private), oauth_token.token)
|
| 229 |
+
except Exception as exc:
|
| 230 |
+
raise gr.Error(f"Push failed: {exc}")
|
| 231 |
+
return f"Pushed. View it at {url}"
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def do_download(records: list):
|
| 235 |
+
if not records:
|
| 236 |
+
raise gr.Error("Build the combined dataset first.")
|
| 237 |
+
out_dir = tempfile.mkdtemp(prefix="sage_dataset_")
|
| 238 |
+
out_path = os.path.join(out_dir, "combined_dataset.jsonl")
|
| 239 |
+
hf_publish.write_jsonl(records, out_path)
|
| 240 |
+
return out_path
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
# --- UI -----------------------------------------------------------------------
|
| 244 |
+
|
| 245 |
+
with gr.Blocks(title="Dataset Creator") as demo:
|
| 246 |
+
gr.Markdown("# Dataset Creator")
|
| 247 |
+
gr.Markdown(
|
| 248 |
+
"Combine chat-format data from multiple Hugging Face datasets into one "
|
| 249 |
+
"shuffled set. Sign in to push the result to your own account, or just "
|
| 250 |
+
"download it as JSONL."
|
| 251 |
+
)
|
| 252 |
+
gr.LoginButton()
|
| 253 |
+
|
| 254 |
+
entries_state = gr.State([])
|
| 255 |
+
combined_state = gr.State([])
|
| 256 |
+
|
| 257 |
+
add_btn = gr.Button("+ Add dataset")
|
| 258 |
+
|
| 259 |
+
@gr.render(inputs=entries_state)
|
| 260 |
+
def render_entries(entries):
|
| 261 |
+
for entry in entries:
|
| 262 |
+
uid = entry.uid
|
| 263 |
+
uid_state = gr.State(uid)
|
| 264 |
+
|
| 265 |
+
with gr.Group():
|
| 266 |
+
gr.Markdown(f"**{entry.repo_id or 'New dataset'}**", key=f"title-{uid}")
|
| 267 |
+
|
| 268 |
+
with gr.Row():
|
| 269 |
+
repo_tb = gr.Textbox(
|
| 270 |
+
label="HF dataset repo",
|
| 271 |
+
value=entry.repo_id,
|
| 272 |
+
placeholder="e.g. NousResearch/hermes-function-calling-v1",
|
| 273 |
+
key=f"repo-{uid}",
|
| 274 |
+
)
|
| 275 |
+
subset_tb = gr.Textbox(
|
| 276 |
+
label="Config / subset (optional)", value=entry.subset, key=f"subset-{uid}"
|
| 277 |
+
)
|
| 278 |
+
split_tb = gr.Textbox(label="Split", value=entry.split or "train", key=f"split-{uid}")
|
| 279 |
+
limit_num = gr.Number(
|
| 280 |
+
label="Sample limit", value=entry.limit, precision=0, key=f"limit-{uid}"
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
system_tb = gr.Textbox(
|
| 284 |
+
label="System prompt override",
|
| 285 |
+
value=entry.system_prompt,
|
| 286 |
+
lines=2,
|
| 287 |
+
key=f"sys-{uid}",
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
repo_field = gr.State("repo_id")
|
| 291 |
+
subset_field = gr.State("subset")
|
| 292 |
+
split_field = gr.State("split")
|
| 293 |
+
sys_field = gr.State("system_prompt")
|
| 294 |
+
|
| 295 |
+
repo_tb.change(update_field, [entries_state, uid_state, repo_field, repo_tb], entries_state)
|
| 296 |
+
subset_tb.change(update_field, [entries_state, uid_state, subset_field, subset_tb], entries_state)
|
| 297 |
+
split_tb.change(update_field, [entries_state, uid_state, split_field, split_tb], entries_state)
|
| 298 |
+
limit_num.change(update_limit, [entries_state, uid_state, limit_num], entries_state)
|
| 299 |
+
system_tb.change(update_field, [entries_state, uid_state, sys_field, system_tb], entries_state)
|
| 300 |
+
|
| 301 |
+
with gr.Row():
|
| 302 |
+
detect_btn = gr.Button("Detect schema", key=f"detect-{uid}")
|
| 303 |
+
remove_btn = gr.Button("Remove", variant="stop", key=f"remove-{uid}")
|
| 304 |
+
|
| 305 |
+
gr.Markdown(_status_text(entry), key=f"status-{uid}")
|
| 306 |
+
|
| 307 |
+
detect_btn.click(detect_entry, [entries_state, uid_state], entries_state)
|
| 308 |
+
remove_btn.click(remove_entry, [entries_state, uid_state], entries_state)
|
| 309 |
+
|
| 310 |
+
if entry.status == "needs_mapping":
|
| 311 |
+
gr.Markdown(
|
| 312 |
+
f"Columns found: `{', '.join(entry.detected_columns)}`", key=f"cols-{uid}"
|
| 313 |
+
)
|
| 314 |
+
with gr.Row():
|
| 315 |
+
user_field_dd = gr.Dropdown(
|
| 316 |
+
choices=entry.detected_columns, label="User field", key=f"userf-{uid}"
|
| 317 |
+
)
|
| 318 |
+
asst_field_dd = gr.Dropdown(
|
| 319 |
+
choices=entry.detected_columns, label="Assistant field", key=f"asstf-{uid}"
|
| 320 |
+
)
|
| 321 |
+
apply_flat_btn = gr.Button("Apply flat mapping", key=f"applyflat-{uid}")
|
| 322 |
+
apply_flat_btn.click(
|
| 323 |
+
apply_flat_mapping,
|
| 324 |
+
[entries_state, uid_state, user_field_dd, asst_field_dd],
|
| 325 |
+
entries_state,
|
| 326 |
+
)
|
| 327 |
+
|
| 328 |
+
if entry.detected_list_info:
|
| 329 |
+
info = entry.detected_list_info
|
| 330 |
+
gr.Markdown(
|
| 331 |
+
f"Also found a conversation-style column: `{info['list_field']}` "
|
| 332 |
+
f"(role key `{info['role_key']}`, content key `{info['content_key']}`, "
|
| 333 |
+
f"tags seen: {info['tag_values']})",
|
| 334 |
+
key=f"listinfo-{uid}",
|
| 335 |
+
)
|
| 336 |
+
with gr.Row():
|
| 337 |
+
human_tag_dd = gr.Dropdown(
|
| 338 |
+
choices=info["tag_values"], label="Human/user tag", key=f"humant-{uid}"
|
| 339 |
+
)
|
| 340 |
+
asst_tag_dd = gr.Dropdown(
|
| 341 |
+
choices=info["tag_values"], label="Assistant tag", key=f"asstt-{uid}"
|
| 342 |
+
)
|
| 343 |
+
apply_list_btn = gr.Button(
|
| 344 |
+
"Apply conversation-list mapping", key=f"applylist-{uid}"
|
| 345 |
+
)
|
| 346 |
+
|
| 347 |
+
list_field_state = gr.State(info["list_field"])
|
| 348 |
+
role_key_state = gr.State(info["role_key"])
|
| 349 |
+
content_key_state = gr.State(info["content_key"])
|
| 350 |
+
|
| 351 |
+
apply_list_btn.click(
|
| 352 |
+
apply_list_mapping,
|
| 353 |
+
[
|
| 354 |
+
entries_state,
|
| 355 |
+
uid_state,
|
| 356 |
+
list_field_state,
|
| 357 |
+
role_key_state,
|
| 358 |
+
content_key_state,
|
| 359 |
+
human_tag_dd,
|
| 360 |
+
asst_tag_dd,
|
| 361 |
+
],
|
| 362 |
+
entries_state,
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
+
add_btn.click(add_entry, entries_state, entries_state)
|
| 366 |
+
|
| 367 |
+
gr.Markdown("---")
|
| 368 |
+
gr.Markdown("## Combine")
|
| 369 |
+
seed_num = gr.Number(label="Shuffle seed (optional)", precision=0)
|
| 370 |
+
build_btn = gr.Button("Build combined dataset", variant="primary")
|
| 371 |
+
summary_md = gr.Markdown()
|
| 372 |
+
build_btn.click(run_pipeline, [entries_state, seed_num], [combined_state, summary_md])
|
| 373 |
+
|
| 374 |
+
gr.Markdown("## Export")
|
| 375 |
+
with gr.Row():
|
| 376 |
+
repo_name_tb = gr.Textbox(label="Dataset name (goes under your HF username)")
|
| 377 |
+
private_cb = gr.Checkbox(label="Private", value=True)
|
| 378 |
+
push_btn = gr.Button("Push to Hub")
|
| 379 |
+
push_result_md = gr.Markdown()
|
| 380 |
+
push_btn.click(do_push, [combined_state, repo_name_tb, private_cb], push_result_md)
|
| 381 |
+
|
| 382 |
+
download_btn = gr.Button("Download as JSONL")
|
| 383 |
+
download_file = gr.File(label="Download")
|
| 384 |
+
download_btn.click(do_download, combined_state, download_file)
|
| 385 |
+
|
| 386 |
+
if __name__ == "__main__":
|
| 387 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=5.6.0
|
| 2 |
+
datasets>=2.19.0
|
| 3 |
+
huggingface_hub>=0.24.0
|
| 4 |
+
requests>=2.31.0
|