Beepeen24 commited on
Commit
2d69b13
·
verified ·
1 Parent(s): 2f712b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -12
app.py CHANGED
@@ -94,7 +94,13 @@ try:
94
  except Exception as e:
95
  print(f"⚠️ Could not patch Gradio client utils: {e}")
96
 
97
- MODEL_PATH = "fraud_lgbm_calibrated.pkl"
 
 
 
 
 
 
98
  model = None
99
 
100
  def load_model():
@@ -1450,35 +1456,64 @@ with gr.Blocks(
1450
 
1451
  # Function to use dataset file directly
1452
  def use_dataset_file():
1453
- dataset_path = "dataset/fraudTest.csv"
1454
- if os.path.exists(dataset_path):
1455
  # Return the file path as a string - the predict function will handle it
1456
- return dataset_path
1457
  else:
1458
  return None
1459
 
1460
  # When button is clicked, trigger prediction with dataset file
1461
  def use_dataset_and_predict(threshold_val):
1462
- dataset_path = "dataset/fraudTest.csv"
1463
- if os.path.exists(dataset_path):
 
 
 
1464
  # Call predict function directly with dataset path
1465
- return predict_fraud_enhanced(dataset_path, threshold_val)
1466
  else:
 
 
 
 
 
 
1467
  return (
1468
- "❌ Error: dataset/fraudTest.csv not found. Please upload a file instead.",
 
 
 
 
1469
  pd.DataFrame(),
1470
  None, None, None, None, None, None, None, None, None, None, None, None, None, None
1471
  )
1472
 
1473
  # When Sample button is clicked, use sample dataset
1474
  def use_sample_and_predict(threshold_val):
1475
- sample_path = "sample_transactions.csv"
1476
- if os.path.exists(sample_path):
 
 
 
 
 
1477
  # Call predict function directly with sample path
1478
- return predict_fraud_enhanced(sample_path, threshold_val)
1479
  else:
 
 
 
 
 
 
1480
  return (
1481
- "❌ Error: sample_transactions.csv not found. Please run generate_sample_dataset.py first.",
 
 
 
 
 
 
1482
  pd.DataFrame(),
1483
  None, None, None, None, None, None, None, None, None, None, None, None, None, None
1484
  )
 
94
  except Exception as e:
95
  print(f"⚠️ Could not patch Gradio client utils: {e}")
96
 
97
+ # Get the directory where this script is located
98
+ SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) if '__file__' in globals() else os.getcwd()
99
+
100
+ MODEL_PATH = os.path.join(SCRIPT_DIR, "fraud_lgbm_calibrated.pkl")
101
+ SAMPLE_DATASET_PATH = os.path.join(SCRIPT_DIR, "sample_transactions.csv")
102
+ DATASET_PATH = os.path.join(SCRIPT_DIR, "dataset", "fraudTest.csv")
103
+
104
  model = None
105
 
106
  def load_model():
 
1456
 
1457
  # Function to use dataset file directly
1458
  def use_dataset_file():
1459
+ if os.path.exists(DATASET_PATH):
 
1460
  # Return the file path as a string - the predict function will handle it
1461
+ return DATASET_PATH
1462
  else:
1463
  return None
1464
 
1465
  # When button is clicked, trigger prediction with dataset file
1466
  def use_dataset_and_predict(threshold_val):
1467
+ print(f"DEBUG: Looking for dataset at: {DATASET_PATH}")
1468
+ print(f"DEBUG: File exists: {os.path.exists(DATASET_PATH)}")
1469
+
1470
+ if os.path.exists(DATASET_PATH):
1471
+ print(f"DEBUG: Found dataset, calling predict_fraud_enhanced...")
1472
  # Call predict function directly with dataset path
1473
+ return predict_fraud_enhanced(DATASET_PATH, threshold_val)
1474
  else:
1475
+ # Try relative path as fallback
1476
+ fallback_path = "dataset/fraudTest.csv"
1477
+ if os.path.exists(fallback_path):
1478
+ print(f"DEBUG: Found dataset at relative path: {fallback_path}")
1479
+ return predict_fraud_enhanced(fallback_path, threshold_val)
1480
+
1481
  return (
1482
+ f"❌ Error: Dataset not found.\n\n"
1483
+ f"**Expected locations:**\n"
1484
+ f"- {DATASET_PATH}\n"
1485
+ f"- {fallback_path}\n\n"
1486
+ f"Please upload a file instead.",
1487
  pd.DataFrame(),
1488
  None, None, None, None, None, None, None, None, None, None, None, None, None, None
1489
  )
1490
 
1491
  # When Sample button is clicked, use sample dataset
1492
  def use_sample_and_predict(threshold_val):
1493
+ print(f"DEBUG: Looking for sample dataset at: {SAMPLE_DATASET_PATH}")
1494
+ print(f"DEBUG: File exists: {os.path.exists(SAMPLE_DATASET_PATH)}")
1495
+ print(f"DEBUG: Current working directory: {os.getcwd()}")
1496
+ print(f"DEBUG: Script directory: {SCRIPT_DIR}")
1497
+
1498
+ if os.path.exists(SAMPLE_DATASET_PATH):
1499
+ print(f"DEBUG: Found sample dataset, calling predict_fraud_enhanced...")
1500
  # Call predict function directly with sample path
1501
+ return predict_fraud_enhanced(SAMPLE_DATASET_PATH, threshold_val)
1502
  else:
1503
+ # Try relative path as fallback
1504
+ fallback_path = "sample_transactions.csv"
1505
+ if os.path.exists(fallback_path):
1506
+ print(f"DEBUG: Found sample dataset at relative path: {fallback_path}")
1507
+ return predict_fraud_enhanced(fallback_path, threshold_val)
1508
+
1509
  return (
1510
+ f"❌ Error: Sample dataset not found.\n\n"
1511
+ f"**Expected locations:**\n"
1512
+ f"- {SAMPLE_DATASET_PATH}\n"
1513
+ f"- {fallback_path}\n\n"
1514
+ f"**Current working directory:** {os.getcwd()}\n"
1515
+ f"**Script directory:** {SCRIPT_DIR}\n\n"
1516
+ f"Please ensure the sample_transactions.csv file exists in one of these locations.",
1517
  pd.DataFrame(),
1518
  None, None, None, None, None, None, None, None, None, None, None, None, None, None
1519
  )