dgith commited on
Commit
485e0ad
·
verified ·
1 Parent(s): 28114ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -1,9 +1,6 @@
1
  """
2
- SEB AI Monitoring System - Hugging Face Space Implementation
3
- Optimized for Hugging Face Free Tier - FIXED VERSION
4
-
5
- Business Value: Demonstrates real-time detection of unauthorized AI access during exams
6
- with minimal resource usage, perfect for the free tier constraints.
7
  """
8
 
9
  import os
@@ -22,10 +19,10 @@ import warnings
22
  warnings.filterwarnings('ignore')
23
 
24
  # ===========================
25
- # MODEL HANDLING
26
  # ===========================
27
  class SEBMonitorModel:
28
- """Encapsulates the model and prediction logic"""
29
 
30
  def __init__(self):
31
  self.scaler = None
@@ -135,6 +132,7 @@ class SEBMonitorModel:
135
  """Makes prediction using the ensemble with verification."""
136
  try:
137
  # Ensure sample is a numpy array with correct shape (1, 5)
 
138
  sample_array = np.array(sample, dtype=float).reshape(1, -1)
139
 
140
  # Scale the sample
@@ -168,7 +166,7 @@ class SEBMonitorModel:
168
  # Return safe defaults on error
169
  return 0, 0.0, [0, 0, 0]
170
 
171
- # Initialize the model globally
172
  print("Initializing SEB Monitor Model...")
173
  model = SEBMonitorModel()
174
 
@@ -180,6 +178,22 @@ def analyze_behavior(app_switches, network_requests, keystrokes, mouse_movements
180
  try:
181
  start_time = time.time()
182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  # Prepare input data
184
  current_data = [
185
  float(app_switches),
@@ -511,7 +525,7 @@ def create_interface():
511
 
512
  ---
513
 
514
- *Built with ❤️ for Academic Integrity by SENTA team*
515
  """)
516
 
517
  # Connect the analyze button to the function
 
1
  """
2
+ SEB AI Monitoring System - Hugging Face Space Implementation (FINAL FIXED VERSION)
3
+ Optimized for Hugging Face Free Tier with proper error handling and persistence
 
 
 
4
  """
5
 
6
  import os
 
19
  warnings.filterwarnings('ignore')
20
 
21
  # ===========================
22
+ # MODEL HANDLING WITH PERSISTENCE
23
  # ===========================
24
  class SEBMonitorModel:
25
+ """Encapsulates the model and prediction logic with proper persistence"""
26
 
27
  def __init__(self):
28
  self.scaler = None
 
132
  """Makes prediction using the ensemble with verification."""
133
  try:
134
  # Ensure sample is a numpy array with correct shape (1, 5)
135
+ # Convert to float and handle any string inputs
136
  sample_array = np.array(sample, dtype=float).reshape(1, -1)
137
 
138
  # Scale the sample
 
166
  # Return safe defaults on error
167
  return 0, 0.0, [0, 0, 0]
168
 
169
+ # Global model instance
170
  print("Initializing SEB Monitor Model...")
171
  model = SEBMonitorModel()
172
 
 
178
  try:
179
  start_time = time.time()
180
 
181
+ # Validate input ranges
182
+ valid_ranges = {
183
+ 'app_switches': (0, 15),
184
+ 'network_requests': (0, 10),
185
+ 'keystrokes': (0, 10),
186
+ 'mouse_movements': (0, 10),
187
+ 'time_on_exam': (0, 60)
188
+ }
189
+
190
+ # Check if inputs are within valid ranges
191
+ for param, value in zip(['app_switches', 'network_requests', 'keystrokes', 'mouse_movements', 'time_on_exam'],
192
+ [app_switches, network_requests, keystrokes, mouse_movements, time_on_exam]):
193
+ min_val, max_val = valid_ranges[param]
194
+ if not (min_val <= value <= max_val):
195
+ raise ValueError(f"{param} out of valid range [{min_val}, {max_val}]")
196
+
197
  # Prepare input data
198
  current_data = [
199
  float(app_switches),
 
525
 
526
  ---
527
 
528
+ *Built with ❤️ by SENTA for Academic Integrity*
529
  """)
530
 
531
  # Connect the analyze button to the function