Wajahat698 commited on
Commit
95adada
·
verified ·
1 Parent(s): 5d2ee0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +168 -165
app.py CHANGED
@@ -2554,177 +2554,180 @@ def load_nps_and_r2(file_path):
2554
 
2555
 
2556
 
 
 
2557
  with gr.Blocks(css=css, js=js_func) as demo:
2558
- gr.HTML("""
2559
- <style>
2560
- #trust_driver_img, #nps_driver_img {
2561
- display: inline-block;
2562
- width: 49%;
2563
- margin-right: 1%;
2564
- vertical-align: top;
2565
- }
2566
- </style>
2567
- """)
2568
-
2569
- # Title and intro
2570
- with gr.Column():
2571
- gr.Markdown("""
2572
- <h2 style="text-align: center; font-size: 2.25rem; font-weight: 600;">
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")
2604
-
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()
2611
-
2612
- with gr.Column(scale=1):
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
 
 
2554
 
2555
 
2556
 
2557
+
2558
+
2559
  with gr.Blocks(css=css, js=js_func) as demo:
2560
+ gr.HTML("""
2561
+ <style>
2562
+ #trust_driver_img, #nps_driver_img {
2563
+ display: inline-block;
2564
+ width: 49%;
2565
+ margin-right: 1%;
2566
+ vertical-align: top;
2567
+ }
2568
+ </style>
2569
+ """)
2570
+
2571
+ # Title and intro
2572
+ with gr.Column():
2573
+ gr.Markdown("""
2574
+ <h2 style="text-align: center; font-size: 2.25rem; font-weight: 600;">
2575
+ TrustLogic AI Demo
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
2732
 
2733