Spaces:
Paused
Paused
2b commited on
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -177,6 +177,17 @@ def parse_tag_list(genre_tags: str) -> list[str]:
|
|
| 177 |
return unique_keep_order(tags)
|
| 178 |
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
def pick_profile_name(profile: str, backend: str) -> str:
|
| 181 |
profile_key = normalize_key(profile)
|
| 182 |
if profile_key in PROFILE_PRESETS:
|
|
@@ -238,7 +249,13 @@ def extract_lyric_structure(lyrics: str) -> tuple[list[str], list[str]]:
|
|
| 238 |
return unique_keep_order(sections), key_lines
|
| 239 |
|
| 240 |
|
| 241 |
-
def build_prompt(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
tags = parse_tag_list(genre_tags)
|
| 243 |
sections, lyric_lines = extract_lyric_structure(lyrics)
|
| 244 |
|
|
@@ -272,6 +289,8 @@ def build_prompt(project_name: str, genre_tags: str, lyrics: str, profile_name:
|
|
| 272 |
profile_hint = PROFILE_PRESETS.get(profile_name, {}).get("prompt_hint", "")
|
| 273 |
if profile_hint:
|
| 274 |
prompt_parts.append(f"Production target: {profile_hint}.")
|
|
|
|
|
|
|
| 275 |
prompt_parts.append("Clean low-end, defined drums, clear lead elements, modern stereo image.")
|
| 276 |
return " ".join(prompt_parts)[:MAX_PROMPT_CHARS]
|
| 277 |
|
|
@@ -331,7 +350,8 @@ def generate_music(
|
|
| 331 |
|
| 332 |
model, processor = get_musicgen_components(model_id)
|
| 333 |
preset = PROFILE_PRESETS.get(profile_name, PROFILE_PRESETS[DEFAULT_PROFILE])
|
| 334 |
-
|
|
|
|
| 335 |
|
| 336 |
seed = int(parameters.get("seed", 12345))
|
| 337 |
run_n_segments = int(parameters.get("run_n_segments", int(preset.get("run_n_segments", 1))))
|
|
@@ -362,6 +382,8 @@ def generate_music(
|
|
| 362 |
debug = {
|
| 363 |
"prompt": prompt,
|
| 364 |
"prompt_chars": len(prompt),
|
|
|
|
|
|
|
| 365 |
"seed": seed,
|
| 366 |
"max_new_tokens": max_new_tokens,
|
| 367 |
"temperature": temperature,
|
|
|
|
| 177 |
return unique_keep_order(tags)
|
| 178 |
|
| 179 |
|
| 180 |
+
def parse_negative_terms(value: Any) -> list[str]:
|
| 181 |
+
if value is None:
|
| 182 |
+
return []
|
| 183 |
+
if isinstance(value, list):
|
| 184 |
+
raw_values = [str(item) for item in value]
|
| 185 |
+
else:
|
| 186 |
+
raw_values = str(value).replace(";", ",").replace("|", ",").split(",")
|
| 187 |
+
cleaned = [" ".join(item.split()) for item in raw_values if " ".join(item.split())]
|
| 188 |
+
return unique_keep_order(cleaned)
|
| 189 |
+
|
| 190 |
+
|
| 191 |
def pick_profile_name(profile: str, backend: str) -> str:
|
| 192 |
profile_key = normalize_key(profile)
|
| 193 |
if profile_key in PROFILE_PRESETS:
|
|
|
|
| 249 |
return unique_keep_order(sections), key_lines
|
| 250 |
|
| 251 |
|
| 252 |
+
def build_prompt(
|
| 253 |
+
project_name: str,
|
| 254 |
+
genre_tags: str,
|
| 255 |
+
lyrics: str,
|
| 256 |
+
profile_name: str,
|
| 257 |
+
negative_terms: list[str] | None = None,
|
| 258 |
+
) -> str:
|
| 259 |
tags = parse_tag_list(genre_tags)
|
| 260 |
sections, lyric_lines = extract_lyric_structure(lyrics)
|
| 261 |
|
|
|
|
| 289 |
profile_hint = PROFILE_PRESETS.get(profile_name, {}).get("prompt_hint", "")
|
| 290 |
if profile_hint:
|
| 291 |
prompt_parts.append(f"Production target: {profile_hint}.")
|
| 292 |
+
if negative_terms:
|
| 293 |
+
prompt_parts.append(f"Avoid: {', '.join(negative_terms[:16])}.")
|
| 294 |
prompt_parts.append("Clean low-end, defined drums, clear lead elements, modern stereo image.")
|
| 295 |
return " ".join(prompt_parts)[:MAX_PROMPT_CHARS]
|
| 296 |
|
|
|
|
| 350 |
|
| 351 |
model, processor = get_musicgen_components(model_id)
|
| 352 |
preset = PROFILE_PRESETS.get(profile_name, PROFILE_PRESETS[DEFAULT_PROFILE])
|
| 353 |
+
negative_terms = parse_negative_terms(parameters.get("negative_prompt", ""))
|
| 354 |
+
prompt = build_prompt(project_name, genre_tags, lyrics, profile_name, negative_terms=negative_terms)
|
| 355 |
|
| 356 |
seed = int(parameters.get("seed", 12345))
|
| 357 |
run_n_segments = int(parameters.get("run_n_segments", int(preset.get("run_n_segments", 1))))
|
|
|
|
| 382 |
debug = {
|
| 383 |
"prompt": prompt,
|
| 384 |
"prompt_chars": len(prompt),
|
| 385 |
+
"negative_terms": negative_terms,
|
| 386 |
+
"prompt_filter_level": str(parameters.get("prompt_filter_level", "balanced")),
|
| 387 |
"seed": seed,
|
| 388 |
"max_new_tokens": max_new_tokens,
|
| 389 |
"temperature": temperature,
|