rwcuffney commited on
Commit
850b35f
1 Parent(s): 427fa72

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +284 -0
app.py ADDED
@@ -0,0 +1,284 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import requests
5
+ import io
6
+ from transformers import pipeline
7
+
8
+ #import streamlit as st
9
+ #import pandas as pd
10
+ #import matplotlib.pyplot as plt
11
+
12
+
13
+
14
+ st.title('Playing cards Image Analysis')
15
+
16
+
17
+ #sample slider; feel free to remove:
18
+ #x = st.slider('Select a value')
19
+ #st.write(x, 'squared is', x * x)
20
+
21
+ '''
22
+ This next piece of code will hit GitHub for two csv files
23
+ One is the original dataset, broken up into test, train, valid.
24
+ The second csv is the test dataset, with the results after the models were run through the API
25
+ '''
26
+ # Downloading the csv file from your GitHub account
27
+ url = "https://huggingface.co/datasets/rwcuffney/autotrain-data-pick_a_card/raw/main/cards.csv"
28
+ download = requests.get(url).content
29
+
30
+ # Reading the downloaded content and turning it into a pandas data frame
31
+ df = pd.read_csv(io.StringIO(download.decode('utf-8')))
32
+ #df = pd.read_csv('playing_cards/cards.csv').sort_values('class index')
33
+ df_fulldataset=df
34
+
35
+ # Downloading the csv file from your GitHub account
36
+ url = "https://huggingface.co/datasets/rwcuffney/autotrain-data-pick_a_card/raw/main/ML_results.csv"
37
+ download = requests.get(url).content
38
+
39
+ # Reading the downloaded content and turning it into a pandas data frame
40
+ df = pd.read_csv(io.StringIO(download.decode('utf-8')))
41
+ #df = pd.read_csv('playing_cards/cards.csv').sort_values('class index')
42
+ df_test = df
43
+
44
+
45
+
46
+ # Create the button
47
+ if st.button('Click me to re-run code',key='RunCode_button'):
48
+ # Call the function when the button is clicked
49
+ st.experimental_rerun()
50
+
51
+ st.header('Sample of the .csv data:')
52
+ x = st.slider('Select a value',value=10,max_value=8000)
53
+ st.table(df_fulldataset.sample(x))
54
+
55
+ ### HORIZONTAL BAR ###
56
+
57
+ st.header('Distribution of the playing card images:')
58
+
59
+ # Get the value counts of the 'labels' column
60
+ value_counts = df_fulldataset.groupby('labels')['class index'].count().iloc[::-1]
61
+
62
+
63
+ fig, ax = plt.subplots(figsize=(10,10))
64
+
65
+ # Create a bar chart of the value counts
66
+ ax = value_counts.plot.barh()
67
+ # Set the chart title and axis labels
68
+ ax.set_title('Value Counts of Labels')
69
+ ax.set_xlabel('Label')
70
+ ax.set_ylabel('Count')
71
+
72
+ # Show the chart
73
+ st.pyplot(fig)
74
+
75
+
76
+ ### PIE CHART ###
77
+
78
+ st.header('Balance of Train,Valid,Test datasets:')
79
+
80
+ # Get the value counts of the 'labels' column
81
+ value_counts = df_fulldataset.groupby('data set')['class index'].count().iloc[::-1]
82
+
83
+ value_counts =df_fulldataset['data set'].value_counts()
84
+
85
+ fig, ax = plt.subplots(figsize=(5,5)
86
+ )
87
+ # Create a bar chart of the value counts
88
+ ax = value_counts.plot.pie(autopct='%1.1f%%')
89
+
90
+ # Set the chart title and axis labels
91
+ # Show the chart
92
+ st.pyplot(fig)
93
+
94
+
95
+
96
+
97
+
98
+ models_run= ['SwinForImageClassification_24',
99
+ 'ViTForImageClassification_22',
100
+ 'SwinForImageClassification_21',
101
+ 'ResNetForImageClassification_23',
102
+ 'BeitForImageClassification_25']
103
+
104
+
105
+ from enum import Enum
106
+
107
+ API_dict = dict(
108
+ SwinForImageClassification_21="https://api-inference.huggingface.co/models/rwcuffney/autotrain-pick_a_card-3726099221",
109
+ ViTForImageClassification_22="https://api-inference.huggingface.co/models/rwcuffney/autotrain-pick_a_card-3726099222",
110
+ ResNetForImageClassification_23= "https://api-inference.huggingface.co/models/rwcuffney/autotrain-pick_a_card-3726099223",
111
+ SwinForImageClassification_24 = "https://api-inference.huggingface.co/models/rwcuffney/autotrain-pick_a_card-3726099224",
112
+ BeitForImageClassification_25="https://api-inference.huggingface.co/models/rwcuffney/autotrain-pick_a_card-3726099225")
113
+
114
+
115
+ # printing enum member as string
116
+ #print(Api_URL.ViTForImageClassification_22.value)
117
+
118
+
119
+ ####Try it out ###
120
+ import requests
121
+
122
+ st.header("Try it out")
123
+
124
+ '''
125
+ Warning: it will error out at first, resubmit a few times.
126
+ Each model needs to 'warm up' before they start working.
127
+
128
+ You can use any image... try test/queen of hearts/4.jpg to see an example that
129
+ Got different results with different models
130
+ '''
131
+
132
+ headers = {"Authorization": "Bearer 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"}
133
+ def query(filename,api_url):
134
+ #with open(filename, "rb") as f:
135
+ #data = f.read()
136
+ response = requests.post(api_url, headers=headers, data=filename)
137
+ return response.json()
138
+
139
+ #API_URL = "https://api-inference.huggingface.co/models/rwcuffney/autotrain-pick_a_card-3726099224"
140
+
141
+
142
+ pipeline = pipeline(task="image-classification", model="rwcuffney/autotrain-pick_a_card-3726099224")
143
+ def predict(image):
144
+ predictions = pipeline(image)
145
+ return {p["label"]: p["score"] for p in predictions}
146
+
147
+
148
+ ##### FORM #####
149
+
150
+ with st.form("api_form"):
151
+ api = st.selectbox('Which model do you want to try?',models_run,key='select_box')
152
+
153
+
154
+
155
+
156
+ uploaded_file = st.file_uploader("Choose a file")
157
+ if uploaded_file is not None:
158
+ # To read file as bytes:
159
+ #bytes_data = uploaded_file.getvalue()
160
+ #st.write(bytes_data)
161
+ st.image(uploaded_file)
162
+
163
+
164
+ submitted = st.form_submit_button("Submit")
165
+ if submitted:
166
+ #st.write(API_dict[api])
167
+ #output = query(bytes_data,API_dict[api])
168
+
169
+ #prediction = output[0]['label']
170
+ #st.write(f'prediction = {prediction}')
171
+ #st.text(output)
172
+ prediction = predict(uploaded_file)
173
+ st.write(prediction)
174
+
175
+
176
+
177
+
178
+ #### FUNCTIONS ####
179
+ import sklearn
180
+ from sklearn import metrics
181
+ import matplotlib.pyplot as plt
182
+
183
+ index = ['accuracy_score','Weighted f1', 'Cohen Kappa','Matthews']
184
+ df_Metrics =pd.DataFrame(index=index)
185
+
186
+ labels = df_test['labels'].unique()
187
+
188
+
189
+
190
+ ### FUNCTION TO SHOW THE METRICS
191
+ def show_metrics(test,pred,name):
192
+ from sklearn import metrics
193
+
194
+ my_Name = name
195
+ my_Accuracy_score=metrics.accuracy_score(test, pred)
196
+ #my_ROC_AUC_score= roc_auc_score(y, model.predict_proba(X), multi_class='ovr')
197
+ my_Weighted_f1= metrics.f1_score(test, pred,average='weighted')
198
+ my_Cohen_Kappa = metrics.cohen_kappa_score(test, pred)
199
+ my_Matthews_coefficient=metrics.matthews_corrcoef(test, pred)
200
+
201
+ st.header(f'Metrics for {my_Name}:')
202
+ report =metrics.classification_report(test, pred, output_dict=True)
203
+ df_report = pd.DataFrame(report).transpose()
204
+ st.dataframe(df_report )
205
+ st.write(f'Accuracy Score........{metrics.accuracy_score(test, pred):.4f}\n\n' \
206
+ #f'ROC AUC Score.........{my_ROC_AUC_score:.4f}\n\n' \
207
+ f'Weighted f1 score.....{my_Weighted_f1:.4f}\n\n' \
208
+ f'Cohen Kappa...........{my_Cohen_Kappa:.4f}\n\n' \
209
+ f'Matthews Coefficient..{my_Matthews_coefficient:.4f}\n\n')
210
+ my_List = [my_Accuracy_score, my_Weighted_f1, my_Cohen_Kappa, my_Matthews_coefficient]
211
+
212
+ df_Metrics[my_Name] = my_List
213
+
214
+ cfm= metrics.confusion_matrix(test, pred)
215
+ st.caption(f'Confusion Matrix: {my_Name}')
216
+ cmd = metrics.ConfusionMatrixDisplay(cfm,display_labels=labels)
217
+ fig, ax = plt.subplots(figsize=(15,15))
218
+ ax = cmd.plot(ax=ax,
219
+ colorbar=False,
220
+ values_format = '.0f',
221
+ cmap='Reds')#='tab20')# see color options here https://matplotlib.org/stable/tutorials/colors/colormaps.html
222
+ plt.xticks(rotation=90)
223
+ st.pyplot(fig)
224
+
225
+
226
+
227
+
228
+
229
+ st.header('Let\'s see how the models performed')
230
+
231
+ '''
232
+ The next part of the code will analyze the full dataset.
233
+ Choose all five models to compare them all
234
+
235
+ '''
236
+
237
+
238
+ ##### FORM #####
239
+
240
+ with st.form("my_form"):
241
+ st.write("You can choose from 1 to 5 models")
242
+
243
+
244
+ selected_options = st.multiselect(
245
+ 'Which models would you like to analyze?', models_run)
246
+
247
+ submitted = st.form_submit_button("Submit")
248
+ if submitted:
249
+ st.write('you selected',selected_options)
250
+
251
+
252
+ ###Show the metrics for each dataset:
253
+ test = df_test['labels']
254
+
255
+ #for m in models_run:
256
+ for m in selected_options:
257
+ pred = df_test[m]
258
+ show_metrics(test,pred,m)
259
+
260
+ st.header('Metrics for all models:')
261
+ st.table(df_Metrics)
262
+
263
+ #### GRAPH THE RESULTS ###
264
+ import seaborn as sns
265
+
266
+ # Reshape the dataframe into long format using pd.melt()
267
+ #subset_df = pd.melt(df_Metrics[['SwinForImageClassification_24',
268
+ #'ViTForImageClassification_22', 'SwinForImageClassification_21', 'ResNetForImageClassification_23', 'BeitForImageClassification_25']].reset_index(), id_vars='index', var_name='Model', value_name='Score')
269
+ subset_df = pd.melt(df_Metrics[selected_options].reset_index(), id_vars='index', var_name='Model', value_name='Score')
270
+
271
+ sns.set_style('whitegrid')
272
+ ax=sns.catplot(data=subset_df,
273
+ x='index',
274
+ y='Score',
275
+ hue='Model',
276
+ kind='bar',
277
+ palette='Blues',
278
+ aspect=2)
279
+
280
+ plt.xlabel('Clusters')
281
+ plt.ylabel('Scores')
282
+
283
+ fig = ax.figure
284
+ st.pyplot(fig)