File size: 5,311 Bytes
6597efe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from __future__ import annotations

import random
from collections.abc import Mapping, Sequence
from typing import Any, List

import pandas as pd


DEFAULT_SEED_TEXT_FIELDS: List[str] = [
    "content_short_description",
    "content_short_description_2",
    "content_technical_description",
    "content_natural_desc_1",
    "content_natural_desc_2",
    "content_natural_desc_3",
    "content_natural_desc_4",
]

SYSTEM_PROMPT_POOLS: dict[str, list[str]] = {
    "system_first": [
        "Generate a motion sequence that matches the following description.",
        "Convert the following language instruction into an executable motion sequence.",
        "From a motion-control perspective, realize the following behavior description.",
        "Read the following description and generate a matching motion.",
        "Complete the motion generation task based on the description below.",
        "Generate a trackable motion sequence centered on the following description.",
        "Given the description below, produce a continuous full-body motion clip.",
        "Use the text instruction below to synthesize a coherent motion trajectory.",
        "Please generate motion conditioned on the following user request.",
        "Generate physically plausible motion according to the description that follows.",
    ],
    "desc_first": [
        "Based on the description above, generate a matching motion sequence.",
        "Using the user request above, synthesize a smooth full-body motion trajectory.",
        "Take the instruction above and convert it into an executable motion sequence.",
        "Generate stable and semantically consistent motion from the description above.",
        "Ground the behavior described above into time-series motion output.",
    ],
    "neutral": [
        "You are a motion generation assistant. Produce a natural and coherent full-body motion from the provided text.",
        "Synthesize a full-body motion trajectory from the text description.",
        "Generate a realistic motion performance consistent with the text prompt.",
        "Output a complete motion sequence from the requirement description without semantic drift.",
        "Generate a motion trajectory that satisfies the given text instruction.",
    ],
}

PROMPT_COMPOSITION_TEMPLATES_BY_LAYOUT: dict[str, list[str]] = {
    "system_first": [
        "{system}\nDescription: {desc}",
        "{system}\nUser request: {desc}",
        "{system} {desc}",
        "{system}\nInput text: \"{desc}\"",
        "{system}\nBehavior description: {desc}",
        "{system} (Description: {desc})",
    ],
    "desc_first": [
        "Description: {desc}\n{system}",
        "User request: {desc}\n{system}",
        "{desc}\n{system}",
        "Input text: \"{desc}\"\n{system}",
        "Behavior description: {desc}\n{system}",
    ],
    "neutral": [
        "{system}\nDescription: {desc}",
        "Description: {desc}\n{system}",
        "{system} {desc}",
        "{desc}\n{system}",
        "{system}\nInput text: {desc}",
    ],
}


def _row_has_key(row: Any, key: str) -> bool:
    if isinstance(row, Mapping):
        return key in row
    index = getattr(row, "index", None)
    return key in index if index is not None else hasattr(row, key)


def _row_get(row: Any, key: str, default: Any = None) -> Any:
    if isinstance(row, Mapping):
        return row.get(key, default)
    if _row_has_key(row, key):
        return row[key]
    return getattr(row, key, default)


def collect_seed_text_candidates(row: Any, text_fields: Sequence[str] | None = None) -> list[str]:
    candidates: list[str] = []
    seen = set()
    for key in text_fields or DEFAULT_SEED_TEXT_FIELDS:
        if not _row_has_key(row, key):
            continue
        value = _row_get(row, key)
        if pd.isna(value):
            continue
        text = str(value).strip()
        if (not text) or text.lower() == "nan":
            continue
        signature = text.lower()
        if signature in seen:
            continue
        seen.add(signature)
        candidates.append(text)
    return candidates


def compose_seed_motion_prompt(desc: str, *, rng: Any = random) -> str:
    desc = str(desc).strip()
    if not desc:
        desc = "seed motion"
    layout = rng.choice(list(SYSTEM_PROMPT_POOLS.keys()))
    system_prompt = rng.choice(SYSTEM_PROMPT_POOLS[layout])
    template = rng.choice(PROMPT_COMPOSITION_TEMPLATES_BY_LAYOUT[layout])
    return template.format(system=system_prompt, desc=desc).strip()


def compose_seed_motion_prompt_from_candidates(
    candidates: Sequence[str],
    *,
    fallback: str = "seed motion",
    rng: Any = random,
) -> str:
    pool = [str(c).strip() for c in candidates if str(c).strip()]
    if not pool:
        pool = [str(fallback).strip() or "seed motion"]
    return compose_seed_motion_prompt(rng.choice(pool), rng=rng)


def compose_seed_motion_prompt_from_row(
    row: Any,
    text_fields: Sequence[str] | None = None,
    *,
    fallback_key: str = "move_name",
    rng: Any = random,
) -> str:
    fallback = str(_row_get(row, fallback_key, "seed motion")).strip() or "seed motion"
    return compose_seed_motion_prompt_from_candidates(
        collect_seed_text_candidates(row, text_fields),
        fallback=fallback,
        rng=rng,
    )