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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -103
app.py CHANGED
@@ -2553,6 +2553,7 @@ def load_nps_and_r2(file_path):
2553
 
2554
 
2555
 
 
2556
  with gr.Blocks(css=css, js=js_func) as demo:
2557
  gr.HTML("""
2558
  <style>
@@ -2572,14 +2573,31 @@ with gr.Blocks(css=css, js=js_func) as demo:
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")
@@ -2587,8 +2605,6 @@ with gr.Blocks(css=css, js=js_func) as demo:
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()
@@ -2597,113 +2613,118 @@ with gr.Blocks(css=css, js=js_func) as demo:
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
 
 
 
2553
 
2554
 
2555
 
2556
+
2557
  with gr.Blocks(css=css, js=js_func) as demo:
2558
  gr.HTML("""
2559
  <style>
 
2573
  What drives your NPS and trust?
2574
  </h2>
2575
  """)
2576
+ gr.Markdown("Experience how quick and easy it is to build the right trust based on insight with our TrustLogic AI")
2577
+
2578
+ # 🧠 AI Demo Section - MOVED TO TOP
2579
+ with gr.Column(elem_id="chat_container"):
2580
+ gr.Markdown("### TrustLogicAI Demo")
2581
+ gr.Markdown("Our AI uses analysis results to generate trust-optimised content.")
2582
+
2583
+ prompt_textbox = gr.Textbox(value=predefined_prompt, scale=4, label="Insert your prompt", visible=True)
2584
+ submit_button = gr.Button("Submit")
2585
+ bot = gr.Chatbot(placeholder=placeholder_text)
2586
+
2587
+ # Button to reveal data analysis
2588
+ with gr.Column():
2589
+ gr.Markdown("After results: Uncover the underlying data used for the prompt.")
2590
+ show_data_btn = gr.Button("Show the data <VW Customer Data>", visible=False) # Initially hidden
2591
+
2592
+ # Analysis Results Section - MOVED BELOW CHAT
2593
+ with gr.Column(visible=False, elem_id="analysis_section") as analysis_section:
2594
  gr.Markdown("Quickly identify what drives your NPS and trust across different segments using the automated analyser.")
2595
  gr.Markdown("""
2596
  <span style="font-size:15px;">Volkswagen Example</span><br>
2597
  As a default, the analysis displays <strong>Volkswagen Owner</strong> results.
2598
+ To trigger the analysis for <strong>Prospects</strong>, toggle to 'VW Prospects'.
2599
  """)
2600
 
 
2601
  with gr.Row():
2602
  vw_customers_btn = gr.Button("VW Owners", elem_id="vw_customers_btn")
2603
  vw_prospects_btn = gr.Button("VW Prospects", elem_id="vw_prospects_btn")
 
2605
  with gr.Row(equal_height=True):
2606
  with gr.Column(scale=1):
2607
  nps_img_output = gr.HTML()
 
 
2608
 
2609
  with gr.Column(scale=1):
2610
  trust_score_output = gr.HTML()
 
2613
  gr.Markdown("""<div style='text-align: center;'><h3>How much of your NPS is determined by TrustLogic®</h3></div>""")
2614
  trust_r2_img = gr.HTML()
2615
 
 
2616
  with gr.Column():
2617
  outputs = reset_outputs()
2618
 
2619
+ # 🔁 States
2620
+ vw_customers_state11 = gr.State(value=["example_files/VW Owners.xlsx"])
2621
+ vw_prospects_state12 = gr.State(value=["example_files/Volkswagen Non Customers.xlsx"])
2622
+ vw_customers_state = gr.State(value=["VW Owners.xlsx"])
2623
+ vw_prospects_state = gr.State(value=["Volkswagen Non Customers.xlsx"])
2624
+ selected_dataset_ai = gr.State(value="VW Owners.xlsx") # ✅ Matches dictionary key
2625
+ chat_history = gr.State(value=[])
2626
+
2627
+ # Modified chatbot response to show data button after interaction
2628
+ def chatbot_response_with_reveal(prompt, history, dataset):
2629
+ response, updated_history = chatbot_response(prompt, history, dataset)
2630
+ return response, updated_history, gr.update(visible=True) # Show the data button
2631
+
2632
+ # Function to reveal analysis section
2633
+ def show_analysis():
2634
+ return gr.update(visible=True)
2635
+
2636
+ # Function definitions for button actions
2637
+ def set_vw_owners():
2638
+ return "VW Owners.xlsx"
2639
+
2640
+ def set_vw_prospects():
2641
+ return "Volkswagen Non Customers.xlsx"
2642
+
2643
+ # Event handlers
2644
+ submit_button.click(
2645
+ fn=chatbot_response_with_reveal,
2646
+ inputs=[prompt_textbox, chat_history, selected_dataset_ai],
2647
+ outputs=[bot, chat_history, show_data_btn]
2648
+ )
2649
 
2650
+ show_data_btn.click(
2651
+ fn=show_analysis,
2652
+ inputs=[],
2653
+ outputs=[analysis_section]
2654
+ )
2655
 
2656
+ ## All widget functions here ##
2657
 
2658
+ vw_customers_btn.click(
2659
+ fn=display_trust_score_1,
2660
+ inputs=[],
2661
+ outputs=trust_score_output,
2662
+ )
2663
+
2664
+ vw_prospects_btn.click(
2665
+ fn=display_trust_score_2,
2666
+ inputs=[],
2667
+ outputs=trust_score_output,
2668
+ )
2669
+
2670
+ vw_customers_btn.click(
2671
+ fn=process_examples,
2672
+ inputs=[vw_customers_state],
2673
+ outputs= outputs,
2674
+ )
2675
+
2676
+ vw_prospects_btn.click(
2677
+ fn=process_examples,
2678
+ inputs=[vw_prospects_state],
2679
+ outputs= outputs,
2680
+ )
2681
+
2682
+ vw_customers_btn.click(
2683
+ fn=set_vw_owners,
2684
+ inputs=[],
2685
+ outputs=selected_dataset_ai
2686
+ )
2687
+
2688
+ vw_prospects_btn.click(
2689
+ fn=set_vw_prospects,
2690
+ inputs=[],
2691
+ outputs=selected_dataset_ai
2692
+ )
2693
+
2694
+ vw_customers_btn.click(
2695
+ fn=lambda f: vwload_nps_and_r2(f[0]),
2696
+ inputs=[vw_customers_state11],
2697
+ outputs=[nps_img_output, trust_r2_img],
2698
+ )
2699
+
2700
+ vw_prospects_btn.click(
2701
+ fn=lambda f: load_nps_and_r2(f[0]),
2702
+ inputs=[vw_prospects_state12],
2703
+ outputs=[nps_img_output, trust_r2_img],
2704
+ )
2705
+
2706
+ # Load functions for initial state (only when analysis section is visible)
2707
+ demo.load(
2708
+ fn=lambda f: vwload_nps_and_r2(f[0]),
2709
+ inputs=[vw_customers_state11],
2710
+ outputs=[nps_img_output, trust_r2_img],
2711
+ )
2712
+ demo.load(
2713
+ fn=display_trust_score_1,
2714
+ inputs=[],
2715
+ outputs=trust_score_output
2716
+ )
2717
+
2718
+ demo.load(
2719
+ fn=process_examples,
2720
+ inputs=[vw_customers_state],
2721
+ outputs=outputs
2722
+ )
2723
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2724
  try:
2725
  demo.launch(server_name="0.0.0.0")
2726
  except Exception as e:
2727
  logger.error(f"Error launching Gradio app: {e}")
2728
+ raise e
2729
+
2730
+