Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# import streamlit as st
|
| 2 |
# import firebase_admin
|
| 3 |
# from firebase_admin import credentials, db
|
|
@@ -85,10 +227,6 @@
|
|
| 85 |
# st.success("Truck allocated successfully!")
|
| 86 |
# else:
|
| 87 |
# st.error("Please enter a valid truck email.")
|
| 88 |
-
|
| 89 |
-
# # Back button to redirect to dashboard
|
| 90 |
-
# st.markdown("<br>", unsafe_allow_html=True)
|
| 91 |
-
# st.markdown("<a href='https://binsight.onrender.com/dashboard.html' target='_self' style='text-decoration:none;'><button style='padding: 10px 20px; font-size: 16px;'>⬅ Back to Dashboard</button></a>", unsafe_allow_html=True)
|
| 92 |
# else:
|
| 93 |
# login()
|
| 94 |
|
|
@@ -104,97 +242,114 @@
|
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
-
# Best version without back button
|
| 108 |
|
| 109 |
-
import streamlit as st
|
| 110 |
-
import firebase_admin
|
| 111 |
-
from firebase_admin import credentials, db
|
| 112 |
-
import base64
|
| 113 |
-
from io import BytesIO
|
| 114 |
-
from PIL import Image
|
| 115 |
-
import requests
|
| 116 |
|
| 117 |
-
# Initialize Firebase Realtime Database
|
| 118 |
-
if not firebase_admin._apps:
|
| 119 |
-
cred = credentials.Certificate("firebase_credentials.json")
|
| 120 |
-
firebase_admin.initialize_app(cred, {
|
| 121 |
-
'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/'
|
| 122 |
-
})
|
| 123 |
|
| 124 |
-
# Firebase Authentication REST API
|
| 125 |
-
FIREBASE_AUTH_URL = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyCwYZcPI5pltxxHiMUxjmQUNIaObNXWydw"
|
| 126 |
|
| 127 |
-
# Streamlit UI
|
| 128 |
-
st.title("Binsight Admin Dashboard")
|
| 129 |
|
| 130 |
-
# Session state for authentication
|
| 131 |
-
if "authenticated" not in st.session_state:
|
| 132 |
-
st.session_state.authenticated = False
|
| 133 |
-
if "user" not in st.session_state:
|
| 134 |
-
st.session_state.user = None
|
| 135 |
|
| 136 |
-
# Authentication UI
|
| 137 |
-
def login():
|
| 138 |
-
st.subheader("Login")
|
| 139 |
-
email = st.text_input("Email")
|
| 140 |
-
password = st.text_input("Password", type="password")
|
| 141 |
-
if st.button("Login"):
|
| 142 |
-
payload = {"email": email, "password": password, "returnSecureToken": True}
|
| 143 |
-
response = requests.post(FIREBASE_AUTH_URL, json=payload)
|
| 144 |
-
if response.status_code == 200:
|
| 145 |
-
st.session_state.authenticated = True
|
| 146 |
-
st.session_state.user = email
|
| 147 |
-
st.rerun()
|
| 148 |
-
else:
|
| 149 |
-
st.error("Invalid credentials or user does not exist.")
|
| 150 |
|
| 151 |
-
# Logout functionality
|
| 152 |
-
def logout():
|
| 153 |
-
st.session_state.authenticated = False
|
| 154 |
-
st.session_state.user = None
|
| 155 |
-
st.rerun()
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
|
| 171 |
-
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
-
# Display Image
|
| 181 |
-
if "image" in value:
|
| 182 |
-
image_data = base64.b64decode(value["image"])
|
| 183 |
-
st.image(Image.open(BytesIO(image_data)), caption="Dustbin Image", use_column_width=True)
|
| 184 |
|
| 185 |
-
if value["status"] == "Pending":
|
| 186 |
-
truck_email = st.text_input(f"Allocate Truck Email for {key}", key=f"truck_{key}")
|
| 187 |
-
if st.button(f"Allocate Truck", key=f"allocate_{key}"):
|
| 188 |
-
if truck_email:
|
| 189 |
-
dustbins_ref.child(key).update({
|
| 190 |
-
"allocated_truck": truck_email,
|
| 191 |
-
"status": "Allocated"
|
| 192 |
-
})
|
| 193 |
-
st.success("Truck allocated successfully!")
|
| 194 |
-
else:
|
| 195 |
-
st.error("Please enter a valid truck email.")
|
| 196 |
-
else:
|
| 197 |
-
login()
|
| 198 |
|
| 199 |
|
| 200 |
|
|
|
|
| 1 |
+
# Final with gratitude email
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import firebase_admin
|
| 7 |
+
from firebase_admin import credentials, db
|
| 8 |
+
import base64
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
from PIL import Image
|
| 11 |
+
import requests
|
| 12 |
+
|
| 13 |
+
# Firebase
|
| 14 |
+
if not firebase_admin._apps:
|
| 15 |
+
cred = credentials.Certificate("firebase_credentials.json")
|
| 16 |
+
firebase_admin.initialize_app(cred, {
|
| 17 |
+
'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/'
|
| 18 |
+
})
|
| 19 |
+
|
| 20 |
+
# Firebase Auth REST API
|
| 21 |
+
FIREBASE_AUTH_URL = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyCwYZcPI5pltxxHiMUxjmQUNIaObNXWydw"
|
| 22 |
+
|
| 23 |
+
# Mailgun Settings
|
| 24 |
+
MAILGUN_API_KEY = "9530f415a53e78317892af32cf3c4b93-79295dd0-b3f02e29" # Replace with your Mailgun API key
|
| 25 |
+
MAILGUN_DOMAIN = "sandboxb2e22240ec8a4b468825577fcb967a85.mailgun.org" # Replace with your Mailgun domain
|
| 26 |
+
Email_Domain = "Binsight.com"
|
| 27 |
+
|
| 28 |
+
def send_gratitude_email(to_email, location):
|
| 29 |
+
try:
|
| 30 |
+
return requests.post(
|
| 31 |
+
f"https://api.mailgun.net/v3/{MAILGUN_DOMAIN}/messages",
|
| 32 |
+
auth=("api", MAILGUN_API_KEY),
|
| 33 |
+
data={
|
| 34 |
+
"from": f"Binsight <noreply@{Email_Domain}>",
|
| 35 |
+
"to": [to_email],
|
| 36 |
+
"subject": "🎉 Thank You for Your Contribution!",
|
| 37 |
+
"text": f"Thank you for reporting the dustbin at {location}. Your help is invaluable in keeping our surroundings clean. 🌱"
|
| 38 |
+
}
|
| 39 |
+
)
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return None
|
| 42 |
+
|
| 43 |
+
# Streamlit UI
|
| 44 |
+
st.title("Binsight Admin Dashboard")
|
| 45 |
+
|
| 46 |
+
if "authenticated" not in st.session_state:
|
| 47 |
+
st.session_state.authenticated = False
|
| 48 |
+
if "user" not in st.session_state:
|
| 49 |
+
st.session_state.user = None
|
| 50 |
+
|
| 51 |
+
def login():
|
| 52 |
+
st.subheader("Login")
|
| 53 |
+
email = st.text_input("Email")
|
| 54 |
+
password = st.text_input("Password", type="password")
|
| 55 |
+
if st.button("Login"):
|
| 56 |
+
payload = {"email": email, "password": password, "returnSecureToken": True}
|
| 57 |
+
response = requests.post(FIREBASE_AUTH_URL, json=payload)
|
| 58 |
+
if response.status_code == 200:
|
| 59 |
+
st.session_state.authenticated = True
|
| 60 |
+
st.session_state.user = email
|
| 61 |
+
st.rerun()
|
| 62 |
+
else:
|
| 63 |
+
st.error("Invalid credentials or user does not exist.")
|
| 64 |
+
|
| 65 |
+
def logout():
|
| 66 |
+
st.session_state.authenticated = False
|
| 67 |
+
st.session_state.user = None
|
| 68 |
+
st.rerun()
|
| 69 |
+
|
| 70 |
+
if st.session_state.authenticated:
|
| 71 |
+
st.sidebar.button("Logout", on_click=logout)
|
| 72 |
+
st.sidebar.header("Filters")
|
| 73 |
+
filter_status = st.sidebar.selectbox("Filter by Status", ["All", "Pending", "Allocated", "Completed"], index=0)
|
| 74 |
+
|
| 75 |
+
dustbins_ref = db.reference("dustbins")
|
| 76 |
+
dustbins = dustbins_ref.get() or {}
|
| 77 |
+
|
| 78 |
+
filtered_dustbins = {
|
| 79 |
+
key: value for key, value in dustbins.items()
|
| 80 |
+
if filter_status == "All" or value["status"] == filter_status
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
st.subheader(f"Showing {filter_status.lower()} dustbins ({len(filtered_dustbins)})")
|
| 84 |
+
|
| 85 |
+
for key, value in filtered_dustbins.items():
|
| 86 |
+
with st.expander(f"📍 {value['address']}"):
|
| 87 |
+
st.write(f"**Latitude**: {value['latitude']}")
|
| 88 |
+
st.write(f"**Longitude**: {value['longitude']}")
|
| 89 |
+
st.write(f"**Status**: {value['status']}")
|
| 90 |
+
|
| 91 |
+
if "image" in value:
|
| 92 |
+
image_data = base64.b64decode(value["image"])
|
| 93 |
+
st.image(Image.open(BytesIO(image_data)), caption="Dustbin Image", use_column_width=True)
|
| 94 |
+
|
| 95 |
+
if value["status"] == "Pending":
|
| 96 |
+
truck_email = st.text_input(f"Allocate Truck Email for {key}", key=f"truck_{key}")
|
| 97 |
+
if st.button(f"Allocate Truck", key=f"allocate_{key}"):
|
| 98 |
+
if truck_email:
|
| 99 |
+
dustbins_ref.child(key).update({
|
| 100 |
+
"allocated_truck": truck_email,
|
| 101 |
+
"status": "Allocated"
|
| 102 |
+
})
|
| 103 |
+
st.success("Truck allocated successfully!")
|
| 104 |
+
else:
|
| 105 |
+
st.error("Please enter a valid truck email.")
|
| 106 |
+
|
| 107 |
+
if value["status"] == "Allocated":
|
| 108 |
+
if st.button("Mark as Completed", key=f"complete_{key}"):
|
| 109 |
+
dustbins_ref.child(key).update({"status": "Completed"})
|
| 110 |
+
st.success("Status marked as Completed!")
|
| 111 |
+
|
| 112 |
+
if value["status"] == "Completed":
|
| 113 |
+
if st.button("Send Gratitude Email", key=f"gratitude_{key}"):
|
| 114 |
+
email = value.get("email")
|
| 115 |
+
location = value.get("address", "Unknown Location")
|
| 116 |
+
if email:
|
| 117 |
+
response = send_gratitude_email(email, location)
|
| 118 |
+
if response and response.status_code == 200:
|
| 119 |
+
st.success("✅ Gratitude email sent successfully!")
|
| 120 |
+
else:
|
| 121 |
+
st.error("❌ Failed to send email.")
|
| 122 |
+
else:
|
| 123 |
+
st.warning("⚠️ Email not available.")
|
| 124 |
+
else:
|
| 125 |
+
login()
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
# Best version without back button
|
| 142 |
+
|
| 143 |
# import streamlit as st
|
| 144 |
# import firebase_admin
|
| 145 |
# from firebase_admin import credentials, db
|
|
|
|
| 227 |
# st.success("Truck allocated successfully!")
|
| 228 |
# else:
|
| 229 |
# st.error("Please enter a valid truck email.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
# else:
|
| 231 |
# login()
|
| 232 |
|
|
|
|
| 242 |
|
| 243 |
|
| 244 |
|
|
|
|
| 245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
|
|
|
|
|
|
| 248 |
|
|
|
|
|
|
|
| 249 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
# import streamlit as st
|
| 256 |
+
# import firebase_admin
|
| 257 |
+
# from firebase_admin import credentials, db
|
| 258 |
+
# import base64
|
| 259 |
+
# from io import BytesIO
|
| 260 |
+
# from PIL import Image
|
| 261 |
+
# import requests
|
| 262 |
+
|
| 263 |
+
# # Initialize Firebase Realtime Database
|
| 264 |
+
# if not firebase_admin._apps:
|
| 265 |
+
# cred = credentials.Certificate("firebase_credentials.json")
|
| 266 |
+
# firebase_admin.initialize_app(cred, {
|
| 267 |
+
# 'databaseURL': 'https://binsight-beda0-default-rtdb.asia-southeast1.firebasedatabase.app/'
|
| 268 |
+
# })
|
| 269 |
+
|
| 270 |
+
# # Firebase Authentication REST API
|
| 271 |
+
# FIREBASE_AUTH_URL = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyCwYZcPI5pltxxHiMUxjmQUNIaObNXWydw"
|
| 272 |
+
|
| 273 |
+
# # Streamlit UI
|
| 274 |
+
# st.title("Binsight Admin Dashboard")
|
| 275 |
+
|
| 276 |
+
# # Session state for authentication
|
| 277 |
+
# if "authenticated" not in st.session_state:
|
| 278 |
+
# st.session_state.authenticated = False
|
| 279 |
+
# if "user" not in st.session_state:
|
| 280 |
+
# st.session_state.user = None
|
| 281 |
+
|
| 282 |
+
# # Authentication UI
|
| 283 |
+
# def login():
|
| 284 |
+
# st.subheader("Login")
|
| 285 |
+
# email = st.text_input("Email")
|
| 286 |
+
# password = st.text_input("Password", type="password")
|
| 287 |
+
# if st.button("Login"):
|
| 288 |
+
# payload = {"email": email, "password": password, "returnSecureToken": True}
|
| 289 |
+
# response = requests.post(FIREBASE_AUTH_URL, json=payload)
|
| 290 |
+
# if response.status_code == 200:
|
| 291 |
+
# st.session_state.authenticated = True
|
| 292 |
+
# st.session_state.user = email
|
| 293 |
+
# st.rerun()
|
| 294 |
+
# else:
|
| 295 |
+
# st.error("Invalid credentials or user does not exist.")
|
| 296 |
+
|
| 297 |
+
# # Logout functionality
|
| 298 |
+
# def logout():
|
| 299 |
+
# st.session_state.authenticated = False
|
| 300 |
+
# st.session_state.user = None
|
| 301 |
+
# st.rerun()
|
| 302 |
+
|
| 303 |
+
# # Main Dashboard
|
| 304 |
+
# if st.session_state.authenticated:
|
| 305 |
+
# st.sidebar.button("Logout", on_click=logout)
|
| 306 |
+
# st.sidebar.header("Filters")
|
| 307 |
+
# filter_status = st.sidebar.selectbox("Filter by Status", ["All", "Pending", "Allocated", "Completed"], index=0)
|
| 308 |
|
| 309 |
+
# dustbins_ref = db.reference("dustbins")
|
| 310 |
+
# dustbins = dustbins_ref.get() or {}
|
| 311 |
|
| 312 |
+
# filtered_dustbins = {
|
| 313 |
+
# key: value for key, value in dustbins.items()
|
| 314 |
+
# if filter_status == "All" or value["status"] == filter_status
|
| 315 |
+
# }
|
| 316 |
|
| 317 |
+
# st.subheader(f"Showing {filter_status.lower()} dustbins ({len(filtered_dustbins)})")
|
| 318 |
|
| 319 |
+
# for key, value in filtered_dustbins.items():
|
| 320 |
+
# with st.expander(f"📍 {value['address']}"):
|
| 321 |
+
# st.write(f"**Latitude**: {value['latitude']}")
|
| 322 |
+
# st.write(f"**Longitude**: {value['longitude']}")
|
| 323 |
+
# st.write(f"**Status**: {value['status']}")
|
| 324 |
+
# # st.write(f"**User**: {value['user_email']}")
|
| 325 |
+
|
| 326 |
+
# # Display Image
|
| 327 |
+
# if "image" in value:
|
| 328 |
+
# image_data = base64.b64decode(value["image"])
|
| 329 |
+
# st.image(Image.open(BytesIO(image_data)), caption="Dustbin Image", use_column_width=True)
|
| 330 |
+
|
| 331 |
+
# if value["status"] == "Pending":
|
| 332 |
+
# truck_email = st.text_input(f"Allocate Truck Email for {key}", key=f"truck_{key}")
|
| 333 |
+
# if st.button(f"Allocate Truck", key=f"allocate_{key}"):
|
| 334 |
+
# if truck_email:
|
| 335 |
+
# dustbins_ref.child(key).update({
|
| 336 |
+
# "allocated_truck": truck_email,
|
| 337 |
+
# "status": "Allocated"
|
| 338 |
+
# })
|
| 339 |
+
# st.success("Truck allocated successfully!")
|
| 340 |
+
# else:
|
| 341 |
+
# st.error("Please enter a valid truck email.")
|
| 342 |
+
|
| 343 |
+
# # Back button to redirect to dashboard
|
| 344 |
+
# st.markdown("<br>", unsafe_allow_html=True)
|
| 345 |
+
# st.markdown("<a href='https://binsight.onrender.com/dashboard.html' target='_self' style='text-decoration:none;'><button style='padding: 10px 20px; font-size: 16px;'>⬅ Back to Dashboard</button></a>", unsafe_allow_html=True)
|
| 346 |
+
# else:
|
| 347 |
+
# login()
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
|
| 351 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
|
| 355 |
|