Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011125 unmodified lines1819202122232425262728293031323334356 unmodified lines4243444546474814 unmodified lines63646566676869import streamlit as stfrom transformers import pipelinefrom PIL import Imageimport google.generativeai as palmimport os# Set up the Google API Key (add this as a secret in Hugging Face Spaces)os.environ["GOOGLE_API_KEY"] = st.secrets["GOOGLE_API_KEY"]palm.configure(api_key=os.environ["GOOGLE_API_KEY"])# Load the image classification pipeline@st.cache_resource5 unmodified linespipe_classification = load_image_classification_pipeline()# Function to generate ingredients using Google Geminidef get_ingredients_google(food_name):"""Generate a list of ingredients for the given food item using Google Gemini AI."""prompt = f"List the main ingredients typically used to prepare {food_name}."try:response = palm.generate_text(prompt=prompt)if response and "candidates" in response:return response["candidates"][0]["output"]else:return "No ingredients found."except Exception as e:return f"Error generating ingredients: {e}"6 unmodified lines# Sidebar for model informationst.sidebar.title("Model Information")st.sidebar.write("**Image Classification Model**: Shresthadev403/food-image-classification")st.sidebar.write("**LLM for Ingredients**: Google Gemini")# 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_google(top_food)st.write(ingredients)except Exception as e:st.error(f"Error generating ingredients: {e}")123456789105 unmodified lines1617181920212223242526272829303132333435363738394041426 unmodified lines4950515253545514 unmodified lines70717273747576import streamlit as stfrom transformers import pipeline, AutoTokenizer, AutoModelForCausalLMfrom PIL import Imageimport os# Hugging Face token login (add this as a secret in Hugging Face Spaces)os.environ["HF_TOKEN"] = st.secrets["HF_TOKEN"]# Load the image classification pipeline@st.cache_resource5 unmodified linespipe_classification = load_image_classification_pipeline()# Load the Llama model for ingredient generation@st.cache_resourcedef load_llama_pipeline():"""Load the Llama model for ingredient generation."""tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-3B-Instruct", use_auth_token=os.environ["HF_TOKEN"])model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-3B-Instruct", use_auth_token=os.environ["HF_TOKEN"])return pipeline("text-generation", model=model, tokenizer=tokenizer)pipe_llama = load_llama_pipeline()# Function to generate ingredients using the Llama modeldef get_ingredients_llama(food_name):"""Generate a list of ingredients for the given food item using the Llama model."""prompt = f"List the main ingredients typically used to prepare {food_name}."try:response = pipe_llama(prompt, max_length=50, num_return_sequences=1)return response[0]["generated_text"].strip()except Exception as e:return f"Error generating ingredients: {e}"6 unmodified lines# Sidebar for model informationst.sidebar.title("Model Information")st.sidebar.write("**Image Classification Model**: Shresthadev403/food-image-classification")st.sidebar.write("**LLM for Ingredients**: meta-llama/Llama-3.2-3B-Instruct")# 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_llama(top_food)st.write(ingredients)except Exception as e:st.error(f"Error generating ingredients: {e}")
app.py+22-15