Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891 unmodified line1112131415161718192021222324252627282930313233343536373839404142438 unmodified lines5253545556575859606162636465import streamlit as stfrom transformers import pipeline, AutoTokenizer, AutoModelForCausalLMfrom PIL import Imageimport requestsimport os# Load the image classification pipeline@st.cache_resourcedef load_image_classification_pipeline():1 unmodified linepipe_classification = load_image_classification_pipeline()# Load the Meta-Llama model and tokenizer for text generation@st.cache_resourcedef load_llama_pipeline():# Retrieve Hugging Face token from environment variablestoken = os.getenv("HF_AUTH_TOKEN")tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-3B-Instruct",use_auth_token=token)model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-3B-Instruct",use_auth_token=token)return pipeline("text-generation", model=model, tokenizer=tokenizer)pipe_llama = load_llama_pipeline()# Function to generate ingredients using Meta-Llamadef get_ingredients(food_name):prompt = f"List the main ingredients typically used to prepare {food_name}:"response = pipe_llama(prompt, max_length=50, num_return_sequences=1)return response[0]['generated_text']# Streamlit appst.title("Food Image Classification with Ingredients Generation")st.write("Upload an image to classify the type of food and get its ingredients!")# Upload imageuploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])8 unmodified lines# Display only the top predictiontop_food = predictions[0]['label']confidence = predictions[0]['score']st.subheader("Top Prediction")st.write(f"**{top_food}** with confidence {confidence:.2f}")# Generate and display ingredients for the top predictionst.subheader("Ingredients")try:ingredients = get_ingredients(top_food)st.write(ingredients)except Exception as e:st.write("Could not generate ingredients. Please try again later.")1234567891011121 unmodified line141516171819202122232425262728293031323334353637383940414243448 unmodified lines535455565758596061626364import streamlit as stfrom 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():1 unmodified linepipe_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("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 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"])8 unmodified lines# Display only the top predictiontop_food = predictions[0]['label']st.header(f"Food: {top_food}")# 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.")
app.py+27-28