Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -136,5 +136,78 @@ def logout():
|
|
| 136 |
|
| 137 |
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
if __name__ == '__main__':
|
| 140 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
+
from twilio.rest import Client
|
| 140 |
+
import firebase_admin
|
| 141 |
+
from firebase_admin import credentials, firestore
|
| 142 |
+
|
| 143 |
+
# Twilio Configuration
|
| 144 |
+
ACCOUNT_SID = 'AC97ad50edcf94fdd7d4576be9651bf4bf'
|
| 145 |
+
AUTH_TOKEN = '6d8b2559ffbbdbc5d06542e545220aaa'
|
| 146 |
+
FROM_PHONE = '+12708175529'
|
| 147 |
+
TO_PHONE = '+916382792828'
|
| 148 |
+
import numpy as np
|
| 149 |
+
import joblib
|
| 150 |
+
|
| 151 |
+
# Load the saved Gradient Boosting model
|
| 152 |
+
loaded_model = joblib.load('dlmodel.pkl')
|
| 153 |
+
|
| 154 |
+
def predict_conc(pdiff):
|
| 155 |
+
if pdiff>3.19:
|
| 156 |
+
return 0
|
| 157 |
+
pdiff_value = pdiff # Replace 1.5 with your input value for "Average Potential Difference"
|
| 158 |
+
new_data = np.array([[pdiff_value]])
|
| 159 |
+
prediction = loaded_model.predict(new_data)
|
| 160 |
+
concentration = 10**prediction[0] # Reverse log transformation
|
| 161 |
+
return f"{concentration:.4f}"
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
# Firebase Configuration
|
| 165 |
+
cred = credentials.Certificate("snippetscript-37175-firebase-adminsdk-cf1z8-7d509b09fd.json")
|
| 166 |
+
firebase_admin.initialize_app(cred)
|
| 167 |
+
db = firestore.client()
|
| 168 |
+
|
| 169 |
+
# Function to send SMS using Twilio
|
| 170 |
+
def send_msg(field1,suggestion):
|
| 171 |
+
# if field;1
|
| 172 |
+
|
| 173 |
+
client = Client(ACCOUNT_SID, AUTH_TOKEN)
|
| 174 |
+
try:
|
| 175 |
+
message = client.messages.create(
|
| 176 |
+
from_=FROM_PHONE,
|
| 177 |
+
body=f"""Test Results:\nE.coli Level: {field1} CFU/mL.
|
| 178 |
+
Status: safe
|
| 179 |
+
Suggestions:\n
|
| 180 |
+
{suggestion}""",
|
| 181 |
+
to=TO_PHONE
|
| 182 |
+
)
|
| 183 |
+
print(f"Message sent successfully! SID: {message.sid}")
|
| 184 |
+
except Exception as e:
|
| 185 |
+
print(f"Failed to send message: {e}")
|
| 186 |
+
|
| 187 |
+
# Firestore Listener for new data in the 'thingspeak_data' collection
|
| 188 |
+
def on_snapshot(doc_snapshot, changes, read_time):
|
| 189 |
+
for change in changes:
|
| 190 |
+
if change.type.name == 'ADDED': # Detects new documents added to the collection
|
| 191 |
+
new_data = change.document.to_dict()
|
| 192 |
+
field1 = new_data.get('field1', 'N/A') # Replace 'field1' with your actual field key
|
| 193 |
+
print(f"New data detected: {new_data}")
|
| 194 |
+
concentration=predict_conc(field1)
|
| 195 |
+
if concentration==0:
|
| 196 |
+
send_msg(concentration,"The water is safe for use!!")
|
| 197 |
+
|
| 198 |
+
else:
|
| 199 |
+
send_msg(concentration,"You may boil the water to 50°C to eradicate the bacteria.")
|
| 200 |
+
|
| 201 |
+
# Trigger the Twilio SMS function
|
| 202 |
+
|
| 203 |
+
# Initialize Firestore Listener
|
| 204 |
+
def start_firestore_listener():
|
| 205 |
+
thingspeak_ref = db.collection('thingspeak_data')
|
| 206 |
+
thingspeak_watch = thingspeak_ref.on_snapshot(on_snapshot)
|
| 207 |
+
print("Listening for new data in Firestore...")
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
|
| 212 |
if __name__ == '__main__':
|
| 213 |
app.run(host='0.0.0.0', port=7860, debug=True)
|