EmTpro01 commited on
Commit
c5a3183
·
verified ·
1 Parent(s): 7329861

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -2
app.py CHANGED
@@ -9,6 +9,7 @@ from dotenv import load_dotenv
9
  import logging
10
  import re
11
  from custom_prompt import get_custom_prompt
 
12
 
13
  # Configure logging
14
  logging.basicConfig(level=logging.INFO,
@@ -20,6 +21,9 @@ load_dotenv()
20
 
21
  app = Flask(__name__)
22
 
 
 
 
23
  # Initialize the environment - Check multiple possible env var names
24
  GOOGLE_API_KEY = (os.getenv("GOOGLE_API_KEY") or
25
  os.getenv("GEMINI_API_KEY") or
@@ -115,7 +119,7 @@ def format_links_as_html(text):
115
  return text
116
 
117
  # Regular URL pattern
118
- url_pattern = r'(https?://[^\s\]]+)'
119
 
120
  # Find all URLs in the text
121
  urls = re.findall(url_pattern, text)
@@ -170,11 +174,83 @@ def format_markdown_with_breaks(text):
170
  def home():
171
  return render_template('index.html')
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  @app.route('/_health')
174
  def health_check():
175
- """Health check endpoint for Docker"""
176
  return jsonify({"status": "healthy"}), 200
177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  @app.route('/api/chat', methods=['POST'])
179
  def chat():
180
  global qa_chain
 
9
  import logging
10
  import re
11
  from custom_prompt import get_custom_prompt
12
+ import time
13
 
14
  # Configure logging
15
  logging.basicConfig(level=logging.INFO,
 
21
 
22
  app = Flask(__name__)
23
 
24
+ # Store app start time for uptime calculation
25
+ app_start_time = time.time()
26
+
27
  # Initialize the environment - Check multiple possible env var names
28
  GOOGLE_API_KEY = (os.getenv("GOOGLE_API_KEY") or
29
  os.getenv("GEMINI_API_KEY") or
 
119
  return text
120
 
121
  # Regular URL pattern
122
+ url_pattern = r'(https?://[^\s\])+)'
123
 
124
  # Find all URLs in the text
125
  urls = re.findall(url_pattern, text)
 
174
  def home():
175
  return render_template('index.html')
176
 
177
+ @app.route('/health')
178
+ def health():
179
+ """Standard health check endpoint for uptime monitoring"""
180
+ try:
181
+ current_time = time.time()
182
+ uptime_seconds = current_time - app_start_time
183
+ uptime_hours = uptime_seconds / 3600
184
+
185
+ # Check if critical components are available
186
+ health_status = {
187
+ "status": "healthy",
188
+ "timestamp": time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime()),
189
+ "uptime_seconds": round(uptime_seconds, 2),
190
+ "uptime_hours": round(uptime_hours, 2),
191
+ "api_key_configured": bool(GOOGLE_API_KEY and GOOGLE_API_KEY != "your_api_key_here"),
192
+ "chatbot_initialized": qa_chain is not None
193
+ }
194
+
195
+ # Return 200 status for healthy
196
+ return jsonify(health_status), 200
197
+
198
+ except Exception as e:
199
+ logger.error(f"Health check failed: {str(e)}")
200
+ return jsonify({
201
+ "status": "unhealthy",
202
+ "error": str(e),
203
+ "timestamp": time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime())
204
+ }), 500
205
+
206
  @app.route('/_health')
207
  def health_check():
208
+ """Legacy health check endpoint for Docker (keeping for compatibility)"""
209
  return jsonify({"status": "healthy"}), 200
210
 
211
+ @app.route('/ping')
212
+ def ping():
213
+ """Simple ping endpoint for basic uptime checks"""
214
+ return "pong", 200
215
+
216
+ @app.route('/status')
217
+ def status():
218
+ """Detailed status endpoint"""
219
+ try:
220
+ current_time = time.time()
221
+ uptime_seconds = current_time - app_start_time
222
+
223
+ status_info = {
224
+ "application": "Flask Chatbot",
225
+ "status": "running",
226
+ "timestamp": time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime()),
227
+ "uptime": {
228
+ "seconds": round(uptime_seconds, 2),
229
+ "minutes": round(uptime_seconds / 60, 2),
230
+ "hours": round(uptime_seconds / 3600, 2),
231
+ "days": round(uptime_seconds / 86400, 2)
232
+ },
233
+ "environment": {
234
+ "python_version": os.sys.version,
235
+ "port": os.environ.get('PORT', 7860)
236
+ },
237
+ "services": {
238
+ "api_key_configured": bool(GOOGLE_API_KEY and GOOGLE_API_KEY != "your_api_key_here"),
239
+ "chatbot_initialized": qa_chain is not None,
240
+ "memory_initialized": memory is not None
241
+ }
242
+ }
243
+
244
+ return jsonify(status_info), 200
245
+
246
+ except Exception as e:
247
+ logger.error(f"Status check failed: {str(e)}")
248
+ return jsonify({
249
+ "status": "error",
250
+ "error": str(e),
251
+ "timestamp": time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime())
252
+ }), 500
253
+
254
  @app.route('/api/chat', methods=['POST'])
255
  def chat():
256
  global qa_chain