Wajahat698 commited on
Commit
3bcd667
·
verified ·
1 Parent(s): fe60076

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +237 -216
app.py CHANGED
@@ -2406,23 +2406,25 @@ from sklearn.metrics import r2_score
2406
 
2407
  import base64
2408
 
 
 
2409
  def reset_variables():
2410
- """
2411
- Reset global variables to handle dataset changes.
2412
- """
2413
- global selected_dataset_ai
2414
- selected_dataset_ai = None
2415
-
2416
  def update_ai_dataset_selection(selection):
2417
- global selected_dataset_ai # Ensure the global variable is updated
2418
- reset_variables()
2419
- chat_history.clear()
2420
-
2421
- if selection == "VW Owners":
2422
- selected_dataset_ai = vw_customers_state.value[0] # Use the customer dataset
2423
- elif selection == "VW Prospects":
2424
- selected_dataset_ai = vw_prospects_state.value[0] # Use the prospects dataset
2425
- return selected_dataset_ai
2426
 
2427
  placeholder_text = """"""
2428
 
@@ -2445,85 +2447,85 @@ Style:
2445
  """
2446
  # Text input box for the user to enter their prompt
2447
  prompt_textbox = gr.Textbox(
2448
- value=predefined_prompt,
2449
- scale=4,
2450
- label="Insert your prompt",
2451
- visible=True,
2452
  )
2453
  submit_button = gr.Button("Submit")
2454
  bot = gr.Chatbot(placeholder=placeholder_text)
2455
 
2456
  js_func = """
2457
  function refresh() {
2458
- // Force light theme if not already
2459
- const url = new URL(window.location);
2460
- if (url.searchParams.get('__theme') !== 'light') {
2461
- url.searchParams.set('__theme', 'light');
2462
- window.location.href = url.href;
2463
- return;
2464
- }
2465
-
2466
- // Highlight toggle
2467
- const btn1 = document.querySelector("#vw_customers_btn");
2468
- const btn2 = document.querySelector("#vw_prospects_btn");
2469
-
2470
- if (btn1 && btn2) {
2471
- btn1.classList.add("active-btn"); // Default on load
2472
-
2473
- btn1.addEventListener("click", () => {
2474
- btn1.classList.add("active-btn");
2475
- btn2.classList.remove("active-btn");
2476
- });
2477
-
2478
- btn2.addEventListener("click", () => {
2479
- btn2.classList.add("active-btn");
2480
- btn1.classList.remove("active-btn");
2481
- });
2482
- }
2483
  }
2484
  """
2485
 
2486
 
2487
  css = """
2488
  #vw_customers_btn, #vw_prospects_btn {
2489
- border: 2px solid #ccc;
2490
- padding: 10px 20px;
2491
- font-size: 16px;
2492
- font-weight: 500;
2493
- border-radius: 6px;
2494
- color: #333;
2495
- background-color: #fff;
2496
- margin: 0 5px;
2497
- transition: all 0.25s ease;
2498
- min-width: 140px;
2499
- text-align: center;
2500
  }
2501
 
2502
  /* Highlighted (clicked) button — with teal text */
2503
  .active-btn {
2504
- background-color: #e0f7f5 !important; /* Light teal background */
2505
- color: teal !important; /* Teal text */
2506
- border-color: teal !important;
2507
- box-shadow: 0 0 6px rgba(0, 128, 128, 0.3);
2508
  }
2509
 
2510
  /* Optional hover effect */
2511
  #vw_customers_btn:hover, #vw_prospects_btn:hover {
2512
- background-color: #f9f9f9;
2513
  }
2514
 
2515
  .gr-button:has(svg) {
2516
- display: none !important;
2517
  }
2518
  .gr-image-label {
2519
- display: none !important;
2520
  }
2521
  #chat_container {
2522
- max-height: 900px;
2523
- overflow-y: auto;
2524
- margin-top: 0 !important;
2525
- margin-bottom: 0 !important;
2526
- padding-bottom: 0 !important;
2527
  }
2528
  """
2529
 
@@ -2531,179 +2533,198 @@ css = """
2531
 
2532
 
2533
  def highlight_button(button_name):
2534
- return gr.Button.update(variant="primary")
2535
 
2536
  # 🧠 Chatbot backend
2537
 
2538
 
2539
 
2540
  def vwload_nps_and_r2(file_path):
2541
- nps_img = calculate_nps_image_from_excel(file_path)
2542
- r2_img = vwcalculate_r2_image_from_excel(file_path)
2543
- return nps_img,r2_img
2544
 
2545
 
2546
 
2547
 
2548
  def load_nps_and_r2(file_path):
2549
- nps_img = calculate_nps_image_from_excel(file_path)
2550
- r2_img = calculate_r2_image_from_excel(file_path)
2551
 
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
 
2406
 
2407
  import base64
2408
 
2409
+
2410
+
2411
  def reset_variables():
2412
+ """
2413
+ Reset global variables to handle dataset changes.
2414
+ """
2415
+ global selected_dataset_ai
2416
+ selected_dataset_ai = None
2417
+
2418
  def update_ai_dataset_selection(selection):
2419
+ global selected_dataset_ai # Ensure the global variable is updated
2420
+ reset_variables()
2421
+ chat_history.clear()
2422
+
2423
+ if selection == "VW Owners":
2424
+ selected_dataset_ai = vw_customers_state.value[0] # Use the customer dataset
2425
+ elif selection == "VW Prospects":
2426
+ selected_dataset_ai = vw_prospects_state.value[0] # Use the prospects dataset
2427
+ return selected_dataset_ai
2428
 
2429
  placeholder_text = """"""
2430
 
 
2447
  """
2448
  # Text input box for the user to enter their prompt
2449
  prompt_textbox = gr.Textbox(
2450
+ value=predefined_prompt,
2451
+ scale=4,
2452
+ label="Insert your prompt",
2453
+ visible=True,
2454
  )
2455
  submit_button = gr.Button("Submit")
2456
  bot = gr.Chatbot(placeholder=placeholder_text)
2457
 
2458
  js_func = """
2459
  function refresh() {
2460
+ // Force light theme if not already
2461
+ const url = new URL(window.location);
2462
+ if (url.searchParams.get('__theme') !== 'light') {
2463
+ url.searchParams.set('__theme', 'light');
2464
+ window.location.href = url.href;
2465
+ return;
2466
+ }
2467
+
2468
+ // Highlight toggle
2469
+ const btn1 = document.querySelector("#vw_customers_btn");
2470
+ const btn2 = document.querySelector("#vw_prospects_btn");
2471
+
2472
+ if (btn1 && btn2) {
2473
+ btn1.classList.add("active-btn"); // Default on load
2474
+
2475
+ btn1.addEventListener("click", () => {
2476
+ btn1.classList.add("active-btn");
2477
+ btn2.classList.remove("active-btn");
2478
+ });
2479
+
2480
+ btn2.addEventListener("click", () => {
2481
+ btn2.classList.add("active-btn");
2482
+ btn1.classList.remove("active-btn");
2483
+ });
2484
+ }
2485
  }
2486
  """
2487
 
2488
 
2489
  css = """
2490
  #vw_customers_btn, #vw_prospects_btn {
2491
+ border: 2px solid #ccc;
2492
+ padding: 10px 20px;
2493
+ font-size: 16px;
2494
+ font-weight: 500;
2495
+ border-radius: 6px;
2496
+ color: #333;
2497
+ background-color: #fff;
2498
+ margin: 0 5px;
2499
+ transition: all 0.25s ease;
2500
+ min-width: 140px;
2501
+ text-align: center;
2502
  }
2503
 
2504
  /* Highlighted (clicked) button — with teal text */
2505
  .active-btn {
2506
+ background-color: #e0f7f5 !important; /* Light teal background */
2507
+ color: teal !important; /* Teal text */
2508
+ border-color: teal !important;
2509
+ box-shadow: 0 0 6px rgba(0, 128, 128, 0.3);
2510
  }
2511
 
2512
  /* Optional hover effect */
2513
  #vw_customers_btn:hover, #vw_prospects_btn:hover {
2514
+ background-color: #f9f9f9;
2515
  }
2516
 
2517
  .gr-button:has(svg) {
2518
+ display: none !important;
2519
  }
2520
  .gr-image-label {
2521
+ display: none !important;
2522
  }
2523
  #chat_container {
2524
+ max-height: 900px;
2525
+ overflow-y: auto;
2526
+ margin-top: 0 !important;
2527
+ margin-bottom: 0 !important;
2528
+ padding-bottom: 0 !important;
2529
  }
2530
  """
2531
 
 
2533
 
2534
 
2535
  def highlight_button(button_name):
2536
+ return gr.Button.update(variant="primary")
2537
 
2538
  # 🧠 Chatbot backend
2539
 
2540
 
2541
 
2542
  def vwload_nps_and_r2(file_path):
2543
+ nps_img = calculate_nps_image_from_excel(file_path)
2544
+ r2_img = vwcalculate_r2_image_from_excel(file_path)
2545
+ return nps_img,r2_img
2546
 
2547
 
2548
 
2549
 
2550
  def load_nps_and_r2(file_path):
2551
+ nps_img = calculate_nps_image_from_excel(file_path)
2552
+ r2_img = calculate_r2_image_from_excel(file_path)
2553
 
2554
+ return nps_img, r2_img
2555
 
2556
 
2557
 
2558
  with gr.Blocks(css=css, js=js_func) as demo:
2559
+ gr.HTML("""
2560
+ <style>
2561
+ #trust_driver_img, #nps_driver_img {
2562
+ display: inline-block;
2563
+ width: 49%;
2564
+ margin-right: 1%;
2565
+ vertical-align: top;
2566
+ }
2567
+ </style>
2568
+ """)
2569
+
2570
+ # Title and intro
2571
+ with gr.Column():
2572
+ gr.Markdown("""
2573
+ <h2 style="text-align: center; font-size: 2.25rem; font-weight: 600;">
2574
+ TrustLogic AI Demo
2575
+ </h2>
2576
+ """)
2577
+ gr.Markdown("### Experience TrustLogic AI")
2578
+ gr.Markdown("Experience how quick and easy it is to build the right trust based on insight with our TrustLogic AI")
2579
+
2580
+ # Dataset selection buttons
2581
+ with gr.Column():
2582
+ with gr.Row():
2583
+ vw_customers_btn = gr.Button("VW Owners", elem_id="vw_customers_btn")
2584
+ vw_prospects_btn = gr.Button("VW Prospects", elem_id="vw_prospects_btn")
2585
+
2586
+ # 🧠 Chat section (moved to top)
2587
+ with gr.Column(elem_id="chat_container"):
2588
+ prompt_textbox = gr.Textbox(value=predefined_prompt, scale=4, label="Insert your prompt", visible=True)
2589
+ submit_button = gr.Button("Submit")
2590
+ bot = gr.Chatbot(placeholder=placeholder_text)
2591
+
2592
+ # Button to show data
2593
+ show_data_btn = gr.Button("Show the data", variant="primary", size="lg")
2594
+
2595
+ # Data analysis section (initially hidden)
2596
+ with gr.Column(visible=False) as data_section:
2597
+ gr.Markdown("### Uncover the underlying data used for the prompt")
2598
+ gr.Markdown("Quickly identify what drives your NPS and trust across different segments using the automated analyser.")
2599
+ gr.Markdown("""
2600
+ <span style="font-size:15px;">Volkswagen Example</span><br>
2601
+ As a default, the analysis displays <strong>Volkswagen Owner</strong> results.
2602
+ To trigger the analysis for <strong>Prospects</strong>, toggle to 'VW Prospects'.
2603
+ """)
2604
+
2605
+ with gr.Row(equal_height=True):
2606
+ with gr.Column(scale=1):
2607
+ nps_img_output = gr.HTML()
 
 
 
 
2608
 
2609
+
2610
+
2611
+ with gr.Column(scale=1):
2612
+ trust_score_output = gr.HTML()
2613
+
2614
+ with gr.Column(scale=1):
2615
+ gr.Markdown("""<div style='text-align: center;'><h3>How much of your NPS is determined by TrustLogic®</h3></div>""")
2616
+ trust_r2_img = gr.HTML()
2617
+
2618
+
2619
+ with gr.Column():
2620
+ outputs = reset_outputs()
2621
 
2622
+ # 🔁 States
2623
+ vw_customers_state11 = gr.State(value=["example_files/VW Owners.xlsx"])
2624
+ vw_prospects_state12 = gr.State(value=["example_files/Volkswagen Non Customers.xlsx"])
2625
+ vw_customers_state = gr.State(value=["VW Owners.xlsx"])
2626
+ vw_prospects_state = gr.State(value=["Volkswagen Non Customers.xlsx"])
2627
+ selected_dataset_ai = gr.State(value="VW Owners.xlsx") # ✅ Matches dictionary key
2628
 
2629
+ chat_history = gr.State(value=[])
 
 
2630
 
2631
+ # Show/hide data section functionality
2632
+ data_visible_state = gr.State(value=False)
2633
+
2634
+ def toggle_data_section(visible):
2635
+ new_visible = not visible
2636
+ button_text = "Hide the data" if new_visible else "Show the data"
2637
+ return gr.Column.update(visible=new_visible), button_text, new_visible
2638
+
2639
+ show_data_btn.click(
2640
+ fn=toggle_data_section,
2641
+ inputs=data_visible_state,
2642
+ outputs=[data_section, show_data_btn, data_visible_state]
2643
+ )
2644
+
2645
+ submit_button.click(
2646
+ fn=chatbot_response,
2647
+ inputs=[prompt_textbox, chat_history, selected_dataset_ai],
2648
+ outputs=[bot, chat_history]
2649
+ )
2650
 
2651
 
2652
 
2653
+ ## All widget functions here ##
2654
 
2655
+ vw_customers_btn.click(
2656
+ fn=display_trust_score_1,
2657
+ inputs=[],
2658
+ outputs=trust_score_output,
2659
+ )
2660
+
2661
+ vw_prospects_btn.click(
2662
+ fn=display_trust_score_2,
2663
+ inputs=[],
2664
+ outputs=trust_score_output,
2665
+ )
2666
+
2667
+ vw_customers_btn.click(
2668
+ fn=process_examples,
2669
+ inputs=[vw_customers_state],
2670
+ outputs= outputs,
2671
+ )
2672
+
2673
+ vw_prospects_btn.click(
2674
+ fn=process_examples,
2675
+ inputs=[vw_prospects_state],
2676
+ outputs= outputs,
2677
+ )
2678
+ def set_vw_owners():
2679
+ return "VW Owners.xlsx"
2680
+
2681
+ def set_vw_prospects():
2682
+ return "Volkswagen Non Customers.xlsx"
2683
 
2684
+
2685
+ vw_customers_btn.click(
2686
+ fn=set_vw_owners,
2687
+ inputs=[],
2688
+ outputs=selected_dataset_ai
2689
+ )
2690
+
2691
+ vw_prospects_btn.click(
2692
+ fn=set_vw_prospects,
2693
+ inputs=[],
2694
+ outputs=selected_dataset_ai
2695
+ )
2696
+ vw_customers_btn.click(
2697
+ fn=lambda f: vwload_nps_and_r2(f[0]),
2698
+ inputs=[vw_customers_state11],
2699
+ outputs=[nps_img_output, trust_r2_img],
2700
+ )
2701
+
2702
+ vw_prospects_btn.click(
2703
+ fn=lambda f: load_nps_and_r2(f[0]),
2704
+ inputs=[vw_prospects_state12],
2705
+ outputs=[nps_img_output, trust_r2_img],
2706
+ )
2707
+
2708
+
2709
+ demo.load(
2710
+ fn=lambda f: vwload_nps_and_r2(f[0]),
2711
+ inputs=[vw_customers_state11],
2712
+ outputs=[nps_img_output, trust_r2_img],
2713
+ )
2714
+ demo.load(
2715
+ fn=display_trust_score_1,
2716
+ inputs=[],
2717
+ outputs=trust_score_output
2718
+ )
2719
+
2720
+ demo.load(
2721
+ fn=process_examples,
2722
+ inputs=[vw_customers_state],
2723
+ outputs=outputs
2724
+ )
2725
+
2726
  try:
2727
+ demo.launch(server_name="0.0.0.0")
2728
  except Exception as e:
2729
+ logger.error(f"Error launching Gradio app: {e}")
2730
+ raise e