jithenderchoudary commited on
Commit
5f70f3c
·
verified ·
1 Parent(s): 4c48892

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import libraries
2
+ import numpy as np
3
+ import pandas as pd
4
+ from sklearn.ensemble import RandomForestClassifier
5
+ from sklearn.model_selection import train_test_split
6
+ from sklearn.metrics import accuracy_score
7
+ import gradio as gr
8
+ import joblib
9
+
10
+ # Step 1: Generate or Load Sample Data
11
+ # Features: Pressure, Temperature, Material Type (encoded as integers)
12
+ # Target: Defect Type (0 = No Defect, 1 = Crack, 2 = Wrinkle)
13
+ np.random.seed(42)
14
+ data = pd.DataFrame({
15
+ "Pressure": np.random.randint(50, 200, 200),
16
+ "Temperature": np.random.randint(300, 700, 200),
17
+ "Material_Type": np.random.randint(1, 5, 200),
18
+ "Defect_Type": np.random.choice([0, 1, 2], 200, p=[0.6, 0.3, 0.1])
19
+ })
20
+
21
+ # Splitting data
22
+ X = data[["Pressure", "Temperature", "Material_Type"]]
23
+ y = data["Defect_Type"]
24
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
25
+
26
+ # Step 2: Train the Model
27
+ model = RandomForestClassifier(n_estimators=100, random_state=42)
28
+ model.fit(X_train, y_train)
29
+
30
+ # Save the model
31
+ joblib.dump(model, "defect_model.pkl")
32
+
33
+ # Step 3: Test the Model
34
+ y_pred = model.predict(X_test)
35
+ print(f"Model Accuracy: {accuracy_score(y_test, y_pred):.2f}")
36
+
37
+ # Step 4: Create a Gradio Interface
38
+ def predict_defect(pressure, temperature, material_type):
39
+ # Load the model
40
+ loaded_model = joblib.load("defect_model.pkl")
41
+ # Predict
42
+ input_data = np.array([[pressure, temperature, material_type]])
43
+ prediction = loaded_model.predict(input_data)[0]
44
+ defect_map = {0: "No Defect", 1: "Crack", 2: "Wrinkle"}
45
+ return defect_map[prediction]
46
+
47
+ # Gradio Interface
48
+ interface = gr.Interface(
49
+ fn=predict_defect,
50
+ inputs=[
51
+ gr.inputs.Slider(50, 200, step=1, label="Pressure"),
52
+ gr.inputs.Slider(300, 700, step=1, label="Temperature"),
53
+ gr.inputs.Dropdown(["1", "2", "3", "4"], label="Material Type"),
54
+ ],
55
+ outputs="text",
56
+ title="Defect Prediction Model",
57
+ description="Predicts defect type based on input features: Pressure, Temperature, and Material Type."
58
+ )
59
+
60
+ # Launch the app
61
+ if __name__ == "__main__":
62
+ interface.launch()