Unmeshraj commited on
Commit
3a34ced
Β·
1 Parent(s): cfed788

update dockerfile

Browse files
Files changed (2) hide show
  1. Dockerfile +10 -6
  2. app.py +44 -21
Dockerfile CHANGED
@@ -2,24 +2,28 @@ FROM python:3.10-slim
2
 
3
  WORKDIR /app
4
 
5
- # Install system dependencies
6
  RUN apt-get update && apt-get install -y \
 
 
7
  build-essential \
 
8
  && rm -rf /var/lib/apt/lists/*
9
 
10
- # Copy requirements and install Python dependencies
11
  COPY requirements.txt .
12
  RUN pip install --no-cache-dir -r requirements.txt
13
 
14
- # Copy application
15
  COPY . .
16
 
17
- # Expose port
 
 
18
  EXPOSE 7860
19
 
20
- # Run Flask app
21
  ENV FLASK_APP=app.py
22
  ENV FLASK_ENV=production
23
  ENV BACKEND_PORT=7860
24
 
25
- CMD ["python", "app.py"]
 
2
 
3
  WORKDIR /app
4
 
5
+ # System deps + git-lfs
6
  RUN apt-get update && apt-get install -y \
7
+ git \
8
+ git-lfs \
9
  build-essential \
10
+ && git lfs install \
11
  && rm -rf /var/lib/apt/lists/*
12
 
13
+ # Copy requirements and install Python deps
14
  COPY requirements.txt .
15
  RUN pip install --no-cache-dir -r requirements.txt
16
 
17
+ # Copy app code (includes LFS pointers)
18
  COPY . .
19
 
20
+ # πŸ”΄ THIS IS THE IMPORTANT PART
21
+ RUN git lfs pull
22
+
23
  EXPOSE 7860
24
 
 
25
  ENV FLASK_APP=app.py
26
  ENV FLASK_ENV=production
27
  ENV BACKEND_PORT=7860
28
 
29
+ CMD ["python", "app.py"]
app.py CHANGED
@@ -11,53 +11,76 @@ import pandas as pd
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
- DATASET_PATH = os.path.join(MODEL_DIR, 'dataset_cleaned.csv')
30
 
31
  print(f"\nπŸ“‚ BASE_DIR: {BASE_DIR}")
32
- print(f"πŸ“Š DATASET_PATH: {DATASET_PATH}")
33
  print(f"πŸ“‚ MODEL_DIR: {MODEL_DIR}")
34
- print(f"βœ“ Dataset exists: {os.path.exists(DATASET_PATH)}")
35
- print(f"βœ“ Model dir exists: {os.path.exists(MODEL_DIR)}\n")
 
36
 
 
 
 
37
  df = None
38
  model1 = None
39
  model2 = None
40
  le = None
41
 
42
- def load_dataset():
43
- """Load the crime dataset"""
 
 
44
  global df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  try:
46
  if not os.path.exists(DATASET_PATH):
47
- print(f"❌ Dataset not found at: {DATASET_PATH}")
48
  df = pd.DataFrame()
49
  return False
50
-
51
  df = pd.read_csv(DATASET_PATH)
52
- print(f"βœ… Dataset loaded: {len(df)} records")
53
- print(f" Columns: {list(df.columns)}")
54
  return True
55
- except Exception as e:
56
- print(f"❌ Error loading dataset: {e}")
 
57
  traceback.print_exc()
58
  df = pd.DataFrame()
59
  return False
60
-
61
  def load_models():
62
  """Load trained models with fallback options"""
63
  global model1, model2, le
 
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