File size: 5,329 Bytes
5d65665
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Task-specific input generation for benchmarking
 * Based on Transformers.js usage examples
 */

export interface TaskInput {
  inputs: any;
  options?: any;
}

/**
 * Generate appropriate input for a given task type
 */
export function getTaskInput(task: string, batchSize: number = 1): TaskInput {
  // Normalize task name
  const normalizedTask = task.toLowerCase().trim();

  // Text tasks
  if (normalizedTask === "fill-mask") {
    const inputs = Array(batchSize).fill("The goal of life is [MASK].");
    return { inputs };
  }

  if (normalizedTask === "question-answering") {
    const question = "Who was Jim Henson?";
    const context = "Jim Henson was a nice puppet.";
    // For batch, repeat the same question-context pair
    const inputs = Array(batchSize).fill({ question, context });
    return { inputs: batchSize === 1 ? { question, context } : inputs };
  }

  if (normalizedTask === "summarization") {
    const text =
      "The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, " +
      "and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. " +
      "During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest " +
      "man-made structure in the world, a title it held for 41 years until the Chrysler Building in New " +
      "York City was finished in 1930.";
    const inputs = Array(batchSize).fill(text);
    return { inputs, options: { max_new_tokens: 100 } };
  }

  if (normalizedTask === "sentiment-analysis" || normalizedTask === "text-classification") {
    const inputs = Array(batchSize).fill("I love transformers!");
    return { inputs };
  }

  if (normalizedTask === "text-generation") {
    const inputs = Array(batchSize).fill("Once upon a time, there was");
    return { inputs, options: { max_new_tokens: 10 } };
  }

  if (normalizedTask === "text2text-generation") {
    const inputs = Array(batchSize).fill("how can I become more healthy?");
    return { inputs, options: { max_new_tokens: 100 } };
  }

  if (normalizedTask === "token-classification" || normalizedTask === "ner") {
    const inputs = Array(batchSize).fill("My name is Sarah and I live in London");
    return { inputs };
  }

  if (normalizedTask === "translation") {
    const inputs = Array(batchSize).fill("Life is like a box of chocolate.");
    return { inputs, options: { src_lang: "eng_Latn", tgt_lang: "fra_Latn" } };
  }

  if (normalizedTask === "zero-shot-classification") {
    const text = "I love transformers!";
    const labels = ["positive", "negative"];
    // For batching, would need to handle differently - for now use single input
    return { inputs: batchSize === 1 ? [text, labels] : Array(batchSize).fill([text, labels]) };
  }

  if (normalizedTask === "feature-extraction") {
    const inputs = Array(batchSize).fill("This is a simple test.");
    return { inputs };
  }

  // Vision tasks - use public image URLs
  if (normalizedTask === "background-removal") {
    const url = "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/portrait-of-woman_small.jpg";
    const inputs = Array(batchSize).fill(url);
    return { inputs };
  }

  if (normalizedTask === "depth-estimation") {
    const url = "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg";
    const inputs = Array(batchSize).fill(url);
    return { inputs };
  }

  if (normalizedTask === "image-classification") {
    const url = "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg";
    const inputs = Array(batchSize).fill(url);
    return { inputs };
  }

  if (normalizedTask === "image-segmentation") {
    const url = "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg";
    const inputs = Array(batchSize).fill(url);
    return { inputs };
  }

  if (normalizedTask === "image-to-image") {
    const url = "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg";
    const inputs = Array(batchSize).fill(url);
    return { inputs };
  }

  if (normalizedTask === "object-detection") {
    const url = "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg";
    const inputs = Array(batchSize).fill(url);
    return { inputs, options: { threshold: 0.9 } };
  }

  if (normalizedTask === "image-feature-extraction") {
    const url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png";
    const inputs = Array(batchSize).fill(url);
    return { inputs };
  }

  // Audio tasks
  if (normalizedTask === "audio-classification") {
    const url = "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav";
    const inputs = Array(batchSize).fill(url);
    return { inputs };
  }

  if (normalizedTask === "automatic-speech-recognition") {
    const url = "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav";
    const inputs = Array(batchSize).fill(url);
    return { inputs };
  }

  // Default fallback for unknown tasks - use text
  console.warn(`Unknown task type: ${task}, using default text input`);
  const inputs = Array(batchSize).fill("The quick brown fox jumps over the lazy dog.");
  return { inputs };
}