File size: 22,224 Bytes
17a7958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
"""
AI Effector - DiffVox LLM ๊ธฐ๋ฐ˜ ์ดํŽ™ํŠธ ํŒŒ๋ผ๋ฏธํ„ฐ ์˜ˆ์ธก
===================================================
V9: Compressor threshold ๋ฒ”์œ„ ์ˆ˜์ • (0 ~ -5dB)
"""

import os
import json
import re
import math
import torch
import numpy as np
from typing import Dict, List, Optional, Any, Tuple
from pathlib import Path
from datetime import datetime
import warnings

warnings.filterwarnings("ignore")


def sigmoid(x: float) -> float:
    try:
        return 1 / (1 + math.exp(-x))
    except OverflowError:
        return 0.0 if x < 0 else 1.0


def minmax_transform(raw: float, min_val: float, max_val: float) -> float:
    return sigmoid(raw) * (max_val - min_val) + min_val


PARAM_TRANSFORMS = {
    "eq_peak1.params.freq": {"type": "minmax", "min": 33.0, "max": 17500.0},
    "eq_peak1.params.Q": {"type": "minmax", "min": 0.2, "max": 20.0},
    "eq_peak1.params.gain": {"type": "none"},
    "eq_peak2.params.freq": {"type": "minmax", "min": 33.0, "max": 17500.0},
    "eq_peak2.params.Q": {"type": "minmax", "min": 0.2, "max": 20.0},
    "eq_peak2.params.gain": {"type": "none"},
    "eq_lowshelf.params.freq": {"type": "minmax", "min": 30.0, "max": 200.0},
    "eq_lowshelf.params.gain": {"type": "none"},
    "eq_highshelf.params.freq": {"type": "minmax", "min": 2500.0, "max": 16000.0},
    "eq_highshelf.params.gain": {"type": "none"},
    "delay.delay_time": {"type": "none"},
    "delay.feedback": {"type": "sigmoid"},
    "delay.mix": {"type": "sigmoid"},
    "distortion_amount": {"type": "sigmoid_scale", "scale": 0.1},
    "final_wet_mix": {"type": "sigmoid"},
}

DEFAULT_PARAMETERS = {
    "eq_peak1.params.freq": 1000.0,
    "eq_peak1.params.gain": 0.0,
    "eq_peak1.params.Q": 1.0,
    "eq_peak2.params.freq": 4000.0,
    "eq_peak2.params.gain": 0.0,
    "eq_peak2.params.Q": 1.0,
    "eq_lowshelf.params.freq": 115.0,
    "eq_lowshelf.params.gain": 0.0,
    "eq_highshelf.params.freq": 8000.0,
    "eq_highshelf.params.gain": 0.0,
    # V9: Compressor threshold ๊ธฐ๋ณธ๊ฐ’ -3dB
    "compressor.threshold": -3.0,
    "compressor.ratio": 2.0,
    "distortion_amount": 0.0,
    "delay.delay_time": 0.02,
    "delay.feedback": 0.15,
    "delay.mix": 0.1,
    "reverb.room_size": 0.3,
    "reverb.damping": 0.5,
    "reverb.wet_level": 0.0,
    "reverb.dry_level": 1.0,
    "final_wet_mix": 0.5
}

# V9: Compressor threshold ๋ฒ”์œ„ 0 ~ -5dB
PARAM_RANGES = {
    "eq_peak1.params.freq": (33.0, 17500.0),
    "eq_peak1.params.gain": (-12.0, 12.0),
    "eq_peak1.params.Q": (0.2, 20.0),
    "eq_peak2.params.freq": (33.0, 17500.0),
    "eq_peak2.params.gain": (-12.0, 12.0),
    "eq_peak2.params.Q": (0.2, 20.0),
    "eq_lowshelf.params.freq": (30.0, 200.0),
    "eq_lowshelf.params.gain": (-12.0, 12.0),
    "eq_highshelf.params.freq": (2500.0, 16000.0),
    "eq_highshelf.params.gain": (-12.0, 12.0),
    # V9: 0 ~ -5dB (๊ฐ€๋ฒผ์šด ์••์ถ•)
    "compressor.threshold": (-5.0, 0.0),
    "compressor.ratio": (1.5, 4.0),
    "distortion_amount": (0.0, 0.05),
    "delay.delay_time": (0.01, 0.3),
    "delay.feedback": (0.0, 0.25),
    "delay.mix": (0.0, 0.2),
    "reverb.room_size": (0.0, 0.6),
    "reverb.damping": (0.0, 1.0),
    "reverb.wet_level": (0.0, 0.3),
    "reverb.dry_level": (0.7, 1.0),
    "final_wet_mix": (0.3, 0.7),
}

SYNONYM_MAP = {
    "calm": "warm soft", "relaxed": "warm soft", "chill": "warm soft",
    "smooth": "warm", "mellow": "warm soft", "breezy": "bright spacious",
    "airy": "bright spacious", "light": "bright", "crisp": "bright",
    "clean": "bright", "dreamy": "warm spacious", "ethereal": "bright spacious",
    "atmospheric": "spacious", "ambient": "spacious warm",
    "aggressive": "saturated bright", "powerful": "saturated",
    "punchy": "saturated bright", "hard": "saturated",
    "gritty": "saturated dark", "soft": "warm", "harsh": "bright saturated",
    "muddy": "dark", "thin": "bright", "thick": "warm dark",
    "full": "warm", "reverb": "spacious", "echo": "spacious", "wet": "spacious",
}

# V9: Compressor threshold 0 ~ -5dB ๋ฒ”์œ„
STYLE_PRESETS = {
    "warm": {
        "compressor.threshold": -3.0,
        "compressor.ratio": 2.0,
        "delay.delay_time": 0.02,
        "delay.feedback": 0.12,
        "delay.mix": 0.08,
        "reverb.room_size": 0.25,
        "reverb.wet_level": 0.1,
        "reverb.dry_level": 0.9,
    },
    "bright": {
        "compressor.threshold": -2.0,
        "compressor.ratio": 2.0,
        "delay.delay_time": 0.02,
        "delay.feedback": 0.1,
        "delay.mix": 0.06,
        "reverb.room_size": 0.2,
        "reverb.wet_level": 0.08,
        "reverb.dry_level": 0.92,
    },
    "spacious": {
        "compressor.threshold": -4.0,
        "compressor.ratio": 1.8,
        "delay.delay_time": 0.06,
        "delay.feedback": 0.2,
        "delay.mix": 0.15,
        "reverb.room_size": 0.45,
        "reverb.wet_level": 0.2,
        "reverb.dry_level": 0.8,
    },
    "dark": {
        "compressor.threshold": -4.0,
        "compressor.ratio": 2.0,
        "delay.delay_time": 0.03,
        "delay.feedback": 0.15,
        "delay.mix": 0.1,
        "reverb.room_size": 0.35,
        "reverb.wet_level": 0.15,
        "reverb.dry_level": 0.85,
    },
    "saturated": {
        "compressor.threshold": -2.0,
        "compressor.ratio": 3.0,
        "delay.delay_time": 0.02,
        "delay.feedback": 0.08,
        "delay.mix": 0.05,
        "reverb.room_size": 0.15,
        "reverb.wet_level": 0.06,
        "reverb.dry_level": 0.94,
    },
    "soft": {
        "compressor.threshold": -5.0,
        "compressor.ratio": 1.5,
        "delay.delay_time": 0.025,
        "delay.feedback": 0.15,
        "delay.mix": 0.1,
        "reverb.room_size": 0.3,
        "reverb.wet_level": 0.12,
        "reverb.dry_level": 0.88,
    },
}


class CLAPAudioEncoder:
    def __init__(self, output_dim: int = 64, model_name: str = "laion/larger_clap_music"):
        self.output_dim = output_dim
        self.model_name = model_name
        self.target_sr = 48000
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.model = None
        self.processor = None
        self._load_model()
    
    def _load_model(self):
        try:
            from transformers import ClapModel, ClapProcessor
            print(f"[CLAPEncoder] CLAP ๋ชจ๋ธ ๋กœ๋”ฉ ์ค‘: {self.model_name}")
            self.processor = ClapProcessor.from_pretrained(self.model_name)
            self.model = ClapModel.from_pretrained(self.model_name)
            self.model = self.model.to(self.device)
            self.model.eval()
            print(f"[CLAPEncoder] โœ… CLAP ๋ชจ๋ธ ๋กœ๋“œ ์™„๋ฃŒ")
        except Exception as e:
            print(f"[CLAPEncoder] โŒ ๋ชจ๋ธ ๋กœ๋“œ ์‹คํŒจ: {e}")
    
    def get_audio_features(self, audio_path: str) -> List[float]:
        if self.model is None:
            return [0.0] * self.output_dim
        try:
            import librosa
            audio, sr = librosa.load(audio_path, sr=self.target_sr, mono=True)
            inputs = self.processor(audios=audio, sampling_rate=self.target_sr, return_tensors="pt", padding=True).to(self.device)
            with torch.no_grad():
                outputs = self.model.get_audio_features(**inputs)
            features_512 = outputs[0].cpu().numpy()
            return self._reduce_dimension(features_512).tolist()
        except Exception as e:
            print(f"[CLAPEncoder] ํŠน์ง• ์ถ”์ถœ ์‹คํŒจ: {e}")
            return [0.0] * self.output_dim
    
    def _reduce_dimension(self, features: np.ndarray) -> np.ndarray:
        current_dim = len(features)
        if current_dim == self.output_dim:
            return features
        pool_size = current_dim // self.output_dim
        remainder = current_dim % self.output_dim
        pooled = []
        idx = 0
        for i in range(self.output_dim):
            size = pool_size + (1 if i < remainder else 0)
            pooled.append(np.mean(features[idx:idx+size]))
            idx += size
        return np.array(pooled)
    
    def is_loaded(self) -> bool:
        return self.model is not None


class AIEffector:
    def __init__(self, model_repo_id: str = "heybaeheef/KU_SW_Academy", model_subfolder: str = "checkpoints", base_model_name: str = "Qwen/Qwen3-8B", audio_feature_dim: int = 64, use_huggingface: bool = True):
        self.model_repo_id = model_repo_id
        self.model_subfolder = model_subfolder
        self.base_model_name = base_model_name
        self.audio_feature_dim = audio_feature_dim
        self.use_huggingface = use_huggingface
        self.model = None
        self.tokenizer = None
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        print(f"[AIEffector V9] CLAP ์ธ์ฝ”๋” ์ดˆ๊ธฐํ™”...")
        self.audio_encoder = CLAPAudioEncoder(output_dim=audio_feature_dim)
        self.request_count = 0
        self._load_model()
    
    def _load_model(self):
        try:
            from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
            from peft import PeftModel
            print(f"[AIEffector] ๋ฒ ์ด์Šค ๋ชจ๋ธ ๋กœ๋”ฉ: {self.base_model_name}")
            if torch.cuda.is_available():
                bnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.float16, bnb_4bit_use_double_quant=True)
                base_model = AutoModelForCausalLM.from_pretrained(self.base_model_name, quantization_config=bnb_config, device_map="auto", trust_remote_code=True)
            else:
                base_model = AutoModelForCausalLM.from_pretrained(self.base_model_name, torch_dtype=torch.float32, device_map="auto", trust_remote_code=True)
            self.tokenizer = AutoTokenizer.from_pretrained(self.base_model_name, trust_remote_code=True)
            if self.tokenizer.pad_token is None:
                self.tokenizer.pad_token = self.tokenizer.eos_token
            print(f"[AIEffector] LoRA ์–ด๋Œ‘ํ„ฐ ๋กœ๋”ฉ...")
            if self.use_huggingface:
                self.model = PeftModel.from_pretrained(base_model, self.model_repo_id, subfolder=self.model_subfolder, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
            else:
                local_path = os.path.join(self.model_repo_id, self.model_subfolder)
                self.model = PeftModel.from_pretrained(base_model, local_path, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
            self.model.eval()
            print(f"[AIEffector] โœ… ๋ชจ๋ธ ๋กœ๋“œ ์„ฑ๊ณต!")
        except Exception as e:
            print(f"[AIEffector] โŒ ๋ชจ๋ธ ๋กœ๋“œ ์‹คํŒจ: {e}")
            import traceback
            traceback.print_exc()
            self.model = None
            self.tokenizer = None
    
    def is_loaded(self) -> bool:
        return self.model is not None
    
    def _preprocess_text(self, text: str) -> str:
        text_lower = text.lower()
        for synonym, replacement in SYNONYM_MAP.items():
            if synonym in text_lower:
                text_lower = text_lower.replace(synonym, replacement)
        return text_lower
    
    def _apply_preset(self, prompt: str) -> Dict[str, float]:
        params = {}
        prompt_lower = prompt.lower()
        matched = []
        for style_name, style_params in STYLE_PRESETS.items():
            if style_name in prompt_lower:
                params.update(style_params)
                matched.append(style_name)
        if matched:
            print(f"    [Preset] ๋งค์นญ: {matched}")
        else:
            params.update(STYLE_PRESETS["warm"])
            print(f"    [Preset] ๊ธฐ๋ณธ๊ฐ’ ์ ์šฉ: warm")
        return params
    
    def _format_prompt(self, text_prompt: str, audio_features: List[float]) -> str:
        audio_state_str = json.dumps(audio_features)
        return f"""Task: Convert text to audio parameters.
Audio: {audio_state_str}
Text: {text_prompt}
Parameters:"""
    
    def _preprocess_json(self, json_str: str) -> str:
        json_str = re.sub(r'(\d)_(\d)', r'\1\2', json_str)
        json_str = re.sub(r',(\s*[}\]])', r'\1', json_str)
        json_str = re.sub(r'\bNaN\b', '0', json_str)
        json_str = re.sub(r'\bInfinity\b', '999999', json_str)
        json_str = re.sub(r'-Infinity\b', '-999999', json_str)
        return json_str
    
    def _normalize_key(self, key: str) -> str:
        return re.sub(r'\.parametrizations\.(\w+)\.original', r'.\1', key)
    
    def _extract_json_object(self, text: str) -> Optional[str]:
        start = text.find('{')
        if start == -1:
            return None
        depth = 0
        for i, char in enumerate(text[start:], start):
            if char == '{':
                depth += 1
            elif char == '}':
                depth -= 1
                if depth == 0:
                    return text[start:i+1]
        return None
    
    def _convert_raw_to_actual(self, params: Dict[str, float]) -> Dict[str, float]:
        result = params.copy()
        for key, transform in PARAM_TRANSFORMS.items():
            if key not in result:
                continue
            raw = result[key]
            transform_type = transform["type"]
            if transform_type == "none":
                actual = raw
            elif transform_type == "minmax":
                actual = minmax_transform(raw, transform["min"], transform["max"])
                print(f"    [MinMax] {key}: {raw:.4f} โ†’ {actual:.2f}")
            elif transform_type == "sigmoid":
                actual = sigmoid(raw)
                print(f"    [Sigmoid] {key}: {raw:.4f} โ†’ {actual:.4f}")
            elif transform_type == "sigmoid_scale":
                actual = sigmoid(raw) * transform["scale"]
                print(f"    [Sigmoid*{transform['scale']}] {key}: {raw:.4f} โ†’ {actual:.4f}")
            else:
                actual = raw
            result[key] = actual
        return result
    
    def _parse_output(self, output_text: str) -> Dict[str, float]:
        print(f"    [Parse] Raw output ๊ธธ์ด: {len(output_text)} ๋ฌธ์ž")
        try:
            text = re.sub(r'<think>.*?</think>', '', output_text, flags=re.DOTALL)
            code_match = re.search(r'```(?:json)?\s*([\s\S]*?)```', text)
            if code_match:
                text = code_match.group(1)
            json_str = self._extract_json_object(text)
            if json_str:
                print(f"    [Parse] JSON ๋ฐœ๊ฒฌ (๊ธธ์ด: {len(json_str)})")
                json_str = self._preprocess_json(json_str)
                raw_params = json.loads(json_str)
                result = DEFAULT_PARAMETERS.copy()
                parsed_count = 0
                for key, value in raw_params.items():
                    try:
                        norm_key = self._normalize_key(key)
                        float_val = float(value)
                        if norm_key in DEFAULT_PARAMETERS:
                            result[norm_key] = float_val
                            parsed_count += 1
                        else:
                            for default_key in DEFAULT_PARAMETERS.keys():
                                norm_parts = norm_key.split('.')
                                default_parts = default_key.split('.')
                                if len(norm_parts) >= 3 and len(default_parts) >= 3:
                                    if norm_parts[0] == default_parts[0] and norm_parts[-1] == default_parts[-1]:
                                        result[default_key] = float_val
                                        parsed_count += 1
                                        break
                    except (ValueError, TypeError):
                        pass
                print(f"    [Parse] โœ… {parsed_count}๊ฐœ ํŒŒ๋ผ๋ฏธํ„ฐ ๋งคํ•‘๋จ")
                return result
        except json.JSONDecodeError as e:
            print(f"    [Parse] โŒ JSON ์—๋Ÿฌ: {e}")
        except Exception as e:
            print(f"    [Parse] โŒ ์˜ˆ์™ธ: {e}")
        print(f"    [Parse] โš ๏ธ ๊ธฐ๋ณธ๊ฐ’ ํด๋ฐฑ")
        return DEFAULT_PARAMETERS.copy()
    
    def predict(self, audio_path: str, text_prompt: str = "") -> Dict[str, float]:
        self.request_count += 1
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print(f"\n{'='*60}")
        print(f"[AIEffector V9] ๐ŸŽต ์š”์ฒญ #{self.request_count} - {timestamp}")
        print(f"{'='*60}")
        print(f"  ๐Ÿ“‚ ์˜ค๋””์˜ค: {Path(audio_path).name}")
        print(f"  ๐Ÿ’ฌ ์›๋ณธ: '{text_prompt}'")
        processed_prompt = self._preprocess_text(text_prompt)
        print(f"  ๐Ÿค– ๋ชจ๋ธ: {'AI' if self.is_loaded() else 'ํ”„๋ฆฌ์…‹'}")
        
        if not self.is_loaded():
            print(f"\n  โš ๏ธ AI ๋ชจ๋ธ ๋ฏธ๋กœ๋“œ")
            params = DEFAULT_PARAMETERS.copy()
            params.update(self._apply_preset(processed_prompt))
            self._log_parameters(params)
            return self._convert_to_effect_chain_format(params)
        
        try:
            print(f"\n  ๐Ÿ“Š [Step 1] CLAP ํŠน์ง• ์ถ”์ถœ...")
            audio_features = self.audio_encoder.get_audio_features(audio_path)
            if not audio_features or all(f == 0 for f in audio_features):
                print(f"    โš ๏ธ ์‹คํŒจ, ํ”„๋ฆฌ์…‹ ํด๋ฐฑ")
                params = DEFAULT_PARAMETERS.copy()
                params.update(self._apply_preset(processed_prompt))
                self._log_parameters(params)
                return self._convert_to_effect_chain_format(params)
            print(f"    โœ… {len(audio_features)}์ฐจ์›")
            
            print(f"\n  ๐Ÿ”ค [Step 2] ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ...")
            prompt = self._format_prompt(processed_prompt, audio_features)
            
            print(f"\n  ๐Ÿ”ข [Step 3] ํ† ํฐํ™”...")
            inputs = self.tokenizer(prompt, return_tensors="pt", truncation=False).to(self.device)
            print(f"    ํ† ํฐ ์ˆ˜: {inputs['input_ids'].shape[1]}")
            
            print(f"\n  ๐Ÿง  [Step 4] LLM ์ถ”๋ก ...")
            import time
            start = time.time()
            with torch.no_grad():
                outputs = self.model.generate(**inputs, max_new_tokens=500, do_sample=False, temperature=0.1, pad_token_id=self.tokenizer.pad_token_id, eos_token_id=self.tokenizer.eos_token_id)
            print(f"    ์ถ”๋ก  ์‹œ๊ฐ„: {time.time()-start:.2f}์ดˆ")
            
            print(f"\n  ๐Ÿ“ [Step 5] ๋””์ฝ”๋”ฉ...")
            gen_tokens = outputs[0][inputs['input_ids'].shape[1]:]
            output_text = self.tokenizer.decode(gen_tokens, skip_special_tokens=True).strip()
            print(f"    ์ถœ๋ ฅ (์ฒ˜์Œ 500์ž):\n{output_text[:500]}")
            
            print(f"\n  ๐Ÿ”ง [Step 6] ํŒŒ์‹ฑ...")
            raw_params = self._parse_output(output_text)
            
            print(f"\n  ๐Ÿ”„ [Step 7] Raw โ†’ Actual ๋ณ€ํ™˜...")
            actual_params = self._convert_raw_to_actual(raw_params)
            
            print(f"\n  ๐Ÿ“ [Step 8] ๊ฐ’ ํด๋žจํ•‘ (EQ๋งŒ)...")
            eq_keys = [k for k in PARAM_RANGES.keys() if k.startswith('eq_')]
            for key in eq_keys:
                if key in actual_params:
                    min_val, max_val = PARAM_RANGES[key]
                    original = actual_params[key]
                    clamped = max(min_val, min(max_val, original))
                    if abs(clamped - original) > 0.001:
                        print(f"    [Clamp] {key}: {original:.4f} โ†’ {clamped:.4f}")
                    actual_params[key] = clamped
            
            print(f"\n  ๐ŸŽ›๏ธ [Step 9] ํ”„๋ฆฌ์…‹ ์ ์šฉ (Compressor/Reverb/Delay)...")
            preset = self._apply_preset(processed_prompt)
            for key in preset:
                actual_params[key] = preset[key]
                print(f"    {key}: {preset[key]}")
            
            actual_params["final_wet_mix"] = max(0.3, min(0.7, actual_params.get("final_wet_mix", 0.5)))
            print(f"    final_wet_mix: {actual_params['final_wet_mix']:.2f}")
            
            self._log_parameters(actual_params)
            print(f"\n  โœ… ์™„๋ฃŒ!")
            print(f"{'='*60}\n")
            return self._convert_to_effect_chain_format(actual_params)
            
        except Exception as e:
            print(f"\n  โŒ ์‹คํŒจ: {e}")
            import traceback
            traceback.print_exc()
            params = DEFAULT_PARAMETERS.copy()
            params.update(self._apply_preset(processed_prompt))
            self._log_parameters(params)
            return self._convert_to_effect_chain_format(params)
    
    def _convert_to_effect_chain_format(self, params: Dict[str, float]) -> Dict[str, float]:
        result = {}
        for key, value in params.items():
            new_key = key.replace('.Q', '.q')
            result[new_key] = value
        return result
    
    def _log_parameters(self, params: Dict[str, float]):
        print(f"\n  ๐Ÿ“‹ ์ตœ์ข… ํŒŒ๋ผ๋ฏธํ„ฐ:")
        print(f"    [EQ Peak 1] freq={params.get('eq_peak1.params.freq',0):.0f}Hz, gain={params.get('eq_peak1.params.gain',0):.2f}dB, Q={params.get('eq_peak1.params.Q',0):.2f}")
        print(f"    [EQ Peak 2] freq={params.get('eq_peak2.params.freq',0):.0f}Hz, gain={params.get('eq_peak2.params.gain',0):.2f}dB, Q={params.get('eq_peak2.params.Q',0):.2f}")
        print(f"    [Low Shelf] freq={params.get('eq_lowshelf.params.freq',0):.0f}Hz, gain={params.get('eq_lowshelf.params.gain',0):.2f}dB")
        print(f"    [High Shelf] freq={params.get('eq_highshelf.params.freq',0):.0f}Hz, gain={params.get('eq_highshelf.params.gain',0):.2f}dB")
        print(f"    [Compressor] threshold={params.get('compressor.threshold',-3):.1f}dB, ratio={params.get('compressor.ratio',2):.1f}")
        print(f"    [Distortion] {params.get('distortion_amount',0):.4f}")
        print(f"    [Delay] time={params.get('delay.delay_time',0):.3f}s, fb={params.get('delay.feedback',0):.2f}, mix={params.get('delay.mix',0):.2f}")
        print(f"    [Reverb] room={params.get('reverb.room_size',0):.2f}, damp={params.get('reverb.damping',0):.2f}, wet={params.get('reverb.wet_level',0):.2f}, dry={params.get('reverb.dry_level',1):.2f}")
        print(f"    [Wet Mix] {params.get('final_wet_mix',0):.2f}")