|
| 1 | +import { |
| 2 | + ChangeDetectionStrategy, |
| 3 | + ChangeDetectorRef, |
| 4 | + Component, |
| 5 | + EventEmitter, |
| 6 | + Inject, |
| 7 | + Input, |
| 8 | + LOCALE_ID, |
| 9 | + Output, |
| 10 | +} from '@angular/core'; |
| 11 | +import { DatePipe } from '@angular/common'; |
| 12 | + |
| 13 | +import { HuggingfaceService } from '@core/services/api/rest/huggingface.service'; |
| 14 | + |
| 15 | +import { ModelDomain, modelDomainNames } from '@store/model-store/model.model'; |
| 16 | + |
| 17 | +import { IHuggingfaceModel } from '@shared/models/huggingface/huggingface-model'; |
| 18 | +import { IParameter } from '@shared/components/model-details/parameter-details/parameter-details.component'; |
| 19 | + |
| 20 | +import { MarkdownService } from './markdown/markdown.service'; |
| 21 | +import { IHuggingfaceTagsSets } from '../hugging-face-import-ribbon-content.component'; |
| 22 | + |
| 23 | +@Component({ |
| 24 | + selector: 'wb-huggingface-model-details', |
| 25 | + templateUrl: './huggingface-model-details.component.html', |
| 26 | + styleUrls: ['./huggingface-model-details.component.scss'], |
| 27 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 28 | +}) |
| 29 | +export class HuggingfaceModelDetailsComponent { |
| 30 | + private _model: IHuggingfaceModel = null; |
| 31 | + |
| 32 | + @Input() set huggingfaceModel(value: IHuggingfaceModel) { |
| 33 | + this._model = value; |
| 34 | + if (this._model) { |
| 35 | + this.parameters = this._extractHfModelParameters(this._model); |
| 36 | + (async () => { |
| 37 | + this.readme = await this._fetchReadme(); |
| 38 | + this._cdr.detectChanges(); |
| 39 | + })(); |
| 40 | + } else { |
| 41 | + this.parameters = null; |
| 42 | + this.readme = null; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + get huggingfaceModel(): IHuggingfaceModel { |
| 47 | + return this._model; |
| 48 | + } |
| 49 | + |
| 50 | + @Input() tagsSets: IHuggingfaceTagsSets; |
| 51 | + |
| 52 | + @Output() import = new EventEmitter<void>(); |
| 53 | + @Output() hide = new EventEmitter<void>(); |
| 54 | + |
| 55 | + parameters: IParameter[] = []; |
| 56 | + readme: string = null; |
| 57 | + |
| 58 | + constructor( |
| 59 | + private readonly _hfService: HuggingfaceService, |
| 60 | + private readonly _cdr: ChangeDetectorRef, |
| 61 | + private readonly _mdService: MarkdownService, |
| 62 | + @Inject(LOCALE_ID) private readonly _localeId: string |
| 63 | + ) {} |
| 64 | + |
| 65 | + private _extractTags(tags: string[], tag_group: Set<string>): string { |
| 66 | + return tags.filter((tag) => tag_group.has(tag)).join(', ') || 'N/A'; |
| 67 | + } |
| 68 | + |
| 69 | + private _extractHfModelParameters(model: IHuggingfaceModel): IParameter[] { |
| 70 | + return [ |
| 71 | + { label: 'Domain', value: modelDomainNames[ModelDomain.NLP], tooltip: 'Domain' }, |
| 72 | + { label: 'Library', value: this._extractTags(model.tags, this.tagsSets.libraries) }, |
| 73 | + { label: 'Tasks', value: this._extractTags(model.tags, this.tagsSets.pipelineTags) }, |
| 74 | + { label: 'Languages', value: this._extractTags(model.tags, this.tagsSets.languages) }, |
| 75 | + { label: 'Licenses', value: this._extractTags(model.tags, this.tagsSets.licenses) }, |
| 76 | + { label: 'Downloads', value: model.downloads }, |
| 77 | + { label: 'Updated', value: new DatePipe(this._localeId).transform(model.lastModified, 'YYYY/MM/dd, hh:mm') }, |
| 78 | + ]; |
| 79 | + } |
| 80 | + |
| 81 | + private async _fetchReadme(): Promise<string> { |
| 82 | + const markdown = await this._hfService.getModelDetails$(this._model.id).toPromise(); |
| 83 | + return this._mdService.parse(markdown); |
| 84 | + } |
| 85 | +} |
0 commit comments