-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
167 lines (136 loc) · 4.93 KB
/
app.py
File metadata and controls
167 lines (136 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from flask import Flask, render_template, request, redirect, url_for, jsonify
import openai
import random
from datetime import datetime, timedelta
import numpy as np
from sklearn.linear_model import LinearRegression
import threading
import paho.mqtt.client as mqtt
app = Flask(__name__)
# OpenAI Setup
client = openai.OpenAI(
api_key=os.getenv("OPENAI_API_KEY")
)
chat_history = []
tank_history = []
current_tds = 0
# MQTT Setup
MQTT_BROKER = "192.168.106.124"
MQTT_PORT = 1883
MQTT_TOPIC = "sensor/tds"
mqtt_client = mqtt.Client()
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("✅ MQTT Connected Successfully")
client.subscribe(MQTT_TOPIC)
else:
print("❌ MQTT Connection Failed with rc=", rc)
def on_message(client, userdata, msg):
global current_tds
try:
current_tds = float(msg.payload.decode())
print(f"📡 Received TDS: {current_tds} ppm")
except:
print("Error decoding TDS value.")
def start_mqtt():
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.connect(MQTT_BROKER, MQTT_PORT, 60)
mqtt_client.loop_forever()
mqtt_thread = threading.Thread(target=start_mqtt)
mqtt_thread.daemon = True
mqtt_thread.start()
# Pre-fill tank history
current_time = datetime.now()
current_level = random.randint(60, 100)
for i in range(10):
tank_history.append((current_time - timedelta(minutes=5 * (9 - i)), current_level))
current_level = max(0, current_level - random.randint(1, 5))
def generate_sensor_data():
temperature = round(random.uniform(15, 35), 2)
tank_level = random.randint(0, 100)
return temperature, tank_level
def predict_tank_levels(history):
if len(history) < 5:
return None, None
times = np.array([(t - history[0][0]).total_seconds() / 3600 for t, _ in history]).reshape(-1, 1)
levels = np.array([lvl for _, lvl in history])
model = LinearRegression()
model.fit(times, levels)
future_hours = np.array([2, 4, 6]).reshape(-1, 1)
predicted_levels = model.predict(future_hours)
if model.coef_[0] >= 0:
empty_time = None
else:
empty_hours = -model.intercept_ / model.coef_[0]
empty_time = history[0][0] + timedelta(hours=empty_hours)
return predicted_levels, empty_time
def generate_usage_prediction():
times = np.array(range(0, 10)).reshape(-1, 1)
usage = 5 + np.random.rand(10) * 5 # random usage between 5-10 liters/hr
model = LinearRegression()
model.fit(times, usage)
future_times = np.array(range(10, 20)).reshape(-1, 1)
predictions = model.predict(future_times)
return predictions.tolist()
@app.route("/", methods=["GET", "POST"])
def index():
global chat_history, tank_history
if request.method == "POST":
user_input = request.form.get("message")
temperature, tank_level = generate_sensor_data()
timestamp = datetime.now()
tank_history.append((timestamp, tank_level))
if len(tank_history) > 20:
tank_history.pop(0)
# 🆕 SHORT, ON-POINT PROMPT
full_prompt = f"""
Sensor Readings:
- TDS: {current_tds} ppm
- Temperature: {temperature} °C
- Tank Level: {tank_level} %
User Question: {user_input}
Instructions:
Reply in 2-3 lines maximum.
Directly answer whether the water is safe or unsafe.
Mention TDS, temperature, and tank level briefly.
No long explanations. Be clear, short, and on-point like a technician.
"""
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful, technical water management assistant."},
{"role": "user", "content": full_prompt}
],
temperature=0.5,
)
bot_message = response.choices[0].message.content.strip()
except Exception as e:
bot_message = "Error fetching response."
chat_history.append({
"sender": "user",
"text": user_input,
"time": datetime.now().strftime("%I:%M %p")
})
chat_history.append({
"sender": "bot",
"text": bot_message,
"time": datetime.now().strftime("%I:%M %p")
})
return redirect(url_for('index'))
else:
temperature, tank_level = generate_sensor_data()
timestamp = datetime.now()
tank_history.append((timestamp, tank_level))
if len(tank_history) > 20:
tank_history.pop(0)
predicted_levels, empty_time = predict_tank_levels(tank_history)
usage_predictions = generate_usage_prediction()
return render_template("index.html", chat_history=chat_history, predicted_levels=predicted_levels, empty_time=empty_time, usage_predictions=usage_predictions)
@app.route("/latest-tds")
def latest_tds():
global current_tds
return jsonify({"tds": current_tds})
if __name__ == "__main__":
app.run(debug=True, port=5000)