{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## **Churn Predictor UI with Gradio**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "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", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "#\n", "import gradio as gr\n", "import pandas as pd\n", "import pickle\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.ensemble import RandomForestClassifier\n", "from sklearn.preprocessing import StandardScaler, LabelEncoder\n", "from sklearn.impute import SimpleImputer\n", "from imblearn.over_sampling import RandomOverSampler\n", "from sklearn.preprocessing import FunctionTransformer\n", "import joblib" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "xtrain= pd.read_csv('Xtrains.csv')\n", "ytrain=pd.read_csv('Ytrains.csv')" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "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", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n", "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", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n" ] } ], "source": [ "# Loading Models\n", "with open(\"model.pkl\", \"rb\") as f:\n", "\tclf = pickle.load(f)" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
RandomForestClassifier(n_estimators=10)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ], "text/plain": [ "RandomForestClassifier(n_estimators=10)" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clf.fit(xtrain, ytrain.values.ravel())" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 621 }, "id": "5SkaWb6RRZ3i", "outputId": "c686baee-fb96-4cf1-e008-ceee011ec160" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "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", " warnings.warn(\n", "C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\gradio\\inputs.py:222: UserWarning: `optional` parameter is deprecated, and it has no effect\n", " super().__init__(\n", "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", " warnings.warn(\n", "C:\\Users\\Jamal\\AppData\\Roaming\\Python\\Python311\\site-packages\\gradio\\inputs.py:93: UserWarning: `optional` parameter is deprecated, and it has no effect\n", " super().__init__(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7903\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "{'text': 'Customer will not churn', 'entities': []}\n" ] } ], "source": [ "\n", "tenure_labels = {\n", " 0: \"3-6 months\",\n", " 1: \"6-9 months\",\n", " 2: \"9-12 months\",\n", " 3: \"12-15 months\",\n", " 4: \"15-18 months\",\n", " 5: \"18-21 months\",\n", " 6: \"21-24 months\",\n", " 7: \"> 24 months\"\n", "}\n", "\n", "# Reverse the mapping for predictions\n", "tenure_values = {v: k for k, v in tenure_labels.items()}\n", "\n", "def predict(tenure, montant, freq_rech, revenue, arpu, freq, data_vol, on_net, orange, tigo, freq_top_pack, regularity):\n", " \n", " tenure_value = tenure_values[tenure]\n", "\n", " \n", "\n", " input_df = pd.DataFrame({\n", " 'TENURE': [tenure_value],\n", " 'MONTANT': [montant],\n", " 'FREQUENCE_RECH': [freq_rech],\n", " 'REVENUE': [revenue],\n", " 'ARPU_SEGMENT': [arpu],\n", " 'FREQUENCE': [freq],\n", " 'DATA_VOLUME': [data_vol],\n", " 'ON_NET': [on_net],\n", " 'ORANGE': [orange],\n", " 'TIGO': [tigo],\n", " 'REGULARITY':[regularity],\n", " 'FREQ_TOP_PACK': [freq_top_pack]\n", " })\n", "\n", " prediction = clf.predict(input_df)\n", "\n", "\n", "\n", " churn_label = \"Customer will churn\" if prediction == 1 else \"Customer will not churn\"\n", " result = {\n", " 'text': churn_label, # Use the churn label as 'text'\n", " 'entities': [] # You can leave 'entities' as an empty list if no entities need highlighting\n", " }\n", " print(result)\n", " return result\n", "\n", "\n", "\n", "# Create a dropdown menu with labels\n", "tenure_dropdown = gr.inputs.Dropdown(list(tenure_labels.values()), label=\"TENURE\")\n", "\n", "iface = gr.Interface(\n", " fn=predict,\n", " inputs=[\n", " tenure_dropdown, # Dropdown instead of slider\n", " #gr.inputs.Slider(minimum=1, maximum=7, label=\"TENURE\"),\n", " gr.inputs.Slider(minimum=20, maximum=470000, label=\"MONTANT\"),\n", " gr.inputs.Slider(minimum=1, maximum=131, label=\"FREQUENCE_RECH\"),\n", " gr.inputs.Slider(minimum=1, maximum=530000, label=\"REVENUE\"),\n", " gr.inputs.Slider(minimum=0, maximum=2453, label=\"ARPU_SEGMENT\"),\n", " gr.inputs.Slider(minimum=1, maximum=91, label=\"FREQUENCE\"),\n", " gr.inputs.Slider(minimum=1, maximum=1702309, label=\"DATA_VOLUME\"),\n", " gr.inputs.Slider(minimum=0, maximum=51000, label=\"ON_NET\"),\n", " gr.inputs.Slider(minimum=0, maximum=12040, label=\"ORANGE\"),\n", " gr.inputs.Slider(minimum=0, maximum=4174, label=\"TIGO\"),\n", " gr.inputs.Slider(minimum=0, maximum=624, label=\"FREQ_TOP_PACK\"),\n", " gr.inputs.Slider(minimum=0, maximum=62, label=\"REGULARITY\")\n", " ],\n", " \n", " outputs=output,\n", " title=\"Team Paris Customer Churn Prediction App\",\n", " description=\"Let's Get Started With Some Predictions!\"\n", ")\n", "\n", "iface.launch()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Ia0pOjLxRdqx" }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "i3bmV8-IRd5t" }, "outputs": [], "source": [ "\n", "Nikoi Afutu\n", "5:43 PM (0 minutes ago)\n", "to me\n", "\n", "# Create a label encoder for 'TENURE'\n", "tenure_encoder = LabelEncoder()\n", "\n", "# Define a function to make predictions\n", "def predict_churn(tenure, montant, freq_rech, revenue, arp_segment, freq, data_vol, on_net, orange, tigo, zone1, zone2, regularity, freq_top_pack):\n", " # Encode 'TENURE' into numerical values\n", " tenure_encoded = tenure_encoder.transform([tenure])[0]\n", "\n", " # Create a DataFrame with the input features\n", " input_data = pd.DataFrame({\n", " 'TENURE': [tenure_encoded],\n", " 'MONTANT': [montant],\n", " 'FREQUENCY_RECH': [freq_rech],\n", " 'REVENUE': [revenue],\n", " 'ARP_SEGMENT': [arp_segment],\n", " 'FREQUENCY': [freq],\n", " 'DATA_VOLUME': [data_vol],\n", " 'ON_NET': [on_net],\n", " 'ORANGE': [orange],\n", " 'TIGO': [tigo],\n", " 'ZONE1': [zone1],\n", " 'ZONE2': [zone2],\n", " 'REGULARITY': [regularity],\n", " 'FREQ_TOP_PACK': [freq_top_pack]\n", " })\n", "\n", " # Make predictions using the loaded model\n", " prediction = model.predict(input_data)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "5sNawztlReBg" }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "kl8E75DUReSK" }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "kOAWiEayRbL0" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "twqCbs6fptdQ" }, "source": [ "# Into pipeline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "KEWdvsgdyQLt" }, "outputs": [], "source": [] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.2" } }, "nbformat": 4, "nbformat_minor": 0 }