uploads / synthgen /multiturn2singleturn.py
EdwardSJ151's picture
Upload folder using huggingface_hub
3af75d1 verified
#!/usr/bin/env python3
import json
input_path = "cemig_resumo.jsonl"
output_path = "cemig_resumo_v0.jsonl"
TARGET = "Informação não encontrada no contexto fornecido."
def bad(t):
return not isinstance(t, str) or not t.strip() or t.strip() == TARGET
def first_pair(conv):
if not isinstance(conv, list):
return None
u = None
for i, m in enumerate(conv):
if isinstance(m, dict) and m.get("role") == "user":
u = (i, m)
break
if not u:
return None
for m in conv[u[0] + 1:]:
if isinstance(m, dict) and m.get("role") == "assistant":
return u[1], m
return None
total = dropped = 0
with open(input_path, encoding="utf-8") as fin, open(output_path, "w", encoding="utf-8") as fout:
for line in fin:
if not line.strip():
continue
total += 1
try:
obj = json.loads(line)
except Exception:
dropped += 1
continue
pair = first_pair(obj.get("conversation"))
if not pair:
dropped += 1
continue
u, a = pair
if bad(u.get("content", "")) or bad(a.get("content", "")):
dropped += 1
continue
obj["conversation"] = [u, a]
fout.write(json.dumps(obj, ensure_ascii=False) + "\n")
lost = (dropped / total * 100) if total else 0.0
print(f"{lost:.2f}%")