soiz1 commited on
Commit
04fc3c7
·
verified ·
1 Parent(s): 5242696

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -96
app.py CHANGED
@@ -113,108 +113,147 @@ def restore_image():
113
  @app.route('/')
114
  def index():
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
- </style>
131
- </head>
132
- <body>
133
- <h1>Image Upscaling & Restoration API</h1>
134
- <div class="container">
135
- <form id="uploadForm" enctype="multipart/form-data">
136
- <div class="form-group">
137
- <label for="file">Upload Image:</label>
138
- <input type="file" id="file" name="file" required>
139
- </div>
140
- <div class="form-group">
141
- <label for="version">Version:</label>
142
- <select id="version" name="version">
143
- <option value="v1.2">v1.2</option>
144
- <option value="v1.3">v1.3</option>
145
- <option value="v1.4" selected>v1.4</option>
146
- <option value="RestoreFormer">RestoreFormer</option>
147
- <option value="CodeFormer">CodeFormer</option>
148
- <option value="RealESR-General-x4v3">RealESR-General-x4v3</option>
149
- </select>
150
- </div>
151
- <div class="form-group">
152
- <label for="scale">Rescaling factor:</label>
153
- <input type="number" id="scale" name="scale" value="2" step="0.1" min="1" max="4" required>
154
- </div>
155
- <!-- CodeFormer用のweightパラメータが必要な場合 -->
156
- <!--
 
157
  <div class="form-group">
158
  <label for="weight">Weight (only for CodeFormer):</label>
159
  <input type="range" id="weight" name="weight" min="0" max="100" value="50">
160
  <span id="weightValue">50</span>
161
  </div>
162
  -->
163
- <button type="submit">Process Image</button>
164
- </form>
165
-
166
- <div id="result">
167
- <h3>Result:</h3>
168
- <div id="outputContainer" style="display: none;">
169
- <img id="preview" src="" alt="Processed Image">
170
- <a id="downloadLink" href="#" download>Download Image</a>
171
- </div>
172
- </div>
173
- </div>
174
-
175
- <script>
176
- document.getElementById('uploadForm').addEventListener('submit', function(e) {
177
- e.preventDefault();
178
-
179
- const formData = new FormData();
180
- formData.append('file', document.getElementById('file').files[0]);
181
- formData.append('version', document.getElementById('version').value);
182
- formData.append('scale', document.getElementById('scale').value);
183
- // formData.append('weight', document.getElementById('weight').value); // CodeFormer用
184
-
185
- fetch('/api/restore', {
186
- method: 'POST',
187
- body: formData
188
- })
189
- .then(response => {
190
- if (!response.ok) {
191
- return response.json().then(err => { throw new Error(err.error || 'Unknown error'); });
192
- }
193
- return response.blob();
194
- })
195
- .then(blob => {
196
- const url = URL.createObjectURL(blob);
197
- const preview = document.getElementById('preview');
198
- const downloadLink = document.getElementById('downloadLink');
199
- const outputContainer = document.getElementById('outputContainer');
200
-
201
- preview.src = url;
202
- downloadLink.href = url;
203
- downloadLink.download = 'restored_' + document.getElementById('file').files[0].name;
204
- outputContainer.style.display = 'block';
205
- })
206
- .catch(error => {
207
- alert('Error: ' + error.message);
208
- });
209
- });
210
-
211
- // CodeFormer用のweightパラメータが必要な場合
212
- // document.getElementById('weight').addEventListener('input', function() {
213
- // document.getElementById('weightValue').textContent = this.value;
214
- // });
215
- </script>
216
- </body>
217
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
  """
219
 
220
  if __name__ == '__main__':
 
113
  @app.route('/')
114
  def index():
115
  return """
116
+ <!DOCTYPE html>
117
+ <html>
118
+
119
+ <head>
120
+ <title>Image Upscaling & Restoration API</title>
121
+ <style>
122
+ body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
123
+ .container { border: 1px solid #ddd; padding: 20px; border-radius: 5px; }
124
+ .form-group { margin-bottom: 15px; }
125
+ label { display: block; margin-bottom: 5px; }
126
+ input, select { width: 100%; padding: 8px; box-sizing: border-box; }
127
+ button { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; }
128
+ button:hover { background-color: #45a049; }
129
+ #result { margin-top: 20px; }
130
+ #preview { max-width: 100%; margin-top: 10px; }
131
+ </style>
132
+ </head>
133
+
134
+ <body>
135
+ <h1>Image Upscaling & Restoration API</h1>
136
+ <div class="container">
137
+ <form id="uploadForm" enctype="multipart/form-data">
138
+ <div class="form-group">
139
+ <label for="file">Upload Image:</label>
140
+ <input type="file" id="file" name="file" required>
141
+ </div>
142
+ <div class="form-group">
143
+ <label for="version">Version:</label>
144
+ <select id="version" name="version">
145
+ <option value="v1.2">v1.2</option>
146
+ <option value="v1.3">v1.3</option>
147
+ <option value="v1.4" selected>v1.4</option>
148
+ <option value="RestoreFormer">RestoreFormer</option>
149
+ <option value="CodeFormer">CodeFormer</option>
150
+ <option value="RealESR-General-x4v3">RealESR-General-x4v3</option>
151
+ </select>
152
+ </div>
153
+ <div class="form-group">
154
+ <label for="scale">Rescaling factor:</label>
155
+ <input type="number" id="scale" name="scale" value="2" step="0.1" min="1" max="4" required>
156
+ </div>
157
+ <!--
158
  <div class="form-group">
159
  <label for="weight">Weight (only for CodeFormer):</label>
160
  <input type="range" id="weight" name="weight" min="0" max="100" value="50">
161
  <span id="weightValue">50</span>
162
  </div>
163
  -->
164
+ <button type="submit">Process Image</button>
165
+ </form>
166
+ <div id="result">
167
+ <h3>Result:</h3>
168
+ <div id="outputContainer" style="display: none;">
169
+ <img id="preview" src="" alt="Processed Image">
170
+ <a id="downloadLink" href="#" download>Download Image</a>
171
+ </div>
172
+ </div>
173
+ </div>
174
+ <script>
175
+ document.getElementById('uploadForm').addEventListener('submit', function(e) {
176
+ e.preventDefault();
177
+
178
+ const formData = new FormData();
179
+ formData.append('file', document.getElementById('file').files[0]);
180
+ formData.append('version', document.getElementById('version').value);
181
+ formData.append('scale', document.getElementById('scale').value);
182
+ // formData.append('weight', document.getElementById('weight').value); // CodeFormer用
183
+
184
+ fetch('/api/restore', {
185
+ method: 'POST',
186
+ body: formData
187
+ })
188
+ .then(response => {
189
+ if (!response.ok) {
190
+ return response.json().then(err => { throw new Error(err.error || 'Unknown error'); });
191
+ }
192
+ return response.blob();
193
+ })
194
+ .then(blob => {
195
+ const url = URL.createObjectURL(blob);
196
+ const preview = document.getElementById('preview');
197
+ const downloadLink = document.getElementById('downloadLink');
198
+ const outputContainer = document.getElementById('outputContainer');
199
+
200
+ preview.src = url;
201
+ downloadLink.href = url;
202
+ downloadLink.download = 'restored_' + document.getElementById('file').files[0].name;
203
+ outputContainer.style.display = 'block';
204
+ })
205
+ .catch(error => {
206
+ alert('Error: ' + error.message);
207
+ });
208
+ });
209
+
210
+ </script>
211
+ <div id="apiUsage" style="margin-top: 30px;">
212
+ <h3>API:</h3> <pre><code id="apiCode" style="white-space: pre-wrap; background: #f8f8f8; padding: 10px; border-radius: 5px;"></code></pre>
213
+ </div>
214
+ <script>
215
+ function updateApiUsage() {
216
+ const fileInput = document.getElementById('file');
217
+ const version = document.getElementById('version').value;
218
+ const scale = document.getElementById('scale').value;
219
+ const baseURL = window.location.origin;
220
+
221
+ let codeSnippet = `
222
+ const formData = new FormData();
223
+ formData.append('file', "file");
224
+ formData.append('version', '${version}');
225
+ formData.append('scale', '${scale}');
226
+
227
+ fetch('${baseURL}/api/restore', {
228
+ method: 'POST',
229
+ body: formData
230
+ })
231
+ .then(response => {
232
+ if (!response.ok) {
233
+ return response.json().then(err => { throw new Error(err.error || 'Unknown error'); });
234
+ }
235
+ return response.blob();
236
+ })
237
+ .then(blob => {
238
+ const url = URL.createObjectURL(blob);
239
+
240
+ })
241
+ .catch(error => {
242
+ console.error('Error:', error.message);
243
+ });`.trim();
244
+
245
+ document.getElementById('apiCode').textContent = codeSnippet;
246
+ }
247
+
248
+ document.getElementById('uploadForm').addEventListener('change', updateApiUsage);
249
+ document.getElementById('version').addEventListener('change', updateApiUsage);
250
+ document.getElementById('scale').addEventListener('input', updateApiUsage);
251
+
252
+ updateApiUsage();
253
+ </script>
254
+ </body>
255
+
256
+ </html>
257
  """
258
 
259
  if __name__ == '__main__':