Spaces:
Runtime error
Runtime error
File size: 3,321 Bytes
8969f81 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import { ObjectID } from 'mongodb';
import { MongoObject } from './MongoObject';
import { Utils } from '../lib/Utils';
import { config } from '../lib/config';
import db from './Database';
export enum ModelId {
'distil-gpt2' = 'distil-gpt2',
'arxiv-nlp' = 'arxiv-nlp',
'gpt2-large' = 'gpt2-large',
'gpt2-xl' = 'gpt2-xl',
'xlnet' = 'xlnet',
'gpt' = 'gpt',
'ctrl' = 'ctrl',
'pplm' = 'pplm',
}
export const ALL_MODELS = Object.values(ModelId) as string[];
namespace Doc {
interface InsertDeltaOperation {
insert: string;
attributes?: {
link?: string;
bold?: true;
};
}
export interface Contents {
ops: InsertDeltaOperation[];
}
}
class Doc extends MongoObject {
protected static __type = Doc;
protected static __collectionName: string = 'docs';
protected static __idField: string = 'shortId';
_id: ObjectID;
shortId: string;
longId?: string;
model: ModelId;
contents: Doc.Contents;
/**
* Optional reference to another doc's `shortId`.
*/
clonedFrom?: string;
title?: string;
/**
* Whether to display it on the front page.
*/
featured?: boolean;
static fromObject(o: Partial<Doc>): Doc {
return Object.assign(new Doc(), o);
}
/**
* Initialize a new doc with random ids.
*/
static seed(model: ModelId): Doc {
return this.fromObject({
shortId: Utils.randomStr(10),
longId: Utils.randomStr(24),
model
});
}
/**
* Insert a new doc duplicated from an existing one.
*/
async duplicate(): Promise<Doc> {
const newdoc = Doc.seed(this.model);
newdoc.contents = this.contents;
newdoc.title = this.title;
newdoc.clonedFrom = this.shortId;
await db.docs.insertOne(newdoc);
return newdoc;
}
/**
* This is displayed next to the doc title in a `<code>` tag.
*/
get modelFamily(): string {
if ([
'arxiv-nlp',
'gpt2-large',
'gpt2-xl',
].includes(this.model)) {
return 'gpt2';
}
if (this.model === 'ctrl') {
return `salesforce/ctrl`;
}
if (this.model === 'pplm') {
return `uber/pplm`;
}
return this.model;
}
get hasCustomLogo(): boolean {
return ['ctrl', 'pplm'].includes(this.model);
}
get modelInfoLink(): string {
if (this.model === 'pplm') {
return `https://github.com/huggingface/transformers/tree/master/examples/pplm`;
}
return `https://github.com/huggingface/transformers`;
}
get shareUrl(): string {
return `${config.transformerAutocompleteUrl}/share/${this.shortId}`;
}
get editUrl(): string {
return `${config.transformerAutocompleteUrl}/doc/${this.model}/${this.longId}/edit`;
}
/**
* Construct a (safe) HTML representation of the quill delta content format.
*/
get getHTML(): string | undefined {
if (! this.contents) {
return ;
}
return this.contents.ops.map(op => {
const escaped = op.insert?.toString().replace(/</g, "<") ?? "";
if (op.attributes && op.attributes.bold) {
return `<strong>${escaped}</strong>`;
}
return escaped;
}).join("");
}
/**
* Construct a (safe) JSON representation of the doc.contents.
*/
get contentsJson(): string | undefined {
if (! this.contents) {
return ;
}
return JSON.stringify(this.contents).replace(/</g, "<");
}
duck(): string {
return '___quack___';
}
}
export default Doc;
|