reachy-gestures-data / lab /real_sources.py
andito's picture
andito HF Staff
Data, training code and reproduction steps for reachy-gestures
0cc835c verified
Raw History Blame Contribute Delete
3.06 kB
"""Sample real conversational sentences from permissively licensed Hub datasets into generate.py's format.
python real_sources.py real.jsonl
OpenAssistant 2 (Apache 2.0): user turns -> react, first sentences of assistant replies -> say, many languages.
GoEmotions (Apache 2.0): English Reddit comments -> react. BRIGHTER (CC BY 4.0): emotional text in many languages -> react.
"""
import re
import sys
import gzip
import json
import random
import pandas as pd
from huggingface_hub import hf_hub_download
rng = random.Random(0)
SENTENCE = re.compile(r"(?<=[.!?…])\s+|(?<=[。!?])")
out: list[dict] = []
def short(text: str, max_words: int = 30) -> bool:
words = len(text.split()) if re.search(r"[a-zA-Zа-яА-Яа-я]", text) else len(text) / 2
return 1 <= words <= max_words and "```" not in text and "http" not in text and "\n" not in text.strip()
def add(kind: str, source: str, language: str, sentences: list[str]) -> None:
for start in range(0, len(sentences), 20):
out.append({"kind": kind, "source": source, "language": language, "req": f"{source}-{len(out)}",
"sentences": sentences[start : start + 20]})
# OpenAssistant 2
path = hf_hub_download("OpenAssistant/oasst2", "2023-11-05_oasst2_ready.messages.jsonl.gz", repo_type="dataset")
by_lang: dict[tuple[str, str], list[str]] = {}
for line in gzip.open(path, "rt"):
m = json.loads(line)
text = m["text"].strip()
if m["role"] == "prompter" and short(text):
by_lang.setdefault(("react", m["lang"]), []).append(text)
elif m["role"] == "assistant" and len(text) < 600:
first = [s for s in SENTENCE.split(text) if s.strip()][: rng.choice([1, 1, 2])]
joined = " ".join(first)
if first and short(joined):
by_lang.setdefault(("say", m["lang"]), []).append(joined)
for (kind, lang), texts in by_lang.items():
rng.shuffle(texts)
cap = 500 if lang == "en" else 120
add(kind, "oasst2", lang, texts[:cap])
# GoEmotions
frame = pd.read_parquet(hf_hub_download("google-research-datasets/go_emotions", "simplified/train-00000-of-00001.parquet", repo_type="dataset"))
texts = [t for t in frame["text"].tolist() if short(t) and "[NAME]" not in t]
rng.shuffle(texts)
add("react", "go_emotions", "en", texts[:600])
# BRIGHTER
for lang in ["deu", "esp", "rus", "chn", "hin", "ptbr", "ron", "ukr", "swe", "eng", "mar", "arq"]:
frame = pd.read_parquet(hf_hub_download("brighter-dataset/BRIGHTER-emotion-categories", f"{lang}/train-00000-of-00001.parquet", repo_type="dataset"))
texts = [t for t in frame["text"].tolist() if isinstance(t, str) and short(t) and "@" not in t]
rng.shuffle(texts)
add("react", "brighter", lang, texts[:80])
with open(sys.argv[1], "w") as f:
for row in out:
f.write(json.dumps(row, ensure_ascii=False) + "\n")
counts: dict[str, int] = {}
for row in out:
counts[f"{row['source']}/{row['kind']}"] = counts.get(f"{row['source']}/{row['kind']}", 0) + len(row["sentences"])
print(counts, "languages:", len({r["language"] for r in out}))