class ModelCard:
"""Model card generator."""
def __init__(self):
self.sources = {'Hugging Face': {'icon': '🤗', 'url': ''},
'Website': {'icon': '🌐', 'url': ''},
'Research Paper': {'icon': '📄', 'url': ''},
'GitHub': {'icon': '🐱', 'url': ''},
'Demo': {'icon': '🎬', 'url': ''},
'Wikipedia': {'icon': '🕸️', 'url': ''},
'About Model': {'icon': '📖', 'url': ''}}
self.ATTRIBUTES = ['Name', 'Size', 'License', 'Publisher',
'Description', 'Keywords', 'Version',
'Release Date', 'Training corpus', 'Training method',
'Evaluation method', 'Use Cases', 'Compute', 'Features',
'Limitations', 'Strengths']
self.extract = {}
self.summary = ''
def set_url(self, source: str, url: str):
"""Set the model source URL."""
self.sources[source]['url'] = url
def get_url(self, source: str):
"""Get the model source URL."""
return self.sources[source]['url']
def set_extract(self, model_card_extract: str):
"""Initialize the model card generator."""
self.extract = json.loads(model_card_extract.replace('```', ''))
def set_summary(self, summary: str):
"""Set the model summary."""
self.summary = summary
def get_unknown_attributes(self):
return [attr for attr in self.ATTRIBUTES if self.extract[attr] == 'Unknown']
def set_unknowns(self, model_card_extract: str):
"""Set the model card attributes from extract json as argument if values in self.extract are Unknown."""
extract = json.loads(model_card_extract.replace('```', ''))
for attr in self.ATTRIBUTES:
if self.extract[attr] == 'Unknown':
self.extract[attr] = extract[attr]
def generate_material_mkdown(self):
"""Generate the model card as Material for MkDocs Markdown."""
model_card = '---\n'
model_card += 'title: ' + self.extract['Name'] + '\n'
model_card += 'description: ' + self.extract['Description'] + '\n'
model_card += 'tags:\n'
for tag in self.extract['Keywords'].split(','):
model_card += ' - ' + tag.strip() + '\n'
model_card += '---\n\n'
model_card += '# ' + self.extract['Name'] + '\n\n'
model_card += '**' + self.extract['Description']+ '**\n\n'
model_card += '| Publisher | License | Version | Release |\n'
model_card += '| --- | --- | --- | --- |\n'
model_card += '| ' + self.extract['Publisher'] + ' | ' + self.extract['License'] + ' | ' + self.extract['Version'] + ' | ' + self.extract['Release Date'] + ' |\n\n'
model_card += '## Model Summary\n\n'
model_card += self.summary + '\n\n'
model_card += '## Model Resources\n\n'
for source in self.sources:
if self.sources[source]['url']:
model_card += '[{icon} {source}]({url})'.format(
icon=self.sources[source]['icon'],
source=source, url=self.sources[source]['url'])
model_card += ' | ' if source != 'About Model' else ''
if model_card.endswith(' | '):
model_card = model_card[:-3]
model_card += '\n\n'
model_card += '## Model Details\n\n'
# Render rest of the model card attributes as attribute name in bold: attribute value
model_card += '**Size:** ' + self.extract['Size'] + '\n\n'
model_card += '**Use Cases:** ' + self.extract['Use Cases'] + '\n\n'
model_card += '**Training corpus:** ' + self.extract['Training corpus'] + '\n\n'
model_card += '**Training method:** ' + self.extract['Training method'] + '\n\n'
model_card += '**Evaluation method:** ' + self.extract['Evaluation method'] + '\n\n'
model_card += '**Compute:** ' + self.extract['Compute'] + '\n\n'
model_card += '**Features:** ' + self.extract['Features'] + '\n\n'
model_card += '**Limitations:** ' + self.extract['Limitations'] + '\n\n'
model_card += '**Strengths:** ' + self.extract['Strengths'] + '\n\n'
return model_card
def save_markdown(self, generated_markdown: str, filename: str, folder: str):
"""Save the model card as a Markdown file."""
filepath = folder + utils.search_friendly_filename(filename) + '.md'
with open(filepath, 'w') as f:
f.write(generated_markdown)
return 'Model card saved as ' + filepath
class ModelCard: """Model card generator.""" def __init__(self): self.sources = {'Hugging Face': {'icon': '🤗', 'url': ''}, 'Website': {'icon': '🌐', 'url': ''}, 'Research Paper': {'icon': '📄', 'url': ''}, 'GitHub': {'icon': '🐱', 'url': ''}, 'Demo': {'icon': '🎬', 'url': ''}, 'Wikipedia': {'icon': '🕸️', 'url': ''}, 'About Model': {'icon': '📖', 'url': ''}} self.ATTRIBUTES = ['Name', 'Size', 'License', 'Publisher', 'Description', 'Keywords', 'Version', 'Release Date', 'Training corpus', 'Training method', 'Evaluation method', 'Use Cases', 'Compute', 'Features', 'Limitations', 'Strengths'] self.extract = {} self.summary = '' def set_url(self, source: str, url: str): """Set the model source URL.""" self.sources[source]['url'] = url def get_url(self, source: str): """Get the model source URL.""" return self.sources[source]['url'] def set_extract(self, model_card_extract: str): """Initialize the model card generator.""" self.extract = json.loads(model_card_extract.replace('```', '')) def set_summary(self, summary: str): """Set the model summary.""" self.summary = summary def get_unknown_attributes(self): return [attr for attr in self.ATTRIBUTES if self.extract[attr] == 'Unknown'] def set_unknowns(self, model_card_extract: str): """Set the model card attributes from extract json as argument if values in self.extract are Unknown.""" extract = json.loads(model_card_extract.replace('```', '')) for attr in self.ATTRIBUTES: if self.extract[attr] == 'Unknown': self.extract[attr] = extract[attr] def generate_material_mkdown(self): """Generate the model card as Material for MkDocs Markdown.""" model_card = '---\n' model_card += 'title: ' + self.extract['Name'] + '\n' model_card += 'description: ' + self.extract['Description'] + '\n' model_card += 'tags:\n' for tag in self.extract['Keywords'].split(','): model_card += ' - ' + tag.strip() + '\n' model_card += '---\n\n' model_card += '# ' + self.extract['Name'] + '\n\n' model_card += '**' + self.extract['Description']+ '**\n\n' model_card += '| Publisher | License | Version | Release |\n' model_card += '| --- | --- | --- | --- |\n' model_card += '| ' + self.extract['Publisher'] + ' | ' + self.extract['License'] + ' | ' + self.extract['Version'] + ' | ' + self.extract['Release Date'] + ' |\n\n' model_card += '## Model Summary\n\n' model_card += self.summary + '\n\n' model_card += '## Model Resources\n\n' for source in self.sources: if self.sources[source]['url']: model_card += '[{icon} {source}]({url})'.format( icon=self.sources[source]['icon'], source=source, url=self.sources[source]['url']) model_card += ' | ' if source != 'About Model' else '' if model_card.endswith(' | '): model_card = model_card[:-3] model_card += '\n\n' model_card += '## Model Details\n\n' # Render rest of the model card attributes as attribute name in bold: attribute value model_card += '**Size:** ' + self.extract['Size'] + '\n\n' model_card += '**Use Cases:** ' + self.extract['Use Cases'] + '\n\n' model_card += '**Training corpus:** ' + self.extract['Training corpus'] + '\n\n' model_card += '**Training method:** ' + self.extract['Training method'] + '\n\n' model_card += '**Evaluation method:** ' + self.extract['Evaluation method'] + '\n\n' model_card += '**Compute:** ' + self.extract['Compute'] + '\n\n' model_card += '**Features:** ' + self.extract['Features'] + '\n\n' model_card += '**Limitations:** ' + self.extract['Limitations'] + '\n\n' model_card += '**Strengths:** ' + self.extract['Strengths'] + '\n\n' return model_card def save_markdown(self, generated_markdown: str, filename: str, folder: str): """Save the model card as a Markdown file.""" filepath = folder + utils.search_friendly_filename(filename) + '.md' with open(filepath, 'w') as f: f.write(generated_markdown) return 'Model card saved as ' + filepath