llm_cp2 / data /convert_hapticcap_to_llava.py
csuhan's picture
Upload folder using huggingface_hub
b0c0df0 verified
import json
import random
import glob
import os
# === Prompt 组 ===
prompts = {
"sensory": [
"What does this vibration physically feel like?",
"Describe the physical sensation produced by this vibration.",
"How would you characterize the tactile feeling of this vibration?",
"What kind of physical texture or movement does this vibration create?",
"In terms of touch, how does this vibration feel on the skin?"
],
"emotional": [
"What emotion or mood does this vibration convey?",
"How does this vibration make you feel emotionally?",
"What emotional tone or atmosphere is expressed by this vibration?",
"Describe the emotional impression that this vibration gives.",
"What kind of mood or feeling is evoked by this vibration?"
],
"associative": [
"What real-world sensation or object does this vibration resemble?",
"What familiar experience or phenomenon does this vibration remind you of?",
"Describe what this vibration is similar to in the real world.",
"What everyday sound, movement, or event could this vibration represent?",
"What real-life action or object feels most like this vibration?"
]
}
# === 加载原始数据 ===
with open("data/HapticCap/signal_map_5.1.json", "r", encoding="utf-8") as f:
raw_data = json.load(f)
llava_data = []
base_dir = "data/HapticCap/aug_signal"
for sample_id, sample_content in raw_data.items():
if not isinstance(sample_content, dict):
continue
# 匹配音频文件(可能有多个 augmentation)
pattern = os.path.join(base_dir, f"F{sample_id}_loop*.wav")
matched_files = sorted(glob.glob(pattern)) # sorted 保证可复现顺序
if not matched_files:
# 如果找不到音频文件则跳过该 sample_id
continue
for audio_path in matched_files:
# 为每个匹配到的音频都生成条目(后续会根据 user_entry 生成多条)
for user_id, user_entry in sample_content.items():
if not isinstance(user_entry, dict):
continue
for modality, key in zip(
["sensory", "emotional", "associative"],
["free_text_sensory", "free_text_emotional", "free_text_association"]
):
raw_text = user_entry.get(key, "")
if not isinstance(raw_text, str):
continue
text = raw_text.strip()
if not text or text.lower() in ["n.a.", "na", "none"]:
continue
conv = [
{"from": "human", "value": "<audio>"+random.choice(prompts[modality])},
{"from": "gpt", "value": text}
]
llava_data.append({
"audios": audio_path.strip('data/'),
"conversations": conv
})
# === 保存为 JSONL ===
out_path = "data/HapticCap/haptic_llava.json"
with open(out_path, "w", encoding="utf-8") as f:
json.dump(llava_data, f, ensure_ascii=False, indent=2)
print(f"✅ Saved {len(llava_data)} samples to {out_path}")