Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234565 unmodified lines1213141516171819202122232425262728293031323334353637383940414243444546474849505152535410 unmodified lines65666768697071import streamlit as stfrom transformers import pipeline, AutoTokenizer, AutoModelForCausalLMfrom PIL import Image# Load the image classification pipeline@st.cache_resource5 unmodified linespipe_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):"""Generate a list of ingredients for the given food item using the Qwen model."""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()# Streamlit appst.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)# 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")# 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 imageimage = Image.open(uploaded_file)10 unmodified lines# Generate and display ingredients for the top predictionst.subheader("Ingredients")try:ingredients = get_ingredients_qwen(top_food, tokenizer, model)st.write(ingredients)except Exception as e:st.error(f"Error generating ingredients: {e}")12345678910111213141516175 unmodified lines232425262728293031323334353637383940414243444546474849505110 unmodified lines62636465666768697071import streamlit as stfrom 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 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}:"response = llm.predict(prompt)return response.strip()# Streamlit app setupst.title("Food Image Recognition with Ingredients")# 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**: Google Gemini 1.5 Pro")# Upload imageuploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])if uploaded_file is not None:# Display the uploaded imageimage = Image.open(uploaded_file)10 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}")# Footerst.sidebar.markdown("Created with ❤️ using Streamlit and Hugging Face.")
app.py+26-26