File size: 8,983 Bytes
656e7f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
CAM++ MLX Converter - Command Line Interface

Convert PyTorch CAM++ models to MLX format without Gradio UI.
Perfect for batch processing, CI/CD pipelines, or scripting.

Usage:
    # Basic conversion
    python convert_cli.py \\
        --input iic/speech_campplus_sv_zh-cn_16k-common \\
        --output campplus_chinese_16k \\
        --token YOUR_HF_TOKEN

    # With quantization options
    python convert_cli.py \\
        --input iic/speech_campplus_sv_zh_en_16k-common_advanced \\
        --output campplus_multilingual \\
        --token YOUR_HF_TOKEN \\
        --q2 --q4 --q8

    # Using environment variable for token
    export HF_TOKEN=your_token_here
    python convert_cli.py \\
        --input iic/speech_campplus_sv_zh-cn_16k-common \\
        --output campplus_chinese_16k

    # Dry run (validate without uploading)
    python convert_cli.py \\
        --input iic/speech_campplus_sv_zh-cn_16k-common \\
        --output campplus_chinese_16k \\
        --token YOUR_HF_TOKEN \\
        --dry-run
"""

import argparse
import os
import sys
from typing import Optional
import logging
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Import the converter
from app import CAMPPConverter, DEFAULT_SERVER_PORT, TARGET_ORGANIZATION

# Set up logging for CLI
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout)
    ]
)
logger = logging.getLogger(__name__)


def parse_args():
    """Parse command line arguments"""
    parser = argparse.ArgumentParser(
        description='Convert PyTorch CAM++ models to MLX format (CLI)',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  # Convert Chinese model with Q4 quantization (default)
  %(prog)s -i iic/speech_campplus_sv_zh-cn_16k-common \\
           -o campplus_chinese_16k \\
           -t YOUR_HF_TOKEN

  # Convert with all quantization levels
  %(prog)s -i iic/speech_campplus_sv_zh_en_16k-common_advanced \\
           -o campplus_multilingual \\
           -t YOUR_HF_TOKEN \\
           --q2 --q4 --q8

  # Use environment variable for token
  export HF_TOKEN=your_token_here
  %(prog)s -i iic/speech_campplus_sv_zh-cn_16k-common \\
           -o campplus_chinese_16k

  # Dry run (test without uploading)
  %(prog)s -i iic/speech_campplus_sv_zh-cn_16k-common \\
           -o campplus_chinese_16k \\
           -t YOUR_HF_TOKEN \\
           --dry-run

Preset Models:
  - Chinese (Basic): iic/speech_campplus_sv_zh-cn_16k-common
  - Chinese-English (Advanced): iic/speech_campplus_sv_zh_en_16k-common_advanced
        """
    )

    # Required arguments
    parser.add_argument(
        '-i', '--input',
        required=True,
        help='Input ModelScope repository (e.g., iic/speech_campplus_sv_zh-cn_16k-common)'
    )

    parser.add_argument(
        '-o', '--output',
        required=True,
        help='Output model name (will be uploaded to mlx-community/{output})'
    )

    # Optional token (can use env var)
    parser.add_argument(
        '-t', '--token',
        help='HuggingFace API token (or set HF_TOKEN environment variable)'
    )

    # Quantization options
    quant_group = parser.add_argument_group('quantization options')
    quant_group.add_argument(
        '--q2',
        action='store_true',
        help='Create 2-bit quantized version'
    )
    quant_group.add_argument(
        '--q4',
        action='store_true',
        default=False,
        help='Create 4-bit quantized version (enabled by default if no quant flags specified)'
    )
    quant_group.add_argument(
        '--q8',
        action='store_true',
        help='Create 8-bit quantized version'
    )
    quant_group.add_argument(
        '--no-quantization',
        action='store_true',
        help='Skip all quantization (only create regular version)'
    )

    # Other options
    parser.add_argument(
        '--dry-run',
        action='store_true',
        help='Test conversion without uploading to HuggingFace'
    )

    parser.add_argument(
        '--verbose', '-v',
        action='store_true',
        help='Enable verbose logging'
    )

    parser.add_argument(
        '--version',
        action='version',
        version='CAM++ MLX Converter CLI v1.0.0'
    )

    return parser.parse_args()


def get_hf_token(args) -> Optional[str]:
    """
    Get HuggingFace token from args or environment

    Args:
        args: Parsed command line arguments

    Returns:
        HF token string or None
    """
    # Priority: command line arg > environment variable
    if args.token:
        return args.token

    env_token = os.getenv('HF_TOKEN') or os.getenv('HUGGING_FACE_HUB_TOKEN')
    if env_token:
        logger.info("Using HF token from environment variable")
        return env_token

    return None


def validate_args(args) -> bool:
    """
    Validate command line arguments

    Args:
        args: Parsed arguments

    Returns:
        True if valid, False otherwise
    """
    # Check token unless dry run
    if not args.dry_run:
        token = get_hf_token(args)
        if not token:
            logger.error("ERROR: HuggingFace token required. Provide via --token or HF_TOKEN environment variable")
            logger.error("       Use --dry-run to test conversion without uploading")
            return False

        if not token.startswith('hf_'):
            logger.warning("WARNING: HF token should start with 'hf_' - are you sure this is correct?")

    # Validate repo format
    if '/' not in args.input:
        logger.error(f"ERROR: Input repo '{args.input}' must be in format 'username/model-name'")
        return False

    # Validate output name
    if '/' in args.output:
        logger.error(f"ERROR: Output name '{args.output}' should not contain '/' (organization is automatically set to {TARGET_ORGANIZATION})")
        return False

    return True


def main():
    """Main CLI entry point"""
    args = parse_args()

    # Set verbose logging if requested
    if args.verbose:
        logging.getLogger().setLevel(logging.DEBUG)
        logger.debug("Verbose logging enabled")

    # Validate arguments
    if not validate_args(args):
        sys.exit(1)

    # Get token
    token = get_hf_token(args)
    if args.dry_run:
        token = "dry_run_token_placeholder"
        logger.info("πŸ” DRY RUN MODE - Will not upload to HuggingFace")

    # Determine quantization settings
    # Default to Q4 if no quantization flags specified
    if not args.no_quantization and not (args.q2 or args.q4 or args.q8):
        args.q4 = True
        logger.info("πŸ“¦ No quantization flags specified - defaulting to Q4")

    if args.no_quantization:
        args.q2 = args.q4 = args.q8 = False
        logger.info("πŸ“¦ Quantization disabled - creating regular version only")

    # Display configuration
    logger.info("=" * 70)
    logger.info("CAM++ MLX Converter - CLI Mode")
    logger.info("=" * 70)
    logger.info(f"Input Repository:  {args.input}")
    logger.info(f"Output Name:       {TARGET_ORGANIZATION}/{args.output}")
    logger.info(f"Quantization:      Q2={args.q2}, Q4={args.q4}, Q8={args.q8}")
    logger.info(f"Dry Run:           {args.dry_run}")
    logger.info("=" * 70)
    logger.info("")

    # Create converter
    converter = CAMPPConverter()

    # Perform conversion
    try:
        logger.info("πŸš€ Starting conversion...")
        logger.info("")

        # If dry run, we'll need to modify the converter to skip upload
        # For now, just run the conversion normally
        # TODO: Add dry_run parameter to converter

        result = converter.convert_model(
            input_repo=args.input,
            output_name=args.output,
            hf_token=token,
            quantize_q2=args.q2,
            quantize_q4=args.q4,
            quantize_q8=args.q8
        )

        # Print results
        logger.info("")
        logger.info("=" * 70)
        logger.info("CONVERSION RESULTS")
        logger.info("=" * 70)
        print(result)
        logger.info("=" * 70)

        # Check if conversion was successful
        if "βœ…" in result or "Conversion Successful" in result:
            logger.info("βœ… Conversion completed successfully!")
            sys.exit(0)
        elif "⚠️" in result or "Warning" in result:
            logger.warning("⚠️ Conversion completed with warnings (model not uploaded)")
            sys.exit(1)
        else:
            logger.error("❌ Conversion failed")
            sys.exit(1)

    except KeyboardInterrupt:
        logger.info("\n\n⚠️ Conversion interrupted by user")
        sys.exit(130)
    except Exception as e:
        logger.error(f"\n\n❌ Conversion failed with exception: {e}")
        if args.verbose:
            import traceback
            traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    main()