23"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" ",
24),
25{ headers: { "Content-Type": "image/svg+xml" } },
26);
27}
14};
1516const promptKey = 'imagePromptValue';
1718export function updateForm() {
4546const model = document.getElementById('model').value;
47const imageSize = document.getElementById('image_size')?.value;
48const prompt = document.getElementById('prompt').value;
4959loadingShimmer.className = 'loading-shimmer';
6061const imageFooter = document.createElement('div');
62imageFooter.className = 'image-footer';
6364const modelElement = document.createElement('p');
71removeButton.onclick = () => loadingContainer.remove();
7273imageFooter.appendChild(modelElement);
74imageFooter.appendChild(removeButton);
7576loadingContainer.appendChild(loadingShimmer);
77loadingContainer.appendChild(imageFooter);
78resultDiv.prepend(loadingContainer);
7987'landscape_16_9': 16 / 9
88};
89const aspectRatio = aspectRatios[imageSize] || 1; // Default to 1 if imageSize is not found
90const containerWidth = loadingContainer.offsetWidth;
91loadingShimmer.style.height = `${containerWidth / aspectRatio}px`;
97const guidanceScale = document.getElementById('guidance_scale')?.value;
98const seed = document.getElementById('seed')?.value;
99const numImages = document.getElementById('num_images')?.value;
100const enableSafetyChecker = document.getElementById('enable_safety_checker')?.checked;
101const syncMode = document.getElementById('sync_mode')?.checked;
108prompt,
109negative_prompt: negativePrompt || undefined,
110image_size: imageSize,
111num_inference_steps: numInferenceSteps ? parseInt(numInferenceSteps) : undefined,
112guidance_scale: guidanceScale ? parseFloat(guidanceScale) : undefined,
113seed: seed ? parseInt(seed) : undefined,
114num_images: numImages ? parseInt(numImages) : undefined,
115enable_safety_checker: enableSafetyChecker !== undefined ? enableSafetyChecker : undefined,
116sync_mode: syncMode !== undefined ? syncMode : undefined,
119};
120121const response = await fetch('/generate-image', {
122method: 'POST',
123headers: {
128129const data = await response.json();
130handleImageResponse(data, imageSize, model);
131}
132133function handleImageResponse(data, imageSize, model) {
134const resultDiv = document.getElementById('result');
135136if (data.imageUrls) {
137data.imageUrls.forEach((imageUrl, index) => {
138const imgElement = new Image();
139imgElement.src = imageUrl;
140imgElement.alt = `Generated Image ${index + 1}`;
141imgElement.style.display = 'none';
142147loadingShimmer.remove();
148imgElement.style.display = 'block';
149loadingContainer.insertBefore(imgElement, loadingContainer.querySelector('.image-footer'));
150};
151173updateForm();
174const modelElement = document.getElementById('model');
175const formElement = document.getElementById('imageForm');
176const resetButton = document.getElementById('resetButton');
177
3Outputs:
4```text
5ImageMagick 7.1.1-30 Q8 x86_64 dd459b01f:20240407 https://imagemagick.org
6Delegates: freetype heic jng jp2 jpeg jxl lcms lqr openexr png raw tiff webp xml zlib
7Features: Cipher
19<Url type="text/html" method="get" template="https://janpaul123-valtownsemanticsearch.web.val.run/search?q={searchTerms}"/>
20<Url type="application/opensearchdescription+xml" template="https://janpaul123-valtownsemanticsearch.web.val.run/opensearch.xml"/>
21<Image height="16" width="16" type="image/png">https://pomdtr-favicons.web.val.run/val-town</Image>
22<moz:SearchForm>https://janpaul123-valtownsemanticsearch.web.val.run/search</moz:SearchForm>
23</OpenSearchDescription>`,
formStylesmain.tsx2 matches
112}
113114.image-container {
115display: flex;
116flex-direction: column;
120}
121122.image-footer {
123display: flex;
124justify-content: space-between;
textToImagePlaygroundmain.tsx13 matches
1import { generateImageHandler } from 'https://esm.town/v/iamseeley/generateImageHandler';
2import { realtimeGenerateImageHandler } from 'https://esm.town/v/iamseeley/realtimeGenerateImageHandler';
3import { formStyles } from "https://esm.town/v/iamseeley/formStyles";
49<meta charset="UTF-8">
10<meta name="viewport" content="width=device-width, initial-scale=1.0">
11<title>Image Generator</title>
12<style>
13${formStyles}
16<body>
17<header>
18<h2>Text to Image Playground</h2>
19<a target="blank" href="https://fal.ai">fal.ai</a>
20</header>
25</select> -->
26
27<form id="imageForm">
28<label for="model">Model:</label>
29<select id="model" name="model">
40</div>
41<div class="buttons">
42<button type="submit">Generate Image</button>
43<button type="button" id="resetButton">Reset</button>
44</div>
63const type = this.value;
64if (type === 'regular') {
65document.getElementById('imageForm').style.display = 'block';
66document.getElementById('realtimeForm').style.display = 'none';
67} else {
68document.getElementById('imageForm').style.display = 'none';
69document.getElementById('realtimeForm').style.display = 'block';
70}
74window.addEventListener('DOMContentLoaded', (event) => {
75document.getElementById('generationType').value = 'regular';
76document.getElementById('imageForm').style.display = 'block';
77document.getElementById('realtimeForm').style.display = 'none';
78});
90status: 200,
91});
92} else if (req.method === 'POST' && url.pathname === '/generate-image') {
93return generateImageHandler(req);
94// } else if (req.method === 'POST' && url.pathname === '/realtime-generate-image') {
95// return realtimeGenerateImageHandler(req);
96} else {
97return new Response('Not Found', { status: 404 });
realtimeFormLogicmain.tsx16 matches
8promptInput.addEventListener("input", () => {
9clearTimeout(debounceTimeout);
10debounceTimeout = setTimeout(generateImages, 500);
11});
1213async function generateImages() {
14resultDiv.innerHTML = ''; // Clear previous results
152526try {
27const response = await fetch('/realtime-generate-image', {
28method: 'POST',
29headers: {
42if (data.urls && Array.isArray(data.urls)) {
43data.urls.forEach(url => {
44appendImage(url, model);
45});
46} else {
51console.log('Request aborted');
52} else {
53console.error('Error generating images:', error);
54// Handle the error, display an error message, etc.
55}
59}
6061function appendImage(imageUrl, model) {
62const imgElement = new Image();
63imgElement.src = imageUrl;
64imgElement.alt = "Generated Image";
65imgElement.className = 'generated-image';
6667const loadingContainer = document.createElement('div');
71loadingShimmer.className = 'loading-shimmer';
7273const imageFooter = document.createElement('div');
74imageFooter.className = 'image-footer';
7576const modelElement = document.createElement('p');
83removeButton.onclick = () => loadingContainer.remove();
8485imageFooter.appendChild(modelElement);
86imageFooter.appendChild(removeButton);
8788loadingContainer.appendChild(loadingShimmer);
89loadingContainer.appendChild(imageFooter);
90resultDiv.appendChild(loadingContainer);
9193loadingShimmer.remove();
94imgElement.style.display = 'block';
95loadingContainer.insertBefore(imgElement, imageFooter);
96};
97
realtimeGenerateImageHandlermain.tsx13 matches
5});
67export const realtimeGenerateImageHandler = async (req: Request): Promise<Response> => {
8const { prompt, model } = await req.json();
91819try {
20console.log("Starting real-time image generation...");
2122const result = await new Promise((resolve, reject) => {
27},
28onError: (error) => {
29console.error("Error during real-time image generation:", error);
30reject(error);
31},
35connection.send({
36prompt,
37num_images: 4,
38});
39});
41console.log("Generation completed. Result:", result);
4243if (result.images && Array.isArray(result.images)) {
44const imageUrls = result.images.map((imageObj: any) => {
45if (imageObj && imageObj.url) {
46return imageObj.url; // Assuming there's a 'url' property in the image object
47} else {
48console.warn("Invalid image object:", imageObj);
49return null; // or handle invalid case accordingly
50}
51});
5253write(JSON.stringify({ urls: imageUrls }));
54} else {
55console.error("Invalid or missing 'images' property in the result. Result:", result);
56write(JSON.stringify({ error: "Invalid response received from the server." }));
57}
5859console.log("All image data sent");
60} catch (error) {
61console.error("Error in realtimeGenerateImageHandler:", error);
62write(JSON.stringify({ error: error.message }));
63}
realtimeFormLogicREADME.md1 match
1Migrated from folder: image_gen/realtimeFormLogic
1Migrated from folder: image_gen/realtimeGenerateImageHandler