Spaces:
Sleeping
Sleeping
HassanDataSci
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,33 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import pipeline,
|
3 |
from PIL import Image
|
4 |
import requests
|
5 |
|
6 |
-
# Load the
|
7 |
-
st.title("Food Image Classification with Hugging Face")
|
8 |
-
st.write("Upload an image to classify the type of food!")
|
9 |
-
|
10 |
-
# Load the model
|
11 |
@st.cache_resource
|
12 |
-
def
|
13 |
return pipeline("image-classification", model="Shresthadev403/food-image-classification")
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Upload image
|
18 |
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
|
@@ -24,13 +39,18 @@ if uploaded_file is not None:
|
|
24 |
st.write("Classifying...")
|
25 |
|
26 |
# Make predictions
|
27 |
-
predictions =
|
28 |
|
29 |
-
# Display top prediction
|
|
|
|
|
30 |
st.subheader("Top Prediction")
|
31 |
-
st.write(f"**{
|
32 |
|
33 |
-
#
|
34 |
-
st.subheader("
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
3 |
from PIL import Image
|
4 |
import requests
|
5 |
|
6 |
+
# Load the image classification pipeline
|
|
|
|
|
|
|
|
|
7 |
@st.cache_resource
|
8 |
+
def load_image_classification_pipeline():
|
9 |
return pipeline("image-classification", model="Shresthadev403/food-image-classification")
|
10 |
|
11 |
+
pipe_classification = load_image_classification_pipeline()
|
12 |
+
|
13 |
+
# Load the Meta-Llama model and tokenizer for text generation
|
14 |
+
@st.cache_resource
|
15 |
+
def load_llama_pipeline():
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-3B-Instruct")
|
17 |
+
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-3B-Instruct")
|
18 |
+
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
19 |
+
|
20 |
+
pipe_llama = load_llama_pipeline()
|
21 |
+
|
22 |
+
# Function to generate ingredients using Meta-Llama
|
23 |
+
def get_ingredients(food_name):
|
24 |
+
prompt = f"List the main ingredients typically used to prepare {food_name}:"
|
25 |
+
response = pipe_llama(prompt, max_length=50, num_return_sequences=1)
|
26 |
+
return response[0]['generated_text']
|
27 |
+
|
28 |
+
# Streamlit app
|
29 |
+
st.title("Food Image Classification with Ingredients Generation")
|
30 |
+
st.write("Upload an image to classify the type of food and get its ingredients!")
|
31 |
|
32 |
# Upload image
|
33 |
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
|
|
|
39 |
st.write("Classifying...")
|
40 |
|
41 |
# Make predictions
|
42 |
+
predictions = pipe_classification(image)
|
43 |
|
44 |
+
# Display only the top prediction
|
45 |
+
top_food = predictions[0]['label']
|
46 |
+
confidence = predictions[0]['score']
|
47 |
st.subheader("Top Prediction")
|
48 |
+
st.write(f"**{top_food}** with confidence {confidence:.2f}")
|
49 |
|
50 |
+
# Generate and display ingredients for the top prediction
|
51 |
+
st.subheader("Ingredients")
|
52 |
+
try:
|
53 |
+
ingredients = get_ingredients(top_food)
|
54 |
+
st.write(ingredients)
|
55 |
+
except Exception as e:
|
56 |
+
st.write("Could not generate ingredients. Please try again later.")
|