Vokturz commited on
Commit
0b82ec0
·
1 Parent(s): daa5539

add loader to zero-shot classification and add sentiment analysis pipeline

Browse files
src/App.tsx CHANGED
@@ -1,4 +1,3 @@
1
- // src/App.tsx
2
  import { useState } from "react";
3
  import PipelineSelector from "./components/PipelineSelector";
4
  import ZeroShotClassification from "./components/ZeroShotClassification";
 
 
1
  import { useState } from "react";
2
  import PipelineSelector from "./components/PipelineSelector";
3
  import ZeroShotClassification from "./components/ZeroShotClassification";
src/components/PipelineSelector.tsx CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import React from 'react';
3
 
4
  const pipelines = [
 
 
1
  import React from 'react';
2
 
3
  const pipelines = [
src/components/SentimentAnalysis.tsx CHANGED
@@ -18,6 +18,7 @@ function SentimentAnalysis() {
18
  const [text, setText] = useState<string>(PLACEHOLDER_TEXTS.join("\n"));
19
  const [results, setResults] = useState<ClassificationOutput[]>([]);
20
  const [status, setStatus] = useState<string>("idle");
 
21
 
22
  // Create a reference to the worker object.
23
  const worker = useRef<Worker | null>(null);
@@ -41,11 +42,16 @@ function SentimentAnalysis() {
41
  setStatus("loading");
42
  } else if (status === "ready") {
43
  setStatus("ready");
 
 
 
 
44
  } else if (status === "output") {
45
  const result = e.data.output!;
46
  setResults((prevResults) => [...prevResults, result]);
47
  } else if (status === "complete") {
48
  setStatus("idle");
 
49
  }
50
  };
51
 
@@ -126,7 +132,11 @@ function SentimentAnalysis() {
126
  ? "Model loading..."
127
  : "Processing..."}
128
  </button>
129
-
 
 
 
 
130
  <button
131
  className="py-2 px-4 bg-gray-500 hover:bg-gray-600 rounded text-white font-medium transition-colors"
132
  onClick={handleClear}
 
18
  const [text, setText] = useState<string>(PLACEHOLDER_TEXTS.join("\n"));
19
  const [results, setResults] = useState<ClassificationOutput[]>([]);
20
  const [status, setStatus] = useState<string>("idle");
21
+ const [progress, setProgress] = useState<number>(0);
22
 
23
  // Create a reference to the worker object.
24
  const worker = useRef<Worker | null>(null);
 
42
  setStatus("loading");
43
  } else if (status === "ready") {
44
  setStatus("ready");
45
+ } else if (status === "progress") {
46
+ setStatus("progress");
47
+ if (e.data.output.progress && (e.data.output.file as string).startsWith('onnx'))
48
+ setProgress(e.data.output.progress)
49
  } else if (status === "output") {
50
  const result = e.data.output!;
51
  setResults((prevResults) => [...prevResults, result]);
52
  } else if (status === "complete") {
53
  setStatus("idle");
54
+ setProgress(100)
55
  }
56
  };
57
 
 
132
  ? "Model loading..."
133
  : "Processing..."}
134
  </button>
135
+ { status === "progress" &&
136
+ <div className="text-sm font-medium">
137
+ {progress}%
138
+ </div>
139
+ }
140
  <button
141
  className="py-2 px-4 bg-gray-500 hover:bg-gray-600 rounded text-white font-medium transition-colors"
142
  onClick={handleClear}
src/components/ZeroShotClassification.tsx CHANGED
@@ -45,6 +45,7 @@ function ZeroShotClassification() {
45
  );
46
 
47
  const [status, setStatus] = useState<string>("idle");
 
48
 
49
  // Create a reference to the worker object.
50
  const worker = useRef<Worker | null>(null);
@@ -68,6 +69,10 @@ function ZeroShotClassification() {
68
  setStatus("loading");
69
  } else if (status === "ready") {
70
  setStatus("ready");
 
 
 
 
71
  } else if (status === "output") {
72
  const { sequence, labels, scores } = e.data.output!;
73
 
@@ -86,6 +91,7 @@ function ZeroShotClassification() {
86
  });
87
  } else if (status === "complete") {
88
  setStatus("idle");
 
89
  }
90
  };
91
 
@@ -166,6 +172,11 @@ function ZeroShotClassification() {
166
  ? "Model loading..."
167
  : "Processing"}
168
  </button>
 
 
 
 
 
169
  <div className="flex gap-1">
170
  <button
171
  className="border py-1 px-2 bg-green-400 rounded text-white text-sm font-medium cursor-pointer"
 
45
  );
46
 
47
  const [status, setStatus] = useState<string>("idle");
48
+ const [progress, setProgress] = useState<number>(0);
49
 
50
  // Create a reference to the worker object.
51
  const worker = useRef<Worker | null>(null);
 
69
  setStatus("loading");
70
  } else if (status === "ready") {
71
  setStatus("ready");
72
+ } else if (status === "progress") {
73
+ setStatus("progress");
74
+ if (e.data.output.progress && (e.data.output.file as string).startsWith('onnx'))
75
+ setProgress(e.data.output.progress)
76
  } else if (status === "output") {
77
  const { sequence, labels, scores } = e.data.output!;
78
 
 
91
  });
92
  } else if (status === "complete") {
93
  setStatus("idle");
94
+ setProgress(100)
95
  }
96
  };
97
 
 
172
  ? "Model loading..."
173
  : "Processing"}
174
  </button>
175
+ { status === "progress" &&
176
+ <div className="text-sm font-medium">
177
+ {progress}%
178
+ </div>
179
+ }
180
  <div className="flex gap-1">
181
  <button
182
  className="border py-1 px-2 bg-green-400 rounded text-white text-sm font-medium cursor-pointer"
src/types.ts CHANGED
@@ -12,7 +12,7 @@ export interface ClassificationOutput {
12
 
13
 
14
  export interface WorkerMessage {
15
- status: 'initiate' | 'ready' | 'output' | 'complete';
16
  output?: any;
17
  }
18
 
 
12
 
13
 
14
  export interface WorkerMessage {
15
+ status: 'initiate' | 'ready' | 'output' | 'complete' | 'progress';
16
  output?: any;
17
  }
18
 
src/worker.js CHANGED
@@ -29,7 +29,7 @@ self.addEventListener("message", async (event) => {
29
  const pipe = await PipelineFactory.getInstance((x) => {
30
  // We also add a progress callback to the pipeline so that we can
31
  // track model loading.
32
- self.postMessage(x);
33
  });
34
 
35
  // Run the pipeline
 
29
  const pipe = await PipelineFactory.getInstance((x) => {
30
  // We also add a progress callback to the pipeline so that we can
31
  // track model loading.
32
+ self.postMessage({ status: "progress", data: x });
33
  });
34
 
35
  // Run the pipeline
src/workers/sentiment-analysis.js CHANGED
@@ -22,7 +22,7 @@ self.addEventListener("message", async (event) => {
22
  const classifier = await MyTextClassificationPipeline.getInstance((x) => {
23
  // We also add a progress callback to the pipeline so that we can
24
  // track model loading.
25
- self.postMessage(x);
26
  });
27
 
28
  const { text } = event.data;
 
22
  const classifier = await MyTextClassificationPipeline.getInstance((x) => {
23
  // We also add a progress callback to the pipeline so that we can
24
  // track model loading.
25
+ self.postMessage({ status: "progress", output: x });
26
  });
27
 
28
  const { text } = event.data;
src/workers/zero-shot.js CHANGED
@@ -22,7 +22,7 @@ self.addEventListener("message", async (event) => {
22
  const classifier = await MyZeroShotClassificationPipeline.getInstance((x) => {
23
  // We also add a progress callback to the pipeline so that we can
24
  // track model loading.
25
- self.postMessage(x);
26
  });
27
 
28
  const { text, labels } = event.data;
 
22
  const classifier = await MyZeroShotClassificationPipeline.getInstance((x) => {
23
  // We also add a progress callback to the pipeline so that we can
24
  // track model loading.
25
+ self.postMessage({ status: "progress", output: x });
26
  });
27
 
28
  const { text, labels } = event.data;