Awudu-Jamal1 commited on
Commit
663a1bf
1 Parent(s): 73e8d6f
Files changed (1) hide show
  1. capstone_gradio.ipynb +359 -0
capstone_gradio.ipynb ADDED
@@ -0,0 +1,359 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "## **Churn Predictor UI with Gradio**"
8
+ ]
9
+ },
10
+ {
11
+ "cell_type": "code",
12
+ "execution_count": 1,
13
+ "metadata": {},
14
+ "outputs": [
15
+ {
16
+ "name": "stderr",
17
+ "output_type": "stream",
18
+ "text": [
19
+ "C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
20
+ " from .autonotebook import tqdm as notebook_tqdm\n"
21
+ ]
22
+ }
23
+ ],
24
+ "source": [
25
+ "#\n",
26
+ "import gradio as gr\n",
27
+ "import pandas as pd\n",
28
+ "import pickle\n",
29
+ "from sklearn.pipeline import Pipeline\n",
30
+ "from sklearn.ensemble import RandomForestClassifier\n",
31
+ "from sklearn.preprocessing import StandardScaler, LabelEncoder\n",
32
+ "from sklearn.impute import SimpleImputer\n",
33
+ "from imblearn.over_sampling import RandomOverSampler\n",
34
+ "from sklearn.preprocessing import FunctionTransformer\n",
35
+ "import joblib"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": 71,
41
+ "metadata": {},
42
+ "outputs": [],
43
+ "source": [
44
+ "xtrain= pd.read_csv('Xtrains.csv')\n",
45
+ "ytrain=pd.read_csv('Ytrains.csv')"
46
+ ]
47
+ },
48
+ {
49
+ "cell_type": "code",
50
+ "execution_count": 65,
51
+ "metadata": {},
52
+ "outputs": [
53
+ {
54
+ "name": "stderr",
55
+ "output_type": "stream",
56
+ "text": [
57
+ "C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\base.py:299: UserWarning: Trying to unpickle estimator DecisionTreeClassifier from version 1.2.2 when using version 1.2.1. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n",
58
+ "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n",
59
+ " warnings.warn(\n",
60
+ "C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\sklearn\\base.py:299: UserWarning: Trying to unpickle estimator RandomForestClassifier from version 1.2.2 when using version 1.2.1. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n",
61
+ "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n",
62
+ " warnings.warn(\n"
63
+ ]
64
+ }
65
+ ],
66
+ "source": [
67
+ "# Loading Models\n",
68
+ "with open(\"model.pkl\", \"rb\") as f:\n",
69
+ "\tclf = pickle.load(f)"
70
+ ]
71
+ },
72
+ {
73
+ "cell_type": "code",
74
+ "execution_count": 72,
75
+ "metadata": {},
76
+ "outputs": [
77
+ {
78
+ "data": {
79
+ "text/html": [
80
+ "<style>#sk-container-id-2 {color: black;background-color: white;}#sk-container-id-2 pre{padding: 0;}#sk-container-id-2 div.sk-toggleable {background-color: white;}#sk-container-id-2 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-2 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-2 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-2 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-2 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-2 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-2 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-2 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-2 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-2 div.sk-item {position: relative;z-index: 1;}#sk-container-id-2 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-2 div.sk-item::before, #sk-container-id-2 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-2 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-2 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-2 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-2 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-2 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-2 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-2 div.sk-label-container {text-align: center;}#sk-container-id-2 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-2 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>RandomForestClassifier(n_estimators=10)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" checked><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">RandomForestClassifier</label><div class=\"sk-toggleable__content\"><pre>RandomForestClassifier(n_estimators=10)</pre></div></div></div></div></div>"
81
+ ],
82
+ "text/plain": [
83
+ "RandomForestClassifier(n_estimators=10)"
84
+ ]
85
+ },
86
+ "execution_count": 72,
87
+ "metadata": {},
88
+ "output_type": "execute_result"
89
+ }
90
+ ],
91
+ "source": [
92
+ "clf.fit(xtrain, ytrain.values.ravel())"
93
+ ]
94
+ },
95
+ {
96
+ "cell_type": "code",
97
+ "execution_count": 73,
98
+ "metadata": {
99
+ "colab": {
100
+ "base_uri": "https://localhost:8080/",
101
+ "height": 621
102
+ },
103
+ "id": "5SkaWb6RRZ3i",
104
+ "outputId": "c686baee-fb96-4cf1-e008-ceee011ec160"
105
+ },
106
+ "outputs": [
107
+ {
108
+ "name": "stderr",
109
+ "output_type": "stream",
110
+ "text": [
111
+ "C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\gradio\\inputs.py:219: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n",
112
+ " warnings.warn(\n",
113
+ "C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\gradio\\inputs.py:222: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
114
+ " super().__init__(\n",
115
+ "C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\gradio\\inputs.py:89: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n",
116
+ " warnings.warn(\n",
117
+ "C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\gradio\\inputs.py:93: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
118
+ " super().__init__(\n"
119
+ ]
120
+ },
121
+ {
122
+ "name": "stdout",
123
+ "output_type": "stream",
124
+ "text": [
125
+ "Running on local URL: http://127.0.0.1:7903\n",
126
+ "\n",
127
+ "To create a public link, set `share=True` in `launch()`.\n"
128
+ ]
129
+ },
130
+ {
131
+ "data": {
132
+ "text/html": [
133
+ "<div><iframe src=\"http://127.0.0.1:7903/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
134
+ ],
135
+ "text/plain": [
136
+ "<IPython.core.display.HTML object>"
137
+ ]
138
+ },
139
+ "metadata": {},
140
+ "output_type": "display_data"
141
+ },
142
+ {
143
+ "data": {
144
+ "text/plain": []
145
+ },
146
+ "execution_count": 73,
147
+ "metadata": {},
148
+ "output_type": "execute_result"
149
+ },
150
+ {
151
+ "name": "stdout",
152
+ "output_type": "stream",
153
+ "text": [
154
+ "{'text': 'Customer will not churn', 'entities': []}\n"
155
+ ]
156
+ }
157
+ ],
158
+ "source": [
159
+ "\n",
160
+ "tenure_labels = {\n",
161
+ " 0: \"3-6 months\",\n",
162
+ " 1: \"6-9 months\",\n",
163
+ " 2: \"9-12 months\",\n",
164
+ " 3: \"12-15 months\",\n",
165
+ " 4: \"15-18 months\",\n",
166
+ " 5: \"18-21 months\",\n",
167
+ " 6: \"21-24 months\",\n",
168
+ " 7: \"> 24 months\"\n",
169
+ "}\n",
170
+ "\n",
171
+ "# Reverse the mapping for predictions\n",
172
+ "tenure_values = {v: k for k, v in tenure_labels.items()}\n",
173
+ "\n",
174
+ "def predict(tenure, montant, freq_rech, revenue, arpu, freq, data_vol, on_net, orange, tigo, freq_top_pack, regularity):\n",
175
+ " \n",
176
+ " tenure_value = tenure_values[tenure]\n",
177
+ "\n",
178
+ " \n",
179
+ "\n",
180
+ " input_df = pd.DataFrame({\n",
181
+ " 'TENURE': [tenure_value],\n",
182
+ " 'MONTANT': [montant],\n",
183
+ " 'FREQUENCE_RECH': [freq_rech],\n",
184
+ " 'REVENUE': [revenue],\n",
185
+ " 'ARPU_SEGMENT': [arpu],\n",
186
+ " 'FREQUENCE': [freq],\n",
187
+ " 'DATA_VOLUME': [data_vol],\n",
188
+ " 'ON_NET': [on_net],\n",
189
+ " 'ORANGE': [orange],\n",
190
+ " 'TIGO': [tigo],\n",
191
+ " 'REGULARITY':[regularity],\n",
192
+ " 'FREQ_TOP_PACK': [freq_top_pack]\n",
193
+ " })\n",
194
+ "\n",
195
+ " prediction = clf.predict(input_df)\n",
196
+ "\n",
197
+ "\n",
198
+ "\n",
199
+ " churn_label = \"Customer will churn\" if prediction == 1 else \"Customer will not churn\"\n",
200
+ " result = {\n",
201
+ " 'text': churn_label, # Use the churn label as 'text'\n",
202
+ " 'entities': [] # You can leave 'entities' as an empty list if no entities need highlighting\n",
203
+ " }\n",
204
+ " print(result)\n",
205
+ " return result\n",
206
+ "\n",
207
+ "\n",
208
+ "\n",
209
+ "# Create a dropdown menu with labels\n",
210
+ "tenure_dropdown = gr.inputs.Dropdown(list(tenure_labels.values()), label=\"TENURE\")\n",
211
+ "\n",
212
+ "iface = gr.Interface(\n",
213
+ " fn=predict,\n",
214
+ " inputs=[\n",
215
+ " tenure_dropdown, # Dropdown instead of slider\n",
216
+ " #gr.inputs.Slider(minimum=1, maximum=7, label=\"TENURE\"),\n",
217
+ " gr.inputs.Slider(minimum=20, maximum=470000, label=\"MONTANT\"),\n",
218
+ " gr.inputs.Slider(minimum=1, maximum=131, label=\"FREQUENCE_RECH\"),\n",
219
+ " gr.inputs.Slider(minimum=1, maximum=530000, label=\"REVENUE\"),\n",
220
+ " gr.inputs.Slider(minimum=0, maximum=2453, label=\"ARPU_SEGMENT\"),\n",
221
+ " gr.inputs.Slider(minimum=1, maximum=91, label=\"FREQUENCE\"),\n",
222
+ " gr.inputs.Slider(minimum=1, maximum=1702309, label=\"DATA_VOLUME\"),\n",
223
+ " gr.inputs.Slider(minimum=0, maximum=51000, label=\"ON_NET\"),\n",
224
+ " gr.inputs.Slider(minimum=0, maximum=12040, label=\"ORANGE\"),\n",
225
+ " gr.inputs.Slider(minimum=0, maximum=4174, label=\"TIGO\"),\n",
226
+ " gr.inputs.Slider(minimum=0, maximum=624, label=\"FREQ_TOP_PACK\"),\n",
227
+ " gr.inputs.Slider(minimum=0, maximum=62, label=\"REGULARITY\")\n",
228
+ " ],\n",
229
+ " \n",
230
+ " outputs=output,\n",
231
+ " title=\"Team Paris Customer Churn Prediction App\",\n",
232
+ " description=\"Let's Get Started With Some Predictions!\"\n",
233
+ ")\n",
234
+ "\n",
235
+ "iface.launch()\n"
236
+ ]
237
+ },
238
+ {
239
+ "cell_type": "code",
240
+ "execution_count": null,
241
+ "metadata": {
242
+ "id": "Ia0pOjLxRdqx"
243
+ },
244
+ "outputs": [],
245
+ "source": []
246
+ },
247
+ {
248
+ "cell_type": "code",
249
+ "execution_count": null,
250
+ "metadata": {
251
+ "id": "i3bmV8-IRd5t"
252
+ },
253
+ "outputs": [],
254
+ "source": [
255
+ "\n",
256
+ "Nikoi Afutu\n",
257
+ "5:43 PM (0 minutes ago)\n",
258
+ "to me\n",
259
+ "\n",
260
+ "# Create a label encoder for 'TENURE'\n",
261
+ "tenure_encoder = LabelEncoder()\n",
262
+ "\n",
263
+ "# Define a function to make predictions\n",
264
+ "def predict_churn(tenure, montant, freq_rech, revenue, arp_segment, freq, data_vol, on_net, orange, tigo, zone1, zone2, regularity, freq_top_pack):\n",
265
+ " # Encode 'TENURE' into numerical values\n",
266
+ " tenure_encoded = tenure_encoder.transform([tenure])[0]\n",
267
+ "\n",
268
+ " # Create a DataFrame with the input features\n",
269
+ " input_data = pd.DataFrame({\n",
270
+ " 'TENURE': [tenure_encoded],\n",
271
+ " 'MONTANT': [montant],\n",
272
+ " 'FREQUENCY_RECH': [freq_rech],\n",
273
+ " 'REVENUE': [revenue],\n",
274
+ " 'ARP_SEGMENT': [arp_segment],\n",
275
+ " 'FREQUENCY': [freq],\n",
276
+ " 'DATA_VOLUME': [data_vol],\n",
277
+ " 'ON_NET': [on_net],\n",
278
+ " 'ORANGE': [orange],\n",
279
+ " 'TIGO': [tigo],\n",
280
+ " 'ZONE1': [zone1],\n",
281
+ " 'ZONE2': [zone2],\n",
282
+ " 'REGULARITY': [regularity],\n",
283
+ " 'FREQ_TOP_PACK': [freq_top_pack]\n",
284
+ " })\n",
285
+ "\n",
286
+ " # Make predictions using the loaded model\n",
287
+ " prediction = model.predict(input_data)\n"
288
+ ]
289
+ },
290
+ {
291
+ "cell_type": "code",
292
+ "execution_count": null,
293
+ "metadata": {
294
+ "id": "5sNawztlReBg"
295
+ },
296
+ "outputs": [],
297
+ "source": []
298
+ },
299
+ {
300
+ "cell_type": "code",
301
+ "execution_count": null,
302
+ "metadata": {
303
+ "id": "kl8E75DUReSK"
304
+ },
305
+ "outputs": [],
306
+ "source": []
307
+ },
308
+ {
309
+ "cell_type": "code",
310
+ "execution_count": null,
311
+ "metadata": {
312
+ "id": "kOAWiEayRbL0"
313
+ },
314
+ "outputs": [],
315
+ "source": []
316
+ },
317
+ {
318
+ "cell_type": "markdown",
319
+ "metadata": {
320
+ "id": "twqCbs6fptdQ"
321
+ },
322
+ "source": [
323
+ "# Into pipeline"
324
+ ]
325
+ },
326
+ {
327
+ "cell_type": "code",
328
+ "execution_count": null,
329
+ "metadata": {
330
+ "id": "KEWdvsgdyQLt"
331
+ },
332
+ "outputs": [],
333
+ "source": []
334
+ }
335
+ ],
336
+ "metadata": {
337
+ "colab": {
338
+ "provenance": []
339
+ },
340
+ "kernelspec": {
341
+ "display_name": "Python 3",
342
+ "name": "python3"
343
+ },
344
+ "language_info": {
345
+ "codemirror_mode": {
346
+ "name": "ipython",
347
+ "version": 3
348
+ },
349
+ "file_extension": ".py",
350
+ "mimetype": "text/x-python",
351
+ "name": "python",
352
+ "nbconvert_exporter": "python",
353
+ "pygments_lexer": "ipython3",
354
+ "version": "3.11.2"
355
+ }
356
+ },
357
+ "nbformat": 4,
358
+ "nbformat_minor": 0
359
+ }