piyush3 commited on
Commit
94327e5
·
verified ·
1 Parent(s): 39d1490

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -44
app.py CHANGED
@@ -4,60 +4,67 @@ import os
4
  import tempfile
5
  import base64
6
  import json
 
7
 
8
  app = Flask(__name__)
9
 
 
 
 
10
  # Load OSMF model
11
- osmf = YOLO('best.pt', task="classify")
 
12
 
13
  @app.route('/')
14
  def home():
15
- return render_template('index.html')
16
 
17
  @app.route('/predict', methods=['POST'])
18
  def predict():
19
- if 'image' not in request.files:
20
- return jsonify({'error': 'No image provided'})
21
-
22
- file = request.files['image']
23
-
24
- try:
25
- # Create temporary file
26
- with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
27
- file.save(temp_file.name)
28
- temp_path = temp_file.name
29
-
30
- # Process image
31
- predict = osmf(temp_path)
32
- results = predict[0].to_json() # Using the new recommended method
33
- predict_dict = json.loads(results)
34
-
35
- name = predict_dict[0]["name"]
36
- confidence = float(predict_dict[0]["confidence"]) * 100
37
-
38
- # Close file handle and remove
39
- try:
40
- os.close(temp_file.fileno())
41
- except:
42
- pass
43
-
44
- try:
45
- os.unlink(temp_path)
46
- except:
47
- pass
48
-
49
- return jsonify({
50
- 'class': name,
51
- 'confidence': f"{confidence:.2f}%",
52
- 'status': 'success'
53
- })
54
-
55
- except Exception as e:
56
- return jsonify({
57
- 'error': str(e),
58
- 'status': 'error'
59
- }), 500
60
 
 
 
 
 
 
61
 
62
  if __name__ == '__main__':
63
- app.run(debug=True)
 
 
4
  import tempfile
5
  import base64
6
  import json
7
+ from werkzeug.utils import secure_filename
8
 
9
  app = Flask(__name__)
10
 
11
+ # Configure maximum content length for file uploads (10MB)
12
+ app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024
13
+
14
  # Load OSMF model
15
+ model_path = os.getenv('MODEL_PATH', 'best.pt')
16
+ osmf = YOLO(model_path, task="classify")
17
 
18
  @app.route('/')
19
  def home():
20
+ return render_template('index.html')
21
 
22
  @app.route('/predict', methods=['POST'])
23
  def predict():
24
+ if 'image' not in request.files:
25
+ return jsonify({'error': 'No image provided', 'status': 'error'}), 400
26
+
27
+ file = request.files['image']
28
+ if not file.filename:
29
+ return jsonify({'error': 'Empty file provided', 'status': 'error'}), 400
30
+
31
+ try:
32
+ # Secure the filename and create temporary file
33
+ filename = secure_filename(file.filename)
34
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
35
+ file.save(temp_file.name)
36
+ temp_path = temp_file.name
37
+
38
+ # Process image
39
+ predict = osmf(temp_path)
40
+ results = predict[0].to_json()
41
+ predict_dict = json.loads(results)
42
+
43
+ # Extract prediction results
44
+ name = predict_dict[0]["name"]
45
+ confidence = float(predict_dict[0]["confidence"]) * 100
46
+
47
+ except Exception as e:
48
+ return jsonify({
49
+ 'error': str(e),
50
+ 'status': 'error'
51
+ }), 500
52
+
53
+ finally:
54
+ # Cleanup temporary files
55
+ try:
56
+ if 'temp_file' in locals():
57
+ os.close(temp_file.fileno())
58
+ os.unlink(temp_path)
59
+ except Exception:
60
+ pass
 
 
 
 
61
 
62
+ return jsonify({
63
+ 'class': name,
64
+ 'confidence': f"{confidence:.2f}%",
65
+ 'status': 'success'
66
+ })
67
 
68
  if __name__ == '__main__':
69
+ port = int(os.environ.get('PORT', 7860))
70
+ app.run(host='0.0.0.0', port=port)