AADalhat commited on
Commit
256a688
Β·
verified Β·
1 Parent(s): a6b9724

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -55
app.py CHANGED
@@ -82,61 +82,41 @@ def generate_pdf(info, advice, risk_score, risk_level, flags, conditions, lang,
82
  pdf.output(path)
83
  return path
84
 
85
- # Main function
86
- def classify_food(image, conditions, language):
87
- if not conditions:
88
- return "⚠️ Please select at least one health condition.", None, None, None
89
-
90
- # Preprocess image
91
- image = image.convert("RGB").resize((225, 225))
92
- img = tf.keras.preprocessing.image.img_to_array(image)
93
- img = efficientnet_preprocess(img)
94
- img = np.expand_dims(img, axis=0)
95
-
96
- # Predict
97
- preds = model.predict(img)[0]
98
- idx = np.argmax(preds)
99
- predicted_class = class_names[idx]
100
- confidence = round(float(preds[idx]) * 100, 2)
101
-
102
- info = food_info[predicted_class]
103
- display_name = info.get("display_name", predicted_class.replace("_", " ").title())
104
-
105
- # Advice logic
106
- advice, risk_score, flags = generate_advice(info, conditions)
107
- risk_level = get_risk_level(risk_score)
108
-
109
- # Save audio
110
- audio_path = os.path.join(tempfile.gettempdir(), "tts.mp3")
111
- tts = gTTS(text=advice, lang=LANG_CODE.get(language, "en"))
112
- tts.save(audio_path)
113
-
114
- # Save image
115
- image_path = os.path.join(tempfile.gettempdir(), "upload.jpg")
116
- image.save(image_path)
117
-
118
- # Generate PDF
119
- pdf_path = generate_pdf(info, advice, risk_score, risk_level, flags, conditions, language, display_name, image_path)
120
-
121
- # Output summary
122
- result = (
123
- f"🍽️ Food: {display_name}\n"
124
- f"🌍 Ethnicity: {info['ethnicity']}\n"
125
- f"πŸ₯¦ Ingredients: {info['ingredients']}\n"
126
- f"πŸ”₯ Calories: {info['calories']} kcal\n"
127
- f"🍞 Carbs: {info['carbs']}g\n"
128
- f"πŸ₯© Protein: {info['protein']}g\n"
129
- f"🧈 Fat: {info['fat']}g\n"
130
- f"🌱 Diet Type: {info['diet_type']}\n"
131
- f"πŸ” Substitute: {info.get('substitute', 'None')}\n\n"
132
- f"πŸ“ˆ Confidence: {confidence}%\n"
133
- f"πŸ“Š Risk Score: {risk_score}% ({risk_level})\n"
134
- f"⚠️ Risk Factors: {', '.join(flags) if flags else 'None'}\n"
135
- f"βœ… Advice: {advice}\n\n"
136
- f"πŸ“ *Generated by HoodHealth Pro+.*"
137
- )
138
-
139
- return result, audio_path, f"πŸ—£οΈ Language: {language}", pdf_path
140
 
141
  # Gradio interface
142
  interface = gr.Interface(
 
82
  pdf.output(path)
83
  return path
84
 
85
+ def classify_food(image):
86
+ try:
87
+ # Convert to grayscale since model expects 1 channel
88
+ image = image.convert("L").resize((225, 225)) # "L" = grayscale
89
+ img = tf.keras.preprocessing.image.img_to_array(image)
90
+
91
+ # Ensure shape is (225, 225, 1)
92
+ if img.shape[-1] != 1:
93
+ img = np.expand_dims(img, axis=-1)
94
+
95
+ # Preprocess for EfficientNet
96
+ img = efficientnet_preprocess(img)
97
+ img = np.expand_dims(img, axis=0)
98
+
99
+ # Predict
100
+ preds = model.predict(img)
101
+ pred_class = np.argmax(preds[0])
102
+ confidence = float(np.max(preds[0]))
103
+
104
+ food_name = class_names[pred_class]
105
+ food_details = food_info[food_name]
106
+
107
+ result = f"🍽️ Food: {food_name}\n"
108
+ result += f"🌍 Ethnicity: {food_details['Ethnicity']}\n"
109
+ result += f"πŸ₯¦ Ingredients: {', '.join(food_details['Ingredients'])}\n"
110
+ result += f"πŸ”₯ Nutrients: {food_details['Nutrients']}\n"
111
+ result += f"❀️ Health Advice: {food_details['Health_Advice']}\n"
112
+ result += f"🌱 Diet Type: {food_details['Diet_Type']}\n"
113
+ result += f"πŸ“Š Confidence: {confidence:.2f}"
114
+
115
+ return result
116
+
117
+ except Exception as e:
118
+ return f"Error during classification: {str(e)}"
119
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  # Gradio interface
122
  interface = gr.Interface(