File size: 3,146 Bytes
697c26d
 
 
 
 
 
 
 
a3f8ae9
697c26d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a10bb32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
697c26d
a10bb32
 
 
697c26d
 
 
a10bb32
697c26d
a10bb32
 
 
 
697c26d
a10bb32
 
697c26d
a10bb32
 
 
697c26d
 
 
a10bb32
697c26d
 
a10bb32
697c26d
 
 
 
 
 
 
 
a10bb32
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
export async function extractEmbeddings(
  worker,
  weightsURL,
  tokenizerURL,
  configURL,
  modelID,
  sentences,
  updateStatus,
  normalize_embeddings = true
) {
  return new Promise((resolve, reject) => {
    worker.postMessage({
      weightsURL,
      tokenizerURL,
      configURL,
      modelID,
      sentences,
      normalize_embeddings,
    });
    function messageHandler(event) {
      if ("error" in event.data) {
        worker.removeEventListener("message", messageHandler);
        reject(new Error(event.data.error));
      }
      if (event.data.status === "complete") {
        worker.removeEventListener("message", messageHandler);
        resolve(event.data);
      }
      if (updateStatus) updateStatus(event.data);
    }
    worker.addEventListener("message", messageHandler);
  });
}

export async function generateText(
  worker,
  weightsURL,
  tokenizerURL,
  configURL,
  modelID,
  prompt,
  params,
  updateStatus
) {
  return new Promise((resolve, reject) => {
    worker.postMessage({
      weightsURL,
      tokenizerURL,
      configURL,
      modelID,
      prompt,
      params,
    });
    function messageHandler(event) {
      if ("error" in event.data) {
        worker.removeEventListener("message", messageHandler);
        reject(new Error(event.data.error));
      }
      if (event.data.status === "complete") {
        worker.removeEventListener("message", messageHandler);
        resolve(event.data);
      }
      if (updateStatus) updateStatus(event.data);
    }
    worker.addEventListener("message", messageHandler);
  });
}

const TASKS = {
  fluency: {
    prefix: "Fix the grammar: ",
    max_length: 300,
  },
  coherence: {
    prefix: "Rewrite to make this easier to understand: ",
    max_length: 300,
  },
  simplification: {
    prefix: "translate English to Romanian: ",
    max_length: 300,
  },
  simplification: {
    prefix: "Paraphrase this: ",
    max_length: 300,
  },
  formalization: {
    prefix: "Write this more formally: ",
    max_length: 300,
  },
  neutralize: {
    prefix: "Write in a more neutral way: ",
    max_length: 300,
  },
};

export const MODELS = {
  coedit_large_quantized: {
    size: "643 MB",
    base_url: "https://huggingface.co/jbochi/candle-coedit-quantized/resolve/main/",
    model: "model.gguf",
    tokenizer: "tokenizer.json",
    config: "config.json",
    tasks: TASKS,
  },
  coedit_xl_quantized: {
    size: "2.34 GB",
    base_url: "https://huggingface.co/jbochi/candle-coedit-quantized/resolve/main/",
    model: "model-xl.gguf",
    tokenizer: "tokenizer.json",
    config: "config-xl.json",
    tasks: TASKS,
  },
  coedit_large: {
    size: "3.13 GB",
    base_url: "https://huggingface.co/grammarly/coedit-large/resolve/main/",
    model: "model.safetensors",
    tokenizer: "tokenizer.json",
    config: "config.json",
    tasks: TASKS,
  },
};

export function getModelInfo(id, taskID) {
  const model = MODELS[id];
  return {
    modelURL: model.base_url + model.model,
    configURL: model.base_url + model.config,
    tokenizerURL: model.base_url + model.tokenizer,
    maxLength: model.tasks[taskID].max_length,
  };
};