Emmanuel Frimpong Asante commited on
Commit
945f1a8
·
1 Parent(s): f865b4a

update space

Browse files
Files changed (3) hide show
  1. .idea/misc.xml +3 -0
  2. app.py +19 -3
  3. requirements.txt +3 -4
.idea/misc.xml CHANGED
@@ -4,4 +4,7 @@
4
  <option name="sdkName" value="Python 3.11 (Poultry_Diseases_Detector)" />
5
  </component>
6
  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (Generative_AI_with_poultry_disease_detection_system)" project-jdk-type="Python SDK" />
 
 
 
7
  </project>
 
4
  <option name="sdkName" value="Python 3.11 (Poultry_Diseases_Detector)" />
5
  </component>
6
  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (Generative_AI_with_poultry_disease_detection_system)" project-jdk-type="Python SDK" />
7
+ <component name="PythonCompatibilityInspectionAdvertiser">
8
+ <option name="version" value="3" />
9
+ </component>
10
  </project>
app.py CHANGED
@@ -7,14 +7,14 @@ from pymongo import MongoClient, errors
7
  from datetime import datetime
8
  from werkzeug.security import generate_password_hash
9
  from services.utils import PoultryFarmBot
10
- from services.chatbot import build_chatbot_interface
11
  from transformers import AutoModelForCausalLM, AutoTokenizer
12
  from flask import Flask, render_template, request, redirect, url_for, session
13
  from celery import Celery
14
  import time
15
  import threading
16
  import gunicorn.app.base
17
- from gunicorn.six import iteritems
18
 
19
  # Setup logging for better monitoring
20
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -48,12 +48,14 @@ for attempt in range(max_retries):
48
  enquiries_collection = db.enquiries # Collection to store farmer enquiries
49
  users_collection = db.users # Collection to store user credentials
50
  logs_collection = db.logs # Collection to store application logs
 
51
  break
52
  except errors.ServerSelectionTimeoutError as e:
53
  logger.error(f"Failed to connect to MongoDB (Attempt {attempt + 1}/{max_retries}): {e}")
54
  if attempt < max_retries - 1:
55
  time.sleep(5) # Wait for 5 seconds before retrying
56
  else:
 
57
  raise ConnectionError("Could not connect to MongoDB. Please check the MONGO_URI and ensure the database is running.")
58
 
59
  def log_to_db(level, message):
@@ -64,6 +66,7 @@ def log_to_db(level, message):
64
  "timestamp": datetime.utcnow()
65
  }
66
  logs_collection.insert_one(log_entry)
 
67
  except Exception as e:
68
  logger.error(f"Failed to log to database: {e}")
69
 
@@ -93,6 +96,7 @@ else:
93
  # Initialize the bot instance
94
  logger.info("Initializing PoultryFarmBot instance.")
95
  bot = PoultryFarmBot(db)
 
96
 
97
  # Global model and tokenizer variables
98
  model = None
@@ -131,10 +135,13 @@ def authenticate_user(username, password):
131
  Returns:
132
  bool: True if authentication is successful, False otherwise.
133
  """
 
134
  user = bot.authenticate_user(username, password)
135
  if user:
 
136
  return True, user
137
  else:
 
138
  return False, None
139
 
140
  # Registration function
@@ -149,6 +156,7 @@ def register_user(username, password):
149
  Returns:
150
  bool: True if registration is successful, False otherwise.
151
  """
 
152
  try:
153
  if users_collection.find_one({"username": username}):
154
  logger.warning("Username already exists: %s", username)
@@ -167,12 +175,14 @@ def login():
167
  if request.method == 'POST':
168
  username = request.form['username']
169
  password = request.form['password']
 
170
  success, user = authenticate_user(username, password)
171
  if success:
172
  logger.info("Authentication successful for user: %s", username)
173
  session['username'] = username
174
 
175
  # Start loading the model asynchronously after successful login
 
176
  load_model_and_tokenizer.apply_async()
177
 
178
  return redirect(url_for('chatbot'))
@@ -186,6 +196,7 @@ def register():
186
  if request.method == 'POST':
187
  username = request.form['username']
188
  password = request.form['password']
 
189
  success = register_user(username, password)
190
  if success:
191
  logger.info("Registration successful for user: %s", username)
@@ -198,14 +209,18 @@ def register():
198
  @app.route('/chatbot')
199
  def chatbot():
200
  if 'username' not in session:
 
201
  return redirect(url_for('login'))
202
  username = session['username']
203
 
204
  # Wait until the model is loaded with a timeout mechanism
 
205
  if not model_loading_event.wait(timeout=100): # Timeout after 100 seconds
 
206
  return "Model loading timed out. Please try again later."
207
 
208
  if model is None or tokenizer is None:
 
209
  return "Model failed to load. Please try again later."
210
 
211
  logger.info("Launching Gradio chatbot interface for user: %s", username)
@@ -230,6 +245,7 @@ class StandaloneApplication(gunicorn.app.base.BaseApplication):
230
  # Launch the Flask application using Gunicorn in a production-safe manner
231
  if __name__ == "__main__":
232
  try:
 
233
  options = {
234
  'bind': '%s:%s' % ('0.0.0.0', '5000'),
235
  'workers': 2, # Set number of worker processes
@@ -239,4 +255,4 @@ if __name__ == "__main__":
239
  StandaloneApplication(app, options).run()
240
  except Exception as e:
241
  logger.error(f"Failed to launch Flask server: {e}")
242
- raise RuntimeError("Could not launch the Flask server. Please check the application setup.")
 
7
  from datetime import datetime
8
  from werkzeug.security import generate_password_hash
9
  from services.utils import PoultryFarmBot
10
+ from services.chatbot_interface import build_chatbot_interface
11
  from transformers import AutoModelForCausalLM, AutoTokenizer
12
  from flask import Flask, render_template, request, redirect, url_for, session
13
  from celery import Celery
14
  import time
15
  import threading
16
  import gunicorn.app.base
17
+ from six import iteritems
18
 
19
  # Setup logging for better monitoring
20
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
48
  enquiries_collection = db.enquiries # Collection to store farmer enquiries
49
  users_collection = db.users # Collection to store user credentials
50
  logs_collection = db.logs # Collection to store application logs
51
+ logger.info("Successfully connected to MongoDB.")
52
  break
53
  except errors.ServerSelectionTimeoutError as e:
54
  logger.error(f"Failed to connect to MongoDB (Attempt {attempt + 1}/{max_retries}): {e}")
55
  if attempt < max_retries - 1:
56
  time.sleep(5) # Wait for 5 seconds before retrying
57
  else:
58
+ logger.critical("Could not connect to MongoDB after multiple attempts. Exiting application.")
59
  raise ConnectionError("Could not connect to MongoDB. Please check the MONGO_URI and ensure the database is running.")
60
 
61
  def log_to_db(level, message):
 
66
  "timestamp": datetime.utcnow()
67
  }
68
  logs_collection.insert_one(log_entry)
69
+ logger.debug(f"Logged to database: {log_entry}")
70
  except Exception as e:
71
  logger.error(f"Failed to log to database: {e}")
72
 
 
96
  # Initialize the bot instance
97
  logger.info("Initializing PoultryFarmBot instance.")
98
  bot = PoultryFarmBot(db)
99
+ logger.info("PoultryFarmBot instance initialized.")
100
 
101
  # Global model and tokenizer variables
102
  model = None
 
135
  Returns:
136
  bool: True if authentication is successful, False otherwise.
137
  """
138
+ logger.info(f"Authenticating user: {username}")
139
  user = bot.authenticate_user(username, password)
140
  if user:
141
+ logger.info(f"Authentication successful for user: {username}")
142
  return True, user
143
  else:
144
+ logger.warning(f"Authentication failed for user: {username}")
145
  return False, None
146
 
147
  # Registration function
 
156
  Returns:
157
  bool: True if registration is successful, False otherwise.
158
  """
159
+ logger.info(f"Registering user: {username}")
160
  try:
161
  if users_collection.find_one({"username": username}):
162
  logger.warning("Username already exists: %s", username)
 
175
  if request.method == 'POST':
176
  username = request.form['username']
177
  password = request.form['password']
178
+ logger.info(f"Login attempt for user: {username}")
179
  success, user = authenticate_user(username, password)
180
  if success:
181
  logger.info("Authentication successful for user: %s", username)
182
  session['username'] = username
183
 
184
  # Start loading the model asynchronously after successful login
185
+ logger.info("Starting model loading asynchronously.")
186
  load_model_and_tokenizer.apply_async()
187
 
188
  return redirect(url_for('chatbot'))
 
196
  if request.method == 'POST':
197
  username = request.form['username']
198
  password = request.form['password']
199
+ logger.info(f"Registration attempt for user: {username}")
200
  success = register_user(username, password)
201
  if success:
202
  logger.info("Registration successful for user: %s", username)
 
209
  @app.route('/chatbot')
210
  def chatbot():
211
  if 'username' not in session:
212
+ logger.warning("Access to chatbot attempted without login. Redirecting to login page.")
213
  return redirect(url_for('login'))
214
  username = session['username']
215
 
216
  # Wait until the model is loaded with a timeout mechanism
217
+ logger.info("Waiting for model to load.")
218
  if not model_loading_event.wait(timeout=100): # Timeout after 100 seconds
219
+ logger.error("Model loading timed out for user: %s", username)
220
  return "Model loading timed out. Please try again later."
221
 
222
  if model is None or tokenizer is None:
223
+ logger.error("Model failed to load for user: %s", username)
224
  return "Model failed to load. Please try again later."
225
 
226
  logger.info("Launching Gradio chatbot interface for user: %s", username)
 
245
  # Launch the Flask application using Gunicorn in a production-safe manner
246
  if __name__ == "__main__":
247
  try:
248
+ logger.info("Launching Flask server for AdminLTE authentication.")
249
  options = {
250
  'bind': '%s:%s' % ('0.0.0.0', '5000'),
251
  'workers': 2, # Set number of worker processes
 
255
  StandaloneApplication(app, options).run()
256
  except Exception as e:
257
  logger.error(f"Failed to launch Flask server: {e}")
258
+ raise RuntimeError("Could not launch the Flask server. Please check the application setup.")
requirements.txt CHANGED
@@ -4,6 +4,7 @@ keras==2.9.0
4
  torch
5
  torchvision
6
  numpy==1.24.3
 
7
  # Transformers and AI model handling
8
  transformers
9
  accelerate
@@ -11,16 +12,14 @@ accelerate
11
  # Image Processing Libraries
12
  opencv-python
13
 
14
-
15
  # Data Handling and Analysis
16
  pandas
17
  celery
18
  gunicorn
 
19
  # OpenAI Integration
20
  openai
21
- Werkzeug
22
  gradio
23
  flask
24
  pymongo
25
-
26
-
 
4
  torch
5
  torchvision
6
  numpy==1.24.3
7
+
8
  # Transformers and AI model handling
9
  transformers
10
  accelerate
 
12
  # Image Processing Libraries
13
  opencv-python
14
 
 
15
  # Data Handling and Analysis
16
  pandas
17
  celery
18
  gunicorn
19
+
20
  # OpenAI Integration
21
  openai
22
+ werkzeug
23
  gradio
24
  flask
25
  pymongo