Spaces:
Sleeping
Sleeping
new file: server.py
Browse files- Dockerfile +21 -0
- requirements.txt +0 -0
- rf_model.pkl +3 -0
- server.py +157 -0
Dockerfile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 4 |
+
ENV PYTHONUNBUFFERED=1
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
RUN apt-get update && apt-get install -y \
|
| 8 |
+
build-essential \
|
| 9 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
+
|
| 11 |
+
COPY requirements.txt .
|
| 12 |
+
|
| 13 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY . .
|
| 16 |
+
|
| 17 |
+
ENV PORT=7860
|
| 18 |
+
|
| 19 |
+
EXPOSE 7860
|
| 20 |
+
|
| 21 |
+
CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "7860"]
|
requirements.txt
ADDED
|
File without changes
|
rf_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ddc856310178b00a3743ea7cfce981641154fb85f12177c536025df2ebc5ecd0
|
| 3 |
+
size 143497
|
server.py
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
import numpy as np
|
| 5 |
+
from collections import deque
|
| 6 |
+
import joblib
|
| 7 |
+
import time
|
| 8 |
+
import datetime
|
| 9 |
+
from features import extract_features
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# -----------------------------
|
| 14 |
+
# β
CORS FIX (IMPORTANT)
|
| 15 |
+
# -----------------------------
|
| 16 |
+
app.add_middleware(
|
| 17 |
+
CORSMiddleware,
|
| 18 |
+
allow_origins=["*"],
|
| 19 |
+
allow_credentials=True,
|
| 20 |
+
allow_methods=["*"],
|
| 21 |
+
allow_headers=["*"],
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# -----------------------------
|
| 25 |
+
# CONFIG
|
| 26 |
+
# -----------------------------
|
| 27 |
+
WINDOW_SIZE = 120
|
| 28 |
+
CRASH_THRESHOLD = 0.5
|
| 29 |
+
COOLDOWN = 10
|
| 30 |
+
|
| 31 |
+
model = joblib.load("rf_model.pkl")
|
| 32 |
+
buffer = deque(maxlen=WINDOW_SIZE)
|
| 33 |
+
|
| 34 |
+
last_crash_time = 0
|
| 35 |
+
|
| 36 |
+
# -----------------------------
|
| 37 |
+
# π LOCATION STORAGE
|
| 38 |
+
# -----------------------------
|
| 39 |
+
latest_location = {"lat": 0.0, "lon": 0.0}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@app.post("/location")
|
| 43 |
+
def update_location(data: dict):
|
| 44 |
+
global latest_location
|
| 45 |
+
latest_location = {
|
| 46 |
+
"lat": float(data.get("lat", 0)),
|
| 47 |
+
"lon": float(data.get("lon", 0))
|
| 48 |
+
}
|
| 49 |
+
print(f"π Location Updated: {latest_location}")
|
| 50 |
+
return {"status": "location updated"}
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@app.get("/location")
|
| 54 |
+
def get_location():
|
| 55 |
+
return latest_location
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# -----------------------------
|
| 59 |
+
# SENSOR MODEL
|
| 60 |
+
# -----------------------------
|
| 61 |
+
class SensorData(BaseModel):
|
| 62 |
+
ax: float
|
| 63 |
+
ay: float
|
| 64 |
+
az: float
|
| 65 |
+
gx: float
|
| 66 |
+
gy: float
|
| 67 |
+
gz: float
|
| 68 |
+
speed: float
|
| 69 |
+
timestamp: float
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
@app.post("/sensor")
|
| 73 |
+
def sensor(data: SensorData):
|
| 74 |
+
global last_crash_time
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
buffer.append([
|
| 78 |
+
data.ax, data.ay, data.az,
|
| 79 |
+
data.gx, data.gy, data.gz,
|
| 80 |
+
data.speed
|
| 81 |
+
])
|
| 82 |
+
|
| 83 |
+
if len(buffer) < WINDOW_SIZE:
|
| 84 |
+
return {"status": "collecting"}
|
| 85 |
+
|
| 86 |
+
window = np.array(buffer)
|
| 87 |
+
|
| 88 |
+
# ML prediction
|
| 89 |
+
features = extract_features(window)
|
| 90 |
+
prob = model.predict_proba([features])[0][1]
|
| 91 |
+
|
| 92 |
+
# physics boost
|
| 93 |
+
acc = np.sqrt(window[:,0]**2 + window[:,1]**2 + window[:,2]**2)
|
| 94 |
+
if np.max(acc) > 40:
|
| 95 |
+
prob += 0.2
|
| 96 |
+
|
| 97 |
+
prob = float(min(prob, 1.0))
|
| 98 |
+
|
| 99 |
+
# smoothing
|
| 100 |
+
if not hasattr(sensor, "history"):
|
| 101 |
+
sensor.history = []
|
| 102 |
+
|
| 103 |
+
sensor.history.append(prob)
|
| 104 |
+
sensor.history = sensor.history[-5:]
|
| 105 |
+
|
| 106 |
+
avg_prob = float(sum(sensor.history) / len(sensor.history))
|
| 107 |
+
|
| 108 |
+
# confirmation
|
| 109 |
+
if not hasattr(sensor, "trigger_count"):
|
| 110 |
+
sensor.trigger_count = 0
|
| 111 |
+
|
| 112 |
+
if avg_prob > CRASH_THRESHOLD:
|
| 113 |
+
sensor.trigger_count += 1
|
| 114 |
+
else:
|
| 115 |
+
sensor.trigger_count = 0
|
| 116 |
+
|
| 117 |
+
current_time = float(time.time())
|
| 118 |
+
|
| 119 |
+
crash_detected = (
|
| 120 |
+
sensor.trigger_count >= 2 and
|
| 121 |
+
(current_time - last_crash_time > COOLDOWN)
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
if crash_detected:
|
| 125 |
+
last_crash_time = current_time
|
| 126 |
+
|
| 127 |
+
event_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 128 |
+
confidence = float(min(1.0, avg_prob + 0.2))
|
| 129 |
+
|
| 130 |
+
log_message = f"""
|
| 131 |
+
CRASH DETECTED!
|
| 132 |
+
Time: {event_time}
|
| 133 |
+
Confidence: {round(confidence, 3)}
|
| 134 |
+
-----------------------------------
|
| 135 |
+
"""
|
| 136 |
+
|
| 137 |
+
print("\033[91m" + log_message + "\033[0m")
|
| 138 |
+
|
| 139 |
+
with open("crash_log.txt", "a", encoding="utf-8") as f:
|
| 140 |
+
f.write(log_message)
|
| 141 |
+
|
| 142 |
+
return {
|
| 143 |
+
"event": "CRASH_DETECTED",
|
| 144 |
+
"confidence": confidence,
|
| 145 |
+
"timestamp": current_time
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
return {"status": "monitoring"}
|
| 149 |
+
|
| 150 |
+
except Exception as e:
|
| 151 |
+
print("SERVER ERROR:", e)
|
| 152 |
+
return {"error": str(e)}
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
@app.get("/")
|
| 156 |
+
def home():
|
| 157 |
+
return {"status": "Crash Detection Server Running π"}
|