MCPcopy Create free account
hub / github.com/ModalityDance/Omni-R1 / save

Method save

src/transformers/src/transformers/agents/tools.py:144–202  ·  view source on GitHub ↗

Saves the relevant code files for your tool so it can be pushed to the Hub. This will copy the code of your tool in `output_dir` as well as autogenerate: - a config file named `tool_config.json` - an `app.py` file so that your tool can be converted to a space

(self, output_dir)

Source from the content-addressed store, hash-verified

142 self.is_initialized = True
143
144 def save(self, output_dir):
145 """
146 Saves the relevant code files for your tool so it can be pushed to the Hub. This will copy the code of your
147 tool in `output_dir` as well as autogenerate:
148
149 - a config file named `tool_config.json`
150 - an `app.py` file so that your tool can be converted to a space
151 - a `requirements.txt` containing the names of the module used by your tool (as detected when inspecting its
152 code)
153
154 You should only use this method to save tools that are defined in a separate module (not `__main__`).
155
156 Args:
157 output_dir (`str`): The folder in which you want to save your tool.
158 """
159 os.makedirs(output_dir, exist_ok=True)
160 # Save module file
161 if self.__module__ == "__main__":
162 raise ValueError(
163 f"We can't save the code defining {self} in {output_dir} as it's been defined in __main__. You "
164 "have to put this code in a separate module so we can include it in the saved folder."
165 )
166 module_files = custom_object_save(self, output_dir)
167
168 module_name = self.__class__.__module__
169 last_module = module_name.split(".")[-1]
170 full_name = f"{last_module}.{self.__class__.__name__}"
171
172 # Save config file
173 config_file = os.path.join(output_dir, "tool_config.json")
174 if os.path.isfile(config_file):
175 with open(config_file, "r", encoding="utf-8") as f:
176 tool_config = json.load(f)
177 else:
178 tool_config = {}
179
180 tool_config = {
181 "tool_class": full_name,
182 "description": self.description,
183 "name": self.name,
184 "inputs": self.inputs,
185 "output_type": str(self.output_type),
186 }
187 with open(config_file, "w", encoding="utf-8") as f:
188 f.write(json.dumps(tool_config, indent=2, sort_keys=True) + "\n")
189
190 # Save app file
191 app_file = os.path.join(output_dir, "app.py")
192 with open(app_file, "w", encoding="utf-8") as f:
193 f.write(APP_FILE_TEMPLATE.format(module_name=last_module, class_name=self.__class__.__name__))
194
195 # Save requirements file
196 requirements_file = os.path.join(output_dir, "requirements.txt")
197 imports = []
198 for module in module_files:
199 imports.extend(get_imports(module))
200 imports = list(set(imports))
201 with open(requirements_file, "w", encoding="utf-8") as f:

Callers 15

push_to_hubMethod · 0.95
save_pretrainedMethod · 0.45
save_pretrainedMethod · 0.45
_save_pretrainedMethod · 0.45
_tune_save_checkpointMethod · 0.45
_save_rng_stateMethod · 0.45
_save_tpuMethod · 0.45
_saveMethod · 0.45
_push_from_checkpointMethod · 0.45
push_to_hubMethod · 0.45
runMethod · 0.45

Calls 6

custom_object_saveFunction · 0.85
get_importsFunction · 0.85
splitMethod · 0.80
loadMethod · 0.80
writeMethod · 0.80
formatMethod · 0.80