JARVIS-JI commited on
Commit
0a08961
·
verified ·
1 Parent(s): 744dd01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -22
app.py CHANGED
@@ -1,26 +1,82 @@
 
1
  import gradio as gr
2
  import numpy as np
3
- import joblib
4
-
5
- # Load model (ensure this .pkl is uploaded)
6
- model = joblib.load("gas_classification_model.pkl")
7
-
8
- # Define feature input labels
9
- sensor_labels = [f"Sensor {i+1}" for i in range(10)]
10
-
11
- # Predict function
12
- def predict_gas(*sensor_values):
13
- input_data = np.array(sensor_values).reshape(1, -1)
14
- prediction = model.predict(input_data)[0]
15
- return f"Predicted Gas Type: {prediction}"
16
-
17
- # Gradio UI
18
- iface = gr.Interface(
19
- fn=predict_gas,
20
- inputs=[gr.Number(label=label) for label in sensor_labels],
21
- outputs=gr.Textbox(label="Gas Type"),
22
- title="Gas Sensor Classifier",
23
- description="Enter 10 sensor readings to classify the gas type.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  )
25
 
26
- iface.launch()
 
 
1
+
2
  import gradio as gr
3
  import numpy as np
4
+ import pickle
5
+
6
+ # Load your trained model
7
+ with open("gas_classification_model.pkl", "rb") as f:
8
+ model = pickle.load(f)
9
+
10
+ # --- Feature Extraction ---
11
+
12
+ def compute_ema(series, alpha):
13
+ ema = 0
14
+ ema_values = []
15
+ for val in series:
16
+ ema = alpha * val + (1 - alpha) * ema
17
+ ema_values.append(ema)
18
+ return np.array(ema_values)
19
+
20
+ def extract_features(sensor_data, baseline_range=(0, 20), exposure_range=(20, 80)):
21
+ features = []
22
+ for sensor_id in range(1, 17):
23
+ R = np.array(sensor_data[sensor_id])
24
+ baseline = np.mean(R[baseline_range[0]:baseline_range[1]])
25
+ exposure = R[exposure_range[0]:exposure_range[1]]
26
+ peak = np.max(exposure)
27
+ delta_R = peak - baseline
28
+ norm_delta_R = peak / baseline if baseline != 0 else 0
29
+
30
+ peak_index = np.argmax(exposure) + exposure_range[0]
31
+ rising = R[baseline_range[1]:peak_index]
32
+ decaying = R[peak_index:]
33
+
34
+ alphas = [0.001, 0.01, 0.1]
35
+ ema_rising = [np.max(compute_ema(rising, a)) for a in alphas]
36
+ ema_decaying = [np.min(compute_ema(decaying, a)) for a in alphas]
37
+
38
+ sensor_features = [delta_R, norm_delta_R] + ema_rising + ema_decaying
39
+ features.extend(sensor_features)
40
+
41
+ return np.array(features)
42
+
43
+ # --- Prediction Pipeline ---
44
+
45
+ def predict(sensor1, sensor2, sensor3, sensor4, sensor5, sensor6, sensor7, sensor8,
46
+ sensor9, sensor10, sensor11, sensor12, sensor13, sensor14, sensor15, sensor16):
47
+
48
+ # Convert all inputs to float lists
49
+ sensors = [sensor1, sensor2, sensor3, sensor4, sensor5, sensor6, sensor7, sensor8,
50
+ sensor9, sensor10, sensor11, sensor12, sensor13, sensor14, sensor15, sensor16]
51
+
52
+ sensor_data = {}
53
+ for i in range(16):
54
+ try:
55
+ values = [float(x.strip()) for x in sensors[i].split(",")]
56
+ if len(values) < 100:
57
+ return f"Sensor {i+1} must have at least 100 values."
58
+ sensor_data[i+1] = values
59
+ except:
60
+ return f"Invalid input format for Sensor {i+1}. Use comma-separated numbers."
61
+
62
+ features = extract_features(sensor_data)
63
+ prediction = model.predict([features])[0]
64
+ return f"🚨 Predicted Gas Type: {prediction}"
65
+
66
+ # --- Gradio UI ---
67
+
68
+ sensor_inputs = [
69
+ gr.Textbox(label=f"Sensor {i+1} Readings (comma-separated, 100+ values)", lines=2, placeholder="e.g. 1.01, 1.02, 1.03, ...")
70
+ for i in range(16)
71
+ ]
72
+
73
+ demo = gr.Interface(
74
+ fn=predict,
75
+ inputs=sensor_inputs,
76
+ outputs=gr.Text(label="Prediction"),
77
+ title="MOS Sensor Gas Prediction",
78
+ description="Paste comma-separated time-series readings (100+ values) from each of the 16 sensors to get a gas type prediction."
79
  )
80
 
81
+ if __name__ == "__main__":
82
+ demo.launch()