Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,27 +13,28 @@ def load_image_classification_pipeline():
|
|
13 |
|
14 |
pipe_classification = load_image_classification_pipeline()
|
15 |
|
16 |
-
# Load the
|
17 |
@st.cache_resource
|
18 |
-
def
|
19 |
"""
|
20 |
-
Load the
|
21 |
"""
|
22 |
-
return pipeline("text-generation", model="
|
23 |
|
24 |
-
|
25 |
|
26 |
-
|
|
|
27 |
"""
|
28 |
-
Generate a list of ingredients for the given food item using
|
29 |
Returns a clean, comma-separated list of ingredients.
|
30 |
"""
|
31 |
prompt = (
|
32 |
-
f"List only the ingredients
|
33 |
-
"
|
34 |
)
|
35 |
try:
|
36 |
-
response =
|
37 |
generated_text = response[0]["generated_text"].strip()
|
38 |
|
39 |
# Post-process to extract only the list of ingredients
|
@@ -47,13 +48,7 @@ def get_ingredients_llama(food_name):
|
|
47 |
return ingredients
|
48 |
except Exception as e:
|
49 |
return f"Error generating ingredients: {e}"
|
50 |
-
|
51 |
-
# Process the response to ensure it's a clean, comma-separated list
|
52 |
-
ingredients = generated_text.split(":")[-1].strip() # Handle cases like "Ingredients: ..."
|
53 |
-
ingredients = ingredients.replace(".", "").strip() # Remove periods and extra spaces
|
54 |
-
return ingredients
|
55 |
-
except Exception as e:
|
56 |
-
return f"Error generating ingredients: {e}"
|
57 |
# Streamlit app setup
|
58 |
st.title("Food Image Recognition with Ingredients")
|
59 |
|
@@ -63,7 +58,7 @@ st.image("IR_IMAGE.png", caption="Food Recognition Model", use_column_width=True
|
|
63 |
# Sidebar for model information
|
64 |
st.sidebar.title("Model Information")
|
65 |
st.sidebar.write("**Image Classification Model**: Shresthadev403/food-image-classification")
|
66 |
-
st.sidebar.write("**LLM for Ingredients**:
|
67 |
|
68 |
# Upload image
|
69 |
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
|
@@ -84,7 +79,7 @@ if uploaded_file is not None:
|
|
84 |
# Generate and display ingredients for the top prediction
|
85 |
st.subheader("Ingredients")
|
86 |
try:
|
87 |
-
ingredients =
|
88 |
st.write(ingredients)
|
89 |
except Exception as e:
|
90 |
st.error(f"Error generating ingredients: {e}")
|
|
|
13 |
|
14 |
pipe_classification = load_image_classification_pipeline()
|
15 |
|
16 |
+
# Load the BLOOM model for ingredient generation
|
17 |
@st.cache_resource
|
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 |
+
# Function to generate ingredients using BLOOM
|
27 |
+
def get_ingredients_bloom(food_name):
|
28 |
"""
|
29 |
+
Generate a list of ingredients for the given food item using BLOOM.
|
30 |
Returns a clean, comma-separated list of ingredients.
|
31 |
"""
|
32 |
prompt = (
|
33 |
+
f"List only the main ingredients typically used to prepare {food_name}. "
|
34 |
+
"Provide the ingredients as a simple, comma-separated list such as: ingredient1, ingredient2, ingredient3."
|
35 |
)
|
36 |
try:
|
37 |
+
response = pipe_bloom(prompt, max_length=50, num_return_sequences=1)
|
38 |
generated_text = response[0]["generated_text"].strip()
|
39 |
|
40 |
# Post-process to extract only the list of ingredients
|
|
|
48 |
return ingredients
|
49 |
except Exception as e:
|
50 |
return f"Error generating ingredients: {e}"
|
51 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
# Streamlit app setup
|
53 |
st.title("Food Image Recognition with Ingredients")
|
54 |
|
|
|
58 |
# Sidebar for model information
|
59 |
st.sidebar.title("Model Information")
|
60 |
st.sidebar.write("**Image Classification Model**: Shresthadev403/food-image-classification")
|
61 |
+
st.sidebar.write("**LLM for Ingredients**: bigscience/bloom-1b7")
|
62 |
|
63 |
# Upload image
|
64 |
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
|
|
|
79 |
# Generate and display ingredients for the top prediction
|
80 |
st.subheader("Ingredients")
|
81 |
try:
|
82 |
+
ingredients = get_ingredients_bloom(top_food)
|
83 |
st.write(ingredients)
|
84 |
except Exception as e:
|
85 |
st.error(f"Error generating ingredients: {e}")
|