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
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
/** @jsx jsx */
import { jsx } from "https://deno.land/x/hono@v3.11.7/middleware.ts";
import { Hono } from "https://deno.land/x/hono@v3.11.7/mod.ts";
import { ModelProvider, modelProvider } from "https://esm.town/v/yawnxyz/ai";
const app = new Hono();
const Layout = ({ children, title = "Dynamic Form Example" }) => (
<html lang="en">
<head>
<title>{title}</title>
<script src="https://unpkg.com/htmx.org@1.9.9"></script>
</head>
<body>
{children}
</body>
</html>
);
const Home: FC = () => (
<main>
<form hx-post="/generate-form" hx-swap="outerHTML" hx-target="#dynamicFormSection">
<label for="prompt">Form Prompt:</label>
<input type="text" id="prompt" name="prompt" />
<button type="submit">Generate Form</button>
</form>
<div id="dynamicFormSection"></div>
</main>
);
app.get("/", (c) => {
return c.html(
<Layout>
<Home />
</Layout>,
);
});
const generateForm = async (prompt: string) => {
// Generate AI prompt based on the user's input
const response = await modelProvider.gen({
prompt: `Generate a form based on the following prompt: ${prompt}`,
provider: 'google',
});
// Extract the generated text from the response
const generatedText = response.text;
// Parse the AI-generated form fields from the Markdown-formatted string
const fields = generatedText
.split('\n')
.filter((line) => line.startsWith('**') && line.endsWith(':**'))
.map((field) => field.slice(2, -2).trim());
// Generate the form based on the extracted fields
return (
<form>
{fields.map((field) => (
<div key={field}>
<label htmlFor={field}>{field}:</label>
<input type="text" id={field} name={field} />
</div>
))}
<button type="submit">Submit</button>
</form>
);
};
app.post("/generate-form", async (c) => {
const body = await c.req.parseBody();
const { prompt } = body;
const form = await generateForm(prompt);
return c.html(form);
});
export default app.fetch;
yawnxyz-dynamicforminput.web.val.run
June 4, 2024