Spaces:
Running
Running
Update index.html
Browse files- index.html +48 -55
index.html
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
-
<title>HF API - Генерация (
|
| 7 |
<style>
|
| 8 |
body { font-family: system-ui, sans-serif; max-width: 600px; margin: 40px auto; padding: 20px; background: #f9fafb; color: #111827; }
|
| 9 |
h2 { margin-top: 0; }
|
|
@@ -38,20 +38,31 @@
|
|
| 38 |
font-size: 13px;
|
| 39 |
overflow-x: auto;
|
| 40 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
</style>
|
| 42 |
</head>
|
| 43 |
<body>
|
| 44 |
-
<h2>Генерация изображений (
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
<label for="hf-token">Твой Access Token (hf_...):</label>
|
| 47 |
<input type="password" id="hf-token" placeholder="Вставь токен">
|
| 48 |
|
| 49 |
<label for="modelSelect">Модель:</label>
|
| 50 |
<select id="modelSelect">
|
| 51 |
-
<option value="black-forest-labs/FLUX.1-
|
| 52 |
-
<option value="black-forest-labs/FLUX.
|
| 53 |
-
<option value="
|
| 54 |
-
<option value="ByteDance/SDXL-Lightning">SDXL-Lightning (Очень быстрая)</option>
|
| 55 |
</select>
|
| 56 |
|
| 57 |
<label for="prompt">Промпт:</label>
|
|
@@ -63,7 +74,9 @@
|
|
| 63 |
<div id="errorContainer"></div>
|
| 64 |
<img id="outputImage" alt="Result">
|
| 65 |
|
| 66 |
-
<script>
|
|
|
|
|
|
|
| 67 |
const btn = document.getElementById('generateBtn');
|
| 68 |
const tokenInput = document.getElementById('hf-token');
|
| 69 |
const modelSelect = document.getElementById('modelSelect');
|
|
@@ -72,9 +85,6 @@
|
|
| 72 |
const errorContainer = document.getElementById('errorContainer');
|
| 73 |
const outputImage = document.getElementById('outputImage');
|
| 74 |
|
| 75 |
-
// НОВЫЙ API ENDPOINT 2026
|
| 76 |
-
const NEW_API_BASE = 'https://router.huggingface.co/hf-inference/models';
|
| 77 |
-
|
| 78 |
function showFullError(title, details) {
|
| 79 |
errorContainer.innerHTML = `
|
| 80 |
<div class="error-block">
|
|
@@ -106,49 +116,21 @@
|
|
| 106 |
}
|
| 107 |
|
| 108 |
try {
|
| 109 |
-
|
| 110 |
-
const apiUrl = `${NEW_API_BASE}/${model}`;
|
| 111 |
-
|
| 112 |
-
statusText.textContent = `Запрос к API: ${apiUrl}`;
|
| 113 |
-
|
| 114 |
-
const response = await fetch(apiUrl, {
|
| 115 |
-
method: 'POST',
|
| 116 |
-
headers: {
|
| 117 |
-
'Authorization': `Bearer ${token}`,
|
| 118 |
-
'Content-Type': 'application/json',
|
| 119 |
-
'Accept': 'image/png'
|
| 120 |
-
},
|
| 121 |
-
body: JSON.stringify({ inputs: prompt })
|
| 122 |
-
});
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
if (!response.ok) {
|
| 127 |
-
const errorBody = await response.text();
|
| 128 |
-
let errorFormatted = errorBody;
|
| 129 |
-
|
| 130 |
-
try {
|
| 131 |
-
const jsonError = JSON.parse(errorBody);
|
| 132 |
-
errorFormatted = JSON.stringify(jsonError, null, 2);
|
| 133 |
-
} catch (e) {}
|
| 134 |
-
|
| 135 |
-
const fullErrorDetails = `${responseInfo}\n\nResponse Body:\n${errorFormatted}`;
|
| 136 |
-
|
| 137 |
-
showFullError(`Ошибка ${response.status}`, fullErrorDetails);
|
| 138 |
-
statusText.textContent = '';
|
| 139 |
-
throw new Error(`HTTP ${response.status}`);
|
| 140 |
-
}
|
| 141 |
|
| 142 |
-
statusText.textContent =
|
| 143 |
-
const blob = await response.blob();
|
| 144 |
-
|
| 145 |
-
if (!blob.type.startsWith('image/')) {
|
| 146 |
-
const textResponse = await blob.text();
|
| 147 |
-
showFullError('Неожиданный ответ', `${responseInfo}\n\nОтвет не является изображением:\n${textResponse}`);
|
| 148 |
-
throw new Error('Not an image');
|
| 149 |
-
}
|
| 150 |
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
outputImage.src = imageUrl;
|
| 154 |
outputImage.style.display = 'block';
|
|
@@ -159,7 +141,9 @@
|
|
| 159 |
console.error(error);
|
| 160 |
console.trace();
|
| 161 |
|
| 162 |
-
|
|
|
|
|
|
|
| 163 |
showFullError('Сетевая ошибка',
|
| 164 |
`Браузер не смог отправить запрос!\n\n` +
|
| 165 |
`Возможные причины:\n` +
|
|
@@ -168,11 +152,20 @@
|
|
| 168 |
`• CORS политика\n\n` +
|
| 169 |
`Откройте DevTools (F12) → вкладка Network (Сеть) → посмотрите на красный запрос.`
|
| 170 |
);
|
| 171 |
-
} else if (
|
| 172 |
-
showFullError('
|
| 173 |
-
`
|
| 174 |
-
`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
);
|
|
|
|
|
|
|
| 176 |
}
|
| 177 |
} finally {
|
| 178 |
btn.disabled = false;
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>HF API - Генерация (fal-ai Provider 2026)</title>
|
| 7 |
<style>
|
| 8 |
body { font-family: system-ui, sans-serif; max-width: 600px; margin: 40px auto; padding: 20px; background: #f9fafb; color: #111827; }
|
| 9 |
h2 { margin-top: 0; }
|
|
|
|
| 38 |
font-size: 13px;
|
| 39 |
overflow-x: auto;
|
| 40 |
}
|
| 41 |
+
.info {
|
| 42 |
+
background: #dbeafe;
|
| 43 |
+
border: 1px solid #3b82f6;
|
| 44 |
+
border-radius: 6px;
|
| 45 |
+
padding: 10px;
|
| 46 |
+
margin-bottom: 15px;
|
| 47 |
+
font-size: 14px;
|
| 48 |
+
}
|
| 49 |
</style>
|
| 50 |
</head>
|
| 51 |
<body>
|
| 52 |
+
<h2>Генерация изображений (fal-ai Provider)</h2>
|
| 53 |
+
|
| 54 |
+
<div class="info">
|
| 55 |
+
<strong>ℹ️ Важно:</strong> С июля 2026 HF изменил API. Теперь используем провайдера <strong>fal-ai</strong> для генерации изображений.
|
| 56 |
+
</div>
|
| 57 |
|
| 58 |
<label for="hf-token">Твой Access Token (hf_...):</label>
|
| 59 |
<input type="password" id="hf-token" placeholder="Вставь токен">
|
| 60 |
|
| 61 |
<label for="modelSelect">Модель:</label>
|
| 62 |
<select id="modelSelect">
|
| 63 |
+
<option value="black-forest-labs/FLUX.1-dev">FLUX.1-dev (Высокое качество)</option>
|
| 64 |
+
<option value="black-forest-labs/FLUX.2-klein-9B">FLUX.2-klein-9B (Быстрая)</option>
|
| 65 |
+
<option value="krea/Krea-2-Turbo">Krea-2-Turbo (Очень быстрая)</option>
|
|
|
|
| 66 |
</select>
|
| 67 |
|
| 68 |
<label for="prompt">Промпт:</label>
|
|
|
|
| 74 |
<div id="errorContainer"></div>
|
| 75 |
<img id="outputImage" alt="Result">
|
| 76 |
|
| 77 |
+
<script type="module">
|
| 78 |
+
import { InferenceClient } from 'https://cdn.jsdelivr.net/npm/@huggingface/inference@3.0.0/+esm';
|
| 79 |
+
|
| 80 |
const btn = document.getElementById('generateBtn');
|
| 81 |
const tokenInput = document.getElementById('hf-token');
|
| 82 |
const modelSelect = document.getElementById('modelSelect');
|
|
|
|
| 85 |
const errorContainer = document.getElementById('errorContainer');
|
| 86 |
const outputImage = document.getElementById('outputImage');
|
| 87 |
|
|
|
|
|
|
|
|
|
|
| 88 |
function showFullError(title, details) {
|
| 89 |
errorContainer.innerHTML = `
|
| 90 |
<div class="error-block">
|
|
|
|
| 116 |
}
|
| 117 |
|
| 118 |
try {
|
| 119 |
+
statusText.textContent = `Инициализация клиента с провайдером fal-ai...`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
// Создаем клиент с указанием провайдера fal-ai
|
| 122 |
+
const client = new InferenceClient(token);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
+
statusText.textContent = `Генерация изображения через fal-ai...`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
// Используем textToImage с явным указанием провайдера
|
| 127 |
+
const imageBlob = await client.textToImage({
|
| 128 |
+
model: model,
|
| 129 |
+
provider: "fal-ai",
|
| 130 |
+
inputs: prompt,
|
| 131 |
+
});
|
| 132 |
+
|
| 133 |
+
const imageUrl = URL.createObjectURL(imageBlob);
|
| 134 |
|
| 135 |
outputImage.src = imageUrl;
|
| 136 |
outputImage.style.display = 'block';
|
|
|
|
| 141 |
console.error(error);
|
| 142 |
console.trace();
|
| 143 |
|
| 144 |
+
let errMsg = error.message;
|
| 145 |
+
|
| 146 |
+
if (errMsg.includes('Failed to fetch')) {
|
| 147 |
showFullError('Сетевая ошибка',
|
| 148 |
`Браузер не смог отправить запрос!\n\n` +
|
| 149 |
`Возможные причины:\n` +
|
|
|
|
| 152 |
`• CORS политика\n\n` +
|
| 153 |
`Откройте DevTools (F12) → вкладка Network (Сеть) → посмотрите на красный запрос.`
|
| 154 |
);
|
| 155 |
+
} else if (errMsg.includes('403') || errMsg.includes('gated')) {
|
| 156 |
+
showFullError('Ошибка доступа',
|
| 157 |
+
`Нет доступа к модели!\n\n` +
|
| 158 |
+
`Зайди на страницу модели на сайте Hugging Face и нажми кнопку:\n` +
|
| 159 |
+
`"Agree and access repository"\n\n` +
|
| 160 |
+
`Модель: ${model}`
|
| 161 |
+
);
|
| 162 |
+
} else if (errMsg.includes('404')) {
|
| 163 |
+
showFullError('Модель не найдена',
|
| 164 |
+
`Модель ${model} не поддерживается провайдером fal-ai.\n\n` +
|
| 165 |
+
`Попробуй другую модель из списка.`
|
| 166 |
);
|
| 167 |
+
} else {
|
| 168 |
+
showFullError('Ошибка', errMsg);
|
| 169 |
}
|
| 170 |
} finally {
|
| 171 |
btn.disabled = false;
|