Spaces:
Sleeping
Sleeping
Commit
·
1bec4ea
1
Parent(s):
56732d1
Initial Commit
Browse files
app.py
CHANGED
|
@@ -9,16 +9,102 @@ from gradio_client import Client
|
|
| 9 |
st.set_page_config(
|
| 10 |
page_title="Food Image Recognition with Ingredients",
|
| 11 |
page_icon="🍔",
|
| 12 |
-
layout="
|
| 13 |
initial_sidebar_state="expanded",
|
| 14 |
)
|
| 15 |
|
| 16 |
# Custom CSS to improve styling and responsiveness
|
| 17 |
-
def local_css(
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
local_css(
|
| 22 |
|
| 23 |
# Hugging Face API key
|
| 24 |
API_KEY = st.secrets["HF_API_KEY"]
|
|
@@ -60,51 +146,55 @@ def get_ingredients_qwen(food_name):
|
|
| 60 |
except Exception as e:
|
| 61 |
return f"Error generating ingredients: {e}"
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
st.
|
| 65 |
|
| 66 |
# Add banner image
|
| 67 |
-
st.image("IR_IMAGE.png",
|
| 68 |
|
| 69 |
# Sidebar for model information
|
| 70 |
with st.sidebar:
|
| 71 |
st.title("Model Information")
|
| 72 |
-
st.write("**Image Classification Model
|
| 73 |
-
st.write("
|
|
|
|
|
|
|
| 74 |
st.markdown("---")
|
| 75 |
st.markdown("<p style='text-align: center;'>Developed by Muhammad Hassan Butt.</p>", unsafe_allow_html=True)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
|
| 79 |
|
| 80 |
if uploaded_file is not None:
|
| 81 |
# Display the uploaded image
|
| 82 |
image = Image.open(uploaded_file)
|
| 83 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 84 |
-
st.write("Classifying...")
|
| 85 |
-
|
| 86 |
-
# Make predictions
|
| 87 |
-
predictions = pipe_classification(image)
|
| 88 |
-
|
| 89 |
-
# Display only the top prediction
|
| 90 |
-
top_food = predictions[0]['label']
|
| 91 |
-
st.header(f"Food: {top_food}")
|
| 92 |
|
| 93 |
-
#
|
| 94 |
-
st.
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
st.set_page_config(
|
| 10 |
page_title="Food Image Recognition with Ingredients",
|
| 11 |
page_icon="🍔",
|
| 12 |
+
layout="centered",
|
| 13 |
initial_sidebar_state="expanded",
|
| 14 |
)
|
| 15 |
|
| 16 |
# Custom CSS to improve styling and responsiveness
|
| 17 |
+
def local_css():
|
| 18 |
+
st.markdown(
|
| 19 |
+
"""
|
| 20 |
+
<style>
|
| 21 |
+
/* Main layout */
|
| 22 |
+
.main {
|
| 23 |
+
background-color: #f0f2f6;
|
| 24 |
+
}
|
| 25 |
+
/* Title styling */
|
| 26 |
+
.title h1 {
|
| 27 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 28 |
+
text-align: center;
|
| 29 |
+
color: #ff4b4b;
|
| 30 |
+
font-size: 3rem;
|
| 31 |
+
margin-bottom: 20px;
|
| 32 |
+
}
|
| 33 |
+
/* Image styling */
|
| 34 |
+
.st-image img {
|
| 35 |
+
border-radius: 15px;
|
| 36 |
+
margin-bottom: 20px;
|
| 37 |
+
max-width: 100%;
|
| 38 |
+
}
|
| 39 |
+
/* Sidebar styling */
|
| 40 |
+
[data-testid="stSidebar"] {
|
| 41 |
+
background-color: #ff4b4b;
|
| 42 |
+
}
|
| 43 |
+
[data-testid="stSidebar"] .css-ng1t4o {
|
| 44 |
+
color: white;
|
| 45 |
+
}
|
| 46 |
+
[data-testid="stSidebar"] .css-1d391kg {
|
| 47 |
+
color: white;
|
| 48 |
+
}
|
| 49 |
+
/* File uploader styling */
|
| 50 |
+
.css-1y0tads {
|
| 51 |
+
background-color: #ff4b4b;
|
| 52 |
+
color: white;
|
| 53 |
+
border: none;
|
| 54 |
+
border-radius: 5px;
|
| 55 |
+
}
|
| 56 |
+
.css-1y0tads:hover {
|
| 57 |
+
background-color: #e04343;
|
| 58 |
+
color: white;
|
| 59 |
+
}
|
| 60 |
+
/* Button styling */
|
| 61 |
+
.stButton>button {
|
| 62 |
+
background-color: #ff4b4b;
|
| 63 |
+
color: white;
|
| 64 |
+
border: none;
|
| 65 |
+
padding: 0.5rem 1rem;
|
| 66 |
+
border-radius: 5px;
|
| 67 |
+
font-size: 1rem;
|
| 68 |
+
font-weight: bold;
|
| 69 |
+
margin-top: 10px;
|
| 70 |
+
}
|
| 71 |
+
.stButton>button:hover {
|
| 72 |
+
background-color: #e04343;
|
| 73 |
+
color: white;
|
| 74 |
+
}
|
| 75 |
+
/* Headers styling */
|
| 76 |
+
h2 {
|
| 77 |
+
color: #ff4b4b;
|
| 78 |
+
margin-top: 30px;
|
| 79 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 80 |
+
}
|
| 81 |
+
h3 {
|
| 82 |
+
color: #ff4b4b;
|
| 83 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 84 |
+
}
|
| 85 |
+
/* Text styling */
|
| 86 |
+
.stMarkdown p {
|
| 87 |
+
font-size: 1.1rem;
|
| 88 |
+
}
|
| 89 |
+
/* Footer styling */
|
| 90 |
+
footer {
|
| 91 |
+
visibility: hidden;
|
| 92 |
+
}
|
| 93 |
+
/* Mobile responsiveness */
|
| 94 |
+
@media only screen and (max-width: 600px) {
|
| 95 |
+
.title h1 {
|
| 96 |
+
font-size: 2rem;
|
| 97 |
+
}
|
| 98 |
+
.stButton>button {
|
| 99 |
+
width: 100%;
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
</style>
|
| 103 |
+
""",
|
| 104 |
+
unsafe_allow_html=True
|
| 105 |
+
)
|
| 106 |
|
| 107 |
+
local_css()
|
| 108 |
|
| 109 |
# Hugging Face API key
|
| 110 |
API_KEY = st.secrets["HF_API_KEY"]
|
|
|
|
| 146 |
except Exception as e:
|
| 147 |
return f"Error generating ingredients: {e}"
|
| 148 |
|
| 149 |
+
# Main content
|
| 150 |
+
st.markdown('<div class="title"><h1>Food Image Recognition with Ingredients</h1></div>', unsafe_allow_html=True)
|
| 151 |
|
| 152 |
# Add banner image
|
| 153 |
+
st.image("IR_IMAGE.png", use_column_width=True)
|
| 154 |
|
| 155 |
# Sidebar for model information
|
| 156 |
with st.sidebar:
|
| 157 |
st.title("Model Information")
|
| 158 |
+
st.write("**Image Classification Model**")
|
| 159 |
+
st.write("Shresthadev403/food-image-classification")
|
| 160 |
+
st.write("**LLM for Ingredients**")
|
| 161 |
+
st.write("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 162 |
st.markdown("---")
|
| 163 |
st.markdown("<p style='text-align: center;'>Developed by Muhammad Hassan Butt.</p>", unsafe_allow_html=True)
|
| 164 |
|
| 165 |
+
# File uploader
|
| 166 |
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
|
| 167 |
|
| 168 |
if uploaded_file is not None:
|
| 169 |
# Display the uploaded image
|
| 170 |
image = Image.open(uploaded_file)
|
| 171 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
# Classification button
|
| 174 |
+
if st.button("Classify"):
|
| 175 |
+
with st.spinner("Classifying..."):
|
| 176 |
+
# Make predictions
|
| 177 |
+
predictions = pipe_classification(image)
|
| 178 |
+
|
| 179 |
+
# Display only the top prediction
|
| 180 |
+
top_food = predictions[0]['label']
|
| 181 |
+
st.header(f"🍽️ Food: {top_food}")
|
| 182 |
+
|
| 183 |
+
# Generate and display ingredients for the top prediction
|
| 184 |
+
st.subheader("📝 Ingredients")
|
| 185 |
+
try:
|
| 186 |
+
ingredients = get_ingredients_qwen(top_food)
|
| 187 |
+
st.write(ingredients)
|
| 188 |
+
except Exception as e:
|
| 189 |
+
st.error(f"Error generating ingredients: {e}")
|
| 190 |
+
|
| 191 |
+
st.subheader("💡 Healthier Alternatives")
|
| 192 |
+
try:
|
| 193 |
+
client_gradio = Client("https://8a56cb969da1f9d721.gradio.live/")
|
| 194 |
+
result = client_gradio.predict(
|
| 195 |
+
query=f"What's a healthy {top_food} recipe, and why is it healthy?",
|
| 196 |
+
api_name="/get_response"
|
| 197 |
+
)
|
| 198 |
+
st.write(result)
|
| 199 |
+
except Exception as e:
|
| 200 |
+
st.error(f"Unable to contact RAG: {e}")
|
style.css
DELETED
|
@@ -1,74 +0,0 @@
|
|
| 1 |
-
/* General settings */
|
| 2 |
-
body {
|
| 3 |
-
background-color: #f5f5f5;
|
| 4 |
-
}
|
| 5 |
-
|
| 6 |
-
/* Title styling */
|
| 7 |
-
h1 {
|
| 8 |
-
text-align: center;
|
| 9 |
-
color: #333333;
|
| 10 |
-
font-size: 2.5rem;
|
| 11 |
-
margin-bottom: 20px;
|
| 12 |
-
}
|
| 13 |
-
|
| 14 |
-
/* Image styling */
|
| 15 |
-
.st-image > img {
|
| 16 |
-
border-radius: 10px;
|
| 17 |
-
margin-bottom: 20px;
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
/* Sidebar styling */
|
| 21 |
-
.css-1d391kg {
|
| 22 |
-
background-color: #ffffff;
|
| 23 |
-
border-right: 1px solid #e6e6e6;
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
.stSidebar > div {
|
| 27 |
-
padding: 20px;
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
/* Text elements */
|
| 31 |
-
h2 {
|
| 32 |
-
color: #4285F4;
|
| 33 |
-
margin-top: 30px;
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
-
h3 {
|
| 37 |
-
color: #333333;
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
/* File uploader styling */
|
| 41 |
-
.css-1cpxqw2 {
|
| 42 |
-
background-color: #4285F4;
|
| 43 |
-
color: #ffffff;
|
| 44 |
-
border: none;
|
| 45 |
-
border-radius: 5px;
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
.css-1cpxqw2:hover {
|
| 49 |
-
background-color: #357ae8;
|
| 50 |
-
color: #ffffff;
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
/* Footer styling */
|
| 54 |
-
footer {
|
| 55 |
-
visibility: hidden;
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
/* Adjustments for mobile devices */
|
| 59 |
-
@media only screen and (max-width: 600px) {
|
| 60 |
-
/* Adjust title size */
|
| 61 |
-
h1 {
|
| 62 |
-
font-size: 2rem;
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
/* Adjust image size */
|
| 66 |
-
.st-image > img {
|
| 67 |
-
width: 100%;
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
/* Sidebar adjustments */
|
| 71 |
-
.css-1d391kg {
|
| 72 |
-
width: 100%;
|
| 73 |
-
}
|
| 74 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|