Bio / app.py
JARVIS-JI's picture
Update app.py
0a08961 verified
import gradio as gr
import numpy as np
import pickle
# Load your trained model
with open("gas_classification_model.pkl", "rb") as f:
model = pickle.load(f)
# --- Feature Extraction ---
def compute_ema(series, alpha):
ema = 0
ema_values = []
for val in series:
ema = alpha * val + (1 - alpha) * ema
ema_values.append(ema)
return np.array(ema_values)
def extract_features(sensor_data, baseline_range=(0, 20), exposure_range=(20, 80)):
features = []
for sensor_id in range(1, 17):
R = np.array(sensor_data[sensor_id])
baseline = np.mean(R[baseline_range[0]:baseline_range[1]])
exposure = R[exposure_range[0]:exposure_range[1]]
peak = np.max(exposure)
delta_R = peak - baseline
norm_delta_R = peak / baseline if baseline != 0 else 0
peak_index = np.argmax(exposure) + exposure_range[0]
rising = R[baseline_range[1]:peak_index]
decaying = R[peak_index:]
alphas = [0.001, 0.01, 0.1]
ema_rising = [np.max(compute_ema(rising, a)) for a in alphas]
ema_decaying = [np.min(compute_ema(decaying, a)) for a in alphas]
sensor_features = [delta_R, norm_delta_R] + ema_rising + ema_decaying
features.extend(sensor_features)
return np.array(features)
# --- Prediction Pipeline ---
def predict(sensor1, sensor2, sensor3, sensor4, sensor5, sensor6, sensor7, sensor8,
sensor9, sensor10, sensor11, sensor12, sensor13, sensor14, sensor15, sensor16):
# Convert all inputs to float lists
sensors = [sensor1, sensor2, sensor3, sensor4, sensor5, sensor6, sensor7, sensor8,
sensor9, sensor10, sensor11, sensor12, sensor13, sensor14, sensor15, sensor16]
sensor_data = {}
for i in range(16):
try:
values = [float(x.strip()) for x in sensors[i].split(",")]
if len(values) < 100:
return f"Sensor {i+1} must have at least 100 values."
sensor_data[i+1] = values
except:
return f"Invalid input format for Sensor {i+1}. Use comma-separated numbers."
features = extract_features(sensor_data)
prediction = model.predict([features])[0]
return f"🚨 Predicted Gas Type: {prediction}"
# --- Gradio UI ---
sensor_inputs = [
gr.Textbox(label=f"Sensor {i+1} Readings (comma-separated, 100+ values)", lines=2, placeholder="e.g. 1.01, 1.02, 1.03, ...")
for i in range(16)
]
demo = gr.Interface(
fn=predict,
inputs=sensor_inputs,
outputs=gr.Text(label="Prediction"),
title="MOS Sensor Gas Prediction",
description="Paste comma-separated time-series readings (100+ values) from each of the 16 sensors to get a gas type prediction."
)
if __name__ == "__main__":
demo.launch()