CTP_Project / app.py
HassanDataSci's picture
Update app.py
94c304e verified
raw
history blame
2.39 kB
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.")