krishaamer commited on
Commit
bf51904
·
1 Parent(s): ba2cd93

Add AI page

Browse files
Files changed (4) hide show
  1. __pycache__/page_ai.cpython-310.pyc +0 -0
  2. app.py +5 -0
  3. page_ai.py +152 -0
  4. page_shopping.py +1 -142
__pycache__/page_ai.cpython-310.pyc ADDED
Binary file (5.73 kB). View file
 
app.py CHANGED
@@ -4,6 +4,7 @@ import page_attitudes
4
  import page_demographics
5
  import page_shopping
6
  import page_investing
 
7
  import page_personas
8
  import page_tests
9
  from urllib.parse import quote, unquote
@@ -42,6 +43,8 @@ if st.sidebar.button("Shopping"):
42
  st.session_state['page'] = 'Shopping'
43
  if st.sidebar.button("Investing"):
44
  st.session_state['page'] = 'Investing'
 
 
45
  if st.sidebar.button("Tests"):
46
  st.session_state['page'] = 'Tests'
47
 
@@ -63,6 +66,8 @@ elif st.session_state['page'] == 'Shopping':
63
  page_shopping.show(df)
64
  elif st.session_state['page'] == 'Investing':
65
  page_investing.show(df)
 
 
66
  elif st.session_state['page'] == 'Tests':
67
  page_tests.show(df)
68
 
 
4
  import page_demographics
5
  import page_shopping
6
  import page_investing
7
+ import page_ai
8
  import page_personas
9
  import page_tests
10
  from urllib.parse import quote, unquote
 
43
  st.session_state['page'] = 'Shopping'
44
  if st.sidebar.button("Investing"):
45
  st.session_state['page'] = 'Investing'
46
+ if st.sidebar.button("AI Companion"):
47
+ st.session_state['page'] = 'AI'
48
  if st.sidebar.button("Tests"):
49
  st.session_state['page'] = 'Tests'
50
 
 
66
  page_shopping.show(df)
67
  elif st.session_state['page'] == 'Investing':
68
  page_investing.show(df)
69
+ elif st.session_state['page'] == 'AI':
70
+ page_ai.show(df)
71
  elif st.session_state['page'] == 'Tests':
72
  page_tests.show(df)
73
 
page_ai.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from kmodes.kmodes import KModes
3
+ import pandas as pd
4
+ from matplotlib.font_manager import FontProperties
5
+ import matplotlib.pyplot as plt
6
+ import seaborn as sns
7
+ import numpy as np
8
+ from fields.prod_feat_flat_fields import prod_feat_flat_fields
9
+ from fields.feature_translations import feature_translations
10
+
11
+ @st.cache_data
12
+ def show(df):
13
+ # Load the Chinese font
14
+ chinese_font = FontProperties(fname='mingliu.ttf', size=12)
15
+ st.title("AI Companion")
16
+ st.write("Clustering students based on AI-assistant feature choices")
17
+ clusters = perform_kmodes_clustering(df, prod_feat_flat_fields)
18
+ st.markdown(
19
+ f"<h2 style='text-align: center;'>Feature Preferences</h2>", unsafe_allow_html=True)
20
+ show_radar_chart(clusters, font_prop=chinese_font)
21
+ plot_feature_preferences(clusters, font_prop=chinese_font)
22
+
23
+
24
+ def perform_kmodes_clustering(df, feature_columns, n_clusters=3):
25
+ # Extract the relevant fields for clustering
26
+ cluster_data = df[feature_columns]
27
+
28
+ # Convert boolean features to integer type
29
+ cluster_data_encoded = cluster_data.astype(int)
30
+
31
+ # Define the K-modes model
32
+ km = KModes(n_clusters=n_clusters, init='Huang', n_init=5, verbose=1)
33
+
34
+ # Fit the cluster model
35
+ clusters = km.fit_predict(cluster_data_encoded)
36
+
37
+ # Add the cluster labels to the original dataframe
38
+ df['Cluster'] = clusters
39
+
40
+ # Create a dictionary to store dataframes for each cluster
41
+ cluster_dict = {}
42
+ for cluster in df['Cluster'].unique():
43
+ cluster_df = df[df['Cluster'] == cluster]
44
+ cluster_dict[cluster] = cluster_df
45
+
46
+ return cluster_dict
47
+
48
+
49
+ def show_radar_chart(clusters, font_prop):
50
+
51
+ df_dict={
52
+ 'Conscious (n=340)': clusters[0],
53
+ 'Interested (n=215)': clusters[1],
54
+ 'Advocate (n=126)': clusters[2]
55
+ }
56
+
57
+ feature_translations_dict = dict(zip(prod_feat_flat_fields, feature_translations))
58
+ persona_averages = [df[list(feature_translations_dict.keys())].mean().tolist() for df in df_dict.values()]
59
+
60
+ # Append the first value at the end of each list for the radar chart
61
+ for averages in persona_averages:
62
+ averages += averages[:1]
63
+
64
+ # Prepare the English labels for plotting
65
+ english_feature_labels = list(feature_translations)
66
+ english_feature_labels += [english_feature_labels[0]] # Repeat the first label to close the loop
67
+
68
+ # Number of variables we're plotting
69
+ num_vars = len(english_feature_labels)
70
+
71
+ # Split the circle into even parts and save the angles
72
+ angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False).tolist()
73
+ angles += angles[:1] # Complete the loop
74
+
75
+ # Set up the font properties for using a custom font
76
+ fig, ax = plt.subplots(figsize=(12, 12), subplot_kw=dict(polar=True))
77
+ fig.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
78
+
79
+ # Draw one axe per variable and add labels
80
+ plt.xticks(angles[:-1], english_feature_labels, color='grey', size=12, fontproperties=font_prop)
81
+
82
+ # Draw ylabels
83
+ ax.set_rlabel_position(0)
84
+ plt.yticks([0.2, 0.4, 0.6, 0.8, 1], ["0.2", "0.4", "0.6", "0.8", "1"], color="grey", size=7)
85
+ plt.ylim(0, 1)
86
+
87
+ # Plot data and fill with color
88
+ for label, data in zip(df_dict.keys(), persona_averages):
89
+ data += data[:1] # Complete the loop
90
+ ax.plot(angles, data, label=label, linewidth=1, linestyle='solid')
91
+ ax.fill(angles, data, alpha=0.25)
92
+
93
+ # Add legend
94
+ plt.legend(title='Personas')
95
+ plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1))
96
+
97
+ # Add a title
98
+ plt.title('Product Feature Preferences by Persona', size=20, color='grey', y=1.1, fontproperties=font_prop)
99
+
100
+ # Display the radar chart
101
+ st.pyplot(fig)
102
+
103
+
104
+ def plot_feature_preferences(clusters, font_prop):
105
+ # Given comparative table data
106
+ data = {
107
+ 'Feature': [
108
+ "買東西先查看產品的運輸距離(是不是當地食品)\nCheck Product Transportation Distance (Whether it is Local Food)",
109
+ "買東西先查看公司生產過程 多環保\nCheck Company's Eco-Friendly Production Process",
110
+ "買東西先查看公司工人的員工 福利多好\nCheck Company Worker Welfare",
111
+ "投資前查看AI摘要的消費者對公司環保的評論\nReview Consumer Eco Comments on Companies before Investing",
112
+ "用社交網絡認識其他環保的同學\nMeet Eco-Friendly Peers on Social Networks",
113
+ "買東西先了解哪些產品污染最嚴重,以便避免它們\nUnderstand Which Products are Most Polluting to Avoid Them",
114
+ "每個月查看我自己的環保分數報告(了解我用錢的方式多環保\nMonthly Review of My Eco Score Report (Understanding How My Spending is Eco-Friendly)",
115
+ "買東西先尋找有機產品 Search for\nOrganic Products Before Purchasing",
116
+ "跟我的AI幫手討論環保問題\nDiscuss Environmental Issues with My AI Assistant",
117
+ "投資前查看公司的環保認證和生態評分\nCheck Company's Environmental Certifications and Eco-Scores Before Investing",
118
+ "如何讓我支持的公司更環保\nHow to Make the Companies I Support More Eco-Friendly",
119
+ "買東西先了解我吃的動物性食品動物的生活環境\nUnderstand the Living Conditions of Animals for the Animal Products I Consume",
120
+ "老實說我對任何環保資訊都沒有太多興趣\nHonestly, I'm Not Very Interested in Any Eco Information",
121
+ "投資前比較公司的環保表現\nCompare Companies' Environmental Performance Before Investing"
122
+ ],
123
+ 'Conscious (n=340)': [0.367, 0.415, 0.191, 0.176, 0.079, 1.000, 0.197, 0.265, 0.144, 0.241, 0.144, 0.332, 0.044, 0.188],
124
+ 'Interested (n=215)': [0.260, 0.163, 0.153, 0.191, 0.107, 0.000, 0.135, 0.219, 0.172, 0.186, 0.093, 0.214, 0.233, 0.130],
125
+ 'Advocate (n=126)': [0.825, 0.881, 0.460, 0.746, 0.230, 0.881, 0.667, 0.690, 0.421, 0.865, 0.468, 0.778, 0.143, 0.738]
126
+ }
127
+ # Create a DataFrame
128
+ df = pd.DataFrame(data)
129
+
130
+ # Set the 'Feature' column as the index
131
+ df.set_index('Feature', inplace=True)
132
+
133
+ # Plot
134
+ fig, ax = plt.subplots(figsize=(14, 8))
135
+ df.plot(kind='bar', width=0.8, ax=ax)
136
+
137
+ # Set titles and labels using the Chinese font where necessary
138
+ plt.title('Comparison of Product Feature Preferences by Persona', fontproperties=font_prop)
139
+ plt.ylabel('Average Score', fontproperties=font_prop)
140
+ plt.xlabel('Feature', fontproperties=font_prop)
141
+ plt.xticks(rotation=45, ha='right', fontproperties=font_prop)
142
+
143
+ # Set the x-tick labels to use the Chinese font
144
+ ax.set_xticklabels(df.index, fontproperties=font_prop, rotation=45, ha='right')
145
+
146
+ plt.legend(title='Personas')
147
+
148
+ # Ensure layout is tight so everything fits
149
+ plt.tight_layout()
150
+
151
+ # Streamlit uses st.pyplot() to display matplotlib charts
152
+ st.pyplot(fig)
page_shopping.py CHANGED
@@ -1,24 +1,15 @@
1
  import streamlit as st
2
- from kmodes.kmodes import KModes
3
  from matplotlib.font_manager import FontProperties
4
  import matplotlib.pyplot as plt
5
  import seaborn as sns
6
  import pandas as pd
7
  import numpy as np
8
- from fields.prod_feat_flat_fields import prod_feat_flat_fields
9
- from fields.feature_translations import feature_translations
10
 
11
  @st.cache_data
12
  def show(df):
13
  # Load the Chinese font
14
  chinese_font = FontProperties(fname='mingliu.ttf', size=12)
15
  st.title("Shopping")
16
- st.write("Clustering students based on AI-assistant feature choices")
17
- clusters = perform_kmodes_clustering(df, prod_feat_flat_fields)
18
- st.markdown(
19
- f"<h2 style='text-align: center;'>Feature Preferences</h2>", unsafe_allow_html=True)
20
- show_radar_chart(clusters, font_prop=chinese_font)
21
- plot_feature_preferences(clusters, font_prop=chinese_font)
22
  st.markdown(
23
  f"<h2 style='text-align: center;'>Boycott Count</h2>", unsafe_allow_html=True)
24
  show_boycott_count(df, font_prop=chinese_font)
@@ -44,136 +35,4 @@ def show_boycott_count(df, font_prop):
44
  plt.text(index, value, str(value), ha='center', va='bottom', fontproperties=font_prop)
45
 
46
  # Display the chart in Streamlit
47
- st.pyplot(plt)
48
-
49
-
50
-
51
- def perform_kmodes_clustering(df, feature_columns, n_clusters=3):
52
- # Extract the relevant fields for clustering
53
- cluster_data = df[feature_columns]
54
-
55
- # Convert boolean features to integer type
56
- cluster_data_encoded = cluster_data.astype(int)
57
-
58
- # Define the K-modes model
59
- km = KModes(n_clusters=n_clusters, init='Huang', n_init=5, verbose=1)
60
-
61
- # Fit the cluster model
62
- clusters = km.fit_predict(cluster_data_encoded)
63
-
64
- # Add the cluster labels to the original dataframe
65
- df['Cluster'] = clusters
66
-
67
- # Create a dictionary to store dataframes for each cluster
68
- cluster_dict = {}
69
- for cluster in df['Cluster'].unique():
70
- cluster_df = df[df['Cluster'] == cluster]
71
- cluster_dict[cluster] = cluster_df
72
-
73
- return cluster_dict
74
-
75
-
76
- def show_radar_chart(clusters, font_prop):
77
-
78
- df_dict={
79
- 'Conscious (n=340)': clusters[0],
80
- 'Interested (n=215)': clusters[1],
81
- 'Advocate (n=126)': clusters[2]
82
- }
83
-
84
- feature_translations_dict = dict(zip(prod_feat_flat_fields, feature_translations))
85
- persona_averages = [df[list(feature_translations_dict.keys())].mean().tolist() for df in df_dict.values()]
86
-
87
- # Append the first value at the end of each list for the radar chart
88
- for averages in persona_averages:
89
- averages += averages[:1]
90
-
91
- # Prepare the English labels for plotting
92
- english_feature_labels = list(feature_translations)
93
- english_feature_labels += [english_feature_labels[0]] # Repeat the first label to close the loop
94
-
95
- # Number of variables we're plotting
96
- num_vars = len(english_feature_labels)
97
-
98
- # Split the circle into even parts and save the angles
99
- angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False).tolist()
100
- angles += angles[:1] # Complete the loop
101
-
102
- # Set up the font properties for using a custom font
103
- fig, ax = plt.subplots(figsize=(12, 12), subplot_kw=dict(polar=True))
104
- fig.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
105
-
106
- # Draw one axe per variable and add labels
107
- plt.xticks(angles[:-1], english_feature_labels, color='grey', size=12, fontproperties=font_prop)
108
-
109
- # Draw ylabels
110
- ax.set_rlabel_position(0)
111
- plt.yticks([0.2, 0.4, 0.6, 0.8, 1], ["0.2", "0.4", "0.6", "0.8", "1"], color="grey", size=7)
112
- plt.ylim(0, 1)
113
-
114
- # Plot data and fill with color
115
- for label, data in zip(df_dict.keys(), persona_averages):
116
- data += data[:1] # Complete the loop
117
- ax.plot(angles, data, label=label, linewidth=1, linestyle='solid')
118
- ax.fill(angles, data, alpha=0.25)
119
-
120
- # Add legend
121
- plt.legend(title='Personas')
122
- plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1))
123
-
124
- # Add a title
125
- plt.title('Product Feature Preferences by Persona', size=20, color='grey', y=1.1, fontproperties=font_prop)
126
-
127
- # Display the radar chart
128
- st.pyplot(fig)
129
-
130
-
131
- def plot_feature_preferences(clusters, font_prop):
132
- # Given comparative table data
133
- data = {
134
- 'Feature': [
135
- "買東西先查看產品的運輸距離(是不是當地食品)\nCheck Product Transportation Distance (Whether it is Local Food)",
136
- "買東西先查看公司生產過程 多環保\nCheck Company's Eco-Friendly Production Process",
137
- "買東西先查看公司工人的員工 福利多好\nCheck Company Worker Welfare",
138
- "投資前查看AI摘要的消費者對公司環保的評論\nReview Consumer Eco Comments on Companies before Investing",
139
- "用社交網絡認識其他環保的同學\nMeet Eco-Friendly Peers on Social Networks",
140
- "買東西先了解哪些產品污染最嚴重,以便避免它們\nUnderstand Which Products are Most Polluting to Avoid Them",
141
- "每個月查看我自己的環保分數報告(了解我用錢的方式多環保\nMonthly Review of My Eco Score Report (Understanding How My Spending is Eco-Friendly)",
142
- "買東西先尋找有機產品 Search for\nOrganic Products Before Purchasing",
143
- "跟我的AI幫手討論環保問題\nDiscuss Environmental Issues with My AI Assistant",
144
- "投資前查看公司的環保認證和生態評分\nCheck Company's Environmental Certifications and Eco-Scores Before Investing",
145
- "如何讓我支持的公司更環保\nHow to Make the Companies I Support More Eco-Friendly",
146
- "買東西先了解我吃的動物性食品動物的生活環境\nUnderstand the Living Conditions of Animals for the Animal Products I Consume",
147
- "老實說我對任何環保資訊都沒有太多興趣\nHonestly, I'm Not Very Interested in Any Eco Information",
148
- "投資前比較公司的環保表現\nCompare Companies' Environmental Performance Before Investing"
149
- ],
150
- 'Conscious (n=340)': [0.367, 0.415, 0.191, 0.176, 0.079, 1.000, 0.197, 0.265, 0.144, 0.241, 0.144, 0.332, 0.044, 0.188],
151
- 'Interested (n=215)': [0.260, 0.163, 0.153, 0.191, 0.107, 0.000, 0.135, 0.219, 0.172, 0.186, 0.093, 0.214, 0.233, 0.130],
152
- 'Advocate (n=126)': [0.825, 0.881, 0.460, 0.746, 0.230, 0.881, 0.667, 0.690, 0.421, 0.865, 0.468, 0.778, 0.143, 0.738]
153
- }
154
- # Create a DataFrame
155
- df = pd.DataFrame(data)
156
-
157
- # Set the 'Feature' column as the index
158
- df.set_index('Feature', inplace=True)
159
-
160
- # Plot
161
- fig, ax = plt.subplots(figsize=(14, 8))
162
- df.plot(kind='bar', width=0.8, ax=ax)
163
-
164
- # Set titles and labels using the Chinese font where necessary
165
- plt.title('Comparison of Product Feature Preferences by Persona', fontproperties=font_prop)
166
- plt.ylabel('Average Score', fontproperties=font_prop)
167
- plt.xlabel('Feature', fontproperties=font_prop)
168
- plt.xticks(rotation=45, ha='right', fontproperties=font_prop)
169
-
170
- # Set the x-tick labels to use the Chinese font
171
- ax.set_xticklabels(df.index, fontproperties=font_prop, rotation=45, ha='right')
172
-
173
- plt.legend(title='Personas')
174
-
175
- # Ensure layout is tight so everything fits
176
- plt.tight_layout()
177
-
178
- # Streamlit uses st.pyplot() to display matplotlib charts
179
- st.pyplot(fig)
 
1
  import streamlit as st
 
2
  from matplotlib.font_manager import FontProperties
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
  import pandas as pd
6
  import numpy as np
 
 
7
 
8
  @st.cache_data
9
  def show(df):
10
  # Load the Chinese font
11
  chinese_font = FontProperties(fname='mingliu.ttf', size=12)
12
  st.title("Shopping")
 
 
 
 
 
 
13
  st.markdown(
14
  f"<h2 style='text-align: center;'>Boycott Count</h2>", unsafe_allow_html=True)
15
  show_boycott_count(df, font_prop=chinese_font)
 
35
  plt.text(index, value, str(value), ha='center', va='bottom', fontproperties=font_prop)
36
 
37
  # Display the chart in Streamlit
38
+ st.pyplot(plt)