Spaces:
Runtime error
Runtime error
| # app.py | |
| import streamlit as st | |
| from PIL import Image | |
| import tensorflow as tf | |
| import numpy as np | |
| from tensorflow.keras.applications.efficientnet import preprocess_input | |
| import json | |
| from io import BytesIO | |
| import google.generativeai as genai | |
| from dotenv import load_dotenv | |
| import os | |
| # Load environment variables | |
| load_dotenv() | |
| GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") | |
| genai.configure(api_key=GOOGLE_API_KEY) | |
| # Load model | |
| IMG_SIZE = (299, 299) | |
| model = tf.keras.models.load_model("model.h5") | |
| with open("trained_classes.json", "r") as f: | |
| class_labels = json.load(f) | |
| # Streamlit UI | |
| st.title("NutriSync: Food Nutrition Analyzer") | |
| uploaded_file = st.file_uploader("Upload food image", type=["jpg","jpeg","png"]) | |
| def predict_food(img): | |
| img = img.resize(IMG_SIZE) | |
| img_array = np.array(img) | |
| if len(img_array.shape) == 2: | |
| img_array = np.stack((img_array,) * 3, axis=-1) | |
| if img_array.shape[-1] == 4: | |
| img_array = img_array[..., :3] | |
| img_array = preprocess_input(img_array) | |
| img_array = np.expand_dims(img_array, axis=0) | |
| preds = model.predict(img_array) | |
| idx = np.argmax(preds[0]) | |
| confidence = float(preds[0][idx]) | |
| food = class_labels[idx] if idx < len(class_labels) else "unknown" | |
| return food, confidence | |
| def get_gemini_response(food_item, img_bytes, prompt): | |
| try: | |
| model_gemini = genai.GenerativeModel("gemini-1.5-flash-latest") | |
| response = model_gemini.generate_content([food_item, {"mime_type":"image/jpeg","data":img_bytes}, prompt]) | |
| return response.text | |
| except Exception as e: | |
| return f"Error retrieving nutritional info: {e}" | |
| if uploaded_file is not None: | |
| img = Image.open(uploaded_file).convert("RGB") | |
| st.image(img, caption="Uploaded Image", use_column_width=True) | |
| food, confidence = predict_food(img) | |
| st.write(f"Predicted Food: {food}, Confidence: {confidence:.2f}") | |
| # Reset file pointer for Gemini | |
| uploaded_file.seek(0) | |
| img_bytes = uploaded_file.read() | |
| prompt = ( | |
| f"Analyze the nutritional content of {food}. " | |
| "Provide estimated values for Calories, Protein (g), Carbs (g), Fats (g), Fiber (g), Sugars (g) " | |
| "with short dietary recommendations." | |
| if food != "unknown" and confidence >= 0.6 | |
| else "Analyze the nutritional content of food. Provide estimated values only." | |
| ) | |
| nutrition_info = get_gemini_response(food, img_bytes, prompt) | |
| st.write("Nutritional Info:") | |
| st.write(nutrition_info) | |