bhd82 commited on
Commit
fa2a0fa
·
verified ·
1 Parent(s): ae09a0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -45
app.py CHANGED
@@ -17,54 +17,54 @@ 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)
 
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)