Video-XL-Pro-Union / tools /finevideo_map.py
SamChen567's picture
Upload folder using huggingface_hub
30ba310 verified
Raw History Blame Contribute Delete
2.3 kB
# Pass 1: build mapping finevideo_43k.json id -> (shard, original_video_filename) using QA-pair overlap.
import json, sys, collections
from concurrent.futures import ThreadPoolExecutor
from huggingface_hub import HfFileSystem
import pyarrow.parquet as pq
ANN = '/root/datasets/finetune/finevideo_43k.json'
OUT = '/root/datasets/tools/finevideo_map.json'
REPO = 'datasets/HuggingFaceFV/finevideo/data/'
def norm(s):
return ' '.join(s.replace('<image>', '').split()).lower()
d = json.load(open(ANN))
pair2ids = collections.defaultdict(set)
npairs = {}
for x in d:
c = x['conversations']
pairs = {(norm(c[i]['value']), norm(c[i + 1]['value'])) for i in range(0, len(c) - 1, 2)}
npairs[x['id']] = len(pairs)
for p in pairs:
pair2ids[p].add(x['id'])
fs = HfFileSystem()
shards = sorted(f.split('/')[-1] for f in fs.ls(REPO, detail=False) if f.endswith('.parquet'))
def scan(shard):
for attempt in range(5):
try:
with fs.open(REPO + shard, 'rb') as f:
rows = pq.ParquetFile(f).read(columns=['json']).column('json').to_pylist()
break
except Exception as e:
err = e
else:
return shard, None, repr(err)
res = []
for r in rows:
score = collections.Counter()
for qa in (r['content_metadata'].get('qAndA') or []):
for i in pair2ids.get((norm(qa['question']), norm(qa['answer'])), ()):
score[i] += 1
res.append((r['original_video_filename'], dict(score)))
return shard, res, None
best = {} # id -> (score, shard, filename)
failed = []
with ThreadPoolExecutor(16) as ex:
for n, (shard, res, err) in enumerate(ex.map(scan, shards)):
if res is None:
failed.append((shard, err)); continue
for fn, score in res:
for i, s in score.items():
if s > best.get(i, (0,))[0]:
best[i] = (s, shard, fn)
if n % 100 == 0:
print(n, len(shards), len(best), flush=True)
full = sum(1 for i, (s, _, _) in best.items() if s == npairs[i])
print('mapped', len(best), 'of', len(d), 'fully-matched', full, 'failed shards', failed, flush=True)
json.dump({'map': {i: [sh, fn, s, npairs[i]] for i, (s, sh, fn) in best.items()}, 'failed': failed}, open(OUT, 'w'))