Spaces:
Sleeping
Sleeping
HassanDataSci
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,17 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import pipeline
|
3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Load the image classification pipeline
|
6 |
@st.cache_resource
|
@@ -12,43 +23,29 @@ def load_image_classification_pipeline():
|
|
12 |
|
13 |
pipe_classification = load_image_classification_pipeline()
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
def load_qwen_model():
|
18 |
-
"""
|
19 |
-
Load the Qwen/Qwen2.5-Coder-32B-Instruct model and tokenizer.
|
20 |
-
"""
|
21 |
-
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-Coder-32B-Instruct")
|
22 |
-
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-Coder-32B-Instruct", device_map="auto")
|
23 |
-
return tokenizer, model
|
24 |
-
|
25 |
-
# Function to generate ingredients using Qwen
|
26 |
-
def get_ingredients_qwen(food_name, tokenizer, model):
|
27 |
"""
|
28 |
-
Generate a list of ingredients for the given food item using
|
29 |
"""
|
30 |
prompt = f"List the main ingredients typically used to prepare {food_name}:"
|
31 |
-
|
32 |
-
|
33 |
-
return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
34 |
|
35 |
-
# Streamlit app
|
36 |
st.title("Food Image Recognition with Ingredients")
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
|
41 |
# Sidebar for model information
|
42 |
st.sidebar.title("Model Information")
|
43 |
st.sidebar.write("**Image Classification Model**: Shresthadev403/food-image-classification")
|
44 |
-
st.sidebar.write("**LLM for Ingredients**:
|
45 |
|
46 |
# Upload image
|
47 |
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
|
48 |
|
49 |
-
# Load the Qwen model and tokenizer
|
50 |
-
tokenizer, model = load_qwen_model()
|
51 |
-
|
52 |
if uploaded_file is not None:
|
53 |
# Display the uploaded image
|
54 |
image = Image.open(uploaded_file)
|
@@ -65,7 +62,10 @@ if uploaded_file is not None:
|
|
65 |
# Generate and display ingredients for the top prediction
|
66 |
st.subheader("Ingredients")
|
67 |
try:
|
68 |
-
ingredients =
|
69 |
st.write(ingredients)
|
70 |
except Exception as e:
|
71 |
-
st.error(f"Error generating ingredients: {e}")
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
from PIL import Image
|
4 |
+
from langchain.chat_models import ChatGoogleGenerativeAI
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Set up the Google API Key (add this as a secret in Hugging Face Spaces)
|
8 |
+
os.environ["GOOGLE_API_KEY"] = st.secrets["GOOGLE_API_KEY"]
|
9 |
+
|
10 |
+
# Initialize Google Gemini model
|
11 |
+
llm = ChatGoogleGenerativeAI(
|
12 |
+
model="gemini-1.5-pro",
|
13 |
+
temperature=0
|
14 |
+
)
|
15 |
|
16 |
# Load the image classification pipeline
|
17 |
@st.cache_resource
|
|
|
23 |
|
24 |
pipe_classification = load_image_classification_pipeline()
|
25 |
|
26 |
+
# Function to generate ingredients using Google Gemini
|
27 |
+
def get_ingredients_google(food_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
"""
|
29 |
+
Generate a list of ingredients for the given food item using Google Gemini AI.
|
30 |
"""
|
31 |
prompt = f"List the main ingredients typically used to prepare {food_name}:"
|
32 |
+
response = llm.predict(prompt)
|
33 |
+
return response.strip()
|
|
|
34 |
|
35 |
+
# Streamlit app setup
|
36 |
st.title("Food Image Recognition with Ingredients")
|
37 |
|
38 |
+
# Add banner image
|
39 |
+
st.image("IR_IMAGE.png", caption="Food Recognition Model", use_column_width=True)
|
40 |
|
41 |
# Sidebar for model information
|
42 |
st.sidebar.title("Model Information")
|
43 |
st.sidebar.write("**Image Classification Model**: Shresthadev403/food-image-classification")
|
44 |
+
st.sidebar.write("**LLM for Ingredients**: Google Gemini 1.5 Pro")
|
45 |
|
46 |
# Upload image
|
47 |
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
|
48 |
|
|
|
|
|
|
|
49 |
if uploaded_file is not None:
|
50 |
# Display the uploaded image
|
51 |
image = Image.open(uploaded_file)
|
|
|
62 |
# Generate and display ingredients for the top prediction
|
63 |
st.subheader("Ingredients")
|
64 |
try:
|
65 |
+
ingredients = get_ingredients_google(top_food)
|
66 |
st.write(ingredients)
|
67 |
except Exception as e:
|
68 |
+
st.error(f"Error generating ingredients: {e}")
|
69 |
+
|
70 |
+
# Footer
|
71 |
+
st.sidebar.markdown("Created with ❤️ using Streamlit and Hugging Face.")
|