@@ -650,43 +650,44 @@ def save_pretrained(self, save_directory, push_to_hub: bool = False, **kwargs):
650650 processor_dict = self .to_dict ()
651651 # Save `chat_template` in its own file. We can't get it from `processor_dict` as we popped it in `to_dict`
652652 # to avoid serializing chat template in json config file. So let's get it from `self` directly
653- save_as_jinja = kwargs .get ("save_jinja_files" , True )
654- is_single_template = isinstance (self .chat_template , str )
655-
656- if save_as_jinja and is_single_template :
657- # New format for single templates is to save them as chat_template.jinja
658- with open (output_chat_template_file_jinja , "w" , encoding = "utf-8" ) as f :
659- f .write (self .chat_template )
660- logger .info (f"chat template saved in { output_chat_template_file_jinja } " )
661- elif save_as_jinja and not is_single_template :
662- # New format for multiple templates is to save the default as chat_template.jinja
663- # and the other templates in the chat_templates/ directory
664- for template_name , template in self .chat_template .items ():
665- if template_name == "default" :
666- with open (output_chat_template_file_jinja , "w" , encoding = "utf-8" ) as f :
667- f .write (self .chat_template ["default" ])
668- logger .info (f"chat template saved in { output_chat_template_file_jinja } " )
669- else :
670- os .makedirs (chat_template_dir , exist_ok = True )
671- template_filepath = os .path .join (chat_template_dir , f"{ template_name } .jinja" )
672- with open (template_filepath , "w" , encoding = "utf-8" ) as f :
673- f .write (template )
674- logger .info (f"chat template saved in { template_filepath } " )
675- elif is_single_template :
676- # Legacy format for single templates: Put them in chat_template.json
677- chat_template_json_string = (
678- json .dumps ({"chat_template" : self .chat_template }, indent = 2 , sort_keys = True ) + "\n "
679- )
680- with open (output_chat_template_file_legacy , "w" , encoding = "utf-8" ) as writer :
681- writer .write (chat_template_json_string )
682- logger .info (f"chat template saved in { output_chat_template_file_legacy } " )
683- elif self .chat_template is not None :
684- # At this point we have multiple templates in the legacy format, which is not supported
685- # chat template dicts are saved to chat_template.json as lists of dicts with fixed key names.
686- raise ValueError (
687- "Multiple chat templates are not supported in the legacy format. Please save them as separate files "
688- "using the `save_jinja_files` argument."
689- )
653+ if self .chat_template is not None :
654+ save_as_jinja = kwargs .get ("save_jinja_files" , True )
655+ is_single_template = isinstance (self .chat_template , str )
656+
657+ if save_as_jinja and is_single_template :
658+ # New format for single templates is to save them as chat_template.jinja
659+ with open (output_chat_template_file_jinja , "w" , encoding = "utf-8" ) as f :
660+ f .write (self .chat_template )
661+ logger .info (f"chat template saved in { output_chat_template_file_jinja } " )
662+ elif save_as_jinja and not is_single_template :
663+ # New format for multiple templates is to save the default as chat_template.jinja
664+ # and the other templates in the chat_templates/ directory
665+ for template_name , template in self .chat_template .items ():
666+ if template_name == "default" :
667+ with open (output_chat_template_file_jinja , "w" , encoding = "utf-8" ) as f :
668+ f .write (self .chat_template ["default" ])
669+ logger .info (f"chat template saved in { output_chat_template_file_jinja } " )
670+ else :
671+ os .makedirs (chat_template_dir , exist_ok = True )
672+ template_filepath = os .path .join (chat_template_dir , f"{ template_name } .jinja" )
673+ with open (template_filepath , "w" , encoding = "utf-8" ) as f :
674+ f .write (template )
675+ logger .info (f"chat template saved in { template_filepath } " )
676+ elif is_single_template :
677+ # Legacy format for single templates: Put them in chat_template.json
678+ chat_template_json_string = (
679+ json .dumps ({"chat_template" : self .chat_template }, indent = 2 , sort_keys = True ) + "\n "
680+ )
681+ with open (output_chat_template_file_legacy , "w" , encoding = "utf-8" ) as writer :
682+ writer .write (chat_template_json_string )
683+ logger .info (f"chat template saved in { output_chat_template_file_legacy } " )
684+ elif self .chat_template is not None :
685+ # At this point we have multiple templates in the legacy format, which is not supported
686+ # chat template dicts are saved to chat_template.json as lists of dicts with fixed key names.
687+ raise ValueError (
688+ "Multiple chat templates are not supported in the legacy format. Please save them as "
689+ "separate files using the `save_jinja_files` argument."
690+ )
690691
691692 # For now, let's not save to `processor_config.json` if the processor doesn't have extra attributes and
692693 # `auto_map` is not specified.
0 commit comments