import streamlit as st from transformers import pipeline from PIL import Image import openai # Set your OpenAI API key openai.api_key = "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" # OpenAI model to use OPENAI_MODEL = "gpt-4o" # Replace with the model you want to display # Load the image classification pipeline @st.cache_resource def load_image_classification_pipeline(): return pipeline("image-classification", model="Shresthadev403/food-image-classification") pipe_classification = load_image_classification_pipeline() # Function to generate ingredients using OpenAI def get_ingredients_openai(food_name): prompt = f"List the main ingredients typically used to prepare {food_name}:" response = openai.Completion.create( engine=OPENAI_MODEL, prompt=prompt, max_tokens=50 ) return response['choices'][0]['text'].strip() # Streamlit app st.title("Food Image Recognition with Ingredients") # Display OpenAI model being used st.sidebar.title("Model Information") st.sidebar.write(f"**OpenAI Model Used**: {OPENAI_MODEL}") # Upload image uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"]) if uploaded_file is not None: # Display the uploaded image image = Image.open(uploaded_file) st.image(image, caption="Uploaded Image", use_column_width=True) st.write("Classifying...") # Make predictions predictions = pipe_classification(image) # Display only the top prediction top_food = predictions[0]['label'] st.header(f"Food: {top_food}") # Generate and display ingredients for the top prediction st.subheader("Ingredients") try: ingredients = get_ingredients_openai(top_food) st.write(ingredients) except Exception as e: st.write("Could not generate ingredients. Please try again later.")