File size: 2,385 Bytes
04f475a
94c304e
04f475a
94c304e
4583379
04f475a
94c304e
 
 
f825898
04f475a
f825898
04f475a
 
f825898
 
94c304e
 
f825898
94c304e
 
 
 
 
 
f825898
 
94c304e
f825898
04f475a
94c304e
 
 
 
 
 
 
 
 
 
 
04f475a
 
 
 
 
 
 
 
 
 
f825898
04f475a
f825898
 
94c304e
04f475a
f825898
 
 
94c304e
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
59
60
61
62
63
64
import streamlit as st
from transformers import pipeline
from PIL import Image
import openai
import os

# Set your OpenAI API key (replace YOUR_OPENAI_API_KEY with your key)
openai.api_key = "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"

# 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, model="text-davinci-003"):
    prompt = f"List the main ingredients typically used to prepare {food_name}:"
    response = openai.Completion.create(
        engine=model,  # Specify the model here
        prompt=prompt,
        max_tokens=50
    )
    return response['choices'][0]['text'].strip()

# Streamlit app
st.title("Food Image Recognition Model")
st.write("Upload an image to classify the type of food and get its ingredients!")

# Display a sample image showing the concept of image recognition
st.image("https://upload.wikimedia.org/wikipedia/commons/6/69/Classification_example_image.png", 
         caption="Example of an Image Recognition Model", use_column_width=True)

# Select OpenAI model
st.sidebar.title("Choose a Model")
model_choice = st.sidebar.selectbox(
    "Select an OpenAI Model:",
    ["text-davinci-003", "gpt-3.5-turbo", "gpt-4", "curie"]
)

# 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, model=model_choice)
        st.write(ingredients)
    except Exception as e:
        st.write("Could not generate ingredients. Please try again later.")