File size: 1,777 Bytes
5736862 a2afbef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
!pip install scikit-learn
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
import gradio as gr
import numpy as np
# Generate random sensor data
np.random.seed(42)
data = pd.DataFrame({
"Temperature": np.random.randint(0, 100, 100),
"Humidity": np.random.randint(0, 100, 100),
"Pressure": np.random.randint(0, 100, 100),
"Vibration": np.random.randint(0, 100, 100),
"Alert": np.random.randint(0, 2, 100)
})
# Split the data into features and target
X = data.drop(columns=["Alert"])
y = data["Alert"]
# Scale the features using the StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Train a random forest classifier on the scaled data
clf = RandomForestClassifier(random_state=42)
clf.fit(X_scaled, y)
# Define the input and output components for Gradio
input_components = [
gr.inputs.Slider(minimum=0, maximum=100, default=50, label="Temperature"),
gr.inputs.Slider(minimum=0, maximum=100, default=50, label="Humidity"),
gr.inputs.Slider(minimum=0, maximum=100, default=50, label="Pressure"),
gr.inputs.Slider(minimum=0, maximum=100, default=50, label="Vibration"),
]
output_component = gr.outputs.Textbox(label="Alert")
# Define the predict function to make the prediction using the model
def predict(temp, humidity, pressure, vibration):
input_data = [[temp, humidity, pressure, vibration]]
input_data_scaled = scaler.transform(input_data)
prediction = clf.predict(input_data_scaled)[0]
return "Alert!" if prediction == 1 else "No alert"
# Create the Gradio interface and run the app
interface = gr.Interface(predict, inputs=input_components, outputs=output_component, title="5G IoT Alert System")
interface.launch()
|