Public
HTTP (deprecated)
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
48
import Pipeline from "https://esm.town/v/iamseeley/pipeline";
export async function questionAnsweringHandler(req) {
if (req.method === "GET") {
return new Response(`
<!DOCTYPE html>
<html>
<body>
<h2>Question Answering</h2>
<form id="qaForm">
<label for="question">Question:</label><br>
<input type="text" id="question" name="question"><br><br>
<label for="context">Context:</label><br>
<textarea id="context" name="context"></textarea><br><br>
<input type="submit" value="Get Answer">
</form>
<div id="result"></div>
<script>
document.getElementById('qaForm').addEventListener('submit', async (event) => {
event.preventDefault();
const question = document.getElementById('question').value;
const context = document.getElementById('context').value;
const response = await fetch('/qa', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ inputs: { question, context } })
});
const data = await response.json();
let answer = "No answer found";
if (data && data.answer) {
answer = data.answer;
}
document.getElementById('result').innerHTML = '<p>Question: ' + question + '</p><p>Answer: ' + answer + '</p>';
});
</script>
</body>
</html>
`, { headers: { "Content-Type": "text/html" } });
} else if (req.method === "POST") {
const { inputs } = await req.json();
const pipeline = new Pipeline("question-answering", "deepset/roberta-base-squad2");
const result = await pipeline.run(inputs);
return new Response(JSON.stringify(result), { headers: { "Content-Type": "application/json" } });
}
}
iamseeley-examplequestionanswering.web.val.run
June 19, 2024