Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line234567891011122 unmodified lines151617181920212223242526272829303132333435363738394041424314 unmodified lines585960616263641 unmodified linefrom transformers import pipelinefrom PIL import Imageimport openaiimport 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_resourcedef load_image_classification_pipeline():2 unmodified linespipe_classification = load_image_classification_pipeline()# Function to generate ingredients using OpenAIdef 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 hereprompt=prompt,max_tokens=50)return response['choices'][0]['text'].strip()# Streamlit appst.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 recognitionst.image("/Users/hassanbutt/Desktop/Screenshot 2024-11-19 at 8.04.00 PM.png",caption="Example of an Image Recognition Model", use_column_width=True)# Select OpenAI modelst.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 imageuploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])14 unmodified lines# Generate and display ingredients for the top predictionst.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.")1 unmodified line2345678910111213142 unmodified lines17181920212223242526272829303132333435363714 unmodified lines525354555657581 unmodified linefrom transformers import pipelinefrom PIL import Imageimport openai# Set your OpenAI API keyopenai.api_key = "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"# OpenAI model to useOPENAI_MODEL = "gpt-4o" # Replace with the model you want to display# Load the image classification pipeline@st.cache_resourcedef load_image_classification_pipeline():2 unmodified linespipe_classification = load_image_classification_pipeline()# Function to generate ingredients using OpenAIdef 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 appst.title("Food Image Recognition with Ingredients")# Display OpenAI model being usedst.sidebar.title("Model Information")st.sidebar.write(f"**OpenAI Model Used**: {OPENAI_MODEL}")# Upload imageuploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])14 unmodified lines# Generate and display ingredients for the top predictionst.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.")
app.py+11-17