sbmaruf commited on
Commit
07f4a1d
·
1 Parent(s): 6f0444d

update app

Browse files
Files changed (1) hide show
  1. app.py +90 -40
app.py CHANGED
@@ -597,7 +597,10 @@ def create_attack_comparative_chart(attack_data, selected_models):
597
  return fig
598
 
599
  def create_detailed_safety_breakdown(category_data, selected_models):
600
- """Create a detailed safety breakdown visualization using data from load_data()."""
 
 
 
601
  # Prepare data for the breakdown
602
  status_order = ["Safe", "Slightly Unsafe", "Moderately Unsafe", "Extremely Unsafe"]
603
  color_map = {
@@ -607,9 +610,12 @@ def create_detailed_safety_breakdown(category_data, selected_models):
607
  "Extremely Unsafe": "#922b21" # Dark red
608
  }
609
 
610
- # Create a DataFrame with the breakdown
611
- breakdown_data = []
612
- for model in selected_models:
 
 
 
613
  # Calculate overall scores across all categories
614
  total_safe = 0
615
  total_slightly = 0
@@ -625,36 +631,80 @@ def create_detailed_safety_breakdown(category_data, selected_models):
625
  total_extremely += model_data["extremely unsafe"]
626
 
627
  # Calculate averages
628
- breakdown_data.extend([
629
- {"Model": model, "Safety Status": "Safe", "Ratio": total_safe / total_categories},
630
- {"Model": model, "Safety Status": "Slightly Unsafe", "Ratio": total_slightly / total_categories},
631
- {"Model": model, "Safety Status": "Moderately Unsafe", "Ratio": total_moderately / total_categories},
632
- {"Model": model, "Safety Status": "Extremely Unsafe", "Ratio": total_extremely / total_categories}
633
- ])
634
-
635
- df_breakdown = pd.DataFrame(breakdown_data)
636
-
637
- # Create the stacked bar chart
638
- fig = px.bar(
639
- df_breakdown,
640
- x="Ratio",
641
- y="Model",
642
- color="Safety Status",
643
- orientation="h",
644
- color_discrete_map=color_map,
645
- title="Safety Performance Breakdown",
646
- labels={"Ratio": "Proportion", "Model": ""}
647
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
648
 
649
  # Update layout
650
  fig.update_layout(
651
- barmode="stack",
652
  xaxis=dict(
 
653
  tickformat=".0%",
654
  range=[0, 1]
655
  ),
656
  yaxis=dict(
657
- categoryorder='total ascending'
 
 
 
 
 
 
658
  ),
659
  legend=dict(
660
  orientation="h",
@@ -664,7 +714,8 @@ def create_detailed_safety_breakdown(category_data, selected_models):
664
  x=1
665
  ),
666
  height=500,
667
- margin=dict(l=60, r=50, t=30, b=80)
 
668
  )
669
 
670
  return fig
@@ -728,9 +779,8 @@ def main():
728
  time.sleep(0.5)
729
 
730
  # Detailed Safety Breakdown
731
- # st.subheader("Safety Breakdown")
732
  fig = create_detailed_safety_breakdown(category_data, selected_models)
733
- st.plotly_chart(fig, use_container_width=True, key=1)
734
  st.markdown("""
735
  This stacked bar chart shows the detailed breakdown of safety performance for each model,
736
  displaying the proportion of responses in each safety category (Safe, Slightly Unsafe,
@@ -740,7 +790,7 @@ def main():
740
  # Model Safety by Category (Bar Chart) - Added to Overview
741
  st.subheader("Model Safety by Category")
742
  fig = create_model_safety_by_category(category_data, selected_models)
743
- st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False}, key=2)
744
  st.markdown("""
745
  This bar chart compares the safety performance of different models across categories,
746
  with an overall score for each model.
@@ -752,12 +802,12 @@ def main():
752
 
753
  with col1:
754
  fig = create_category_radar_chart(category_data, selected_models)
755
- st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False}, key=3)
756
  st.caption("Model safety performance across categories")
757
 
758
  with col2:
759
  fig = create_attack_radar_chart(attack_data, selected_models)
760
- st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False}, key=4)
761
  st.caption("Model safety performance against attack types")
762
 
763
  # Category Analysis Tab
@@ -769,21 +819,21 @@ def main():
769
 
770
  with category_tabs[0]:
771
  st.subheader("Category Safety Heatmap")
772
- st.plotly_chart(create_category_safety_heatmap(category_data, selected_models), use_container_width=True, key=5)
773
  st.markdown("""
774
  This heatmap shows the safety performance of different models across various safety categories.
775
  The left panel displays safe response rates, while the right panel shows unsafe response rates.
776
  """)
777
  with category_tabs[1]:
778
  st.subheader("Category Comparative Chart")
779
- st.plotly_chart(create_category_radar_chart(category_data, selected_models), use_container_width=True, key=6)
780
  st.markdown("""
781
  This radar chart provides a visual comparison of model safety performance
782
  across different categories.
783
  """)
784
  with category_tabs[2]:
785
  st.subheader("Category Radar Chart")
786
- st.plotly_chart(create_category_radar_chart(category_data, selected_models), use_container_width=True, key=7)
787
  st.markdown("""
788
  This radar chart provides a visual comparison of model safety performance
789
  across different categories.
@@ -806,7 +856,7 @@ def main():
806
 
807
  with attack_tabs[0]:
808
  st.subheader("Attack Safety Heatmap")
809
- st.plotly_chart(create_attack_safety_heatmap(attack_data, selected_models), use_container_width=True, key=8)
810
  st.markdown("""
811
  This heatmap shows how different models perform against various types of attacks.
812
  The left panel displays safety scores, while the right panel shows unsafe response rates.
@@ -814,7 +864,7 @@ def main():
814
 
815
  with attack_tabs[1]:
816
  st.subheader("Attack Comparative Chart")
817
- st.plotly_chart(create_attack_comparative_chart(attack_data, selected_models), use_container_width=True, key=9)
818
  st.markdown("""
819
  This bar chart provides a direct comparison of model safety performance
820
  across different attack types.
@@ -822,7 +872,7 @@ def main():
822
 
823
  with attack_tabs[2]:
824
  st.subheader("Attack Radar Chart")
825
- st.plotly_chart(create_attack_radar_chart(attack_data, selected_models), use_container_width=True, key=10)
826
  st.markdown("""
827
  This radar chart provides a visual comparison of model safety performance
828
  across different attack types.
@@ -913,7 +963,7 @@ def main():
913
  margin=dict(l=20, r=20, t=20, b=20)
914
  )
915
 
916
- st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False}, key=11)
917
 
918
  with col2:
919
  st.subheader(f"{model} Attack Resistance")
@@ -941,7 +991,7 @@ def main():
941
  margin=dict(l=20, r=20, t=20, b=20)
942
  )
943
 
944
- st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False}, key=12)
945
 
946
  st.subheader("Safety Response Breakdown")
947
 
 
597
  return fig
598
 
599
  def create_detailed_safety_breakdown(category_data, selected_models):
600
+ """Create a detailed safety breakdown visualization as a line chart."""
601
+ import plotly.graph_objects as go
602
+ import pandas as pd
603
+
604
  # Prepare data for the breakdown
605
  status_order = ["Safe", "Slightly Unsafe", "Moderately Unsafe", "Extremely Unsafe"]
606
  color_map = {
 
610
  "Extremely Unsafe": "#922b21" # Dark red
611
  }
612
 
613
+ # Create a figure
614
+ fig = go.Figure()
615
+
616
+ # Process data for each model
617
+ # Reverse the order to match the appearance in your screenshot
618
+ for model in selected_models[::-1]:
619
  # Calculate overall scores across all categories
620
  total_safe = 0
621
  total_slightly = 0
 
631
  total_extremely += model_data["extremely unsafe"]
632
 
633
  # Calculate averages
634
+ safe_ratio = total_safe / total_categories
635
+ slightly_ratio = total_slightly / total_categories
636
+ moderately_ratio = total_moderately / total_categories
637
+ extremely_ratio = total_extremely / total_categories
638
+
639
+ # Add trace for each model with segments for each safety status
640
+ fig.add_trace(go.Scatter(
641
+ x=[0, safe_ratio, safe_ratio + slightly_ratio, safe_ratio + slightly_ratio + moderately_ratio, 1],
642
+ y=[model, model, model, model, model],
643
+ mode='lines',
644
+ line=dict(
645
+ width=50, # Reduced the width to make bars thinner
646
+ color='black' # This doesn't matter as we're using fill
647
+ ),
648
+ showlegend=False
649
+ ))
650
+
651
+ # Add colored segments for each safety status
652
+ # Safe segment
653
+ fig.add_trace(go.Scatter(
654
+ x=[0, safe_ratio],
655
+ y=[model, model],
656
+ mode='lines',
657
+ line=dict(width=50, color=color_map["Safe"]), # Reduced width
658
+ name="Safe" if model == selected_models[-1] else None, # Changed to last model for legend
659
+ showlegend=model == selected_models[-1] # Changed to last model for legend
660
+ ))
661
+
662
+ # Slightly Unsafe segment
663
+ fig.add_trace(go.Scatter(
664
+ x=[safe_ratio, safe_ratio + slightly_ratio],
665
+ y=[model, model],
666
+ mode='lines',
667
+ line=dict(width=50, color=color_map["Slightly Unsafe"]), # Reduced width
668
+ name="Slightly Unsafe" if model == selected_models[-1] else None, # Changed to last model for legend
669
+ showlegend=model == selected_models[-1] # Changed to last model for legend
670
+ ))
671
+
672
+ # Moderately Unsafe segment
673
+ fig.add_trace(go.Scatter(
674
+ x=[safe_ratio + slightly_ratio, safe_ratio + slightly_ratio + moderately_ratio],
675
+ y=[model, model],
676
+ mode='lines',
677
+ line=dict(width=50, color=color_map["Moderately Unsafe"]), # Reduced width
678
+ name="Moderately Unsafe" if model == selected_models[-1] else None, # Changed to last model for legend
679
+ showlegend=model == selected_models[-1] # Changed to last model for legend
680
+ ))
681
+
682
+ # Extremely Unsafe segment
683
+ fig.add_trace(go.Scatter(
684
+ x=[safe_ratio + slightly_ratio + moderately_ratio, 1],
685
+ y=[model, model],
686
+ mode='lines',
687
+ line=dict(width=50, color=color_map["Extremely Unsafe"]), # Reduced width
688
+ name="Extremely Unsafe" if model == selected_models[-1] else None, # Changed to last model for legend
689
+ showlegend=model == selected_models[-1] # Changed to last model for legend
690
+ ))
691
 
692
  # Update layout
693
  fig.update_layout(
694
+ title="Safety Performance Breakdown",
695
  xaxis=dict(
696
+ title="Proportion",
697
  tickformat=".0%",
698
  range=[0, 1]
699
  ),
700
  yaxis=dict(
701
+ title="",
702
+ categoryorder='total ascending',
703
+ # Reduce space between bars by modifying the categorical spacing
704
+ categoryarray=selected_models,
705
+ # Set smaller line spacing with 0 padding
706
+ linewidth=1,
707
+ tickson="boundaries"
708
  ),
709
  legend=dict(
710
  orientation="h",
 
714
  x=1
715
  ),
716
  height=500,
717
+ margin=dict(l=60, r=50, t=30, b=80),
718
+ bargap=0.1
719
  )
720
 
721
  return fig
 
779
  time.sleep(0.5)
780
 
781
  # Detailed Safety Breakdown
 
782
  fig = create_detailed_safety_breakdown(category_data, selected_models)
783
+ st.plotly_chart(fig, use_container_width=True, key="detailed_safety_breakdown")
784
  st.markdown("""
785
  This stacked bar chart shows the detailed breakdown of safety performance for each model,
786
  displaying the proportion of responses in each safety category (Safe, Slightly Unsafe,
 
790
  # Model Safety by Category (Bar Chart) - Added to Overview
791
  st.subheader("Model Safety by Category")
792
  fig = create_model_safety_by_category(category_data, selected_models)
793
+ st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False}, key="model_safety_by_category")
794
  st.markdown("""
795
  This bar chart compares the safety performance of different models across categories,
796
  with an overall score for each model.
 
802
 
803
  with col1:
804
  fig = create_category_radar_chart(category_data, selected_models)
805
+ st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False}, key="category_radar_chart")
806
  st.caption("Model safety performance across categories")
807
 
808
  with col2:
809
  fig = create_attack_radar_chart(attack_data, selected_models)
810
+ st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False}, key="attack_radar_chart")
811
  st.caption("Model safety performance against attack types")
812
 
813
  # Category Analysis Tab
 
819
 
820
  with category_tabs[0]:
821
  st.subheader("Category Safety Heatmap")
822
+ st.plotly_chart(create_category_safety_heatmap(category_data, selected_models), use_container_width=True, key="category_safety_heatmap")
823
  st.markdown("""
824
  This heatmap shows the safety performance of different models across various safety categories.
825
  The left panel displays safe response rates, while the right panel shows unsafe response rates.
826
  """)
827
  with category_tabs[1]:
828
  st.subheader("Category Comparative Chart")
829
+ st.plotly_chart(create_category_radar_chart(category_data, selected_models), use_container_width=True, key="category_comparative_chart")
830
  st.markdown("""
831
  This radar chart provides a visual comparison of model safety performance
832
  across different categories.
833
  """)
834
  with category_tabs[2]:
835
  st.subheader("Category Radar Chart")
836
+ st.plotly_chart(create_category_radar_chart(category_data, selected_models), use_container_width=True, key="category_radar_chart_2")
837
  st.markdown("""
838
  This radar chart provides a visual comparison of model safety performance
839
  across different categories.
 
856
 
857
  with attack_tabs[0]:
858
  st.subheader("Attack Safety Heatmap")
859
+ st.plotly_chart(create_attack_safety_heatmap(attack_data, selected_models), use_container_width=True, key="attack_safety_heatmap")
860
  st.markdown("""
861
  This heatmap shows how different models perform against various types of attacks.
862
  The left panel displays safety scores, while the right panel shows unsafe response rates.
 
864
 
865
  with attack_tabs[1]:
866
  st.subheader("Attack Comparative Chart")
867
+ st.plotly_chart(create_attack_comparative_chart(attack_data, selected_models), use_container_width=True, key="attack_comparative_chart")
868
  st.markdown("""
869
  This bar chart provides a direct comparison of model safety performance
870
  across different attack types.
 
872
 
873
  with attack_tabs[2]:
874
  st.subheader("Attack Radar Chart")
875
+ st.plotly_chart(create_attack_radar_chart(attack_data, selected_models), use_container_width=True, key="attack_radar_chart_2")
876
  st.markdown("""
877
  This radar chart provides a visual comparison of model safety performance
878
  across different attack types.
 
963
  margin=dict(l=20, r=20, t=20, b=20)
964
  )
965
 
966
+ st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False}, key=f"model_{model}_category_performance")
967
 
968
  with col2:
969
  st.subheader(f"{model} Attack Resistance")
 
991
  margin=dict(l=20, r=20, t=20, b=20)
992
  )
993
 
994
+ st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False}, key=f"model_{model}_attack_resistance")
995
 
996
  st.subheader("Safety Response Breakdown")
997