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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// This approach will use the fal.ai API to generate a custom favicon based on a user-provided prompt.
// The favicon will be displayed on the page for the user to download.
/** @jsxImportSource https://esm.sh/react */
import React, { useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
function App() {
const [prompt, setPrompt] = useState("");
const [faviconUrl, setFaviconUrl] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setError("");
setFaviconUrl("");
try {
const response = await fetch(`/generate?prompt=${encodeURIComponent(prompt)}`);
if (!response.ok) {
throw new Error(`Failed to generate image: ${response.statusText}`);
}
const data = await response.json();
setFaviconUrl(data.imageUrl);
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};
return (
<div>
<h1>Favicon Generator</h1>
<form onSubmit={handleSubmit}>
<input
type="text"
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
placeholder="Enter your favicon prompt"
required
/>
<button type="submit" disabled={loading}>
{loading ? "Generating..." : "Generate Favicon"}
</button>
</form>
{error && <p style={{ color: "red" }}>{error}</p>}
{faviconUrl && (
<div>
<h2>Your Favicon:</h2>
<img src={faviconUrl} alt="Generated Favicon" style={{ width: "32px", height: "32px" }} />
<p>
<a href={faviconUrl} download="favicon.png">
Download Favicon
</a>
</p>
</div>
)}
<p>
<a href={import.meta.url.replace("esm.town", "val.town")}>View Source</a>
</p>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") {
client();
}
async function generateImage(prompt: string) {
const FAL_KEY = Deno.env.get("FAL_KEY");
if (!FAL_KEY) {
throw new Error("FAL_KEY environment variable is not set");
}
const response = await fetch("https://fal.run/fal-ai/flux/schnell", {
method: "POST",
headers: {
"Authorization": `Key ${FAL_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
prompt: prompt,
image_size: "square",
num_inference_steps: 2,
guidance_scale: 1,
num_images: 1,
}),
});
if (!response.ok) {
const errorText = await response.text();
console.error("fal.ai API error:", response.status, response.statusText, errorText);
throw new Error(`Failed to generate image: ${response.status} ${response.statusText}`);
iamseeley-favicongenerator.web.val.run
September 9, 2024