Spaces:
Sleeping
Sleeping
HassanDataSci
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,15 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image
|
|
|
4 |
import os
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
# Load the image classification pipeline
|
7 |
@st.cache_resource
|
8 |
def load_image_classification_pipeline():
|
@@ -13,43 +20,30 @@ def load_image_classification_pipeline():
|
|
13 |
|
14 |
pipe_classification = load_image_classification_pipeline()
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
def load_bloom_pipeline():
|
19 |
-
"""
|
20 |
-
Load the BLOOM model for ingredient generation.
|
21 |
-
"""
|
22 |
-
return pipeline("text-generation", model="bigscience/bloom-1b7")
|
23 |
-
|
24 |
-
pipe_bloom = load_bloom_pipeline()
|
25 |
-
|
26 |
-
def get_ingredients_bloom(food_name):
|
27 |
"""
|
28 |
-
Generate a list of ingredients for the given food item using
|
29 |
Returns a clean, comma-separated list of ingredients.
|
30 |
"""
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
try:
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
# Validate the response to ensure no placeholders
|
46 |
-
if "ingredient1" in ingredients.lower() or "example" in ingredients.lower():
|
47 |
-
return "No valid ingredients found. Try again with a different food."
|
48 |
-
|
49 |
-
return ingredients
|
50 |
except Exception as e:
|
51 |
-
# Handle any errors that occur during the process
|
52 |
return f"Error generating ingredients: {e}"
|
|
|
53 |
# Streamlit app setup
|
54 |
st.title("Food Image Recognition with Ingredients")
|
55 |
|
@@ -59,7 +53,7 @@ st.image("IR_IMAGE.png", caption="Food Recognition Model", use_column_width=True
|
|
59 |
# Sidebar for model information
|
60 |
st.sidebar.title("Model Information")
|
61 |
st.sidebar.write("**Image Classification Model**: Shresthadev403/food-image-classification")
|
62 |
-
st.sidebar.write("**LLM for Ingredients**:
|
63 |
|
64 |
# Upload image
|
65 |
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
|
@@ -80,7 +74,7 @@ if uploaded_file is not None:
|
|
80 |
# Generate and display ingredients for the top prediction
|
81 |
st.subheader("Ingredients")
|
82 |
try:
|
83 |
-
ingredients =
|
84 |
st.write(ingredients)
|
85 |
except Exception as e:
|
86 |
st.error(f"Error generating ingredients: {e}")
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
import os
|
6 |
|
7 |
+
# Hugging Face API key
|
8 |
+
API_KEY = st.secrets["HF_API_KEY"]
|
9 |
+
|
10 |
+
# Initialize the Hugging Face Inference Client
|
11 |
+
client = InferenceClient(api_key=API_KEY)
|
12 |
+
|
13 |
# Load the image classification pipeline
|
14 |
@st.cache_resource
|
15 |
def load_image_classification_pipeline():
|
|
|
20 |
|
21 |
pipe_classification = load_image_classification_pipeline()
|
22 |
|
23 |
+
# Function to generate ingredients using Hugging Face Inference Client
|
24 |
+
def get_ingredients_qwen(food_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
"""
|
26 |
+
Generate a list of ingredients for the given food item using Qwen NLP model.
|
27 |
Returns a clean, comma-separated list of ingredients.
|
28 |
"""
|
29 |
+
messages = [
|
30 |
+
{
|
31 |
+
"role": "user",
|
32 |
+
"content": f"List only the main ingredients for {food_name}. "
|
33 |
+
f"Respond in a concise, comma-separated list without any extra text or explanations."
|
34 |
+
}
|
35 |
+
]
|
36 |
try:
|
37 |
+
completion = client.chat.completions.create(
|
38 |
+
model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
39 |
+
messages=messages,
|
40 |
+
max_tokens=50
|
41 |
+
)
|
42 |
+
generated_text = completion.choices[0].message["content"].strip()
|
43 |
+
return generated_text
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
except Exception as e:
|
|
|
45 |
return f"Error generating ingredients: {e}"
|
46 |
+
|
47 |
# Streamlit app setup
|
48 |
st.title("Food Image Recognition with Ingredients")
|
49 |
|
|
|
53 |
# Sidebar for model information
|
54 |
st.sidebar.title("Model Information")
|
55 |
st.sidebar.write("**Image Classification Model**: Shresthadev403/food-image-classification")
|
56 |
+
st.sidebar.write("**LLM for Ingredients**: Qwen/Qwen2.5-Coder-32B-Instruct")
|
57 |
|
58 |
# Upload image
|
59 |
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
|
|
|
74 |
# Generate and display ingredients for the top prediction
|
75 |
st.subheader("Ingredients")
|
76 |
try:
|
77 |
+
ingredients = get_ingredients_qwen(top_food)
|
78 |
st.write(ingredients)
|
79 |
except Exception as e:
|
80 |
st.error(f"Error generating ingredients: {e}")
|