Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
|
| 3 |
st.set_page_config(page_title="Smart Waste Segregation Advisor")
|
| 4 |
|
|
@@ -10,8 +12,11 @@ This tool helps users identify waste categories and provides guidance
|
|
| 10 |
for responsible disposal using AI-based reasoning.
|
| 11 |
""")
|
| 12 |
|
| 13 |
-
# Image upload
|
| 14 |
-
uploaded_image = st.file_uploader(
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Optional text input
|
| 17 |
user_text = st.text_input("Optional: Describe the waste item")
|
|
@@ -19,47 +24,38 @@ user_text = st.text_input("Optional: Describe the waste item")
|
|
| 19 |
st.divider()
|
| 20 |
|
| 21 |
if st.button("Analyze Waste"):
|
| 22 |
-
# Simulated image interpretation
|
| 23 |
if uploaded_image:
|
| 24 |
image_description = "An image showing a waste item uploaded by the user"
|
| 25 |
else:
|
| 26 |
image_description = "No image provided"
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
prompt = f"""
|
| 29 |
You are an AI-powered Smart Waste Segregation Advisor.
|
| 30 |
|
| 31 |
-
|
| 32 |
-
1.
|
| 33 |
-
2.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
2. Classify the item into one of the following categories:
|
| 38 |
-
- Wet Waste
|
| 39 |
-
- Dry Waste
|
| 40 |
-
- E-Waste
|
| 41 |
-
3. Explain briefly why the item belongs to this category.
|
| 42 |
-
4. Suggest the correct and safe disposal method.
|
| 43 |
-
5. Explain the environmental impact if the item is disposed of incorrectly.
|
| 44 |
|
| 45 |
User Input:
|
| 46 |
Image description: {image_description}
|
| 47 |
-
|
| 48 |
"""
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
**Disposal Method:**
|
| 58 |
-
Dispose of the item in a dry waste or recycling bin.
|
| 59 |
-
|
| 60 |
-
**Environmental Impact:**
|
| 61 |
-
Improper disposal can cause long-term pollution and harm wildlife.
|
| 62 |
-
"""
|
| 63 |
|
| 64 |
st.markdown("### 🧠 AI Analysis Result")
|
| 65 |
-
st.
|
|
|
|
| 1 |
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 |
|
|
|
|
| 12 |
for responsible disposal using AI-based reasoning.
|
| 13 |
""")
|
| 14 |
|
| 15 |
+
# Image upload
|
| 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")
|
|
|
|
| 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 |
+
# 🔐 Initialize IBM Granite via HF Inference API
|
| 33 |
+
client = InferenceClient(
|
| 34 |
+
model="ibm-granite/granite-3.0-2b-instruct",
|
| 35 |
+
token=os.getenv("HF_TOKEN")
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
prompt = f"""
|
| 39 |
You are an AI-powered Smart Waste Segregation Advisor.
|
| 40 |
|
| 41 |
+
Tasks:
|
| 42 |
+
1. Identify the waste item.
|
| 43 |
+
2. Classify it as Wet Waste, Dry Waste, or E-Waste.
|
| 44 |
+
3. Explain the reason.
|
| 45 |
+
4. Suggest correct disposal.
|
| 46 |
+
5. Explain environmental impact.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
User Input:
|
| 49 |
Image description: {image_description}
|
| 50 |
+
User text: {user_text}
|
| 51 |
"""
|
| 52 |
|
| 53 |
+
with st.spinner("Analyzing using IBM Granite AI..."):
|
| 54 |
+
response = client.text_generation(
|
| 55 |
+
prompt,
|
| 56 |
+
max_new_tokens=250,
|
| 57 |
+
temperature=0.7
|
| 58 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
st.markdown("### 🧠 AI Analysis Result")
|
| 61 |
+
st.write(response)
|