okok
Browse filesFiles changed (2)
- app.py+23-45
- model/dataset_cleaned.csv+2-2
10 unmodified lines11121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586412 unmodified lines49950050150250310 unmodified linesfrom flask import Flask, jsonify, requestfrom flask_cors import CORSfrom sklearn.preprocessing import LabelEncoderfrom datasets import load_dataset as hf_load_datasetwarnings.filterwarnings("ignore")app = Flask(__name__)CORS(app, resources={r"/api/*": {"origins": "*"}})# ============================================================================# Paths# ============================================================================BASE_DIR = os.path.dirname(os.path.abspath(__file__))MODEL_DIR = os.path.join(BASE_DIR, "model")DATASET_PATH = os.path.join(MODEL_DIR, "dataset_cleaned.csv")print(f"\nπ BASE_DIR: {BASE_DIR}")print(f"π MODEL_DIR: {MODEL_DIR}")print(f"π DATASET_PATH: {DATASET_PATH}")print(f"β Model dir exists: {os.path.exists(MODEL_DIR)}")print(f"β Dataset exists (local): {os.path.exists(DATASET_PATH)}\n")# ============================================================================# Globals# ============================================================================df = Nonemodel1 = Nonemodel2 = Nonele = None# ============================================================================# Dataset loader (HF first, local fallback)# ============================================================================def load_crime_dataset():global df# ---- Attempt 1: Hugging Face dataset (LFS-safe) ----try:print("π Attempting to load dataset via Hugging Face Datasets...")ds = hf_load_dataset("Unmeshraj/opensight",data_files="model/dataset_cleaned.csv",split="train")df = ds.to_pandas()print(f"β HF dataset loaded: {len(df)} rows")print(f"π Columns: {list(df.columns)}")return Trueexcept Exception as hf_error:print("β οΈ HF dataset load failed, falling back to local CSV")print(hf_error)# ---- Attempt 2: Local CSV fallback ----try:if not os.path.exists(DATASET_PATH):print(f"β Local dataset not found at: {DATASET_PATH}")df = pd.DataFrame()return Falsedf = pd.read_csv(DATASET_PATH)print(f"β Local dataset loaded: {len(df)} rows")print(f"π Columns: {list(df.columns)}")return Trueexcept Exception as local_error:print("β Local dataset load failed")traceback.print_exc()df = pd.DataFrame()return Falsedef load_models():"""Load trained models with fallback options"""global model1, model2, le412 unmodified linesprint("\n" + "="*60)print("β Starting OpenSight API Server")print("="*60 + "\n")app.run(host="0.0.0.0",port=int(os.environ.get("PORT", 7860)),debug=False)10 unmodified lines111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364412 unmodified lines47747847948048110 unmodified linesfrom flask import Flask, jsonify, requestfrom flask_cors import CORSfrom sklearn.preprocessing import LabelEncoderwarnings.filterwarnings('ignore')app = Flask(__name__)CORS(app, resources={r"/api/*": {"origins": "*"}})# ============================================================================# FIX: Use raw strings or forward slashes for Windows paths# ================================================# Base directory = where app.py livesBASE_DIR = os.path.dirname(os.path.abspath(__file__))# model/ is INSIDE the same folder as app.pyMODEL_DIR = os.path.join(BASE_DIR, 'model')DATASET_PATH = os.path.join(MODEL_DIR, 'dataset_cleaned.csv')print(f"\nπ BASE_DIR: {BASE_DIR}")print(f"π DATASET_PATH: {DATASET_PATH}")print(f"π MODEL_DIR: {MODEL_DIR}")print(f"β Dataset exists: {os.path.exists(DATASET_PATH)}")print(f"β Model dir exists: {os.path.exists(MODEL_DIR)}\n")df = Nonemodel1 = Nonemodel2 = Nonele = Nonedef load_dataset():"""Load the crime dataset"""global dftry:if not os.path.exists(DATASET_PATH):print(f"β Dataset not found at: {DATASET_PATH}")df = pd.DataFrame()return Falsedf = pd.read_csv(DATASET_PATH)print(f"β Dataset loaded: {len(df)} records")print(f" Columns: {list(df.columns)}")return Trueexcept Exception as e:print(f"β Error loading dataset: {e}")traceback.print_exc()df = pd.DataFrame()return Falsedef load_models():"""Load trained models with fallback options"""global model1, model2, le412 unmodified linesprint("\n" + "="*60)print("β Starting OpenSight API Server")print("="*60 + "\n")app.run(host="0.0.0.0",port=int(os.environ.get("PORT", 5000)),debug=False)
123version https://git-lfs.github.com/spec/v1oid sha256:efbc3e8d31e67cc706d95f596e568ac2aa9ee1a0f8f623be43d0308ad3566630size 41960106123version https://git-lfs.github.com/spec/v1oid sha256:98fa23c026bddae6fd52b1c5f2ce89865f0b54e096b71ea7e7eb81f80f84c516size 41959917
app.py+23-45
model/dataset_cleaned.csv+2-2