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
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 Generation</h2>
<form id="generationForm">
<label for="text">Prompt:</label><br>
<input type="text" id="text" name="text"><br><br>
<input type="submit" value="Generate">
</form>
<div id="result"></div>
<script>
document.getElementById('generationForm').addEventListener('submit', async (event) => {
event.preventDefault();
const text = document.getElementById('text').value;
const response = await fetch('/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ inputs: text })
});
const data = await response.json();
document.getElementById('result').innerHTML = '<p>Prompt: ' + text + '</p><p>Generated: ' + data[0].generated_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-generation", "gpt2");
const result = await pipeline.run(inputs);
return new Response(JSON.stringify(result), { headers: { "Content-Type": "application/json" } });
}
}