Spaces:
Sleeping
Sleeping
marziehben
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#import libraries
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import numpy
|
4 |
+
import sys
|
5 |
+
import numpy as np
|
6 |
+
from sklearn.neighbors import KNeighborsClassifier
|
7 |
+
import pandas as pd
|
8 |
+
from itertools import islice
|
9 |
+
import pickle
|
10 |
+
import streamlit as st
|
11 |
+
import csv
|
12 |
+
from sklearn.model_selection import KFold
|
13 |
+
from sklearn.model_selection import cross_val_score
|
14 |
+
from sklearn.metrics import confusion_matrix
|
15 |
+
import sklearn.metrics as metrics
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
# Read a CSV file and determine X features & y target
|
20 |
+
df=pd.read_csv("C:\\Users\\M\\datatest.csv")
|
21 |
+
X=df.drop("level",axis=1).values
|
22 |
+
y=df.level.values
|
23 |
+
|
24 |
+
|
25 |
+
#Build a model & get a Scores
|
26 |
+
knn = KNeighborsClassifier(n_neighbors=3)
|
27 |
+
knn.fit(X, y)
|
28 |
+
|
29 |
+
k_folds = KFold(n_splits = 5)
|
30 |
+
scores = cross_val_score(knn, X, y, cv = k_folds)
|
31 |
+
print("Cross Validation Scores: ", scores)
|
32 |
+
print("Average CV Score: ", scores.mean())
|
33 |
+
print("Number of CV Scores used in Average: ", len(scores))
|
34 |
+
y_true=df
|
35 |
+
expected=y_true
|
36 |
+
predict = knn.predict(X)
|
37 |
+
confusion_matrix(y_true['level'], y_pred=predict)
|
38 |
+
print(metrics.classification_report(y_true['level'], y_pred=predict))
|
39 |
+
print(type(X))
|
40 |
+
print(type(y))
|
41 |
+
|
42 |
+
# Visualize the dataframe in the Streamlit app
|
43 |
+
st.write("""
|
44 |
+
# Grade of Parts
|
45 |
+
""")
|
46 |
+
st.bar_chart(y)
|
47 |
+
|
48 |
+
# Title
|
49 |
+
st.title ("Hello Engineer")
|
50 |
+
|
51 |
+
# Header
|
52 |
+
st.header("Lets know about Quality Status")
|
53 |
+
|
54 |
+
|
55 |
+
# pickling the model
|
56 |
+
pickle_out = open("knn.pkl", "wb")
|
57 |
+
pickle.dump(knn, pickle_out)
|
58 |
+
pickle_out.close()
|
59 |
+
|
60 |
+
|
61 |
+
# loading in the model to predict(classify) on the data
|
62 |
+
pickle_in = open('knn.pkl', 'rb')
|
63 |
+
classifier = pickle.load(pickle_in)
|
64 |
+
|
65 |
+
def welcome():
|
66 |
+
return 'welcome all'
|
67 |
+
|
68 |
+
# defining the function which will make the prediction using
|
69 |
+
# the data which the user inputs
|
70 |
+
def prediction(ppm, SR, repeat_of_alarm, not_po_ka_yoke, high_price):
|
71 |
+
ppm=int(ppm)
|
72 |
+
SR=int(SR)
|
73 |
+
repeat_of_alarm=int(repeat_of_alarm)
|
74 |
+
not_po_ka_yoke=int(not_po_ka_yoke)
|
75 |
+
high_price=int(high_price)
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
prediction = classifier.predict([[ppm, SR, repeat_of_alarm, not_po_ka_yoke, high_price]])
|
80 |
+
print(prediction)
|
81 |
+
return prediction
|
82 |
+
|
83 |
+
|
84 |
+
# this is the main function in which we define our webpage
|
85 |
+
#dictionary for levels
|
86 |
+
def main():
|
87 |
+
idx2label={
|
88 |
+
0:"A",
|
89 |
+
1:"B",
|
90 |
+
2:"C",
|
91 |
+
3:"D",
|
92 |
+
4:"E",
|
93 |
+
5:"F"
|
94 |
+
}
|
95 |
+
# giving the webpage a title
|
96 |
+
st.title("Classification")
|
97 |
+
html_temp = """
|
98 |
+
<div style ="background-color:yellow;padding:13px">
|
99 |
+
<h1 style ="color:black;text-align:center;"> Classifier ML App </h1>
|
100 |
+
</div>
|
101 |
+
"""
|
102 |
+
|
103 |
+
# this line allows us to display the front end aspects we have
|
104 |
+
st.markdown(html_temp, unsafe_allow_html = True)
|
105 |
+
ppm = st.selectbox("PPM", [0,1])
|
106 |
+
SR = st.selectbox("S/R", [0,1])
|
107 |
+
repeat_of_alarm = st.selectbox("Repeat of Alarm", [0,1])
|
108 |
+
not_po_ka_yoke =st.selectbox("Not Pokayoka", [0,1])
|
109 |
+
high_price= st.selectbox("High Price", [0,1])
|
110 |
+
level = ([])
|
111 |
+
# the below line ensures that when the button called 'Predict' is clicked,
|
112 |
+
# the prediction function defined above is called to make the prediction(classify)
|
113 |
+
# and store it in the variable result \
|
114 |
+
if st.button("See a Grade"):
|
115 |
+
level=prediction(ppm, SR, repeat_of_alarm, not_po_ka_yoke, high_price)
|
116 |
+
st.success((idx2label[level[0]]))
|
117 |
+
|
118 |
+
if __name__=='__main__':
|
119 |
+
main()
|