paulmondon commited on
Commit
a2afbef
1 Parent(s): 3316d2e

Add application file

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