Spaces:
Sleeping
Sleeping
HassanDataSci
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -38,4 +38,52 @@ def get_ingredients_bloom(food_name):
|
|
38 |
response = pipe_bloom(prompt, max_length=50, num_return_sequences=1)
|
39 |
generated_text = response[0]["generated_text"].strip()
|
40 |
|
41 |
-
# Post-process the response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
response = pipe_bloom(prompt, max_length=50, num_return_sequences=1)
|
39 |
generated_text = response[0]["generated_text"].strip()
|
40 |
|
41 |
+
# Post-process the response
|
42 |
+
ingredients = generated_text.split(":")[-1].strip() # Handle cases like "Ingredients: ..."
|
43 |
+
ingredients = ingredients.replace(".", "").strip() # Remove periods and extra spaces
|
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 |
+
return f"Error generating ingredients: {e}"
|
52 |
+
|
53 |
+
# Streamlit app setup
|
54 |
+
st.title("Food Image Recognition with Ingredients")
|
55 |
+
|
56 |
+
# Add banner image
|
57 |
+
st.image("IR_IMAGE.png", caption="Food Recognition Model", use_column_width=True)
|
58 |
+
|
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**: bigscience/bloom-1b7")
|
63 |
+
|
64 |
+
# Upload image
|
65 |
+
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
|
66 |
+
|
67 |
+
if uploaded_file is not None:
|
68 |
+
# Display the uploaded image
|
69 |
+
image = Image.open(uploaded_file)
|
70 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
71 |
+
st.write("Classifying...")
|
72 |
+
|
73 |
+
# Make predictions
|
74 |
+
predictions = pipe_classification(image)
|
75 |
+
|
76 |
+
# Display only the top prediction
|
77 |
+
top_food = predictions[0]['label']
|
78 |
+
st.header(f"Food: {top_food}")
|
79 |
+
|
80 |
+
# Generate and display ingredients for the top prediction
|
81 |
+
st.subheader("Ingredients")
|
82 |
+
try:
|
83 |
+
ingredients = get_ingredients_bloom(top_food)
|
84 |
+
st.write(ingredients)
|
85 |
+
except Exception as e:
|
86 |
+
st.error(f"Error generating ingredients: {e}")
|
87 |
+
|
88 |
+
# Footer
|
89 |
+
st.sidebar.markdown("Created with ❤️ using Streamlit and Hugging Face.")
|