py: if LICENSE exist then include a copy of it

This commit is contained in:
brian khuu 2024-08-05 22:29:52 +10:00
parent 1ef14b3007
commit 323bf80091
4 changed files with 19 additions and 0 deletions

View File

@ -458,6 +458,7 @@ LazyModel: TypeAlias = 'dict[str, LazyTensor]'
ModelFormat: TypeAlias = Literal['ggml', 'torch', 'safetensors', 'none'] ModelFormat: TypeAlias = Literal['ggml', 'torch', 'safetensors', 'none']
@dataclass @dataclass
class ModelPlus: class ModelPlus:
model: LazyModel model: LazyModel
@ -810,6 +811,8 @@ class OutputFile:
self.gguf.add_license_name(metadata.license_name) self.gguf.add_license_name(metadata.license_name)
if metadata.license_link is not None: if metadata.license_link is not None:
self.gguf.add_license_link(metadata.license_link) self.gguf.add_license_link(metadata.license_link)
if metadata.license_content is not None:
self.gguf.add_license_content(metadata.license_content)
if metadata.url is not None: if metadata.url is not None:
self.gguf.add_url(metadata.url) self.gguf.add_url(metadata.url)

View File

@ -43,6 +43,7 @@ class Keys:
LICENSE = "general.license" LICENSE = "general.license"
LICENSE_NAME = "general.license.name" LICENSE_NAME = "general.license.name"
LICENSE_LINK = "general.license.link" LICENSE_LINK = "general.license.link"
LICENSE_CONTENT = "general.license.content"
# Typically represents the converted GGUF repo (Unless native) # Typically represents the converted GGUF repo (Unless native)
URL = "general.url" # Model Website/Paper URL = "general.url" # Model Website/Paper

View File

@ -529,6 +529,9 @@ class GGUFWriter:
def add_license_link(self, license: str) -> None: def add_license_link(self, license: str) -> None:
self.add_string(Keys.General.LICENSE_LINK, license) self.add_string(Keys.General.LICENSE_LINK, license)
def add_license_content(self, license: str) -> None:
self.add_string(Keys.General.LICENSE_CONTENT, license)
def add_url(self, url: str) -> None: def add_url(self, url: str) -> None:
self.add_string(Keys.General.URL, url) self.add_string(Keys.General.URL, url)

View File

@ -38,6 +38,7 @@ class Metadata:
license: Optional[str] = None license: Optional[str] = None
license_name: Optional[str] = None license_name: Optional[str] = None
license_link: Optional[str] = None license_link: Optional[str] = None
license_content: Optional[str] = None
base_models: Optional[list[dict]] = None base_models: Optional[list[dict]] = None
tags: Optional[list[str]] = None tags: Optional[list[str]] = None
languages: Optional[list[str]] = None languages: Optional[list[str]] = None
@ -379,6 +380,7 @@ class Metadata:
use_model_card_metadata("license", "license") use_model_card_metadata("license", "license")
use_model_card_metadata("license_name", "license_name") use_model_card_metadata("license_name", "license_name")
use_model_card_metadata("license_link", "license_link") use_model_card_metadata("license_link", "license_link")
use_model_card_metadata("license_content", "license_content")
use_array_model_card_metadata("tags", "tags") use_array_model_card_metadata("tags", "tags")
use_array_model_card_metadata("tags", "pipeline_tag") use_array_model_card_metadata("tags", "pipeline_tag")
@ -431,6 +433,14 @@ class Metadata:
if metadata.size_label is None and size_label is not None: if metadata.size_label is None and size_label is not None:
metadata.size_label = size_label metadata.size_label = size_label
# Detect LICENSE file and include a copy
#########################################
if metadata.license_content is None:
standard_license_file_path = Path("LICENSE")
if standard_license_file_path.is_file():
with open(standard_license_file_path, 'r') as file:
metadata.license_content = file.read()
return metadata return metadata
def set_gguf_meta_model(self, gguf_writer: gguf.GGUFWriter): def set_gguf_meta_model(self, gguf_writer: gguf.GGUFWriter):
@ -463,6 +473,8 @@ class Metadata:
gguf_writer.add_license_name(self.license_name) gguf_writer.add_license_name(self.license_name)
if self.license_link is not None: if self.license_link is not None:
gguf_writer.add_license_link(self.license_link) gguf_writer.add_license_link(self.license_link)
if self.license_content is not None:
gguf_writer.add_license_content(self.license_content)
if self.url is not None: if self.url is not None:
gguf_writer.add_url(self.url) gguf_writer.add_url(self.url)