lp128396 commited on
Commit
0c10af9
·
verified ·
1 Parent(s): 1e2c914

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -12
app.py CHANGED
@@ -54,18 +54,44 @@ def predict():
54
 
55
  result = {}
56
 
 
 
 
 
57
  if top1_conf >= 80:
58
- result['message'] = 'Fresh' if 'Fresh' in top1_label else 'Rotten'
59
- elif ('Fresh' in top1_label and 'Rotten' in top2_label) or ('Rotten' in top1_label and 'Fresh' in top2_label):
60
- result['message'] = f"{top1_label} ({top1_conf:.1f}%) vs {top2_label} ({top2_conf:.1f}%)"
61
- elif 'Rotten' in top1_label:
62
- result['message'] = '⚠️ Do not eat this fruit.'
63
- elif 'Fresh' in top1_label and top1_conf < 80:
64
- result['message'] = '🍃 Seems fresh, but be cautious.'
65
 
66
- result['confidence'] = round(top1_conf, 2)
67
- result['fruitType'] = top1_label
68
- result['fresh'] = 'Fresh' in top1_label
69
- return jsonify(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
-
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  result = {}
56
 
57
+ top1_is_fresh = 'Fresh' in top1_label
58
+ top2_is_fresh = 'Fresh' in top2_label
59
+
60
+ # Rule 2: top1 confidence ≥ 80%
61
  if top1_conf >= 80:
62
+ result['message'] = ' Looks Fresh!' if top1_is_fresh else '⚠️ Rotten Fruit Detected!'
63
+ result['prediction'] = 'Fresh' if top1_is_fresh else 'Rotten'
64
+ result['confidence'] = round(top1_conf, 2)
 
 
 
 
65
 
66
+ else:
67
+ # Rule 3: top1 and top2 are of different categories
68
+ if top1_is_fresh != top2_is_fresh:
69
+ result['message'] = f"{'Fresh' if top1_is_fresh else 'Rotten'} ({top1_conf:.1f}%) vs {'Fresh' if top2_is_fresh else 'Rotten'} ({top2_conf:.1f}%)"
70
+ result['prediction'] = f"{'Fresh' if top1_is_fresh else 'Rotten'} vs {'Fresh' if top2_is_fresh else 'Rotten'}"
71
+ result['confidence'] = {
72
+ 'Fresh': round(top1_conf, 2) if top1_is_fresh else round(top2_conf, 2),
73
+ 'Rotten': round(top1_conf, 2) if not top1_is_fresh else round(top2_conf, 2)
74
+ }
75
+ # Rule 5 message:
76
+ if not top1_is_fresh and top1_conf > top2_conf:
77
+ result['advice'] = '⚠️ Do not eat this fruit.'
78
+ elif top1_is_fresh and top1_conf > top2_conf:
79
+ result['advice'] = '🍃 Seems fresh, but be cautious.'
80
+ elif top2_is_fresh and top2_conf > top1_conf:
81
+ result['advice'] = '🍃 Seems fresh, but be cautious.'
82
+ else:
83
+ result['advice'] = '⚠️ Do not eat this fruit.'
84
 
85
+ # Rule 4: both predictions from same category (Fresh & Fresh OR Rotten & Rotten)
86
+ else:
87
+ category = 'Fresh' if top1_is_fresh else 'Rotten'
88
+ result['message'] = f"{category} ({top1_conf:.1f}%)"
89
+ result['prediction'] = category
90
+ result['confidence'] = round(top1_conf, 2)
91
+ # Rule 5 message:
92
+ if category == 'Rotten':
93
+ result['advice'] = '⚠️ Do not eat this fruit.'
94
+ else:
95
+ result['advice'] = '🍃 Seems fresh, but be cautious.'
96
+
97
+ return jsonify(result)