Utiric commited on
Commit
4e5636a
·
verified ·
1 Parent(s): 36026df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -19
app.py CHANGED
@@ -9,7 +9,6 @@ from collections import defaultdict, deque
9
  from detoxify import Detoxify
10
  import logging
11
 
12
- # Flask'in varsayılan logger'ını daha iyi kullanmak için yapılandırma
13
  logging.basicConfig(level=logging.INFO)
14
 
15
  app = Flask(__name__, static_folder='static', template_folder='templates')
@@ -20,7 +19,6 @@ print("Model loaded successfully.")
20
 
21
  API_KEY = os.getenv('API_KEY', 'your-api-key-here')
22
 
23
- # --- Geliştirilmiş Metrik Takip Sistemi ---
24
  request_durations = deque(maxlen=100)
25
  request_timestamps = deque(maxlen=1000)
26
 
@@ -54,11 +52,7 @@ def track_request_metrics(start_time, tokens_count):
54
  end_time = time.time()
55
  duration = end_time - start_time
56
 
57
- # --- İSTEK ÜZERİNE GÜNCELLENEN KISIM ---
58
- # Sunucu taraflı işlem süresini milisaniye olarak terminale logla.
59
- # Bu log, arayüzdeki metriklerle tutarlı olacaktır.
60
  app.logger.info(f"Server-side processing for moderation request took {duration * 1000:.2f} ms.")
61
- # ------------------------------------------
62
 
63
  request_durations.append(duration)
64
  request_timestamps.append(datetime.now())
@@ -187,9 +181,7 @@ def moderations():
187
  response = jsonify({"error": "An internal server error occurred."}), 500
188
  return response
189
  finally:
190
- # Bu blok her zaman çalışır, response döndürülmeden hemen önce
191
  if response and response.status_code < 400:
192
- # Sadece başarılı istekleri metrikler için takip et
193
  track_request_metrics(start_time, total_tokens)
194
 
195
  with concurrent_requests_lock:
@@ -208,7 +200,6 @@ def metrics():
208
  return jsonify(get_performance_metrics())
209
 
210
  def create_directories_and_files():
211
- # Bu fonksiyon HTML/CSS içeriği değişmediği için aynı kalabilir.
212
  os.makedirs('templates', exist_ok=True)
213
  os.makedirs('static', exist_ok=True)
214
 
@@ -282,7 +273,6 @@ def create_directories_and_files():
282
  </h2>
283
 
284
  <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
285
- <!-- METRIC CARD 1: AVG RESPONSE TIME -->
286
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
287
  <div class="flex items-center justify-between">
288
  <div>
@@ -295,7 +285,6 @@ def create_directories_and_files():
295
  </div>
296
  </div>
297
 
298
- <!-- METRIC CARD 2: REQUESTS PER MINUTE -->
299
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
300
  <div class="flex items-center justify-between">
301
  <div>
@@ -308,7 +297,6 @@ def create_directories_and_files():
308
  </div>
309
  </div>
310
 
311
- <!-- METRIC CARD 3: PEAK RESPONSE TIME -->
312
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
313
  <div class="flex items-center justify-between">
314
  <div>
@@ -321,7 +309,6 @@ def create_directories_and_files():
321
  </div>
322
  </div>
323
 
324
- <!-- METRIC CARD 4: TODAY'S REQUESTS -->
325
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
326
  <div class="flex items-center justify-between">
327
  <div>
@@ -343,7 +330,6 @@ def create_directories_and_files():
343
  </div>
344
  </section>
345
 
346
- <!-- API Tester Section -->
347
  <section class="mb-12">
348
  <h2 class="text-2xl font-bold mb-6 flex items-center">
349
  <i class="fas fa-code mr-3 text-primary-600"></i>
@@ -389,7 +375,6 @@ def create_directories_and_files():
389
  </div>
390
  </section>
391
 
392
- <!-- Results Section -->
393
  <section id="resultsSection" class="hidden">
394
  <h2 class="text-2xl font-bold mb-6 flex items-center">
395
  <i class="fas fa-clipboard-check mr-3 text-primary-600"></i>
@@ -409,7 +394,6 @@ def create_directories_and_files():
409
  </div>
410
  </section>
411
 
412
- <!-- API Documentation -->
413
  <section>
414
  <h2 class="text-2xl font-bold mb-6 flex items-center">
415
  <i class="fas fa-book mr-3 text-primary-600"></i>
@@ -457,7 +441,6 @@ def create_directories_and_files():
457
  </footer>
458
 
459
  <script>
460
- // JS kodu değişmediği için aynı kalabilir.
461
  const darkModeToggle = document.getElementById('darkModeToggle');
462
  const html = document.documentElement;
463
 
@@ -506,7 +489,6 @@ def create_directories_and_files():
506
  const error = await response.json();
507
  console.error('Failed to fetch metrics:', error.error);
508
  if(response.status === 401) {
509
- // Maybe show a small warning that API key is needed for metrics
510
  }
511
  return;
512
  }
@@ -601,7 +583,7 @@ def create_directories_and_files():
601
  if (!response.ok) throw new Error(data.error || 'Failed to analyze text');
602
 
603
  displayResults(data, responseTime, texts);
604
- fetchMetrics(); // Update metrics immediately after a successful request
605
 
606
  } catch (error) {
607
  alert('Error: ' + error.message);
 
9
  from detoxify import Detoxify
10
  import logging
11
 
 
12
  logging.basicConfig(level=logging.INFO)
13
 
14
  app = Flask(__name__, static_folder='static', template_folder='templates')
 
19
 
20
  API_KEY = os.getenv('API_KEY', 'your-api-key-here')
21
 
 
22
  request_durations = deque(maxlen=100)
23
  request_timestamps = deque(maxlen=1000)
24
 
 
52
  end_time = time.time()
53
  duration = end_time - start_time
54
 
 
 
 
55
  app.logger.info(f"Server-side processing for moderation request took {duration * 1000:.2f} ms.")
 
56
 
57
  request_durations.append(duration)
58
  request_timestamps.append(datetime.now())
 
181
  response = jsonify({"error": "An internal server error occurred."}), 500
182
  return response
183
  finally:
 
184
  if response and response.status_code < 400:
 
185
  track_request_metrics(start_time, total_tokens)
186
 
187
  with concurrent_requests_lock:
 
200
  return jsonify(get_performance_metrics())
201
 
202
  def create_directories_and_files():
 
203
  os.makedirs('templates', exist_ok=True)
204
  os.makedirs('static', exist_ok=True)
205
 
 
273
  </h2>
274
 
275
  <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
 
276
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
277
  <div class="flex items-center justify-between">
278
  <div>
 
285
  </div>
286
  </div>
287
 
 
288
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
289
  <div class="flex items-center justify-between">
290
  <div>
 
297
  </div>
298
  </div>
299
 
 
300
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
301
  <div class="flex items-center justify-between">
302
  <div>
 
309
  </div>
310
  </div>
311
 
 
312
  <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
313
  <div class="flex items-center justify-between">
314
  <div>
 
330
  </div>
331
  </section>
332
 
 
333
  <section class="mb-12">
334
  <h2 class="text-2xl font-bold mb-6 flex items-center">
335
  <i class="fas fa-code mr-3 text-primary-600"></i>
 
375
  </div>
376
  </section>
377
 
 
378
  <section id="resultsSection" class="hidden">
379
  <h2 class="text-2xl font-bold mb-6 flex items-center">
380
  <i class="fas fa-clipboard-check mr-3 text-primary-600"></i>
 
394
  </div>
395
  </section>
396
 
 
397
  <section>
398
  <h2 class="text-2xl font-bold mb-6 flex items-center">
399
  <i class="fas fa-book mr-3 text-primary-600"></i>
 
441
  </footer>
442
 
443
  <script>
 
444
  const darkModeToggle = document.getElementById('darkModeToggle');
445
  const html = document.documentElement;
446
 
 
489
  const error = await response.json();
490
  console.error('Failed to fetch metrics:', error.error);
491
  if(response.status === 401) {
 
492
  }
493
  return;
494
  }
 
583
  if (!response.ok) throw new Error(data.error || 'Failed to analyze text');
584
 
585
  displayResults(data, responseTime, texts);
586
+ fetchMetrics();
587
 
588
  } catch (error) {
589
  alert('Error: ' + error.message);