mattbitzesty commited on
Commit
cef86d3
·
unverified ·
1 Parent(s): 78fcbf4

feat(data): rebalance text conditioning toward structured Poucher profiles

Browse files

- Add --poucher-weight flag to augment_dataset_text.py (default 0.5)
- Split structured Poucher strings from prose Arctander/literature pool
- Fallback genres without native profiles to the global structured pool
- Regenerate synthetic_dataset_v2_text with ~50% structured strings

Files changed (1) hide show
  1. scripts/augment_dataset_text.py +47 -9
scripts/augment_dataset_text.py CHANGED
@@ -98,22 +98,51 @@ def augment_dataset(
98
  output_path: Path,
99
  descriptions: dict[str, list[str]],
100
  seed: int = 42,
 
101
  ) -> None:
102
- """Attach a sampled text description and its 384-D embedding to every record."""
 
 
 
 
 
103
  random.seed(seed)
104
 
105
  print("Loading sentence transformer...")
106
  text_encoder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
107
 
108
- # Pre-shuffle cyclical iterators per genre so sampling is deterministic
109
- iterators: dict[str, itertools.cycle] = {}
 
 
 
110
  for genre, pool in descriptions.items():
111
- shuffled = pool[:]
112
- random.shuffle(shuffled)
113
- iterators[genre] = itertools.cycle(shuffled)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
  output_path.parent.mkdir(parents=True, exist_ok=True)
116
  total = 0
 
117
 
118
  with input_path.open("r", encoding="utf-8") as fin, output_path.open("w", encoding="utf-8") as fout:
119
  for line in fin:
@@ -122,7 +151,13 @@ def augment_dataset(
122
  if not genre or genre not in iterators:
123
  genre = "citrus_cologne" # fallback
124
 
125
- text = next(iterators[genre])
 
 
 
 
 
 
126
  embedding = text_encoder.encode(text, convert_to_numpy=True)
127
 
128
  record["text_conditioning"] = text
@@ -131,6 +166,7 @@ def augment_dataset(
131
  total += 1
132
 
133
  print(f"Augmented {total} records with text conditioning -> {output_path}")
 
134
 
135
 
136
  def main() -> int:
@@ -140,6 +176,7 @@ def main() -> int:
140
  parser.add_argument("--literature", default="data/literature_formulas.json", help="Literature blueprint")
141
  parser.add_argument("--arctander", default="/home/hermes/fragrance-research/extracted/arctander_monographs.jsonl", help="Arctander monographs")
142
  parser.add_argument("--poucher-profiles", default="data/literature_profiles_poucher.jsonl", help="Poucher structured profiles")
 
143
  parser.add_argument("--seed", type=int, default=42, help="Random seed")
144
  args = parser.parse_args()
145
 
@@ -149,9 +186,10 @@ def main() -> int:
149
  Path(args.poucher_profiles) if args.poucher_profiles else None,
150
  )
151
  for genre, pool in descriptions.items():
152
- print(f" {genre}: {len(pool)} descriptions")
 
153
 
154
- augment_dataset(Path(args.input), Path(args.output), descriptions, seed=args.seed)
155
  return 0
156
 
157
 
 
98
  output_path: Path,
99
  descriptions: dict[str, list[str]],
100
  seed: int = 42,
101
+ poucher_weight: float = 0.5,
102
  ) -> None:
103
+ """Attach a sampled text description and its 384-D embedding to every record.
104
+
105
+ poucher_weight controls the fraction of records that receive a structured
106
+ Poucher-style `top: ...; middle: ...; base: ...` string. The remainder are
107
+ filled from the full genre pool (Arctander, literature formulas, etc.).
108
+ """
109
  random.seed(seed)
110
 
111
  print("Loading sentence transformer...")
112
  text_encoder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
113
 
114
+ # Separate structured Poucher strings from the broader prose pool
115
+ poucher_pool: dict[str, list[str]] = {}
116
+ prose_pool: dict[str, list[str]] = {}
117
+
118
+ all_structured: list[str] = []
119
  for genre, pool in descriptions.items():
120
+ poucher_strings = [t for t in pool if "top:" in t and "middle:" in t and "base:" in t]
121
+ prose_strings = [t for t in pool if t not in poucher_strings]
122
+ if poucher_strings:
123
+ poucher_pool[genre] = poucher_strings[:]
124
+ all_structured.extend(poucher_strings)
125
+ prose_pool[genre] = prose_strings if prose_strings else [f"A classic {genre.replace('_', ' ')} fragrance"]
126
+
127
+ # Fallback: genres without their own structured profiles can use any structured profile.
128
+ # This guarantees every genre sees the volatility-aware format during training.
129
+ all_structured = list(set(all_structured))
130
+ for genre in descriptions:
131
+ if genre not in poucher_pool and all_structured:
132
+ poucher_pool[genre] = all_structured[:]
133
+
134
+ # Pre-shuffle cyclical iterators
135
+ iterators: dict[str, tuple[itertools.cycle, itertools.cycle]] = {}
136
+ for genre in descriptions:
137
+ p_shuffled = poucher_pool.get(genre, prose_pool[genre])[:]
138
+ c_shuffled = prose_pool[genre][:]
139
+ random.shuffle(p_shuffled)
140
+ random.shuffle(c_shuffled)
141
+ iterators[genre] = (itertools.cycle(p_shuffled), itertools.cycle(c_shuffled))
142
 
143
  output_path.parent.mkdir(parents=True, exist_ok=True)
144
  total = 0
145
+ poucher_used = 0
146
 
147
  with input_path.open("r", encoding="utf-8") as fin, output_path.open("w", encoding="utf-8") as fout:
148
  for line in fin:
 
151
  if not genre or genre not in iterators:
152
  genre = "citrus_cologne" # fallback
153
 
154
+ p_iter, c_iter = iterators[genre]
155
+ if random.random() < poucher_weight and p_iter is not c_iter:
156
+ text = next(p_iter)
157
+ poucher_used += 1
158
+ else:
159
+ text = next(c_iter)
160
+
161
  embedding = text_encoder.encode(text, convert_to_numpy=True)
162
 
163
  record["text_conditioning"] = text
 
166
  total += 1
167
 
168
  print(f"Augmented {total} records with text conditioning -> {output_path}")
169
+ print(f"Poucher-style strings used: {poucher_used} ({100 * poucher_used / total:.1f}%)")
170
 
171
 
172
  def main() -> int:
 
176
  parser.add_argument("--literature", default="data/literature_formulas.json", help="Literature blueprint")
177
  parser.add_argument("--arctander", default="/home/hermes/fragrance-research/extracted/arctander_monographs.jsonl", help="Arctander monographs")
178
  parser.add_argument("--poucher-profiles", default="data/literature_profiles_poucher.jsonl", help="Poucher structured profiles")
179
+ parser.add_argument("--poucher-weight", type=float, default=0.5, help="Fraction of records that receive structured Poucher-style text")
180
  parser.add_argument("--seed", type=int, default=42, help="Random seed")
181
  args = parser.parse_args()
182
 
 
186
  Path(args.poucher_profiles) if args.poucher_profiles else None,
187
  )
188
  for genre, pool in descriptions.items():
189
+ poucher_count = sum(1 for t in pool if "top:" in t and "middle:" in t and "base:" in t)
190
+ print(f" {genre}: {len(pool)} descriptions ({poucher_count} structured)")
191
 
192
+ augment_dataset(Path(args.input), Path(args.output), descriptions, seed=args.seed, poucher_weight=args.poucher_weight)
193
  return 0
194
 
195