import os from PIL import Image from transformers import ViTFeatureExtractor, ViTForImageClassification import warnings import requests import gradio as gr import logging warnings.filterwarnings('ignore') # Configure logging logging.basicConfig(level=logging.INFO) # Load the pre-trained Vision Transformer model and feature extractor model_name = "google/vit-base-patch16-224" feature_extractor = ViTFeatureExtractor.from_pretrained(model_name) model = ViTForImageClassification.from_pretrained(model_name) # Load the API key from environment variables api_key = os.getenv('NUTRITION_API_KEY') if not api_key: logging.error("API key for nutrition information is not set.") raise ValueError("API key for nutrition information is not set. Please set the NUTRITION_API_KEY environment variable.") def identify_image(image_path): """Identify the food item in the image.""" try: image = Image.open(image_path) except Exception as e: logging.error(f"Failed to open image: {e}") return "Invalid image" inputs = feature_extractor(images=image, return_tensors="pt") outputs = model(**inputs) logits = outputs.logits predicted_class_idx = logits.argmax(-1).item() predicted_label = model.config.id2label[predicted_class_idx] food_name = predicted_label.split(',')[0] logging.info(f"Identified food: {food_name}") return food_name def get_calories(food_name): """Get the calorie information of the identified food item.""" api_url = f'https://api.api-ninjas.com/v1/nutrition?query={food_name}' try: response = requests.get(api_url, headers={'X-Api-Key': api_key}) response.raise_for_status() nutrition_info = response.json() except requests.RequestException as e: logging.error(f"API request failed: {e}") nutrition_info = {"Error": response.status_code, "Message": str(e)} return nutrition_info def format_nutrition_info(nutrition_info): """Format the nutritional information into an HTML table.""" if "Error" in nutrition_info: return f"Error: {nutrition_info['Error']} - {nutrition_info['Message']}" if not nutrition_info: return "No nutritional information found." nutrition_data = nutrition_info[0] table = f"""
Nutrition Facts
Food Name: {nutrition_data['name']}
Calories{nutrition_data['calories']} Serving Size (g){nutrition_data['serving_size_g']}
Total Fat (g){nutrition_data['fat_total_g']} Saturated Fat (g){nutrition_data['fat_saturated_g']}
Protein (g){nutrition_data['protein_g']} Sodium (mg){nutrition_data['sodium_mg']}
Potassium (mg){nutrition_data['potassium_mg']} Cholesterol (mg){nutrition_data['cholesterol_mg']}
Total Carbohydrates (g){nutrition_data['carbohydrates_total_g']} Fiber (g){nutrition_data['fiber_g']}
Sugar (g){nutrition_data['sugar_g']}
""" return table def main_process(image_path): """Identify the food item and fetch its calorie information.""" food_name = identify_image(image_path) if food_name == "Invalid image": return food_name nutrition_info = get_calories(food_name) formatted_nutrition_info = format_nutrition_info(nutrition_info) return formatted_nutrition_info # Define the Gradio interface def gradio_interface(image): formatted_nutrition_info = main_process(image) return formatted_nutrition_info # Create the Gradio UI iface = gr.Interface( fn=gradio_interface, inputs=gr.Image(type="filepath"), outputs="html", title="Food Identification and Nutrition Info", description="Upload an image of food to get nutritional information.", allow_flagging="never" # Disable flagging ) # Launch the Gradio app if __name__ == "__main__": iface.launch()