Suhani0409 commited on
Commit
6f65fff
·
verified ·
1 Parent(s): 5874353

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ import seaborn as sns
5
+ import matplotlib.pyplot as plt
6
+
7
+ from sklearn.datasets import load_iris
8
+ from sklearn.model_selection import train_test_split
9
+ from sklearn.metrics import accuracy_score, classification_report
10
+ from sklearn.preprocessing import StandardScaler
11
+
12
+ from sklearn.linear_model import LogisticRegression
13
+ from sklearn.svm import SVC
14
+ from sklearn.neighbors import KNeighborsClassifier
15
+ from sklearn.tree import DecisionTreeClassifier
16
+
17
+ import mlflow
18
+ import mlflow.sklearn
19
+
20
+ # ----------------------------
21
+ # Load Iris Data
22
+ # ----------------------------
23
+ iris = load_iris()
24
+ df = pd.DataFrame(iris.data, columns=iris.feature_names)
25
+ df["target"] = iris.target
26
+
27
+ # Scale data
28
+ scaler = StandardScaler()
29
+ x_scaled = scaler.fit_transform(iris.data)
30
+ y = iris.target
31
+
32
+ # Split
33
+ x_train, x_test, y_train, y_test = train_test_split(
34
+ x_scaled, y, test_size=0.2, random_state=42
35
+ )
36
+
37
+ # ----------------------------
38
+ # MLflow Setup
39
+ # ----------------------------
40
+ mlflow.set_experiment("iris-Experiment")
41
+
42
+ models = {
43
+ "Logistic Regression": LogisticRegression(),
44
+ "KNN (k=3)": KNeighborsClassifier(n_neighbors=3),
45
+ "Decision Tree": DecisionTreeClassifier(),
46
+ "Linear SVM": SVC(kernel="linear"),
47
+ }
48
+
49
+ trained_models = {}
50
+
51
+ def train_and_log(model, name):
52
+ with mlflow.start_run(run_name=name):
53
+ model.fit(x_train, y_train)
54
+ y_pred = model.predict(x_test)
55
+
56
+ acc = accuracy_score(y_test, y_pred)
57
+
58
+ mlflow.log_param("model_name", name)
59
+ mlflow.log_metric("accuracy", acc)
60
+ mlflow.sklearn.log_model(model, name)
61
+
62
+ trained_models[name] = model
63
+ print(f"Trained {name} - Accuracy: {acc}")
64
+
65
+ for name, model in models.items():
66
+ train_and_log(model, name)
67
+
68
+ # ----------------------------
69
+ # Visualization Function
70
+ # ----------------------------
71
+ def plot_pairplot():
72
+ plt.figure()
73
+ sns.pairplot(df, hue="target")
74
+ plt.tight_layout()
75
+ plt.savefig("pairplot.png")
76
+ return "pairplot.png"
77
+
78
+ # ----------------------------
79
+ # Prediction Function
80
+ # ----------------------------
81
+ def predict(model_name, sepal_length, sepal_width, petal_length, petal_width):
82
+ model = trained_models[model_name]
83
+
84
+ input_data = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
85
+ input_scaled = scaler.transform(input_data)
86
+
87
+ pred = model.predict(input_scaled)[0]
88
+ class_name = iris.target_names[pred]
89
+
90
+ return f"Predicted Class: {class_name}"
91
+
92
+ # ----------------------------
93
+ # Build Gradio UI
94
+ # ----------------------------
95
+ with gr.Blocks() as demo:
96
+ gr.Markdown("## 🌸 Iris Flower Classification (MLflow + Gradio)")
97
+
98
+ with gr.Row():
99
+ model_choice = gr.Dropdown(
100
+ list(trained_models.keys()), label="Select ML Model"
101
+ )
102
+
103
+ with gr.Row():
104
+ sepal_length = gr.Number(label="Sepal Length")
105
+ sepal_width = gr.Number(label="Sepal Width")
106
+ petal_length = gr.Number(label="Petal Length")
107
+ petal_width = gr.Number(label="Petal Width")
108
+
109
+ predict_btn = gr.Button("Predict")
110
+ output_text = gr.Textbox(label="Prediction Result")
111
+
112
+ predict_btn.click(
113
+ predict,
114
+ inputs=[model_choice, sepal_length, sepal_width, petal_length, petal_width],
115
+ outputs=output_text,
116
+ )
117
+
118
+ gr.Markdown("### 📊 Dataset Visualization")
119
+ viz_btn = gr.Button("Show Pairplot")
120
+ viz_output = gr.Image()
121
+
122
+ viz_btn.click(plot_pairplot, outputs=viz_output)
123
+
124
+ # Launch
125
+ demo.launch()