File size: 9,774 Bytes
1b2020c
a17da6c
 
 
 
a047768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4da8013
 
 
f1bc99f
a047768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a17da6c
 
f1bc99f
a047768
4da8013
a047768
 
 
 
 
 
 
 
 
 
4da8013
a047768
4da8013
a047768
4da8013
a047768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4da8013
a047768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cd33b4
f1bc99f
a047768
dae90a7
 
f1bc99f
a047768
 
 
f1bc99f
a047768
 
 
 
f1bc99f
dae90a7
a047768
dae90a7
 
 
a047768
dae90a7
 
f1bc99f
a047768
 
f1bc99f
dae90a7
a047768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dae90a7
a047768
dae90a7
a047768
 
 
 
dae90a7
a047768
 
a17da6c
a047768
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Генерация изображений — Hugging Face SDK</title>
    <style>
        * { box-sizing: border-box; }
        body {
            font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
            max-width: 700px;
            margin: 0 auto;
            padding: 30px 20px;
            background: #f9fafb;
            color: #111827;
        }
        h1 { margin-top: 0; margin-bottom: 8px; }
        .subtitle { color: #6b7280; margin-bottom: 20px; }
        label { display: block; margin: 15px 0 6px; font-weight: 600; font-size: 14px; }
        input, textarea, select, button {
            width: 100%;
            padding: 12px;
            border-radius: 8px;
            border: 1px solid #d1d5db;
            font: inherit;
            background: #fff;
        }
        textarea { min-height: 100px; resize: vertical; }
        
        .accordion {
            border: 1px solid #e5e7eb;
            border-radius: 8px;
            margin: 20px 0;
            background: #ffffff;
            box-shadow: 0 1px 3px rgba(0,0,0,0.05);
        }
        .accordion summary {
            padding: 12px 16px;
            font-weight: 600;
            cursor: pointer;
            user-select: none;
            background: #f3f4f6;
            border-radius: 8px;
            outline: none;
        }
        .accordion[open] summary {
            border-bottom: 1px solid #e5e7eb;
            border-bottom-left-radius: 0;
            border-bottom-right-radius: 0;
        }
        .accordion-content { padding: 15px; }
        .row { display: flex; gap: 15px; }
        .row > div { flex: 1; }

        button {
            margin-top: 10px;
            border: none;
            background: #10b981;
            color: white;
            font-weight: 700;
            cursor: pointer;
        }
        button:hover { background: #059669; }
        button:disabled { background: #9ca3af; cursor: wait; }

        #status { margin-top: 15px; min-height: 24px; color: #4b5563; font-weight: 500; }
        #result {
            display: none;
            width: 100%;
            margin-top: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.12);
        }
        #download {
            display: none;
            margin-top: 15px;
            text-align: center;
            text-decoration: none;
            background: #374151;
            color: white;
            padding: 12px;
            border-radius: 8px;
            font-weight: 600;
        }
        #error {
            display: none;
            margin-top: 15px;
            padding: 15px;
            background: #fef2f2;
            border: 1px solid #fca5a5;
            border-radius: 8px;
            color: #991b1b;
            white-space: pre-wrap;
            word-break: break-word;
        }
        .warning {
            padding: 12px;
            margin-bottom: 20px;
            background: #fff7ed;
            border: 1px solid #fed7aa;
            border-radius: 8px;
            color: #9a3412;
            font-size: 14px;
        }
    </style>
</head>
<body>

    <h1>🎨 Генерация изображений</h1>
    <div class="subtitle">Интерфейс с расширенным выбором моделей и настройками Gradio-style</div>

    <div class="warning">
        ⚠️ Токен используется в браузере. Убедитесь, что используете Read-токен.
    </div>

    <label for="token">Hugging Face Access Token</label>
    <input id="token" type="password" placeholder="hf_..." autocomplete="off">

    <label for="model">Модель</label>
    <select id="model">
        <option value="black-forest-labs/FLUX.1-schnell">FLUX.1-schnell (Сверхбыстрая)</option>
        <option value="black-forest-labs/FLUX.1-dev">FLUX.1-dev (Высокое качество)</option>
        <option value="stabilityai/stable-diffusion-3.5-large">Stable Diffusion 3.5 Large</option>
        <option value="stabilityai/stable-diffusion-xl-base-1.0">Stable Diffusion XL (SDXL)</option>
        <option value="stabilityai/stable-diffusion-2-1">Stable Diffusion 2.1</option>
    </select>

    <details class="accordion">
        <summary>⚙️ Дополнительные параметры (Настройки генерации)</summary>
        <div class="accordion-content">
            <div class="row">
                <div>
                    <label for="steps">Количество шагов (Steps)</label>
                    <input id="steps" type="number" value="4" min="1" max="50">
                </div>
                <div>
                    <label for="guidance">Сила промпта (Guidance Scale)</label>
                    <input id="guidance" type="number" value="3.5" step="0.5" min="1" max="20">
                </div>
            </div>
            <div class="row">
                <div>
                    <label for="width">Ширина (Width)</label>
                    <input id="width" type="number" value="1024" step="64" min="256" max="1440">
                </div>
                <div>
                    <label for="height">Высота (Height)</label>
                    <input id="height" type="number" value="1024" step="64" min="256" max="1440">
                </div>
            </div>
        </div>
    </details>

    <label for="prompt">Промпт</label>
    <textarea id="prompt" placeholder="A cinematic photo of a futuristic city at night, neon lights, rain, ultra detailed..."></textarea>

    <button id="generate">Сгенерировать</button>

    <div id="status"></div>
    <div id="error"></div>
    <img id="result" alt="Generated image">
    <a id="download" download="generated-image.png">Скачать изображение</a>

    <script type="module">
        import { InferenceClient } from "https://esm.sh/@huggingface/inference";

        const tokenInput = document.getElementById("token");
        const modelInput = document.getElementById("model");
        const promptInput = document.getElementById("prompt");
        const stepsInput = document.getElementById("steps");
        const guidanceInput = document.getElementById("guidance");
        const widthInput = document.getElementById("width");
        const heightInput = document.getElementById("height");
        
        const generateButton = document.getElementById("generate");
        const status = document.getElementById("status");
        const errorBox = document.getElementById("error");
        const resultImage = document.getElementById("result");
        const downloadLink = document.getElementById("download");
        
        let currentImageUrl = null;

        function showError(message) {
            errorBox.textContent = message;
            errorBox.style.display = "block";
        }
        function clearError() {
            errorBox.textContent = "";
            errorBox.style.display = "none";
        }
        function clearResult() {
            resultImage.style.display = "none";
            downloadLink.style.display = "none";
            if (currentImageUrl) {
                URL.revokeObjectURL(currentImageUrl);
                currentImageUrl = null;
            }
        }

        generateButton.addEventListener("click", async () => {
            clearError();
            clearResult();

            const token = tokenInput.value.trim();
            const prompt = promptInput.value.trim();
            const model = modelInput.value;

            const steps = parseInt(stepsInput.value) || 4;
            const guidance = parseFloat(guidanceInput.value) || 3.5;
            const width = parseInt(widthInput.value) || 1024;
            const height = parseInt(heightInput.value) || 1024;

            if (!token) {
                showError("Введите Hugging Face Access Token.");
                return;
            }
            if (!prompt) {
                showError("Введите промпт.");
                return;
            }

            generateButton.disabled = true;
            status.textContent = "⏳ Генерация изображения...";

            try {
                const client = new InferenceClient(token);

                const imageBlob = await client.textToImage({
                    model: model,
                    inputs: prompt,
                    parameters: {
                        num_inference_steps: steps,
                        guidance_scale: guidance,
                        width: width,
                        height: height
                    }
                });

                currentImageUrl = URL.createObjectURL(imageBlob);
                resultImage.src = currentImageUrl;
                resultImage.style.display = "block";

                downloadLink.href = currentImageUrl;
                downloadLink.style.display = "block";
                status.textContent = "✅ Изображение готово!";

            } catch (error) {
                console.error(error);
                let message = "Не удалось сгенерировать изображение.";
                if (error) {
                    message += "\n\n" + (error.message || String(error));
                }
                showError(message);
                status.textContent = "❌ Ошибка генерации.";
            } finally {
                generateButton.disabled = false;
            }
        });
    </script>
</body>
</html>