Update app.py
Browse files
app.py
CHANGED
|
@@ -115,141 +115,191 @@ def index():
|
|
| 115 |
return """
|
| 116 |
<!DOCTYPE html>
|
| 117 |
<html>
|
| 118 |
-
|
| 119 |
<head>
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
| 132 |
</head>
|
| 133 |
-
|
| 134 |
<body>
|
| 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 |
-
const reader = new FileReader();
|
| 233 |
-
reader.onload = function (e) {
|
| 234 |
-
const dataUrl = e.target.result;
|
| 235 |
-
const shortenedDataUrl = dataUrl.substring(0, 20) + '...' + dataUrl.slice(-20);
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
});
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
|
| 255 |
"""
|
|
|
|
| 115 |
return """
|
| 116 |
<!DOCTYPE html>
|
| 117 |
<html>
|
|
|
|
| 118 |
<head>
|
| 119 |
+
<title>Image Upscaling & Restoration API</title>
|
| 120 |
+
<style>
|
| 121 |
+
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
|
| 122 |
+
.container { border: 1px solid #ddd; padding: 20px; border-radius: 5px; }
|
| 123 |
+
.form-group { margin-bottom: 15px; }
|
| 124 |
+
label { display: block; margin-bottom: 5px; }
|
| 125 |
+
input, select { width: 100%; padding: 8px; box-sizing: border-box; }
|
| 126 |
+
button { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; }
|
| 127 |
+
button:hover { background-color: #45a049; }
|
| 128 |
+
#result { margin-top: 20px; }
|
| 129 |
+
#preview { max-width: 100%; margin-top: 10px; }
|
| 130 |
+
#apiUsage { background-color: #f5f5f5; padding: 15px; border-radius: 5px; margin-top: 20px; font-family: monospace; white-space: pre-wrap; }
|
| 131 |
+
#apiUsage h3 { margin-top: 0; }
|
| 132 |
+
#formDataPreview { max-height: 200px; overflow-y: auto; }
|
| 133 |
+
</style>
|
| 134 |
</head>
|
|
|
|
| 135 |
<body>
|
| 136 |
+
<h1>Image Upscaling & Restoration API</h1>
|
| 137 |
+
<div class="container">
|
| 138 |
+
<form id="uploadForm" enctype="multipart/form-data">
|
| 139 |
+
<div class="form-group">
|
| 140 |
+
<label for="file">Upload Image:</label>
|
| 141 |
+
<input type="file" id="file" name="file" required>
|
| 142 |
+
</div>
|
| 143 |
+
<div class="form-group">
|
| 144 |
+
<label for="version">Version:</label>
|
| 145 |
+
<select id="version" name="version">
|
| 146 |
+
<option value="v1.2">v1.2</option>
|
| 147 |
+
<option value="v1.3">v1.3</option>
|
| 148 |
+
<option value="v1.4" selected>v1.4</option>
|
| 149 |
+
<option value="RestoreFormer">RestoreFormer</option>
|
| 150 |
+
<option value="CodeFormer">CodeFormer</option>
|
| 151 |
+
<option value="RealESR-General-x4v3">RealESR-General-x4v3</option>
|
| 152 |
+
</select>
|
| 153 |
+
</div>
|
| 154 |
+
<div class="form-group">
|
| 155 |
+
<label for="scale">Rescaling factor:</label>
|
| 156 |
+
<input type="number" id="scale" name="scale" value="2" step="0.1" min="1" max="4" required>
|
| 157 |
+
</div>
|
| 158 |
+
<button type="submit">Process Image</button>
|
| 159 |
+
</form>
|
| 160 |
+
|
| 161 |
+
<div id="apiUsage">
|
| 162 |
+
<h3>API Usage:</h3>
|
| 163 |
+
<p>Here's how to call the API with the current form data:</p>
|
| 164 |
+
<div id="formDataPreview">Fill out the form to see the API call code...</div>
|
| 165 |
+
<div id="fetchCode"></div>
|
| 166 |
+
</div>
|
| 167 |
+
|
| 168 |
+
<div id="result">
|
| 169 |
+
<h3>Result:</h3>
|
| 170 |
+
<div id="outputContainer" style="display: none;">
|
| 171 |
+
<img id="preview" src="" alt="Processed Image">
|
| 172 |
+
<a id="downloadLink" href="#" download>Download Image</a>
|
| 173 |
+
</div>
|
| 174 |
+
</div>
|
| 175 |
+
</div>
|
| 176 |
+
|
| 177 |
+
<script>
|
| 178 |
+
// フォームの変更を監視してAPI使用例を更新
|
| 179 |
+
function updateApiUsage() {
|
| 180 |
+
const form = document.getElementById('uploadForm');
|
| 181 |
+
const fileInput = document.getElementById('file');
|
| 182 |
+
const version = document.getElementById('version').value;
|
| 183 |
+
const scale = document.getElementById('scale').value;
|
| 184 |
+
|
| 185 |
+
// フォームデータのプレビューを作成
|
| 186 |
+
const formData = {
|
| 187 |
+
version: version,
|
| 188 |
+
scale: scale
|
| 189 |
+
};
|
| 190 |
+
|
| 191 |
+
if (fileInput.files.length > 0) {
|
| 192 |
+
const file = fileInput.files[0];
|
| 193 |
+
const reader = new FileReader();
|
| 194 |
+
reader.onload = function(e) {
|
| 195 |
+
const dataURL = e.target.result;
|
| 196 |
+
// データURLを短縮表示
|
| 197 |
+
const shortenedDataURL = dataURL.substring(0, 20) + '...' + dataURL.substring(dataURL.length - 20);
|
| 198 |
+
formData.file = shortenedDataURL;
|
| 199 |
+
formData.fileName = file.name;
|
| 200 |
+
formData.fileType = file.type;
|
| 201 |
+
formData.fileSize = file.size + ' bytes';
|
| 202 |
+
|
| 203 |
+
updateFormDataPreview(formData);
|
| 204 |
+
updateFetchCode();
|
| 205 |
+
};
|
| 206 |
+
reader.readAsDataURL(file);
|
| 207 |
+
} else {
|
| 208 |
+
formData.file = 'No file selected';
|
| 209 |
+
updateFormDataPreview(formData);
|
| 210 |
+
updateFetchCode();
|
| 211 |
+
}
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
function updateFormDataPreview(formData) {
|
| 215 |
+
const previewDiv = document.getElementById('formDataPreview');
|
| 216 |
+
previewDiv.innerHTML = '<strong>Form data to be sent:</strong><br>' +
|
| 217 |
+
JSON.stringify(formData, null, 2);
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
function updateFetchCode() {
|
| 221 |
+
const currentUrl = window.location.href;
|
| 222 |
+
const baseUrl = currentUrl.endsWith('/') ? currentUrl : currentUrl + '/';
|
| 223 |
+
const apiUrl = baseUrl + 'api/restore';
|
| 224 |
+
|
| 225 |
+
const fetchCodeDiv = document.getElementById('fetchCode');
|
| 226 |
+
fetchCodeDiv.innerHTML = `
|
| 227 |
+
<strong>JavaScript fetch code:</strong>
|
| 228 |
+
<pre>
|
| 229 |
+
const formData = new FormData();
|
| 230 |
+
formData.append('file', document.getElementById('file').files[0]);
|
| 231 |
+
formData.append('version', '${document.getElementById('version').value}');
|
| 232 |
+
formData.append('scale', ${document.getElementById('scale').value});
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
+
fetch('${apiUrl}', {
|
| 235 |
+
method: 'POST',
|
| 236 |
+
body: formData
|
| 237 |
+
})
|
| 238 |
+
.then(response => {
|
| 239 |
+
if (!response.ok) {
|
| 240 |
+
return response.json().then(err => { throw new Error(err.error || 'Unknown error'); });
|
| 241 |
}
|
| 242 |
+
return response.blob();
|
| 243 |
+
})
|
| 244 |
+
.then(blob => {
|
| 245 |
+
// Process the returned image blob
|
| 246 |
+
const url = URL.createObjectURL(blob);
|
| 247 |
+
console.log('Image processed successfully', url);
|
| 248 |
+
})
|
| 249 |
+
.catch(error => {
|
| 250 |
+
console.error('Error:', error.message);
|
| 251 |
});
|
| 252 |
+
</pre>`;
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
// フォーム要素の変更を監視
|
| 256 |
+
document.getElementById('file').addEventListener('change', updateApiUsage);
|
| 257 |
+
document.getElementById('version').addEventListener('change', updateApiUsage);
|
| 258 |
+
document.getElementById('scale').addEventListener('input', updateApiUsage);
|
| 259 |
+
|
| 260 |
+
// 初期表示
|
| 261 |
+
updateApiUsage();
|
| 262 |
+
|
| 263 |
+
document.getElementById('uploadForm').addEventListener('submit', function(e) {
|
| 264 |
+
e.preventDefault();
|
| 265 |
+
|
| 266 |
+
const formData = new FormData();
|
| 267 |
+
formData.append('file', document.getElementById('file').files[0]);
|
| 268 |
+
formData.append('version', document.getElementById('version').value);
|
| 269 |
+
formData.append('scale', document.getElementById('scale').value);
|
| 270 |
+
|
| 271 |
+
const currentUrl = window.location.href;
|
| 272 |
+
const baseUrl = currentUrl.endsWith('/') ? currentUrl : currentUrl + '/';
|
| 273 |
+
const apiUrl = baseUrl + 'api/restore';
|
| 274 |
+
|
| 275 |
+
fetch(apiUrl, {
|
| 276 |
+
method: 'POST',
|
| 277 |
+
body: formData
|
| 278 |
+
})
|
| 279 |
+
.then(response => {
|
| 280 |
+
if (!response.ok) {
|
| 281 |
+
return response.json().then(err => { throw new Error(err.error || 'Unknown error'); });
|
| 282 |
+
}
|
| 283 |
+
return response.blob();
|
| 284 |
+
})
|
| 285 |
+
.then(blob => {
|
| 286 |
+
const url = URL.createObjectURL(blob);
|
| 287 |
+
const preview = document.getElementById('preview');
|
| 288 |
+
const downloadLink = document.getElementById('downloadLink');
|
| 289 |
+
const outputContainer = document.getElementById('outputContainer');
|
| 290 |
+
|
| 291 |
+
preview.src = url;
|
| 292 |
+
downloadLink.href = url;
|
| 293 |
+
downloadLink.download = 'restored_' + document.getElementById('file').files[0].name;
|
| 294 |
+
outputContainer.style.display = 'block';
|
| 295 |
+
})
|
| 296 |
+
.catch(error => {
|
| 297 |
+
alert('Error: ' + error.message);
|
| 298 |
+
});
|
| 299 |
+
});
|
| 300 |
+
</script>
|
| 301 |
+
</body>
|
| 302 |
+
</html>
|
| 303 |
|
| 304 |
|
| 305 |
"""
|