!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()