soiz1 commited on
Commit
5862d5b
·
verified ·
1 Parent(s): cd8331c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -31
app.py CHANGED
@@ -344,38 +344,30 @@ fetch('${apiUrl}', {
344
 
345
  @app.route('/api/restore', methods=['POST'])
346
  def api_restore():
347
- if 'file' not in request.files:
 
348
  return jsonify({'error': 'No file uploaded'}), 400
349
-
350
- file = request.files['file']
351
- version = request.form.get('version', 'v1.4')
352
- scale = float(request.form.get('scale', 2))
353
- weight = float(request.form.get('weight', 0.5)) if version == 'CodeFormer' else None
354
-
355
- if file.filename == '':
356
- return jsonify({'error': 'No selected file'}), 400
357
-
358
- try:
359
- # Save uploaded file to temp location
360
- temp_dir = tempfile.mkdtemp()
361
- input_path = os.path.join(temp_dir, file.filename)
362
- file.save(input_path)
363
-
364
- # Process image
365
- output_path = process_image(input_path, version, scale, weight)
366
-
367
- # Return the processed image
368
- return send_file(output_path, mimetype='image/jpeg')
369
-
370
- except Exception as e:
371
- return jsonify({'error': str(e)}), 500
372
-
373
- finally:
374
- # Clean up temp files
375
- if 'input_path' in locals() and os.path.exists(input_path):
376
- os.remove(input_path)
377
- if 'temp_dir' in locals() and os.path.exists(temp_dir):
378
- os.rmdir(temp_dir)
379
 
380
  if __name__ == '__main__':
381
  app.run(host='0.0.0.0', port=7860, debug=True)
 
344
 
345
  @app.route('/api/restore', methods=['POST'])
346
  def api_restore():
347
+ data = request.get_json()
348
+ if not data or 'file' not in data:
349
  return jsonify({'error': 'No file uploaded'}), 400
350
+
351
+ import base64
352
+ from io import BytesIO
353
+ from PIL import Image
354
+
355
+ # Base64データをデコード
356
+ file_data = data['file'].split(",")[1]
357
+ img_bytes = base64.b64decode(file_data)
358
+ temp_dir = tempfile.mkdtemp()
359
+ input_path = os.path.join(temp_dir, "input.jpg")
360
+ with open(input_path, "wb") as f:
361
+ f.write(img_bytes)
362
+
363
+ version = data.get('version', 'v1.4')
364
+ scale = float(data.get('scale', 2))
365
+ weight = float(data.get('weight', 0.5)) if version == 'CodeFormer' else None
366
+
367
+ # 画像処理
368
+ output_path = process_image(input_path, version, scale, weight)
369
+ return send_file(output_path, mimetype="image/jpeg")
370
+
 
 
 
 
 
 
 
 
 
371
 
372
  if __name__ == '__main__':
373
  app.run(host='0.0.0.0', port=7860, debug=True)