Spaces:
Sleeping
Sleeping
Update app.py
Browse files12234457896789105 unmodified lines16171819202122232425262728293021223132332434353637282930313238394041426 unmodified lines495051455253545514 unmodified lines7071726673747576import streamlit as stfrom transformers import pipelinefrom transformers import pipeline, AutoTokenizer, AutoModelForCausalLMfrom 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"])# 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 lines
pipe_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 Google Geminidef get_ingredients_google(food_name):# Function to generate ingredients using the Llama modeldef get_ingredients_llama(food_name): """ Generate a list of ingredients for the given food item using Google Gemini AI. 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 = palm.generate_text(prompt=prompt) if response and "candidates" in response: return response["candidates"][0]["output"] else: return "No ingredients found." 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**: Google Gemini")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 prediction st.subheader("Ingredients") try: ingredients = get_ingredients_google(top_food) ingredients = get_ingredients_llama(top_food) st.write(ingredients) except Exception as e: st.error(f"Error generating ingredients: {e}")app.py+22-15