Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 5 |
st.set_page_config(page_title="Smart Waste Segregation Advisor")
|
| 6 |
|
| 7 |
st.title("♻️ AI-Powered Smart Waste Segregation Advisor")
|
|
@@ -9,29 +10,31 @@ st.subheader("SDG 12: Responsible Consumption and Production")
|
|
| 9 |
|
| 10 |
st.markdown("""
|
| 11 |
This tool helps users identify waste categories and provides guidance
|
| 12 |
-
for responsible disposal using AI-based reasoning.
|
| 13 |
""")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
uploaded_image = st.file_uploader(
|
| 17 |
"Upload an image of the waste item (optional)",
|
| 18 |
type=["jpg", "png", "jpeg"]
|
| 19 |
)
|
| 20 |
|
| 21 |
-
# Optional text input
|
| 22 |
user_text = st.text_input("Optional: Describe the waste item")
|
| 23 |
|
| 24 |
st.divider()
|
| 25 |
|
|
|
|
| 26 |
if st.button("Analyze Waste"):
|
|
|
|
|
|
|
| 27 |
if uploaded_image:
|
| 28 |
image_description = "An image showing a waste item uploaded by the user"
|
| 29 |
else:
|
| 30 |
image_description = "No image provided"
|
| 31 |
|
| 32 |
-
#
|
| 33 |
client = InferenceClient(
|
| 34 |
-
model="ibm-granite/granite-
|
| 35 |
token=os.getenv("HF_TOKEN")
|
| 36 |
)
|
| 37 |
|
|
@@ -40,10 +43,13 @@ You are an AI-powered Smart Waste Segregation Advisor.
|
|
| 40 |
|
| 41 |
Tasks:
|
| 42 |
1. Identify the waste item.
|
| 43 |
-
2. Classify it as
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
User Input:
|
| 49 |
Image description: {image_description}
|
|
@@ -51,11 +57,22 @@ User text: {user_text}
|
|
| 51 |
"""
|
| 52 |
|
| 53 |
with st.spinner("Analyzing using IBM Granite AI..."):
|
| 54 |
-
response = client.
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
temperature=0.7
|
| 58 |
)
|
| 59 |
|
|
|
|
|
|
|
| 60 |
st.markdown("### 🧠 AI Analysis Result")
|
| 61 |
-
st.write(
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# ---------------- PAGE CONFIG ----------------
|
| 6 |
st.set_page_config(page_title="Smart Waste Segregation Advisor")
|
| 7 |
|
| 8 |
st.title("♻️ AI-Powered Smart Waste Segregation Advisor")
|
|
|
|
| 10 |
|
| 11 |
st.markdown("""
|
| 12 |
This tool helps users identify waste categories and provides guidance
|
| 13 |
+
for responsible disposal using AI-based reasoning powered by IBM Granite.
|
| 14 |
""")
|
| 15 |
|
| 16 |
+
# ---------------- USER INPUTS ----------------
|
| 17 |
uploaded_image = st.file_uploader(
|
| 18 |
"Upload an image of the waste item (optional)",
|
| 19 |
type=["jpg", "png", "jpeg"]
|
| 20 |
)
|
| 21 |
|
|
|
|
| 22 |
user_text = st.text_input("Optional: Describe the waste item")
|
| 23 |
|
| 24 |
st.divider()
|
| 25 |
|
| 26 |
+
# ---------------- ANALYZE BUTTON ----------------
|
| 27 |
if st.button("Analyze Waste"):
|
| 28 |
+
|
| 29 |
+
# Simulated image interpretation (Option 1 design choice)
|
| 30 |
if uploaded_image:
|
| 31 |
image_description = "An image showing a waste item uploaded by the user"
|
| 32 |
else:
|
| 33 |
image_description = "No image provided"
|
| 34 |
|
| 35 |
+
# Initialize IBM Granite via Hugging Face Inference API
|
| 36 |
client = InferenceClient(
|
| 37 |
+
model="ibm-granite/granite-4.0-mini",
|
| 38 |
token=os.getenv("HF_TOKEN")
|
| 39 |
)
|
| 40 |
|
|
|
|
| 43 |
|
| 44 |
Tasks:
|
| 45 |
1. Identify the waste item.
|
| 46 |
+
2. Classify it as one of the following:
|
| 47 |
+
- Wet Waste
|
| 48 |
+
- Dry Waste
|
| 49 |
+
- E-Waste
|
| 50 |
+
3. Explain briefly why it belongs to this category.
|
| 51 |
+
4. Suggest the correct and safe disposal method.
|
| 52 |
+
5. Explain the environmental impact if disposed of incorrectly.
|
| 53 |
|
| 54 |
User Input:
|
| 55 |
Image description: {image_description}
|
|
|
|
| 57 |
"""
|
| 58 |
|
| 59 |
with st.spinner("Analyzing using IBM Granite AI..."):
|
| 60 |
+
response = client.chat_completion(
|
| 61 |
+
messages=[
|
| 62 |
+
{
|
| 63 |
+
"role": "system",
|
| 64 |
+
"content": "You are an AI assistant focused on sustainability and responsible waste management."
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"role": "user",
|
| 68 |
+
"content": prompt
|
| 69 |
+
}
|
| 70 |
+
],
|
| 71 |
+
max_tokens=250,
|
| 72 |
temperature=0.7
|
| 73 |
)
|
| 74 |
|
| 75 |
+
ai_output = response.choices[0].message["content"]
|
| 76 |
+
|
| 77 |
st.markdown("### 🧠 AI Analysis Result")
|
| 78 |
+
st.write(ai_output)
|