import gradio as gr from PIL import Image import io import numpy as np import tensorflow as tf from tensorflow.keras.applications.resnet50 import ResNet50 from tensorflow.keras.preprocessing.image import img_to_array from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions # Load the ResNet50 model model = ResNet50(weights='imagenet') def process_image(image): try: if image is None: return "No image provided." # Convert Gradio image to a PIL Image img = Image.fromarray(image) # Resize the image img = img.resize((224, 224)) # ResNet50 input size # Convert to numpy array x = img_to_array(img) # Expand dimensions x = np.expand_dims(x, axis=0) # Normalize input x = preprocess_input(x) return x except Exception as e: error_message = f"Error processing image: {str(e)}" print(error_message) # Print full error message for debugging return error_message def extract_carbs_from_response(response): return 30.0 # Placeholder for carb estimate def calculate_insulin_dose(current_insulin_level, carb_estimate): return current_insulin_level + (carb_estimate * 0.5) def main(): def insulin_dosage_predict(current_insulin_level, food_image): try: if current_insulin_level <= 0: return "Insulin level must be greater than zero." processed_image = process_image(food_image) if isinstance(processed_image, str): return processed_image # Get predictions (you can implement actual inference with the model) preds = model.predict(processed_image) decoded_preds = decode_predictions(preds, top=1)[0][0] food_item = decoded_preds[1] response = f"Predicted food item: {food_item}" carb_estimate = extract_carbs_from_response(response) insulin_dose = calculate_insulin_dose(current_insulin_level, carb_estimate) return f""" Nutritional Information: {response} Insulin Dose Recommendation: Current insulin level: {current_insulin_level} units Estimated carbs intake: {carb_estimate:.2f} grams Recommended insulin dose: {insulin_dose:.2f} units """ except Exception as e: return f"An error occurred: {str(e)}" iface = gr.Interface( fn=insulin_dosage_predict, inputs=[ gr.Number(label="Current Insulin Level"), gr.Image(label="Food Image"), ], outputs=gr.Textbox(), title="Insulin Dosage Calculator", description="Enter your current insulin level and upload an image of your food." ) iface.launch() if __name__ == "__main__": main()