Wajahat698 commited on
Commit
8c00937
·
verified ·
1 Parent(s): df05c7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +342 -259
app.py CHANGED
@@ -1316,17 +1316,13 @@ def bold_high_impact_row(row):
1316
  except:
1317
  pass
1318
  return [''] * len(row)
1319
- import gradio as gr
1320
- import pandas as pd
1321
- from itertools import zip_longest
1322
-
1323
 
1324
- import gradio as gr
1325
- import pandas as pd
1326
- from itertools import zip_longest
1327
 
1328
  def variable_outputs(file_inputs):
1329
- # Run processing
 
 
 
1330
  (
1331
  img_bucketfull_list,
1332
  img_trust_list,
@@ -1336,11 +1332,16 @@ def variable_outputs(file_inputs):
1336
  img_satisfaction_list,
1337
  df_builder_pivot_list,
1338
  output_text_list,
1339
- ) = batch_file_processing(file_inputs)
 
 
 
1340
 
1341
- k = len(file_inputs)
1342
- output_components = []
 
1343
 
 
1344
  for row, (
1345
  img_bucketfull,
1346
  img_trust,
@@ -1350,49 +1351,115 @@ def variable_outputs(file_inputs):
1350
  img_satisfaction,
1351
  df_builder_pivot,
1352
  output_text,
1353
- ) in enumerate(zip_longest(
1354
- img_bucketfull_list,
1355
- img_trust_list,
1356
- img_nps_list,
1357
- img_loyalty_list,
1358
- img_consideration_list,
1359
- img_satisfaction_list,
1360
- df_builder_pivot_list,
1361
- output_text_list,
1362
- )):
1363
- dataset_name = file_inputs[row].split("/")[-1]
1364
-
1365
- # Header and description
1366
- output_components.append(gr.Markdown("<span style='font-size:20px; font-weight:bold;'>Trust and NPS Drivers</span>", visible=True))
1367
- output_components.append(gr.Markdown("The analysis identifies the TrustLogic® dimensions that are most effective in driving your audience's likelihood to recommend and trust you.", visible=True))
1368
-
1369
- # Plots
1370
- output_components += [
1371
- gr.Image(value=img_trust, type="pil", label="Trust Drivers", visible=img_trust is not None),
1372
- gr.Image(value=img_nps, type="pil", label="NPS Drivers", visible=img_nps is not None),
1373
- gr.Image(value=img_loyalty, type="pil", label="Loyalty Drivers", visible=img_loyalty is not None),
1374
- gr.Image(value=img_consideration, type="pil", label="Consideration Drivers", visible=img_consideration is not None),
1375
- gr.Image(value=img_satisfaction, type="pil", label="Satisfaction Drivers", visible=img_satisfaction is not None),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1376
  ]
1377
 
1378
- # Table section
 
 
1379
  if isinstance(df_builder_pivot, pd.DataFrame):
1380
- output_components.append(gr.Markdown("<span style='font-size:20px; font-weight:bold;'> What to say and do to build your trust and Net Promoter Score </span>", visible=True))
1381
- output_components.append(gr.Markdown("<span style='font-size:17px; font-weight:bold;'>The features and messages that improve your NPS and trust most effectively. The key ones are marked in bold.</span>", visible=True))
1382
- output_components.append(gr.Dataframe(value=df_builder_pivot, label=f"{dataset_name}", visible=True, height=800))
1383
- else:
1384
- output_components += [
1385
- gr.Markdown("", visible=False),
1386
- gr.Markdown("", visible=False),
1387
- gr.Dataframe(value=None, label="", visible=False),
1388
- ]
 
 
 
 
 
 
1389
 
1390
- # Additional notes if present
1391
- if isinstance(output_text, str) and output_text.strip():
1392
- output_components.append(gr.Markdown(f"**Additional Notes:**\n\n{output_text.strip()}", visible=True))
1393
 
1394
- # Pad outputs to match expected component count if needed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1395
  plots_invisible = [
 
 
 
1396
  gr.Markdown("", visible=False),
1397
  gr.Markdown("", visible=False),
1398
  gr.Image(label="Trust Drivers", visible=False),
@@ -1400,27 +1467,89 @@ def variable_outputs(file_inputs):
1400
  gr.Image(label="Loyalty Drivers", visible=False),
1401
  gr.Image(label="Consideration Drivers", visible=False),
1402
  gr.Image(label="Satisfaction Drivers", visible=False),
 
1403
  gr.Markdown("", visible=False),
1404
  gr.Markdown("", visible=False),
1405
  gr.Dataframe(value=None, label=" ", visible=False),
1406
  ]
1407
 
1408
- return output_components + plots_invisible * (max_outputs - k)
 
1409
 
1410
  def reset_outputs():
1411
- return [
1412
- gr.Markdown(visible=True), # markdown_3
1413
- gr.Markdown(visible=True), # markdown_4
1414
- gr.Image(visible=True), # trust_plot
1415
- gr.Image(visible=True), # nps_plot
1416
- gr.Image(visible=True), # loyalty_plot
1417
- gr.Image(visible=True), # consideration_plot
1418
- gr.Image(visible=True), # satisfaction_plot
1419
- gr.Textbox(visible=True), # summary_text
1420
- gr.Markdown(visible=True), # markdown_5
1421
- gr.Markdown(visible=True), # markdown_6
1422
- gr.Dataframe(visible=True) # df_builder_pivot
1423
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1424
 
1425
 
1426
  def data_processing(file_path):
@@ -2275,10 +2404,6 @@ def display_trust_score_2():
2275
  from sklearn.linear_model import LinearRegression
2276
  from sklearn.metrics import r2_score
2277
 
2278
- import base64
2279
-
2280
-
2281
-
2282
  import base64
2283
 
2284
  def reset_variables():
@@ -2427,200 +2552,158 @@ def load_nps_and_r2(file_path):
2427
  return nps_img, r2_img
2428
 
2429
 
 
2430
  with gr.Blocks(css=css, js=js_func) as demo:
2431
- gr.HTML("""
2432
- <style>
2433
- #trust_driver_img, #nps_driver_img {
2434
- display: inline-block;
2435
- width: 49%;
2436
- margin-right: 1%;
2437
- vertical-align: top;
2438
- }
2439
- </style>
2440
- """)
2441
-
2442
- # 🔁 States
2443
- vw_customers_state11 = gr.State(value=["example_files/VW Owners.xlsx"])
2444
- vw_prospects_state12 = gr.State(value=["example_files/Volkswagen Non Customers.xlsx"])
2445
- vw_customers_state = gr.State(value=["VW Owners.xlsx"]) # Just the filename
2446
- vw_prospects_state = gr.State(value=["Volkswagen Non Customers.xlsx"]) # Just the filename
2447
- selected_dataset_ai = gr.State(value="VW Owners.xlsx")
2448
- chat_history = gr.State(value=[])
2449
- data_visible_state = gr.State(value=False)
2450
-
2451
- # Title and intro
2452
- with gr.Column():
2453
- gr.Markdown("""
2454
- <h2 style="text-align: center; font-size: 2.25rem; font-weight: 600;">
2455
- TrustLogic AI Demo
2456
- </h2>
2457
- """)
2458
- gr.Markdown("### Experience TrustLogic AI")
2459
- gr.Markdown("Experience how quick and easy it is to build the right trust based on insight with our TrustLogic AI")
2460
-
2461
- # Dataset selection buttons
2462
- with gr.Column():
2463
- with gr.Row():
2464
- vw_customers_btn = gr.Button("VW Owners", elem_id="vw_customers_btn")
2465
- vw_prospects_btn = gr.Button("VW Prospects", elem_id="vw_prospects_btn")
2466
-
2467
- # 🧠 Chat section
2468
- with gr.Column(elem_id="chat_container"):
2469
- prompt_textbox = gr.Textbox(value=predefined_prompt, scale=4, label="Insert your prompt", visible=True)
2470
- submit_button = gr.Button("Submit")
2471
- bot = gr.Chatbot(placeholder=placeholder_text)
2472
-
2473
- # Button to show data
2474
- show_data_btn = gr.Button("Show the data", variant="primary", size="lg")
2475
-
2476
- # Data analysis section (initially hidden)
2477
- with gr.Column(visible=False) as data_section:
2478
- gr.Markdown("### Uncover the underlying data used for the prompt")
2479
- gr.Markdown("Quickly identify what drives your NPS and trust across different segments using the automated analyser.")
2480
- gr.Markdown("""
2481
- <span style="font-size:15px;">Volkswagen Example</span><br>
2482
- As a default, the analysis displays <strong>Volkswagen Owner</strong> results.
2483
- To trigger the analysis for <strong>Prospects</strong>, toggle to 'VW Prospects'.
2484
- """)
2485
-
2486
- with gr.Row(equal_height=True):
2487
- with gr.Column(scale=1):
2488
- nps_img_output = gr.HTML()
2489
-
2490
- with gr.Column(scale=1):
2491
- trust_score_output = gr.HTML()
2492
-
2493
- with gr.Column(scale=1):
2494
- gr.Markdown("""<div style='text-align: center;'><h3>How much of your NPS is determined by TrustLogic®</h3></div>""")
2495
- trust_r2_img = gr.HTML()
2496
-
2497
- # Move outputs components inside data_section
2498
- markdown_3 = gr.Markdown(visible=True)
2499
- markdown_4 = gr.Markdown(visible=True)
2500
- trust_plot = gr.Image(value=None, label="Trust Drivers", visible=True)
2501
- nps_plot = gr.Image(value=None, label="NPS Drivers", visible=True)
2502
- loyalty_plot = gr.Image(value=None, label="Loyalty Drivers", visible=True)
2503
- consideration_plot = gr.Image(value=None, label="Consideration Drivers", visible=True)
2504
- satisfaction_plot = gr.Image(value=None, label="Satisfaction Drivers", visible=True)
2505
- summary_text = gr.Textbox(value=None, label="Analysis Summary", visible=True)
2506
- markdown_5 = gr.Markdown(visible=True)
2507
- markdown_6 = gr.Markdown(visible=True)
2508
- df_builder_pivot = gr.Dataframe(value=None, label="", visible=True)
2509
-
2510
- # Create outputs list for functions
2511
- outputs = [
2512
- markdown_3, markdown_4, trust_plot, nps_plot, loyalty_plot,
2513
- consideration_plot, satisfaction_plot, summary_text,
2514
- markdown_5, markdown_6, df_builder_pivot
2515
- ]
2516
-
2517
- # Modified process_examples to return values
2518
- def process_examples_values(file_name):
2519
- output_components = process_examples(file_name)
2520
- # Extract values from components
2521
- values = []
2522
- for comp in output_components[:11]: # Only first 11 components
2523
- if hasattr(comp, 'value'):
2524
- values.append(comp.value)
2525
- else:
2526
- values.append(None)
2527
- return values
2528
-
2529
- # Show/hide data section functionality
2530
- def toggle_data_section(visible):
2531
- new_visible = not visible
2532
- button_text = "Hide the data" if new_visible else "Show the data"
2533
- return gr.update(visible=new_visible), button_text, new_visible
2534
-
2535
- show_data_btn.click(
2536
- fn=toggle_data_section,
2537
- inputs=data_visible_state,
2538
- outputs=[data_section, show_data_btn, data_visible_state]
2539
- )
2540
-
2541
- submit_button.click(
2542
- fn=chatbot_response,
2543
- inputs=[prompt_textbox, chat_history, selected_dataset_ai],
2544
- outputs=[bot, chat_history]
2545
- )
2546
-
2547
- ## All widget functions here ##
2548
-
2549
- vw_customers_btn.click(
2550
- fn=display_trust_score_1,
2551
- inputs=[],
2552
- outputs=trust_score_output,
2553
- )
2554
-
2555
- vw_prospects_btn.click(
2556
- fn=display_trust_score_2,
2557
- inputs=[],
2558
- outputs=trust_score_output,
2559
- )
2560
-
2561
- vw_customers_btn.click(
2562
- fn=process_examples_values,
2563
- inputs=[vw_customers_state],
2564
- outputs=outputs,
2565
- )
2566
-
2567
- vw_prospects_btn.click(
2568
- fn=process_examples_values,
2569
- inputs=[vw_prospects_state],
2570
- outputs=outputs,
2571
- )
2572
-
2573
- def set_vw_owners():
2574
- return "VW Owners.xlsx"
2575
-
2576
- def set_vw_prospects():
2577
- return "Volkswagen Non Customers.xlsx"
2578
-
2579
- vw_customers_btn.click(
2580
- fn=set_vw_owners,
2581
- inputs=[],
2582
- outputs=selected_dataset_ai
2583
- )
2584
-
2585
- vw_prospects_btn.click(
2586
- fn=set_vw_prospects,
2587
- inputs=[],
2588
- outputs=selected_dataset_ai
2589
- )
2590
-
2591
- vw_customers_btn.click(
2592
- fn=lambda f: vwload_nps_and_r2(f[0]),
2593
- inputs=[vw_customers_state11],
2594
- outputs=[nps_img_output, trust_r2_img],
2595
- )
2596
-
2597
- vw_prospects_btn.click(
2598
- fn=lambda f: load_nps_and_r2(f[0]),
2599
- inputs=[vw_prospects_state12],
2600
- outputs=[nps_img_output, trust_r2_img],
2601
- )
2602
-
2603
- demo.load(
2604
- fn=lambda f: vwload_nps_and_r2(f[0]),
2605
- inputs=[vw_customers_state11],
2606
- outputs=[nps_img_output, trust_r2_img],
2607
- )
2608
-
2609
- demo.load(
2610
- fn=display_trust_score_1,
2611
- inputs=[],
2612
- outputs=trust_score_output
2613
- )
2614
-
2615
- demo.load(
2616
- fn=process_examples_values,
2617
- inputs=[vw_customers_state],
2618
- outputs=outputs
2619
- )
2620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2621
  try:
2622
- demo.launch(server_name="0.0.0.0")
2623
  except Exception as e:
2624
- logger.error(f"Error launching Gradio app: {e}")
2625
- raise e
2626
-
 
1316
  except:
1317
  pass
1318
  return [''] * len(row)
 
 
 
 
1319
 
 
 
 
1320
 
1321
  def variable_outputs(file_inputs):
1322
+
1323
+ file_inputs_single = file_inputs
1324
+
1325
+ # Call batch file processing and get analysis results
1326
  (
1327
  img_bucketfull_list,
1328
  img_trust_list,
 
1332
  img_satisfaction_list,
1333
  df_builder_pivot_list,
1334
  output_text_list,
1335
+ ) = batch_file_processing(file_inputs_single)
1336
+
1337
+ # Get number of datasets uploaded
1338
+ k = len(file_inputs_single)
1339
 
1340
+ # Container for visible plots
1341
+ global plots_visible
1342
+ plots_visible = []
1343
 
1344
+ # Use zip_longest to iterate over the lists, padding with None
1345
  for row, (
1346
  img_bucketfull,
1347
  img_trust,
 
1351
  img_satisfaction,
1352
  df_builder_pivot,
1353
  output_text,
1354
+ ) in enumerate(
1355
+ zip_longest(
1356
+ img_bucketfull_list,
1357
+ img_trust_list,
1358
+ img_nps_list,
1359
+ img_loyalty_list,
1360
+ img_consideration_list,
1361
+ img_satisfaction_list,
1362
+ df_builder_pivot_list,
1363
+ output_text_list,
1364
+ )
1365
+ ):
1366
+ # Get dataset name
1367
+ dataset_name = file_inputs_single[row].split("/")[-1]
1368
+ global plots
1369
+ # Based on the number of files uploaded, determine the content of each textbox
1370
+ plots = [
1371
+
1372
+ gr.Markdown(
1373
+ "<span style='font-size:20px; font-weight:bold;'>Trust and NPS Drivers</span>",
1374
+ visible=True,
1375
+ ),
1376
+ gr.Markdown(
1377
+ """
1378
+ The analysis identifies the TrustLogic® dimensions that are most effective in driving your audience's likelihood to recommend and trust you
1379
+ """,
1380
+ visible=True,
1381
+ ),
1382
+
1383
+ # ✅ Side-by-side Trust & NPS drivers
1384
+ gr.Image(
1385
+ value=combine_two_images_horizontally(img_trust, "Trust Drivers", img_nps, "NPS Drivers"),
1386
+ type="pil",
1387
+ label="Trust + NPS Drivers",
1388
+ visible=True,
1389
+ ),
1390
+ gr.Image(
1391
+ value=None,
1392
+ type="pil",
1393
+ visible=False,
1394
+ ),
1395
+
1396
+
1397
+ gr.Image(
1398
+ value=None,
1399
+ type="pil",
1400
+ visible=False,
1401
+ ),
1402
+ gr.Image(
1403
+ value=None,
1404
+ type="pil",
1405
+ visible=False,
1406
+ ),
1407
+ gr.Image(
1408
+ value=None,
1409
+ type="pil",
1410
+ visible=False,
1411
+ ),
1412
+ gr.Textbox(
1413
+ value=output_text,
1414
+ visible=False,
1415
+ ),
1416
  ]
1417
 
1418
+ # add current plots to container
1419
+ plots_visible += plots
1420
+
1421
  if isinstance(df_builder_pivot, pd.DataFrame):
1422
+ logger.debug(f"df_builder_pivot: {df_builder_pivot}")
1423
+
1424
+ markdown_5 = gr.Markdown(
1425
+ "<span style='font-size:20px; font-weight:bold;'> What to say and do to build your trust and Net Promoter Score </span>",
1426
+ visible=True,
1427
+ )
1428
+
1429
+ markdown_6 = gr.Markdown(
1430
+ "<span style='font-size:17px; font-weight:bold;'>The features and messages that improve your NPS and trust most effectively. They key ones are marked in bold.</span>",
1431
+ #+ "<br>In the table, use the little arrow in each column to toggle the most to least effective TrustBuilders® to fill each Trust Bucket®. Your focus is only on the Trust Bucket® with the highest driver impact. "
1432
+ # + "<br> Note: Even if Trust Buckets® for Customers and Prospects overlap, the most effective statements are very different. This provides clear guidance for acquisition versus loyalty activities.",
1433
+ visible=True,
1434
+ )
1435
+
1436
+ styled_df = df_builder_pivot.style.apply(bold_high_impact_row, axis=1)
1437
 
 
 
 
1438
 
1439
+ table_builder_2 = gr.Dataframe(
1440
+ value=styled_df,
1441
+ headers=list(df_builder_pivot.columns),
1442
+ interactive=False,
1443
+ label=f"{dataset_name}",
1444
+ visible=True,
1445
+ height=800,
1446
+ wrap=True,
1447
+ )
1448
+
1449
+
1450
+
1451
+ plots_visible.append(markdown_5)
1452
+ plots_visible.append(markdown_6)
1453
+ plots_visible.append(table_builder_2)
1454
+ else:
1455
+ plots_visible.append(gr.Markdown("", visible=False))
1456
+ plots_visible.append(gr.Markdown("", visible=False))
1457
+ plots_visible.append(gr.Dataframe(value=None, label="", visible=False))
1458
+
1459
  plots_invisible = [
1460
+ gr.Markdown("", visible=False),
1461
+ gr.Markdown("", visible=False),
1462
+ gr.Image(label="Trust Buckets", visible=False),
1463
  gr.Markdown("", visible=False),
1464
  gr.Markdown("", visible=False),
1465
  gr.Image(label="Trust Drivers", visible=False),
 
1467
  gr.Image(label="Loyalty Drivers", visible=False),
1468
  gr.Image(label="Consideration Drivers", visible=False),
1469
  gr.Image(label="Satisfaction Drivers", visible=False),
1470
+ gr.Textbox(label="Analysis Summary", visible=False),
1471
  gr.Markdown("", visible=False),
1472
  gr.Markdown("", visible=False),
1473
  gr.Dataframe(value=None, label=" ", visible=False),
1474
  ]
1475
 
1476
+ return plots_visible + plots_invisible * (max_outputs - k)
1477
+
1478
 
1479
  def reset_outputs():
1480
+ # Reset outputs
1481
+ outputs = []
1482
+
1483
+ # Create fixed dummy components
1484
+
1485
+
1486
+ markdown_3 = gr.Markdown(
1487
+ "<span style='font-size:20px; font-weight:bold;'>Trust and NPS Drivers</span>",
1488
+ visible=True,
1489
+ )
1490
+ markdown_4 = gr.Markdown(
1491
+ """
1492
+ This analysis shows which Trust Buckets® are most effective in building trust and improving your key performance indicators (KPIs).
1493
+ <br><br>
1494
+ The middle line is the average importance. The bars extending to the right show which Trust Buckets® are most important. The higher the percentage, the more important the Trust Bucket® is to your audience.
1495
+ """,
1496
+ visible=True,
1497
+ )
1498
+ trust_plot = gr.Image(value=None, label="Trust Drivers", visible=False)
1499
+ nps_plot = gr.Image(value=None, label="NPS Drivers", visible=False)
1500
+ loyalty_plot = gr.Image(value=None, label="Loyalty Drivers", visible=False)
1501
+ consideration_plot = gr.Image(
1502
+ value=None, label="Consideration Drivers", visible=False
1503
+ )
1504
+ satisfaction_plot = gr.Image(value=None, label="Satisfaction Drivers", visible=False)
1505
+ summary_text = gr.Textbox(value=None, label="Analysis Summary", visible=False)
1506
+
1507
+ markdown_5 = gr.Markdown(
1508
+ "<span style='font-size:20px; font-weight:bold;'>TrustBuilders®",
1509
+ visible=True,
1510
+ )
1511
+ markdown_6 = gr.Markdown(
1512
+ "These are the specific reasons to trust and recommend. They can be your brand values, features, attributes, programs, and messages. "
1513
+ + "<br>For practical purposes, they tell you exactly what to do and say to build more trust and improve your KPIs. "
1514
+ + "<br>In the table, use the little arrows to toggle by Trust Bucket® or Trust Builder® importance. "
1515
+ + "<br>Tip: Compare Owners and Prospects. Even though some of the Trust Buckets® are the same, the Trust Builders® are very different.",
1516
+ visible=True,
1517
+ )
1518
+
1519
+
1520
+ df_builder_pivot = gr.Dataframe(value=None, label="", visible=True)
1521
+
1522
+
1523
+ outputs.append(markdown_3)
1524
+ outputs.append(markdown_4)
1525
+ outputs.append(trust_plot)
1526
+ outputs.append(nps_plot)
1527
+ outputs.append(loyalty_plot)
1528
+ outputs.append(consideration_plot)
1529
+ outputs.append(satisfaction_plot)
1530
+ outputs.append(summary_text)
1531
+ outputs.append(markdown_5)
1532
+ outputs.append(markdown_6)
1533
+ outputs.append(df_builder_pivot)
1534
+
1535
+ # invisible from second set onwards
1536
+ for i in range(1, max_outputs):
1537
+ outputs.append(gr.Markdown("", visible=False))
1538
+ outputs.append(gr.Markdown("", visible=False))
1539
+ outputs.append(gr.Image(value=None, label="", visible=False))
1540
+ outputs.append(gr.Markdown("", visible=False))
1541
+ outputs.append(gr.Markdown("", visible=False))
1542
+ outputs.append(gr.Image(value=None, label="", visible=False))
1543
+ outputs.append(gr.Image(value=None, label="", visible=False))
1544
+ outputs.append(gr.Image(value=None, label="", visible=False))
1545
+ outputs.append(gr.Image(value=None, label="", visible=False))
1546
+ outputs.append(gr.Image(value=None, label="", visible=False))
1547
+ outputs.append(gr.Textbox(value=None, label="", visible=False))
1548
+ outputs.append(gr.Markdown("", visible=False))
1549
+ outputs.append(gr.Markdown("", visible=False))
1550
+ outputs.append(gr.Dataframe(value=None, label="", visible=False))
1551
+
1552
+ return outputs
1553
 
1554
 
1555
  def data_processing(file_path):
 
2404
  from sklearn.linear_model import LinearRegression
2405
  from sklearn.metrics import r2_score
2406
 
 
 
 
 
2407
  import base64
2408
 
2409
  def reset_variables():
 
2552
  return nps_img, r2_img
2553
 
2554
 
2555
+
2556
  with gr.Blocks(css=css, js=js_func) as demo:
2557
+ gr.HTML("""
2558
+ <style>
2559
+ #trust_driver_img, #nps_driver_img {
2560
+ display: inline-block;
2561
+ width: 49%;
2562
+ margin-right: 1%;
2563
+ vertical-align: top;
2564
+ }
2565
+ </style>
2566
+ """)
2567
+
2568
+ # Title and intro
2569
+ with gr.Column():
2570
+ gr.Markdown("""
2571
+ <h2 style="text-align: center; font-size: 2.25rem; font-weight: 600;">
2572
+ What drives your NPS and trust?
2573
+ </h2>
2574
+ """)
2575
+ gr.Markdown("Quickly identify what drives your NPS and trust across different segments using the automated analyser.")
2576
+ gr.Markdown("""
2577
+ <span style="font-size:15px;">Volkswagen Example</span><br>
2578
+ As a default, the analysis displays <strong>Volkswagen Owner</strong> results.
2579
+ To trigger the analysis for <strong>Prospects</strong>, toggle to ‘VW Prospects’.
2580
+ """)
2581
+
2582
+ with gr.Column():
2583
+ with gr.Row():
2584
+ vw_customers_btn = gr.Button("VW Owners", elem_id="vw_customers_btn")
2585
+ vw_prospects_btn = gr.Button("VW Prospects", elem_id="vw_prospects_btn")
2586
+
2587
+ with gr.Row(equal_height=True):
2588
+ with gr.Column(scale=1):
2589
+ nps_img_output = gr.HTML()
2590
+
2591
+
2592
+
2593
+ with gr.Column(scale=1):
2594
+ trust_score_output = gr.HTML()
2595
+
2596
+ with gr.Column(scale=1):
2597
+ gr.Markdown("""<div style='text-align: center;'><h3>How much of your NPS is determined by TrustLogic®</h3></div>""")
2598
+ trust_r2_img = gr.HTML()
2599
+
2600
+
2601
+ with gr.Column():
2602
+ outputs = reset_outputs()
2603
+
2604
+ # 🔁 States
2605
+ vw_customers_state11 = gr.State(value=["example_files/VW Owners.xlsx"])
2606
+ vw_prospects_state12 = gr.State(value=["example_files/Volkswagen Non Customers.xlsx"])
2607
+ vw_customers_state = gr.State(value=["VW Owners.xlsx"])
2608
+ vw_prospects_state = gr.State(value=["Volkswagen Non Customers.xlsx"])
2609
+ selected_dataset_ai = gr.State(value="VW Owners.xlsx") # Matches dictionary key
2610
+
2611
+ chat_history = gr.State(value=[])
2612
+
2613
+ # 🧠 Chat section
2614
+ with gr.Column(elem_id="chat_container"):
2615
+ gr.Markdown("### Test-drive the results in the TrustLogicAI")
2616
+ gr.Markdown("Our AI uses the analysis results to generate trust-optimised content.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2617
 
2618
+ prompt_textbox = gr.Textbox(value=predefined_prompt, scale=4, label="Insert your prompt", visible=True)
2619
+ submit_button = gr.Button("Submit")
2620
+ bot = gr.Chatbot(placeholder=placeholder_text)
2621
+
2622
+
2623
+
2624
+ submit_button.click(
2625
+ fn=chatbot_response,
2626
+ inputs=[prompt_textbox, chat_history, selected_dataset_ai],
2627
+ outputs=[bot, chat_history]
2628
+ )
2629
+
2630
+
2631
+
2632
+ ## All widget functions here ##
2633
+
2634
+ vw_customers_btn.click(
2635
+ fn=display_trust_score_1,
2636
+ inputs=[],
2637
+ outputs=trust_score_output,
2638
+ )
2639
+
2640
+ vw_prospects_btn.click(
2641
+ fn=display_trust_score_2,
2642
+ inputs=[],
2643
+ outputs=trust_score_output,
2644
+ )
2645
+
2646
+ vw_customers_btn.click(
2647
+ fn=process_examples,
2648
+ inputs=[vw_customers_state],
2649
+ outputs= outputs,
2650
+ )
2651
+
2652
+ vw_prospects_btn.click(
2653
+ fn=process_examples,
2654
+ inputs=[vw_prospects_state],
2655
+ outputs= outputs,
2656
+ )
2657
+ def set_vw_owners():
2658
+ return "VW Owners.xlsx"
2659
+
2660
+ def set_vw_prospects():
2661
+ return "Volkswagen Non Customers.xlsx"
2662
+
2663
+
2664
+ vw_customers_btn.click(
2665
+ fn=set_vw_owners,
2666
+ inputs=[],
2667
+ outputs=selected_dataset_ai
2668
+ )
2669
+
2670
+ vw_prospects_btn.click(
2671
+ fn=set_vw_prospects,
2672
+ inputs=[],
2673
+ outputs=selected_dataset_ai
2674
+ )
2675
+ vw_customers_btn.click(
2676
+ fn=lambda f: vwload_nps_and_r2(f[0]),
2677
+ inputs=[vw_customers_state11],
2678
+ outputs=[nps_img_output, trust_r2_img],
2679
+ )
2680
+
2681
+ vw_prospects_btn.click(
2682
+ fn=lambda f: load_nps_and_r2(f[0]),
2683
+ inputs=[vw_prospects_state12],
2684
+ outputs=[nps_img_output, trust_r2_img],
2685
+ )
2686
+
2687
+
2688
+ demo.load(
2689
+ fn=lambda f: vwload_nps_and_r2(f[0]),
2690
+ inputs=[vw_customers_state11],
2691
+ outputs=[nps_img_output, trust_r2_img],
2692
+ )
2693
+ demo.load(
2694
+ fn=display_trust_score_1,
2695
+ inputs=[],
2696
+ outputs=trust_score_output
2697
+ )
2698
+
2699
+ demo.load(
2700
+ fn=process_examples,
2701
+ inputs=[vw_customers_state],
2702
+ outputs=outputs
2703
+ )
2704
+
2705
  try:
2706
+ demo.launch(server_name="0.0.0.0")
2707
  except Exception as e:
2708
+ logger.error(f"Error launching Gradio app: {e}")
2709
+ raise e