MCPcopy Create free account
hub / github.com/PaddlePaddle/FastDeploy / main

Function main

fastdeploy/entrypoints/cli/tokenizer.py:187–250  ·  view source on GitHub ↗
(args: argparse.Namespace)

Source from the content-addressed store, hash-verified

185
186
187def main(args: argparse.Namespace) -> None:
188
189 def print_separator(title=""):
190 if title:
191 print(f"\n{'='*50}")
192 print(f" {title}")
193 print(f"{'='*50}")
194 else:
195 print(f"\n{'='*50}")
196
197 # 检查参数
198 if not any([args.encode, args.decode, args.vocab_size, args.info, args.vocab_export]):
199 print("请至少指定一个参数:--encode, --decode, --vocab-size, --info, --vocab-export")
200 return
201
202 # 初始化tokenizer
203 preprocessor = InputPreprocessor(model_config=ModelConfig({"model": args.model_name_or_path}))
204 tokenizer = preprocessor.create_processor().tokenizer
205
206 # 执行操作
207 operations_count = 0
208
209 if args.encode:
210 print_separator("ENCODING")
211 print(f"Input text: {args.encode}")
212 encoded_text = tokenizer.encode(args.encode)
213 print(f"Encoded tokens: {encoded_text}")
214 operations_count += 1
215
216 if args.decode:
217 print_separator("DECODING")
218 print(f"Input tokens: {args.decode}")
219 try:
220 if isinstance(args.decode, str):
221 if args.decode.startswith("[") and args.decode.endswith("]"):
222 tokens = eval(args.decode)
223 else:
224 tokens = [int(x.strip()) for x in args.decode.split(",")]
225 else:
226 tokens = args.decode
227
228 decoded_text = tokenizer.decode(tokens)
229 print(f"Decoded text: {decoded_text}")
230 except Exception as e:
231 print(f"Error decoding tokens: {e}")
232 operations_count += 1
233
234 if args.vocab_size:
235 print_separator("VOCABULARY SIZE")
236 print(f"Vocabulary size: {get_vocab_size(tokenizer)}")
237 operations_count += 1
238
239 if args.info:
240 print_separator("TOKENIZER INFO")
241 print(json.dumps(get_tokenizer_info(tokenizer), indent=2))
242 operations_count += 1
243
244 if args.vocab_export:

Callers 2

test_mainMethod · 0.90
cmdMethod · 0.70

Calls 11

create_processorMethod · 0.95
InputPreprocessorClass · 0.90
ModelConfigClass · 0.90
printFunction · 0.85
print_separatorFunction · 0.85
get_vocab_sizeFunction · 0.85
get_tokenizer_infoFunction · 0.85
export_vocabularyFunction · 0.85
splitMethod · 0.80
encodeMethod · 0.45
decodeMethod · 0.45

Tested by 1

test_mainMethod · 0.72