omarcevi commited on
Commit
fc6cf5f
·
verified ·
1 Parent(s): 24d09a0

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. gradio_app.py +46 -3
gradio_app.py CHANGED
@@ -4,6 +4,8 @@ import pandas as pd
4
  import numpy as np
5
  from sklearn.metrics.pairwise import euclidean_distances
6
  import random
 
 
7
 
8
  model = joblib.load("churn_model.pkl")
9
  model_features = joblib.load("model_features.pkl")
@@ -160,14 +162,46 @@ def predict_churn(
160
  input_df = pd.DataFrame([[input_dict[col] for col in model_features]], columns=model_features)
161
  prediction = model.predict_proba(input_df)[0][1]
162
  score = round(prediction * 100, 2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  if score >= 50:
164
  comment = "Müşteri Kaybedilebilir."
165
  else:
166
  comment = "Müşteri Kayıp Riski Taşımıyor."
167
  result = f"Churn Riski: %{score} — {comment}"
 
168
  # Vector similarity
169
  similar_customers = find_similar_customers_vector(input_df.values[0], n=5)
170
- return result, similar_customers
171
 
172
  # Define options for dropdowns (Turkish values)
173
  phone_service_options = ["Evet", "Hayır"]
@@ -214,10 +248,19 @@ with gr.Blocks() as demo:
214
  with gr.Row():
215
  Contract = gr.Dropdown(contract_options, label="Sözleşme (Contract)")
216
  PaymentMethod = gr.Dropdown(payment_method_options, label="Ödeme Yöntemi (PaymentMethod)")
 
217
  autofill_btn = gr.Button("Rastgele Müşteri ile Doldur")
218
  submit_btn = gr.Button("Tahmin Et")
219
- output = gr.Textbox(label="Sonuç")
 
 
 
 
 
 
 
220
  similar_customers_table = gr.Dataframe(label="Benzer Müşteriler (İlk 5)")
 
221
  autofill_btn.click(
222
  autofill_random_customer,
223
  inputs=[],
@@ -230,7 +273,7 @@ with gr.Blocks() as demo:
230
  inputs=[tenure, monthly, total, PhoneService, gender, SeniorCitizen, Partner, Dependents, PaperlessBilling,
231
  MultipleLines, InternetService, OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport,
232
  StreamingTV, StreamingMovies, Contract, PaymentMethod],
233
- outputs=[output, similar_customers_table]
234
  )
235
 
236
  if __name__ == "__main__":
 
4
  import numpy as np
5
  from sklearn.metrics.pairwise import euclidean_distances
6
  import random
7
+ import plotly.graph_objects as go
8
+ import plotly.express as px
9
 
10
  model = joblib.load("churn_model.pkl")
11
  model_features = joblib.load("model_features.pkl")
 
162
  input_df = pd.DataFrame([[input_dict[col] for col in model_features]], columns=model_features)
163
  prediction = model.predict_proba(input_df)[0][1]
164
  score = round(prediction * 100, 2)
165
+
166
+ # Create gauge chart for churn risk
167
+ fig_gauge = go.Figure(go.Indicator(
168
+ mode = "gauge+number",
169
+ value = score,
170
+ domain = {'x': [0, 1], 'y': [0, 1]},
171
+ title = {'text': "Churn Riski"},
172
+ gauge = {
173
+ 'axis': {'range': [0, 100]},
174
+ 'bar': {'color': "darkblue"},
175
+ 'steps': [
176
+ {'range': [0, 30], 'color': "lightgreen"},
177
+ {'range': [30, 70], 'color': "yellow"},
178
+ {'range': [70, 100], 'color': "red"}
179
+ ],
180
+ 'threshold': {
181
+ 'line': {'color': "red", 'width': 4},
182
+ 'thickness': 0.75,
183
+ 'value': 50
184
+ }
185
+ }
186
+ ))
187
+
188
+ # Create pie chart for probability distribution
189
+ fig_pie = px.pie(
190
+ values=[score, 100-score],
191
+ names=['Churn Riski', 'Kalma Olasılığı'],
192
+ title='Müşteri Durumu Dağılımı',
193
+ color_discrete_sequence=['red', 'green']
194
+ )
195
+
196
  if score >= 50:
197
  comment = "Müşteri Kaybedilebilir."
198
  else:
199
  comment = "Müşteri Kayıp Riski Taşımıyor."
200
  result = f"Churn Riski: %{score} — {comment}"
201
+
202
  # Vector similarity
203
  similar_customers = find_similar_customers_vector(input_df.values[0], n=5)
204
+ return result, fig_gauge, fig_pie, similar_customers
205
 
206
  # Define options for dropdowns (Turkish values)
207
  phone_service_options = ["Evet", "Hayır"]
 
248
  with gr.Row():
249
  Contract = gr.Dropdown(contract_options, label="Sözleşme (Contract)")
250
  PaymentMethod = gr.Dropdown(payment_method_options, label="Ödeme Yöntemi (PaymentMethod)")
251
+
252
  autofill_btn = gr.Button("Rastgele Müşteri ile Doldur")
253
  submit_btn = gr.Button("Tahmin Et")
254
+
255
+ with gr.Row():
256
+ output = gr.Textbox(label="Sonuç")
257
+
258
+ with gr.Row():
259
+ gauge_plot = gr.Plot(label="Churn Risk Gauge")
260
+ pie_plot = gr.Plot(label="Probability Distribution")
261
+
262
  similar_customers_table = gr.Dataframe(label="Benzer Müşteriler (İlk 5)")
263
+
264
  autofill_btn.click(
265
  autofill_random_customer,
266
  inputs=[],
 
273
  inputs=[tenure, monthly, total, PhoneService, gender, SeniorCitizen, Partner, Dependents, PaperlessBilling,
274
  MultipleLines, InternetService, OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport,
275
  StreamingTV, StreamingMovies, Contract, PaymentMethod],
276
+ outputs=[output, gauge_plot, pie_plot, similar_customers_table]
277
  )
278
 
279
  if __name__ == "__main__":