Neurolingua commited on
Commit
b87e94a
·
verified ·
1 Parent(s): 06a11c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -24
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  import time
3
  import requests
@@ -9,32 +11,25 @@ import pickle
9
  from firebase_admin import credentials, firestore
10
 
11
  # Twilio Configuration
12
- ACCOUNT_SID = 'AC175419b901537d8dcca5321386e58aa9'
13
  AUTH_TOKEN = 'e5f750f99a87e84ca5b87ddd0c421560'
14
  FROM_PHONE = '+13613093564'
15
  TO_PHONE = '+919080522395'
16
 
17
  # Firebase Configuration
18
- FIREBASE_CREDENTIALS_PATH = r"snippetscript-37175-firebase-adminsdk-cf1z8-7d509b09fd.json"
19
- THINGSPEAK_API_KEY = "P54KXM40TA3CB6W4"
20
- CHANNEL_ID = "2784385"
21
 
22
  # Global variable to track the last entry ID
23
  last_entry_id = None
24
- with open('model.pkl', 'rb') as f:
25
- loaded_model = pickle.load(f)
26
  def predict_conc(pdiff):
27
- try:
28
- if pdiff > 3.19:
29
- return 0
30
- pdiff_value = pdiff # Input for "Average Potential Difference"
31
- new_data = np.array([[pdiff_value]])
32
- prediction = loaded_model.predict(new_data)
33
- concentration = 10**prediction[0] # Reverse log transformation
34
- return f"{concentration:.4f}"
35
- except Exception as e:
36
- print(f"Error in predict_conc: {e}")
37
- return None
38
  # Initialize Firebase
39
  cred = credentials.Certificate(FIREBASE_CREDENTIALS_PATH)
40
  firebase_admin.initialize_app(cred)
@@ -47,7 +42,7 @@ app.secret_key = os.urandom(24) # For session management
47
  # Function to send SMS using Twilio
48
  def send_msg(doc_id, field1):
49
  try:
50
- field1 = str(field1)
51
  # Fetch the document with the given doc_id
52
  doc_ref = db.collection('thingspeak_data').document(doc_id)
53
  doc = doc_ref.get()
@@ -58,7 +53,7 @@ def send_msg(doc_id, field1):
58
 
59
  # Check if the message status is 0
60
  if msg_status == 0:
61
- concen=predict_conc(int(field1))
62
  client = Client(ACCOUNT_SID, AUTH_TOKEN)
63
  if concen!=0:
64
  sugges="You may heat your water to eradicate the bacteria."
@@ -93,11 +88,14 @@ def on_snapshot(doc_snapshot, changes, read_time):
93
  for change in changes:
94
  if change.type.name == 'ADDED':
95
  new_data = change.document.to_dict()
96
- print(f"New data detected: {new_data}") # Add this for more debugging info
97
  field1 = new_data.get('field1', 'N/A')
98
- print(f"Field1: {field1}") # Check the type and value of field1
99
- send_msg(change.document.id, field1)
100
-
 
 
 
101
 
102
  # Firestore Listener Initialization
103
  def start_firestore_listener():
@@ -213,7 +211,29 @@ def main():
213
  print("Process interrupted by user.")
214
  except Exception as e:
215
  print(f"Unexpected error: {e}")
 
 
 
216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  if __name__ == '__main__':
218
  from threading import Thread
219
 
@@ -223,4 +243,4 @@ if __name__ == '__main__':
223
 
224
  # Run Flask app in the main thread with debugging enabled
225
  app.run(host='0.0.0.0', port=7860, debug=True)
226
-
 
1
+ from mistralai import Mistral
2
+ import numpy as np
3
  import os
4
  import time
5
  import requests
 
11
  from firebase_admin import credentials, firestore
12
 
13
  # Twilio Configuration
14
+ ACCOUNT_SID = 'AC7f8c344c6593572a0c925ab4c1b66cc6'
15
  AUTH_TOKEN = 'e5f750f99a87e84ca5b87ddd0c421560'
16
  FROM_PHONE = '+13613093564'
17
  TO_PHONE = '+919080522395'
18
 
19
  # Firebase Configuration
20
+ FIREBASE_CREDENTIALS_PATH = r"shh/snippetscript-37175-firebase-adminsdk-cf1z8-7d509b09fd.json"
21
+ THINGSPEAK_API_KEY = "PIA8Z5BWH2DW6WLF"
22
+ CHANNEL_ID = "2785999"
23
 
24
  # Global variable to track the last entry ID
25
  last_entry_id = None
26
+ # with open(r'C:\Users\tiruv\Desktop\SIH\model.pkl', 'rb') as f:
27
+ # loaded_model = pickle.load(f)
28
  def predict_conc(pdiff):
29
+ # Reverse log transformation
30
+ return pdiff
31
+
32
+
 
 
 
 
 
 
 
33
  # Initialize Firebase
34
  cred = credentials.Certificate(FIREBASE_CREDENTIALS_PATH)
35
  firebase_admin.initialize_app(cred)
 
42
  # Function to send SMS using Twilio
43
  def send_msg(doc_id, field1):
44
  try:
45
+ field1 = (field1)
46
  # Fetch the document with the given doc_id
47
  doc_ref = db.collection('thingspeak_data').document(doc_id)
48
  doc = doc_ref.get()
 
53
 
54
  # Check if the message status is 0
55
  if msg_status == 0:
56
+ concen=predict_conc(field1)
57
  client = Client(ACCOUNT_SID, AUTH_TOKEN)
58
  if concen!=0:
59
  sugges="You may heat your water to eradicate the bacteria."
 
88
  for change in changes:
89
  if change.type.name == 'ADDED':
90
  new_data = change.document.to_dict()
91
+ print(f"New data detected: {new_data}") # Debugging info
92
  field1 = new_data.get('field1', 'N/A')
93
+ try:
94
+ field1 = float(field1) # Convert field1 to float
95
+ print(f"Field1 as float: {field1}")
96
+ send_msg(change.document.id, field1)
97
+ except ValueError:
98
+ print(f"Invalid field1 value: {field1}. Must be numeric.")
99
 
100
  # Firestore Listener Initialization
101
  def start_firestore_listener():
 
211
  print("Process interrupted by user.")
212
  except Exception as e:
213
  print(f"Unexpected error: {e}")
214
+ API_KEY = 'xQ2Zhfsp4cLar4lvBRDWZKljvp0Ej427'
215
+ MODEL = "mistral-large-latest"
216
+ client = Mistral(api_key=API_KEY)
217
 
218
+ @app.route('/chat', methods=['POST'])
219
+ def chat():
220
+ data = request.json
221
+ user_query = data.get("query", "")
222
+
223
+ if not user_query:
224
+ return jsonify({"error": "Query cannot be empty"}), 400
225
+
226
+ try:
227
+ chat_response = client.chat.complete(
228
+ model=MODEL,
229
+ messages=[
230
+ {"role": "user", "content": user_query+"You are the best assistant. Provide the precise response within 2 to 3 sentence.Greet the user if user greets.Your response must be rural area people."},
231
+ ]
232
+ )
233
+ response_content = chat_response.choices[0].message.content
234
+ return jsonify({"response": response_content})
235
+ except Exception as e:
236
+ return jsonify({"error": str(e)}), 500
237
  if __name__ == '__main__':
238
  from threading import Thread
239
 
 
243
 
244
  # Run Flask app in the main thread with debugging enabled
245
  app.run(host='0.0.0.0', port=7860, debug=True)
246
+