Unmeshraj commited on
Commit
f5654fc
Β·
1 Parent(s): de5851b
Files changed (2) hide show
  1. app.py +23 -45
  2. model/dataset_cleaned.csv +2 -2
app.py CHANGED
@@ -11,76 +11,54 @@ import pandas as pd
11
  from flask import Flask, jsonify, request
12
  from flask_cors import CORS
13
  from sklearn.preprocessing import LabelEncoder
14
- from datasets import load_dataset as hf_load_dataset
15
 
16
- warnings.filterwarnings("ignore")
17
 
18
  app = Flask(__name__)
19
  CORS(app, resources={r"/api/*": {"origins": "*"}})
20
 
 
21
  # ============================================================================
22
- # Paths
23
- # ============================================================================
 
24
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
25
- MODEL_DIR = os.path.join(BASE_DIR, "model")
26
- DATASET_PATH = os.path.join(MODEL_DIR, "dataset_cleaned.csv")
 
 
 
27
 
28
  print(f"\nπŸ“‚ BASE_DIR: {BASE_DIR}")
29
- print(f"πŸ“‚ MODEL_DIR: {MODEL_DIR}")
30
  print(f"πŸ“Š DATASET_PATH: {DATASET_PATH}")
31
- print(f"βœ“ Model dir exists: {os.path.exists(MODEL_DIR)}")
32
- print(f"βœ“ Dataset exists (local): {os.path.exists(DATASET_PATH)}\n")
 
33
 
34
- # ============================================================================
35
- # Globals
36
- # ============================================================================
37
  df = None
38
  model1 = None
39
  model2 = None
40
  le = None
41
 
42
- # ============================================================================
43
- # Dataset loader (HF first, local fallback)
44
- # ============================================================================
45
- def load_crime_dataset():
46
  global df
47
-
48
- # ---- Attempt 1: Hugging Face dataset (LFS-safe) ----
49
- try:
50
- print("πŸš€ Attempting to load dataset via Hugging Face Datasets...")
51
- ds = hf_load_dataset(
52
- "Unmeshraj/opensight",
53
- data_files="model/dataset_cleaned.csv",
54
- split="train"
55
- )
56
-
57
- df = ds.to_pandas()
58
- print(f"βœ… HF dataset loaded: {len(df)} rows")
59
- print(f"πŸ“Š Columns: {list(df.columns)}")
60
- return True
61
-
62
- except Exception as hf_error:
63
- print("⚠️ HF dataset load failed, falling back to local CSV")
64
- print(hf_error)
65
-
66
- # ---- Attempt 2: Local CSV fallback ----
67
  try:
68
  if not os.path.exists(DATASET_PATH):
69
- print(f"❌ Local dataset not found at: {DATASET_PATH}")
70
  df = pd.DataFrame()
71
  return False
72
-
73
  df = pd.read_csv(DATASET_PATH)
74
- print(f"βœ… Local dataset loaded: {len(df)} rows")
75
- print(f"πŸ“Š Columns: {list(df.columns)}")
76
  return True
77
-
78
- except Exception as local_error:
79
- print("❌ Local dataset load failed")
80
  traceback.print_exc()
81
  df = pd.DataFrame()
82
  return False
83
-
84
  def load_models():
85
  """Load trained models with fallback options"""
86
  global model1, model2, le
@@ -499,5 +477,5 @@ if __name__ == '__main__':
499
  print("\n" + "="*60)
500
  print("βœ… Starting OpenSight API Server")
501
  print("="*60 + "\n")
502
- app.run(host="0.0.0.0",port=int(os.environ.get("PORT", 7860)),debug=False)
503
 
 
11
  from flask import Flask, jsonify, request
12
  from flask_cors import CORS
13
  from sklearn.preprocessing import LabelEncoder
 
14
 
15
+ warnings.filterwarnings('ignore')
16
 
17
  app = Flask(__name__)
18
  CORS(app, resources={r"/api/*": {"origins": "*"}})
19
 
20
+
21
  # ============================================================================
22
+ # FIX: Use raw strings or forward slashes for Windows paths
23
+ # ================================================
24
+ # Base directory = where app.py lives
25
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
26
+
27
+ # model/ is INSIDE the same folder as app.py
28
+ MODEL_DIR = os.path.join(BASE_DIR, 'model')
29
+
30
+ DATASET_PATH = os.path.join(MODEL_DIR, 'dataset_cleaned.csv')
31
 
32
  print(f"\nπŸ“‚ BASE_DIR: {BASE_DIR}")
 
33
  print(f"πŸ“Š DATASET_PATH: {DATASET_PATH}")
34
+ print(f"πŸ“‚ MODEL_DIR: {MODEL_DIR}")
35
+ print(f"βœ“ Dataset exists: {os.path.exists(DATASET_PATH)}")
36
+ print(f"βœ“ Model dir exists: {os.path.exists(MODEL_DIR)}\n")
37
 
 
 
 
38
  df = None
39
  model1 = None
40
  model2 = None
41
  le = None
42
 
43
+ def load_dataset():
44
+ """Load the crime dataset"""
 
 
45
  global df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  try:
47
  if not os.path.exists(DATASET_PATH):
48
+ print(f"❌ Dataset not found at: {DATASET_PATH}")
49
  df = pd.DataFrame()
50
  return False
51
+
52
  df = pd.read_csv(DATASET_PATH)
53
+ print(f"βœ… Dataset loaded: {len(df)} records")
54
+ print(f" Columns: {list(df.columns)}")
55
  return True
56
+ except Exception as e:
57
+ print(f"❌ Error loading dataset: {e}")
 
58
  traceback.print_exc()
59
  df = pd.DataFrame()
60
  return False
61
+
62
  def load_models():
63
  """Load trained models with fallback options"""
64
  global model1, model2, le
 
477
  print("\n" + "="*60)
478
  print("βœ… Starting OpenSight API Server")
479
  print("="*60 + "\n")
480
+ app.run(host="0.0.0.0",port=int(os.environ.get("PORT", 5000)),debug=False)
481
 
model/dataset_cleaned.csv CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:efbc3e8d31e67cc706d95f596e568ac2aa9ee1a0f8f623be43d0308ad3566630
3
- size 41960106
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:98fa23c026bddae6fd52b1c5f2ce89865f0b54e096b71ea7e7eb81f80f84c516
3
+ size 41959917