Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
import Pipeline from "https://esm.town/v/iamseeley/pipeline";
export default async function handler(req) {
if (req.method === "GET") {
return new Response(`
<!DOCTYPE html>
<html>
<body>
<h2>Text Classification</h2>
<form id="classificationForm">
<label for="text">Text:</label><br>
<input type="text" id="text" name="text"><br><br>
<input type="submit" value="Classify">
</form>
<div id="result"></div>
<script>
document.getElementById('classificationForm').addEventListener('submit', async (event) => {
event.preventDefault();
const text = document.getElementById('text').value;
const response = await fetch('/classify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ inputs: text })
});
const data = await response.json();
if (data && data[0]) {
const bestLabel = data[0][0].label;
const bestScore = data[0][0].score;
document.getElementById('result').innerHTML = '<p>Original: ' + text + '</p><p>Classified: ' + bestLabel + ' (' + bestScore + ')</p>';
} else {
document.getElementById('result').innerHTML = '<p>Error classifying text.</p>';
}
});
</script>
</body>
</html>
`, { headers: { "Content-Type": "text/html" } });
} else if (req.method === "POST") {
const { inputs } = await req.json();
const pipeline = new Pipeline("text-classification", "distilbert-base-uncased-finetuned-sst-2-english");
const result = await pipeline.run(inputs);
return new Response(JSON.stringify(result), { headers: { "Content-Type": "application/json" } });
}
}
iamseeley-exampletextclassification.web.val.run
June 19, 2024