Spaces:
Sleeping
Sleeping
Update app.py
Browse files122345678910111213141516175 unmodified lines2324251516171819202122232425262627282829303131323332333435353637383938394041424344444546474849505149505110 unmodified lines626364686566677168697071import streamlit as stfrom transformers import pipeline, AutoTokenizer, AutoModelForCausalLMfrom transformers import pipelinefrom PIL import Imagefrom langchain.chat_models import ChatGoogleGenerativeAIimport 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"]
# Initialize Google Gemini modelllm = ChatGoogleGenerativeAI( model="gemini-1.5-pro", temperature=0)
# Load the image classification pipeline@st.cache_resource5 unmodified lines
pipe_classification = load_image_classification_pipeline()
# Load Qwen tokenizer and model@st.cache_resourcedef load_qwen_model(): """ Load the Qwen/Qwen2.5-Coder-32B-Instruct model and tokenizer. """ tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-Coder-32B-Instruct") model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-Coder-32B-Instruct", device_map="auto") return tokenizer, model
# Function to generate ingredients using Qwendef get_ingredients_qwen(food_name, tokenizer, model):# Function to generate ingredients using Google Geminidef get_ingredients_google(food_name): """ Generate a list of ingredients for the given food item using the Qwen model. 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}:" inputs = tokenizer(prompt, return_tensors="pt").to(model.device) outputs = model.generate(**inputs, max_new_tokens=50) return tokenizer.decode(outputs[0], skip_special_tokens=True).strip() response = llm.predict(prompt) return response.strip()
# Streamlit app# Streamlit app setupst.title("Food Image Recognition with Ingredients")
# # Add the provided image as a banner# st.image("CTP_Project/IR_IMAGE", caption="Food Recognition Model", use_column_width=True)# Add banner imagest.image("IR_IMAGE.png", caption="Food Recognition Model", use_column_width=True)
# Sidebar for model informationst.sidebar.title("Model Information")st.sidebar.write("**Image Classification Model**: Shresthadev403/food-image-classification")st.sidebar.write("**LLM for Ingredients**: Qwen2.5-Coder-32B-Instruct")st.sidebar.write("**LLM for Ingredients**: Google Gemini 1.5 Pro")
# Upload imageuploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
# Load the Qwen model and tokenizertokenizer, model = load_qwen_model()
if uploaded_file is not None: # Display the uploaded image image = Image.open(uploaded_file)10 unmodified lines # Generate and display ingredients for the top prediction st.subheader("Ingredients") try: ingredients = get_ingredients_qwen(top_food, tokenizer, model) ingredients = get_ingredients_google(top_food) st.write(ingredients) except Exception as e: st.error(f"Error generating ingredients: {e}") st.error(f"Error generating ingredients: {e}")
# Footerst.sidebar.markdown("Created with ❤️ using Streamlit and Hugging Face.")app.py+26-26