Wajahat698 commited on
Commit
16d359a
·
verified ·
1 Parent(s): e5beab9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -145
app.py CHANGED
@@ -74,9 +74,6 @@ df_builder_pivot_str = ""
74
 
75
 
76
 
77
-
78
-
79
-
80
  def plot_model_results(results_df, average_value, title, model_type):
81
  """
82
  Plot model results with specific orders and colors for Trust and NPS models.
@@ -89,117 +86,6 @@ def plot_model_results(results_df, average_value, title, model_type):
89
  Image: Image object containing the plot.
90
  """
91
 
92
- logger.info(
93
- "Plotting model results for %s model with title '%s'.", model_type, title
94
- )
95
- try:
96
- import math
97
-
98
- # Color mapping
99
- color_map = {
100
- "Stability": "#375570",
101
- "Development": "#E3B05B",
102
- "Relationship": "#C63F48",
103
- "Benefit": "#418387",
104
- "Vision": "#DF8859",
105
- "Competence": "#6D93AB",
106
- "Trust": "#f5918a",
107
- }
108
-
109
- # Load Trust Core Image
110
- image_path = "./images/image.png"
111
- try:
112
- trust_core_img = Image.open(image_path)
113
- except FileNotFoundError:
114
- raise FileNotFoundError(f"❌ Error: Trust Core image '{image_path}' not found!")
115
-
116
- # 🟠 NORMAL BAR PLOT
117
- # 🟢 BUBBLE PLOT for NPS (copied exactly from your plot_trust_driver_bubbles)
118
- order = ["Trust", "Stability", "Development", "Relationship", "Benefit", "Vision", "Competence"]
119
-
120
- results_df["Predictor"] = pd.Categorical(
121
- results_df["Predictor"], categories=order, ordered=True
122
- )
123
- results_df.sort_values("Predictor", ascending=False, inplace=True)
124
-
125
- # Extract values
126
- values_dict = results_df.set_index("Predictor")["Importance_percent"].to_dict()
127
- percentages = [values_dict.get(pred, 0) for pred in order]
128
-
129
- # Bubble sizes
130
- min_radius = 0.15
131
- base_percentage = min(percentages) if min(percentages) > 0 else 1
132
- bubble_radii = [min_radius * (p / base_percentage) ** 0.75 for p in percentages]
133
-
134
- # Central core
135
- central_radius = 0.8
136
-
137
- # Bubble positions
138
- default_positions = {
139
- "Trust": (0.0, 1.3),
140
- "Stability": (-1.05, 0.0),
141
- "Development": (1.05, 0.0),
142
- "Relationship": (-0.6, 0.85),
143
- "Benefit": (0.6, -0.85),
144
- "Vision": (0.6, 0.85),
145
- "Competence": (-0.6, -0.85)
146
- }
147
-
148
- # Adjust bubble positions slightly to touch core
149
- for i, predictor in enumerate(order):
150
- x, y = default_positions[predictor]
151
- r = bubble_radii[i]
152
- distance = np.sqrt(x**2 + y**2)
153
- scale_factor = (central_radius + r - 0.2) / distance
154
- default_positions[predictor] = (x * scale_factor, y * scale_factor)
155
-
156
- fig, ax = plt.subplots(figsize=(10, 10), dpi=300)
157
- ax.set_xlim(-2, 2)
158
- ax.set_ylim(-2, 2)
159
- ax.set_aspect('equal')
160
- ax.axis("off")
161
-
162
- # Draw Trust Core
163
- extent = [-central_radius, central_radius, -central_radius, central_radius]
164
- ax.imshow(trust_core_img, extent=extent, alpha=1.0)
165
-
166
- # Draw Bubbles
167
- for i, predictor in enumerate(order):
168
- x, y = default_positions[predictor]
169
- r = bubble_radii[i]
170
- color = color_map.get(predictor, "#cccccc")
171
- circle = patches.Circle((x, y), r, facecolor=color, alpha=1.0, lw=1.5)
172
- ax.add_patch(circle)
173
- ax.text(x, y, f"{percentages[i]:.1f}%", fontsize=10, fontweight="bold", ha="center", va="center", color="white")
174
-
175
- plt.title(title, fontsize=12)
176
-
177
- # Save and return image
178
- img_data = io.BytesIO()
179
- plt.savefig(img_data, format="png", bbox_inches="tight", facecolor=fig.get_facecolor())
180
- img_data.seek(0)
181
- img = Image.open(img_data)
182
- plt.close(fig)
183
-
184
- return img
185
-
186
- except Exception as e:
187
- logger.error("Error plotting model results: %s", e)
188
- raise
189
-
190
-
191
- def plot_model(results_df, average_value, title, model_type):
192
- """
193
- Plot model results with specific orders and colors for Trust and NPS models.
194
- Args:
195
- results_df (DataFrame): DataFrame containing predictor names and their importance.
196
- average_value (float): Average importance value.
197
- title (str): Title of the plot.
198
- model_type (str): Type of model (either "Trust" or "NPS").
199
- Returns:
200
- Image: Image object containing the plot.
201
- """
202
-
203
  logger.info(
204
  "Plotting model results for %s model with title '%s'.", model_type, title
205
  )
@@ -590,11 +476,7 @@ def plot_trust_driver_bubbles(trust_df, title, bubble_positions=None, gap=-0.2):
590
  return Image.open(img_buffer)
591
 
592
 
593
-
594
-
595
-
596
-
597
-
598
  def analyze_excel_single(file_path):
599
  """
600
  Analyzes a single Excel file containing data and generates plots for Trust, NPS, Loyalty, Consideration, and Satisfaction models.
@@ -782,7 +664,6 @@ def analyze_excel_single(file_path):
782
  "NPS",
783
  )
784
 
785
-
786
  # plot loyalty
787
  img_loyalty = None
788
  results_df_loyalty = None
@@ -1135,13 +1016,14 @@ def variable_outputs(file_inputs):
1135
  # Get dataset name
1136
  dataset_name = file_inputs_single[row].split("/")[-1]
1137
 
 
1138
  plots = [
1139
  gr.Markdown(
1140
  "<span style='font-size:20px; font-weight:bold;'>2) Trust Profile</span>",
1141
  visible=True,
1142
  ),
1143
  gr.Markdown(
1144
- "<div style='font-size:16px;'>This analysis shows you how strongly you are trusted in each of the Six Buckets of Trust®. You can also see this for any competitor.</div>",
1145
  visible=True,
1146
  ),
1147
  gr.Image(
@@ -1156,17 +1038,15 @@ def variable_outputs(file_inputs):
1156
  ),
1157
  gr.Markdown(
1158
  """
1159
- <div style='font-size:16px;'>
1160
- This analysis highlights which Trust Buckets® are most effective in improving NPS and building trust.
1161
- <br><br>
1162
- The baseline impact for each driver is <b>16.7%</b> (100% divided across 6 Trust Buckets®). Any percentage above this average indicates higher significance,
1163
- meaning these Trust Buckets® require more attention. To maximise their potential, focus on “filling” them with the right attributes and tailored messaging.
1164
- </div>
1165
  """,
1166
  visible=True,
1167
  ),
1168
-
1169
-
 
1170
  gr.Image(
1171
  value=img_trust,
1172
  type="pil",
@@ -1179,7 +1059,6 @@ def variable_outputs(file_inputs):
1179
  label="NPS Drivers",
1180
  visible=True,
1181
  ),
1182
-
1183
  gr.Image(
1184
  value=img_loyalty,
1185
  type="pil",
@@ -1201,6 +1080,8 @@ def variable_outputs(file_inputs):
1201
  ),
1202
  ]
1203
 
 
 
1204
 
1205
  if isinstance(df_builder_pivot, pd.DataFrame):
1206
  logger.debug(f"df_builder_pivot: {df_builder_pivot}")
@@ -2258,25 +2139,31 @@ def highlight_button(button_name):
2258
  with gr.Blocks(css=css,js=js_func) as demo:
2259
  with gr.Column():
2260
  gr.Markdown(
2261
- """
2262
- <h2 style="text-align: center; font-size: 2.25rem; font-weight: 600;">
2263
- What drives your NPS and trust?
2264
- </h2>
2265
- """
2266
- )
2267
- # gr.HTML(
2268
- # """
2269
- # <div style="text-align: center;">
2270
- # <img src="https://huggingface.co/spaces/trustlogic/temporary-trustlogic-demo/resolve/main/screenshot.png"
2271
- # alt="Trust Buckets" style="max-width: 100%; height: auto; border: none;">
2272
- # </div>
2273
- # """
2274
- # )
2275
 
2276
 
2277
  gr.Markdown(
2278
  """
2279
- Quickly identify what drives your NPS and trust across different segments using the automated analyser.
 
 
 
 
 
 
 
2280
  """
2281
  )
2282
 
@@ -2286,6 +2173,7 @@ with gr.Blocks(css=css,js=js_func) as demo:
2286
  <span style="font-size:15px;">Volkswagen Example</span><br>
2287
  As a default, the analysis displays <strong>Volkswagen Owner</strong> results. To trigger the analysis for
2288
  <strong>Prospects</strong>, toggle to ‘VW Prospects’.
 
2289
 
2290
  """,
2291
  visible=True,
 
74
 
75
 
76
 
 
 
 
77
  def plot_model_results(results_df, average_value, title, model_type):
78
  """
79
  Plot model results with specific orders and colors for Trust and NPS models.
 
86
  Image: Image object containing the plot.
87
  """
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  logger.info(
90
  "Plotting model results for %s model with title '%s'.", model_type, title
91
  )
 
476
  return Image.open(img_buffer)
477
 
478
 
479
+
 
 
 
 
480
  def analyze_excel_single(file_path):
481
  """
482
  Analyzes a single Excel file containing data and generates plots for Trust, NPS, Loyalty, Consideration, and Satisfaction models.
 
664
  "NPS",
665
  )
666
 
 
667
  # plot loyalty
668
  img_loyalty = None
669
  results_df_loyalty = None
 
1016
  # Get dataset name
1017
  dataset_name = file_inputs_single[row].split("/")[-1]
1018
 
1019
+ # Based on the number of files uploaded, determine the content of each textbox
1020
  plots = [
1021
  gr.Markdown(
1022
  "<span style='font-size:20px; font-weight:bold;'>2) Trust Profile</span>",
1023
  visible=True,
1024
  ),
1025
  gr.Markdown(
1026
+ "This analysis shows you how strongly you are trusted in each of the Six Buckets of Trust®. You can also see this for any competitor.",
1027
  visible=True,
1028
  ),
1029
  gr.Image(
 
1038
  ),
1039
  gr.Markdown(
1040
  """
1041
+ This analysis highlights which Trust Buckets® are most effective in building trust and positively impacting your key performance indicators (KPIs).
1042
+ <br>
1043
+ The Trust Buckets® that extend further to the right represent those with greater importance. We illustrate how each bucket over- or under-indexes against the average. The baseline impact for each driver is 16.7% (100% divided across 6 Trust Buckets®). Any percentage above this average indicates higher significance, meaning these Trust Buckets® require more attention. To maximize their potential, focus on “filling” them with the right attributes and tailored messaging.
 
 
 
1044
  """,
1045
  visible=True,
1046
  ),
1047
+
1048
+
1049
+
1050
  gr.Image(
1051
  value=img_trust,
1052
  type="pil",
 
1059
  label="NPS Drivers",
1060
  visible=True,
1061
  ),
 
1062
  gr.Image(
1063
  value=img_loyalty,
1064
  type="pil",
 
1080
  ),
1081
  ]
1082
 
1083
+ # add current plots to container
1084
+ plots_visible += plots
1085
 
1086
  if isinstance(df_builder_pivot, pd.DataFrame):
1087
  logger.debug(f"df_builder_pivot: {df_builder_pivot}")
 
2139
  with gr.Blocks(css=css,js=js_func) as demo:
2140
  with gr.Column():
2141
  gr.Markdown(
2142
+ """
2143
+ ### Which Trust Buckets® are the most important to your audience and how do you fill them best?
2144
+
2145
+ """
2146
+ )
2147
+ gr.HTML(
2148
+ """
2149
+ <div style="text-align: center;">
2150
+ <img src="https://huggingface.co/spaces/trustlogic/temporary-trustlogic-demo/resolve/main/screenshot.png"
2151
+ alt="Trust Buckets" style="max-width: 100%; height: auto; border: none;">
2152
+ </div>
2153
+ """
2154
+ )
 
2155
 
2156
 
2157
  gr.Markdown(
2158
  """
2159
+ To use TrustAnalyser®, you first insert the TrustLogic® quantitative statements into any of your surveys.
2160
+ The module consists of six statements that have been extensively modeled, calibrated, and proven in
2161
+ practice for over 14 years.
2162
+
2163
+ Once your survey results are in, you can export the data as a CSV. Use our Excel template, or seamlessly
2164
+ link your SurveyMonkey survey to our Analyser interface. From there, TrustAnalyser® helps you quickly
2165
+ identify which Trust Buckets® resonate most with your audience and the specific TrustBuilders® that
2166
+ will fill them effectively.
2167
  """
2168
  )
2169
 
 
2173
  <span style="font-size:15px;">Volkswagen Example</span><br>
2174
  As a default, the analysis displays <strong>Volkswagen Owner</strong> results. To trigger the analysis for
2175
  <strong>Prospects</strong>, toggle to ‘VW Prospects’.
2176
+ If you include your competitors in your survey, you can also see their results.
2177
 
2178
  """,
2179
  visible=True,