barunsaha commited on
Commit
68eff7b
·
1 Parent(s): e1720ec

Use uniform format for displaying the supported models names

Browse files
Files changed (1) hide show
  1. src/slidedeckai/cli.py +56 -84
src/slidedeckai/cli.py CHANGED
@@ -10,6 +10,47 @@ from slidedeckai.core import SlideDeckAI
10
  from slidedeckai.global_config import GlobalConfig
11
 
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  class CustomHelpFormatter(argparse.HelpFormatter):
14
  """
15
  Custom formatter for argparse that improves the display of choices.
@@ -32,8 +73,9 @@ class CustomHelpFormatter(argparse.HelpFormatter):
32
  # Special handling for model choices and error messages
33
  lines = []
34
  header = 'Available models:'
 
35
  lines.append(header)
36
- lines.append('-' * len(header))
37
 
38
  # Extract models from text
39
  if text.startswith('choose from'):
@@ -43,23 +85,8 @@ class CustomHelpFormatter(argparse.HelpFormatter):
43
  else:
44
  models = text.split('\n')[1:]
45
 
46
- # Group models by provider
47
- provider_models = {}
48
- for model in sorted(models):
49
- if not model.strip():
50
- continue
51
- if match := GlobalConfig.PROVIDER_REGEX.match(model):
52
- provider = match.group(1)
53
- if provider not in provider_models:
54
- provider_models[provider] = []
55
- provider_models[provider].append(model.strip())
56
-
57
- # Add models grouped by provider
58
- for provider in sorted(provider_models.keys()):
59
- lines.append(f'\n{provider}:')
60
- for model in provider_models[provider]:
61
- lines.append(f' {model}')
62
-
63
  return lines
64
 
65
  return super()._split_lines(text, width)
@@ -74,23 +101,14 @@ class CustomArgumentParser(argparse.ArgumentParser):
74
  if 'invalid choice' in message and '--model' in message:
75
  # Extract models from the error message
76
  choices_str = message[message.find('(choose from'):]
77
- models = [m.strip("' ") for m in choices_str.replace('(choose from', '').rstrip(')').split(',')]
78
-
79
- # Group models by provider
80
- provider_models = {}
81
- for model in sorted(models):
82
- if match := GlobalConfig.PROVIDER_REGEX.match(model):
83
- provider = match.group(1)
84
- if provider not in provider_models:
85
- provider_models[provider] = []
86
- provider_models[provider].append(model.strip())
87
-
88
- # Format the error message with grouped models
89
  error_lines = ['Error: Invalid model choice. Available models:']
90
- for provider in sorted(provider_models.keys()):
91
- error_lines.append(f'\n{provider}:')
92
- for model in sorted(provider_models[provider]):
93
- error_lines.append(f' • {model}')
94
 
95
  self.print_help()
96
  print('\n' + '\n'.join(error_lines), file=sys.stderr)
@@ -101,60 +119,14 @@ class CustomArgumentParser(argparse.ArgumentParser):
101
 
102
  def format_models_list() -> str:
103
  """Format the models list in a nice grouped format with descriptions."""
104
- lines = ['Supported SlideDeck AI models:', '']
105
-
106
- # Group models by provider
107
- provider_models = {}
108
- for model, info in sorted(GlobalConfig.VALID_MODELS.items()):
109
- if match := GlobalConfig.PROVIDER_REGEX.match(model):
110
- provider = match.group(1)
111
- if provider not in provider_models:
112
- provider_models[provider] = []
113
- provider_models[provider].append((model, info))
114
-
115
- # Add models grouped by provider
116
- for provider in sorted(provider_models.keys()):
117
- lines.append(f'{provider}:')
118
- # Find the longest model name for alignment
119
- max_model_len = max(len(model) for model, _ in provider_models[provider])
120
- max_desc_len = max(len(info['description']) for _, info in provider_models[provider])
121
-
122
- # Format as a table with aligned columns
123
- format_str = f' {{:<{max_model_len}}} | {{:<{max_desc_len}}} | {{:>4}}'
124
- lines.append(' ' + '-' * (max_model_len + max_desc_len + 13))
125
-
126
- for model, info in sorted(provider_models[provider]):
127
- paid_status = 'Paid' if info.get('paid', False) else 'Free'
128
- lines.append(format_str.format(
129
- model,
130
- info['description'],
131
- paid_status
132
- ))
133
- lines.append('') # Add spacing between provider sections
134
-
135
- return '\n'.join(lines)
136
 
137
 
138
  def format_model_help() -> str:
139
  """Format model choices as a grouped bulleted list for help text."""
140
- lines = []
141
-
142
- # Group models by provider
143
- provider_models = {}
144
- for model in sorted(GlobalConfig.VALID_MODELS.keys()):
145
- if match := GlobalConfig.PROVIDER_REGEX.match(model):
146
- provider = match.group(1)
147
- if provider not in provider_models:
148
- provider_models[provider] = []
149
- provider_models[provider].append(model)
150
-
151
- # Add models grouped by provider
152
- for provider in sorted(provider_models.keys()):
153
- lines.append(f'\n{provider}:')
154
- for model in sorted(provider_models[provider]):
155
- lines.append(f' • {model}')
156
-
157
- return '\n'.join(lines)
158
 
159
 
160
  def main():
 
10
  from slidedeckai.global_config import GlobalConfig
11
 
12
 
13
+ def group_models_by_provider(models: list[str]) -> dict[str, list[str]]:
14
+ """
15
+ Group model names by their provider.
16
+
17
+ Args:
18
+ models (list[str]): List of model names.
19
+
20
+ Returns:
21
+ dict[str, list[str]]: Dictionary mapping provider codes to lists of model names.
22
+ """
23
+ provider_models = {}
24
+ for model in sorted(models):
25
+ if match := GlobalConfig.PROVIDER_REGEX.match(model):
26
+ provider = match.group(1)
27
+ if provider not in provider_models:
28
+ provider_models[provider] = []
29
+ provider_models[provider].append(model.strip())
30
+
31
+ return provider_models
32
+
33
+
34
+ def format_models_as_bullets(models: list[str]) -> str:
35
+ """
36
+ Format models as a bulleted list, grouped by provider.
37
+
38
+ Args:
39
+ models (list[str]): List of model names.
40
+
41
+ Returns:
42
+ str: Formatted string of models.
43
+ """
44
+ provider_models = group_models_by_provider(models)
45
+ lines = []
46
+ for provider in sorted(provider_models.keys()):
47
+ lines.append(f'\n{provider}:')
48
+ for model in sorted(provider_models[provider]):
49
+ lines.append(f' • {model}')
50
+
51
+ return '\n'.join(lines)
52
+
53
+
54
  class CustomHelpFormatter(argparse.HelpFormatter):
55
  """
56
  Custom formatter for argparse that improves the display of choices.
 
73
  # Special handling for model choices and error messages
74
  lines = []
75
  header = 'Available models:'
76
+ separator = '------------------------' # Fixed-length separator
77
  lines.append(header)
78
+ lines.append(separator)
79
 
80
  # Extract models from text
81
  if text.startswith('choose from'):
 
85
  else:
86
  models = text.split('\n')[1:]
87
 
88
+ # Use the centralized formatting
89
+ lines.extend(format_models_as_bullets(models).split('\n'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  return lines
91
 
92
  return super()._split_lines(text, width)
 
101
  if 'invalid choice' in message and '--model' in message:
102
  # Extract models from the error message
103
  choices_str = message[message.find('(choose from'):]
104
+ models = [
105
+ m.strip("' ") for m in choices_str.replace(
106
+ '(choose from', ''
107
+ ).rstrip(')').split(',')
108
+ ]
109
+
 
 
 
 
 
 
110
  error_lines = ['Error: Invalid model choice. Available models:']
111
+ error_lines.extend(format_models_as_bullets(models).split('\n'))
 
 
 
112
 
113
  self.print_help()
114
  print('\n' + '\n'.join(error_lines), file=sys.stderr)
 
119
 
120
  def format_models_list() -> str:
121
  """Format the models list in a nice grouped format with descriptions."""
122
+ header = 'Supported SlideDeck AI models:\n'
123
+ models = list(GlobalConfig.VALID_MODELS.keys())
124
+ return header + format_models_as_bullets(models)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
 
127
  def format_model_help() -> str:
128
  """Format model choices as a grouped bulleted list for help text."""
129
+ return format_models_as_bullets(list(GlobalConfig.VALID_MODELS.keys()))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
 
132
  def main():