test
Browse filesFiles changed (2)
- BestModel.ptAdded+3
- app.py+603-20
123version https://git-lfs.github.com/spec/v1oid sha256:ee571cc7163696ad7b3b4e2ef470d0524e55601975241350659e37ee446c25c5size 40420314512345678910111213141516171819202122232425262728293031323334353637383940411 unmodified line4344454647481 unmodified line5051525354555657585960616263646566676869707172732 unmodified lines7677787980818283848586873 unmodified lines91929394959697from datetime import datetimefrom flask import Flask, jsonify, requestfrom flask_cors import CORS #type:ignoreapp = Flask(__name__)CORS(app)app.config["JSON_SORT_KEYS"] = Falsedef load_model():"""will load your ML model here later."""return NoneMODEL = load_model()def predict_hotspots(city: str, threshold: float):"""placeholder function to simulate hotspot predictions."""mock_data = {"bangalore": [{"lat": 12.9352, "lon": 77.6245, "risk": 0.85},{"lat": 12.9716, "lon": 77.5946, "risk": 0.72},{"lat": 13.0027, "lon": 77.5914, "risk": 0.61},],"delhi": [{"lat": 28.7041, "lon": 77.1025, "risk": 0.89},{"lat": 28.6328, "lon": 77.2197, "risk": 0.76},],}city_data = mock_data.get(city.lower(), mock_data["bangalore"])results = []for i, point in enumerate(city_data):if point["risk"] >= threshold:1 unmodified line"id": f"{city}-hotspot-{i}","latitude": point["lat"],"longitude": point["lon"],"riskLevel": ("high" if point["risk"] >= 0.75else "medium" if point["risk"] >= 0.61 unmodified line),"crimeCount": int(point["risk"] * 50) + 10,})return results@app.route("/api/health", methods=["GET"])def health():return jsonify({"status": "ok","timestamp": datetime.utcnow().isoformat()})@app.route("/api/hotspots", methods=["GET"])def get_hotspots():city = request.args.get("city", "bangalore")threshold = float(request.args.get("threshold", 0.5))hotspots = predict_hotspots(city, threshold)return jsonify({"city": city,"threshold": threshold,2 unmodified lines})@app.route("/api/predictions", methods=["GET"])def predictions():city = request.args.get("city", "bangalore")data = predict_hotspots(city, threshold=0.0)return jsonify({"city": city,"timestamp": datetime.utcnow().isoformat(),3 unmodified lines@app.errorhandler(404)def not_found(_):return jsonify({"error": "not found"}), 404if __name__ == "__main__":app.run(host="0.0.0.0", port=7860)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213221 unmodified line3243253263273283293301 unmodified line3323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165172 unmodified lines5205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516523 unmodified lines656657658659660661662663664665666667668669670671672673674675676677678679680# app.py - Enhanced Flask API with all GUI.py featuresimport osimport sysfrom datetime import datetime, timedeltafrom typing import Optional, Dict, List, Tupleimport numpy as npimport torchfrom flask import Flask, jsonify, requestfrom flask_cors import CORSapp = Flask(__name__)CORS(app)app.config["JSON_SORT_KEYS"] = False# Ensure we can import project modules in `code/`ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))if ROOT_DIR not in sys.path:sys.path.append(ROOT_DIR)# Import project modulestry:from code import configfrom code.DataPreprocessing import DataPreprocessingfrom code.LSTMModel import ConvLSTMModelfrom code.WeatherModel import WeatherModelfrom code.TimeseriesModel import TimeseriesModelexcept Exception as e:print(f"Warning: Could not import project modules: {e}")ConvLSTMModel = NoneDataPreprocessing = NoneWeatherModel = NoneTimeseriesModel = Noneconfig = None# Global variables for models and dataMODEL = NoneMODEL_DEVICE = "cpu"DATA_PREP = NoneWEATHER_MODEL = NoneTIMESERIES_MODEL = NoneFEATURES = NoneLABELS = NoneDATA_PIVOT = NoneCRIME_DATA = NoneNYC_SHAPE = Nonedef initialize_system():"""Initialize all models and data preprocessing.This mimics the caching behavior of GUI.py's @st.cache decorators."""global MODEL, MODEL_DEVICE, DATA_PREP, WEATHER_MODEL, TIMESERIES_MODELglobal FEATURES, LABELS, DATA_PIVOT, CRIME_DATA, NYC_SHAPEif config is None or ConvLSTMModel is None:print("Warning: Running in mock mode - models not available")return Falsetry:# Load NYC Shape (for filtering grids not on map)print("Loading NYC Shape...")nyc_shape_path = os.path.join(config.PROJECT_DIR, 'Data/PreprocessedDatasets/NYCGridsShape.pkl')if os.path.isfile(nyc_shape_path):import picklewith open(nyc_shape_path, 'rb') as file:NYC_SHAPE = pickle.load(file)else:NYC_SHAPE = [] # Empty for custom datasets like Bengaluru# Load Datasetprint("Loading Dataset...")DATA_PREP = DataPreprocessing(config.PROJECT_DIR)FEATURES = DATA_PREP.featuresLABELS = DATA_PREP.labelsDATA_PIVOT = DATA_PREP.dataPivotCRIME_DATA = DATA_PREP.data# Load ConvLSTM Modelprint("Loading ConvLSTM Model...")model_path = 'BestModel.pt'if os.path.isfile(model_path):checkpoint = torch.load(model_path, map_location=torch.device(config.DEVICE))MODEL_DEVICE = config.DEVICEMODEL = ConvLSTMModel(input_dim=config.CRIME_TYPE_NUM,hidden_dim=config.HIDDEN_DIM,kernel_size=config.KERNEL_SIZE,bias=True)state = checkpoint.get("model") if isinstance(checkpoint, dict) else checkpointMODEL.load_state_dict(state)MODEL.to(torch.device(MODEL_DEVICE))MODEL.eval()# Load Weather Modelprint("Loading Weather Model...")WEATHER_MODEL = WeatherModel(config.PROJECT_DIR)# Load Timeseries Modelprint("Loading Timeseries Model...")TIMESERIES_MODEL = TimeseriesModel(config.PROJECT_DIR, CRIME_DATA)print("System initialization complete!")return Trueexcept Exception as e:print(f"Error during initialization: {e}")import tracebacktraceback.print_exc()return Falsedef get_date_range():"""Get valid date range for predictions."""if config is None:return None, Nonestart_date = datetime.strptime(config.START_SELECT_DATE[1:-1], '%Y-%m-%d')end_date = datetime.strptime(config.END_DATE[1:-1], '%Y-%m-%d')return start_date, end_datedef validate_date(date_str: str) -> Tuple[bool, Optional[datetime], Optional[str]]:"""Validate if a date is within the valid prediction range.Returns: (is_valid, datetime_object, error_message)"""if config is None:return False, None, "Configuration not available"try:dt = datetime.strptime(date_str, '%Y-%m-%d')except ValueError:return False, None, "Invalid date format. Use YYYY-MM-DD"minus_days = config.SEQ_LEN + 1start_date = datetime.strptime(config.START_DATE[1:-1], '%Y-%m-%d')left_limit = start_date + timedelta(days=minus_days)right_limit = datetime.strptime(config.END_DATE[1:-1], '%Y-%m-%d')if dt <= left_limit:return False, None, f"Date must be after {left_limit.strftime('%Y-%m-%d')}"elif dt > right_limit:return False, None, f"Date must be before or on {right_limit.strftime('%Y-%m-%d')}"return True, dt, Nonedef get_prediction_data_by_date(date: str,crime_type_index: int,use_temporal_factors: bool = True) -> Optional[Dict]:"""Get predictions for a specific date and crime type.This replicates the getPredDataByDate function from GUI.py.Args:date: Date string in format 'YYYY-MM-DD'crime_type_index: Index of crime type (0-7)use_temporal_factors: Whether to apply weather and timeseries factorsReturns:Dictionary with prediction data or None on error"""if MODEL is None or DATA_PIVOT is None or FEATURES is None:return None# Validate dateis_valid, dt, error = validate_date(date)if not is_valid:return {"error": error}# Determine start indexminus_days = config.SEQ_LEN + 1if DATA_PIVOT.query(f"date < {config.START_DATE}").shape[0] == 0:start_index = 0else:start_index = int(DATA_PIVOT.query(f"date < {config.START_DATE}").shape[0] / config.CRIME_TYPE_NUM - minus_days)# Get feature index for the given datedate_query = f"'{date}'"found_index = int(DATA_PIVOT.query(f"date < {date_query}").shape[0] / config.CRIME_TYPE_NUM - minus_days) - start_indexif found_index < 0 or found_index >= len(FEATURES):return {"error": "Date index out of range"}# Get features and labelsfeatures_by_date = FEATURES[found_index]labels_by_date = LABELS[found_index] if LABELS is not None else None# Run prediction through ConvLSTMprocessed_features = torch.from_numpy(features_by_date).to(MODEL_DEVICE).unsqueeze(0).float()with torch.no_grad():pred_data = MODEL(processed_features)[0][0]# Get temporal factorsweather_factor = 1.0timeseries_factors = [1.0] * config.CRIME_TYPE_NUMif use_temporal_factors and WEATHER_MODEL is not None and TIMESERIES_MODEL is not None:try:weather_factor = WEATHER_MODEL.getWeatherFactor(date)crime_types = [crime.lower() for crime in config.CRIME_TYPE]timeseries_factors = [TIMESERIES_MODEL.getTimeseriesFactor(crime_name, date)for crime_name in crime_types]except Exception as e:print(f"Warning: Could not get temporal factors: {e}")return {"date": date,"crime_type_index": crime_type_index,"prediction": pred_data.cpu().numpy(),"labels": labels_by_date if labels_by_date is not None else None,"weather_factor": weather_factor,"timeseries_factors": timeseries_factors,"use_temporal_factors": use_temporal_factors}def get_hexagon_data(pred_data: np.ndarray,weather_factor: float,timeseries_factors: List[float],crime_type_index: int,threshold: float,use_temporal_factors: bool = True) -> List[Dict]:"""Convert prediction data to hotspot list with lat/lon coordinates.This replicates the getHexagonData function from GUI.py.Args:pred_data: Prediction array from modelweather_factor: Weather adjustment factortimeseries_factors: Timeseries adjustment factors per crime typecrime_type_index: Which crime type to extractthreshold: Minimum probability thresholduse_temporal_factors: Whether to apply temporal adjustmentsReturns:List of hotspot dictionaries"""if NYC_SHAPE is None or config is None:return []hotspots = []hotspot_id = 0for x in range(pred_data.shape[1]):for y in range(pred_data.shape[2]):# Skip grids not on the map (NYC shape filtering)if (((x, y) in NYC_SHAPE or (x+1, y) in NYC_SHAPE or(x, y+1) in NYC_SHAPE or (x+1, y+1) in NYC_SHAPE) and(x < config.LAT_GRIDS - 1 and y < config.LON_GRIDS - 1)):continueif x >= config.LAT_GRIDS - 1 or y >= config.LON_GRIDS - 1:continue# Get base weight from predictionweight = float(pred_data[crime_type_index][x][y])# Apply temporal factors if enabledif use_temporal_factors:weight = weight * weather_factor * timeseries_factors[crime_type_index]# Apply threshold multiplier for values below thresholdif weight < threshold:weight = weight * config.MULTIPLY_FACTOR# Calculate lat/lon for this grid celllat = config.LAT_BINS[x] + config.DIFF_LATlon = config.LON_BINS[y] + config.DIFF_LON# Only include if above absolute minimumif weight > 0.01:risk_level = "high" if weight >= 0.75 else "medium" if weight >= 0.6 else "low"hotspots.append({"id": f"hotspot-{hotspot_id}","latitude": float(lat),"longitude": float(lon),"risk": float(weight),"riskLevel": risk_level,"crimeCount": int(weight * 50) + 10,})hotspot_id += 1return hotspotsdef get_mock_hotspots(city: str, threshold: float) -> List[Dict]:"""Fallback mock data when models are not available."""mock_data = {"bangalore": [{"lat": 12.9352, "lon": 77.6245, "risk": 0.85},{"lat": 12.9716, "lon": 77.5946, "risk": 0.72},{"lat": 13.0027, "lon": 77.5914, "risk": 0.61},{"lat": 12.9141, "lon": 77.6411, "risk": 0.78},{"lat": 12.9698, "lon": 77.6489, "risk": 0.65},],"delhi": [{"lat": 28.7041, "lon": 77.1025, "risk": 0.89},{"lat": 28.6328, "lon": 77.2197, "risk": 0.76},{"lat": 28.5355, "lon": 77.3910, "risk": 0.68},],}city_data = mock_data.get(city.lower(), mock_data["bangalore"])results = []for i, point in enumerate(city_data):if point["risk"] >= threshold:1 unmodified line"id": f"{city}-hotspot-{i}","latitude": point["lat"],"longitude": point["lon"],"risk": point["risk"],"riskLevel": ("high" if point["risk"] >= 0.75else "medium" if point["risk"] >= 0.61 unmodified line),"crimeCount": int(point["risk"] * 50) + 10,})return results# ==================== API ENDPOINTS ====================@app.route("/api/health", methods=["GET"])def health():"""Health check endpoint."""models_loaded = MODEL is not None and DATA_PREP is not Nonereturn jsonify({"status": "ok","timestamp": datetime.utcnow().isoformat(),"models_loaded": models_loaded,"weather_model": WEATHER_MODEL is not None,"timeseries_model": TIMESERIES_MODEL is not None,})@app.route("/api/info", methods=["GET"])def info():"""Get system information and available date range."""if config is None:return jsonify({"error": "Configuration not available"}), 500start_date, end_date = get_date_range()crime_types = [crime.lower() for crime in config.CRIME_TYPE]return jsonify({"date_range": {"start": start_date.strftime('%Y-%m-%d') if start_date else None,"end": end_date.strftime('%Y-%m-%d') if end_date else None,},"crime_types": crime_types,"grid_info": {"lat_min": float(config.LAT_MIN),"lat_max": float(config.LAT_MAX),"lon_min": float(config.LON_MIN),"lon_max": float(config.LON_MAX),"lat_grids": config.LAT_GRIDS,"lon_grids": config.LON_GRIDS,},"model_info": {"seq_len": config.SEQ_LEN,"hidden_dim": config.HIDDEN_DIM,"kernel_size": config.KERNEL_SIZE,}})@app.route("/api/crime-types", methods=["GET"])def crime_types():"""Get list of available crime types."""if config is None:return jsonify({"error": "Configuration not available"}), 500crime_types_list = [crime.lower() for crime in config.CRIME_TYPE]return jsonify({"crime_types": crime_types_list,"count": len(crime_types_list)})@app.route("/api/predict", methods=["POST"])def predict():"""Advanced prediction endpoint with full temporal factors.Request body:{"date": "2024-01-15","crime_type": "theft", // or crime_type_index"threshold": 0.5,"use_temporal_factors": true}"""if MODEL is None or DATA_PREP is None:# Fallback to mock datadata = request.get_json() or {}city = data.get("city", "bangalore")threshold = float(data.get("threshold", 0.5))return jsonify({"city": city,"threshold": threshold,"hotspots": get_mock_hotspots(city, threshold),"mock": True})try:data = request.get_json() or {}# Parse parametersdate = data.get("date")if not date:return jsonify({"error": "Date parameter required"}), 400# Get crime type indexcrime_type = data.get("crime_type")crime_type_index = data.get("crime_type_index")if crime_type is not None:crime_types = [crime.lower() for crime in config.CRIME_TYPE]try:crime_type_index = crime_types.index(crime_type.lower())except ValueError:return jsonify({"error": f"Invalid crime type. Available: {crime_types}"}), 400elif crime_type_index is None:crime_type_index = 0 # Default to first crime typethreshold = float(data.get("threshold", 0.5))use_temporal_factors = data.get("use_temporal_factors", True)# Get prediction datapred_result = get_prediction_data_by_date(date=date,crime_type_index=crime_type_index,use_temporal_factors=use_temporal_factors)if pred_result is None or "error" in pred_result:return jsonify(pred_result or {"error": "Prediction failed"}), 400# Convert to hotspotshotspots = get_hexagon_data(pred_data=pred_result["prediction"],weather_factor=pred_result["weather_factor"],timeseries_factors=pred_result["timeseries_factors"],crime_type_index=crime_type_index,threshold=threshold,use_temporal_factors=use_temporal_factors)crime_types = [crime.lower() for crime in config.CRIME_TYPE]return jsonify({"date": date,"crime_type": crime_types[crime_type_index],"crime_type_index": crime_type_index,"threshold": threshold,"use_temporal_factors": use_temporal_factors,"temporal_factors": {"weather": float(pred_result["weather_factor"]),"timeseries": [float(f) for f in pred_result["timeseries_factors"]],},"count": len(hotspots),"hotspots": hotspots,})except Exception as e:import tracebacktraceback.print_exc()return jsonify({"error": str(e)}), 500@app.route("/api/hotspots", methods=["GET"])def get_hotspots():"""Simple hotspot endpoint (backward compatible).Query params: city, threshold"""city = request.args.get("city", "bangalore")threshold = float(request.args.get("threshold", 0.5))if MODEL is None:hotspots = get_mock_hotspots(city, threshold)else:# Use latest available datedate = datetime.strptime(config.END_DATE[1:-1], '%Y-%m-%d').strftime('%Y-%m-%d')pred_result = get_prediction_data_by_date(date=date, crime_type_index=0)if pred_result and "error" not in pred_result:hotspots = get_hexagon_data(pred_data=pred_result["prediction"],weather_factor=pred_result["weather_factor"],timeseries_factors=pred_result["timeseries_factors"],crime_type_index=0,threshold=threshold)else:hotspots = get_mock_hotspots(city, threshold)return jsonify({"city": city,"threshold": threshold,2 unmodified lines})@app.route("/api/cumulative", methods=["GET"])def cumulative_heatmap():"""Get cumulative crime data for heatmap visualization.This replicates the "Cumulative Heatmap (All Data)" mode from GUI.py.Query params:- crime_types: comma-separated list (optional, defaults to all)- lat_min, lat_max, lon_min, lon_max: bounding box (optional)"""if CRIME_DATA is None or config is None:return jsonify({"error": "Crime data not available"}), 500try:# Parse crime type filtercrime_types_param = request.args.get("crime_types")if crime_types_param:selected_types = [t.strip() for t in crime_types_param.split(",")]else:selected_types = CRIME_DATA['TYPE'].unique().tolist()# Parse bounding boxlat_min = float(request.args.get("lat_min", config.LAT_MIN))lat_max = float(request.args.get("lat_max", config.LAT_MAX))lon_min = float(request.args.get("lon_min", config.LON_MIN))lon_max = float(request.args.get("lon_max", config.LON_MAX))# Filter datafiltered_data = CRIME_DATA[(CRIME_DATA['Longitude'] >= lon_min) &(CRIME_DATA['Longitude'] <= lon_max) &(CRIME_DATA['Latitude'] >= lat_min) &(CRIME_DATA['Latitude'] <= lat_max) &(CRIME_DATA['TYPE'].isin(selected_types))]# Convert to list of pointspoints = []for _, row in filtered_data.iterrows():points.append({"latitude": float(row['Latitude']),"longitude": float(row['Longitude']),"type": row['TYPE'],"date": row.get('Date', None),})return jsonify({"crime_types": selected_types,"bounds": {"lat_min": lat_min,"lat_max": lat_max,"lon_min": lon_min,"lon_max": lon_max,},"count": len(points),"points": points,})except Exception as e:import tracebacktraceback.print_exc()return jsonify({"error": str(e)}), 500@app.route("/api/temporal-factors", methods=["GET"])def temporal_factors():"""Get weather and timeseries factors for a specific date.Query params:- date: Date in YYYY-MM-DD format"""if WEATHER_MODEL is None or TIMESERIES_MODEL is None or config is None:return jsonify({"error": "Temporal models not available"}), 500date = request.args.get("date")if not date:return jsonify({"error": "Date parameter required"}), 400# Validate dateis_valid, dt, error = validate_date(date)if not is_valid:return jsonify({"error": error}), 400try:weather_factor = WEATHER_MODEL.getWeatherFactor(date)crime_types = [crime.lower() for crime in config.CRIME_TYPE]timeseries_factors = [TIMESERIES_MODEL.getTimeseriesFactor(crime_name, date)for crime_name in crime_types]return jsonify({"date": date,"weather_factor": float(weather_factor),"timeseries_factors": {crime_types[i]: float(timeseries_factors[i])for i in range(len(crime_types))}})except Exception as e:return jsonify({"error": str(e)}), 500@app.route("/api/predictions", methods=["GET"])def predictions():"""Legacy endpoint for backward compatibility."""city = request.args.get("city", "bangalore")if MODEL is None:data = get_mock_hotspots(city, threshold=0.0)else:date = datetime.strptime(config.END_DATE[1:-1], '%Y-%m-%d').strftime('%Y-%m-%d')pred_result = get_prediction_data_by_date(date=date, crime_type_index=0)if pred_result and "error" not in pred_result:data = get_hexagon_data(pred_data=pred_result["prediction"],weather_factor=pred_result["weather_factor"],timeseries_factors=pred_result["timeseries_factors"],crime_type_index=0,threshold=0.0)else:data = get_mock_hotspots(city, 0.0)return jsonify({"city": city,"timestamp": datetime.utcnow().isoformat(),3 unmodified lines@app.errorhandler(404)def not_found(_):return jsonify({"error": "Not found"}), 404@app.errorhandler(500)def internal_error(error):return jsonify({"error": "Internal server error"}), 500# Initialize system on startupprint("=" * 60)print("Initializing Crime Hotspot Prediction System...")print("=" * 60)initialization_success = initialize_system()if initialization_success:print("✓ System ready!")else:print("⚠ Running in mock mode - some features unavailable")print("=" * 60)if __name__ == "__main__":app.run(host="0.0.0.0", port=5000, debug=True)
BestModel.ptAdded+3
app.py+603-20