Wajahat698 commited on
Commit
dca9774
·
verified ·
1 Parent(s): 2a42900

Update app.py

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