File size: 1,986 Bytes
04f475a
94c304e
04f475a
94c304e
04f475a
ff3533c
94c304e
 
ff3533c
 
 
f825898
04f475a
f825898
04f475a
 
f825898
 
94c304e
ff3533c
f825898
94c304e
ff3533c
94c304e
 
 
 
f825898
 
ff3533c
94c304e
ff3533c
 
 
94c304e
04f475a
 
 
 
 
 
 
 
 
 
f825898
04f475a
f825898
 
94c304e
04f475a
f825898
 
 
ff3533c
f825898
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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.")