Esmael-Saleh commited on
Commit
428110c
·
verified ·
1 Parent(s): 12ad740
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ fire_detection_lstm_model.keras filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ WORKDIR /app
7
+
8
+ COPY requirements.txt /app/
9
+
10
+ RUN pip install --no-cache-dir -r requirements.txt
11
+
12
+ COPY . /app/
13
+
14
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import numpy as np
4
+ import joblib
5
+ from tensorflow.keras.models import load_model
6
+ from typing import List
7
+
8
+ # Initialize app
9
+ app = FastAPI(title="🔥 Fire Source Classification API")
10
+
11
+ # Load model and preprocessing tools
12
+ model = load_model("fire_detection_lstm_model.keras")
13
+ scaler = joblib.load("scaler.pkl")
14
+ label_encoder = joblib.load("label_encoder.pkl")
15
+
16
+ # Request schema
17
+ class SensorInput(BaseModel):
18
+ window: List[List[float]] # List of 30 time steps, each with 5 sensor values
19
+
20
+ @app.get("/")
21
+ def root():
22
+ return {"message": "Fire source classification model is ready!"}
23
+
24
+ @app.post("/predict")
25
+ def predict_fire_type(data: SensorInput):
26
+ sensor_window = np.array(data.window)
27
+
28
+ # Validate shape
29
+ if sensor_window.shape != (30, 5):
30
+ raise HTTPException(status_code=400, detail="Input must be a 30x5 list of floats (30 time steps, 5 features).")
31
+
32
+ # Scale and reshape
33
+ try:
34
+ sensor_window_scaled = scaler.transform(sensor_window)
35
+ except Exception as e:
36
+ raise HTTPException(status_code=500, detail=f"Scaling failed: {str(e)}")
37
+
38
+ sensor_input = sensor_window_scaled.reshape(1, 30, 5)
39
+
40
+ # Predict
41
+ try:
42
+ probs = model.predict(sensor_input)
43
+ predicted_index = int(np.argmax(probs, axis=1)[0])
44
+ predicted_class = label_encoder.inverse_transform([predicted_index])[0]
45
+ confidence = float(np.max(probs))
46
+ except Exception as e:
47
+ raise HTTPException(status_code=500, detail=f"Prediction failed: {str(e)}")
48
+
49
+ return {
50
+ "predicted_class": predicted_class,
51
+ "confidence": round(confidence, 4)
52
+ }
fire_detection_lstm_model.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b2ea0e41fac5e2ed3d5ab0dfac52c59b42d6957c7c93fe3ce1ca439e033d074b
3
+ size 1512377
label_encoder.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bb205f4189abb57de2ff58126e8b58060605febdc5a4d254ccccfe528ecadac9
3
+ size 577
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ numpy
4
+ scikit-learn
5
+ tensorflow
6
+ joblib
7
+ pydantic
scaler.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:53e90403fab69862494ff6dcf413fe7769dee7a3bda4f73c03ccc853fe70eaf3
3
+ size 719