binaychandra commited on
Commit
1f21b17
·
1 Parent(s): 211f16d

try resolving path issues

Browse files
Files changed (2) hide show
  1. app.py +43 -20
  2. templates/index.html +28 -3
app.py CHANGED
@@ -94,31 +94,54 @@ def index():
94
 
95
  @app.route('/process', methods=['POST'])
96
  def process():
97
- image_data = None
98
- if 'image' in request.files:
99
- image = request.files['image'].read()
100
- image_data = image
101
- elif 'image_url' in request.form:
102
- image_url = request.form['image_url']
103
- if image_url.startswith('/static/'):
104
- image_path = os.path.join(app.static_folder, image_url[len('/static/'):])
105
- if os.path.exists(image_path):
106
- with open(image_path, 'rb') as f:
107
- image_data = f.read()
108
- else:
109
- return jsonify({'error': f'Image file not found at {image_path}'}), 404
110
-
111
- if not image_data:
112
- return jsonify({'error': 'No image provided'}), 400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
- r_value = int(request.form.get('r_value', 1))
 
115
 
116
- try:
117
  result = process_image(image_data, r_value)
118
- print(result['processed_image'])
119
  return jsonify(result)
 
120
  except Exception as e:
121
- return jsonify({'error': str(e)}), 400
 
 
 
 
 
 
122
 
123
  if __name__ == '__main__':
124
  app.run(debug=True)
 
94
 
95
  @app.route('/process', methods=['POST'])
96
  def process():
97
+ try:
98
+ image_data = None
99
+ if 'image' in request.files:
100
+ image = request.files['image'].read()
101
+ image_data = image
102
+ elif 'image_url' in request.form:
103
+ image_url = request.form['image_url']
104
+ if image_url.startswith('/static/'):
105
+ # Get relative path within static folder
106
+ rel_path = image_url[len('/static/'):]
107
+ image_path = os.path.join(app.static_folder, rel_path)
108
+
109
+ # Debug info
110
+ app.logger.info(f"Trying to access image at: {image_path}")
111
+ app.logger.info(f"File exists: {os.path.exists(image_path)}")
112
+
113
+ if os.path.exists(image_path):
114
+ with open(image_path, 'rb') as f:
115
+ image_data = f.read()
116
+ else:
117
+ # Try alternative path resolution
118
+ alt_path = os.path.join(os.getcwd(), 'static', rel_path)
119
+ app.logger.info(f"Trying alternative path: {alt_path}")
120
+ app.logger.info(f"File exists: {os.path.exists(alt_path)}")
121
+
122
+ if os.path.exists(alt_path):
123
+ with open(alt_path, 'rb') as f:
124
+ image_data = f.read()
125
+ else:
126
+ return jsonify({
127
+ 'error': f'Image file not found. Tried paths: {image_path}, {alt_path}'
128
+ }), 404
129
 
130
+ if not image_data:
131
+ return jsonify({'error': 'No image provided'}), 400
132
 
133
+ r_value = int(request.form.get('r_value', 1))
134
  result = process_image(image_data, r_value)
 
135
  return jsonify(result)
136
+
137
  except Exception as e:
138
+ import traceback
139
+ app.logger.error(f"Error processing image: {str(e)}")
140
+ app.logger.error(traceback.format_exc())
141
+ return jsonify({
142
+ 'error': f'Error processing image: {str(e)}',
143
+ 'traceback': traceback.format_exc()
144
+ }), 500
145
 
146
  if __name__ == '__main__':
147
  app.run(debug=True)
templates/index.html CHANGED
@@ -293,14 +293,38 @@
293
  formData.append('r_value', rValue.value);
294
  try {
295
  const response = await fetch('/process', {method: 'POST', body: formData});
296
- const data = await response.json();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
  if (data.error) {
298
- alert('Error: ' + data.error);
299
- return;
 
300
  }
 
 
301
  processedImage.src = 'data:image/png;base64,' + data.processed_image;
302
  maxR = data.max_r;
303
  rValue.max = maxR;
 
 
304
  document.getElementById('originalStorage').innerHTML = `
305
  <div class="storage-row">
306
  <span>Storage (bytes):</span>
@@ -328,6 +352,7 @@
328
  document.getElementById('processedInfo').textContent =
329
  `Using ${rValue.value} of ${maxR} singular values`;
330
  } catch (error) {
 
331
  alert('Error processing image: ' + error.message);
332
  } finally {
333
  loading.style.display = 'none';
 
293
  formData.append('r_value', rValue.value);
294
  try {
295
  const response = await fetch('/process', {method: 'POST', body: formData});
296
+
297
+ // Check if response is ok (status 200-299)
298
+ if (!response.ok) {
299
+ const text = await response.text();
300
+ console.error('Server response error:', response.status, text);
301
+ throw new Error(`Server error: ${response.status}. Check console for details.`);
302
+ }
303
+
304
+ // Try parsing the response as JSON
305
+ let data;
306
+ try {
307
+ data = await response.json();
308
+ } catch (parseError) {
309
+ console.error('JSON parse error:', parseError);
310
+ const text = await response.text();
311
+ console.error('Raw response:', text);
312
+ throw new Error('Failed to parse server response as JSON');
313
+ }
314
+
315
+ // Check for error in JSON response
316
  if (data.error) {
317
+ console.error('API error:', data.error);
318
+ if (data.traceback) console.error('Traceback:', data.traceback);
319
+ throw new Error(data.error);
320
  }
321
+
322
+ // Process successful response
323
  processedImage.src = 'data:image/png;base64,' + data.processed_image;
324
  maxR = data.max_r;
325
  rValue.max = maxR;
326
+
327
+ // Update storage information
328
  document.getElementById('originalStorage').innerHTML = `
329
  <div class="storage-row">
330
  <span>Storage (bytes):</span>
 
352
  document.getElementById('processedInfo').textContent =
353
  `Using ${rValue.value} of ${maxR} singular values`;
354
  } catch (error) {
355
+ console.error('Error details:', error);
356
  alert('Error processing image: ' + error.message);
357
  } finally {
358
  loading.style.display = 'none';