Update app.py
Browse files
app.py
CHANGED
|
@@ -1,207 +1,3 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
|
| 5 |
-
from cear_model import CEARModel
|
| 6 |
-
|
| 7 |
-
cear_analyzer = CEARModel()
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def build_dataframe_from_inputs(values):
|
| 11 |
-
"""
|
| 12 |
-
values: list of tuples [(platform_name, minutes, variety), ...]
|
| 13 |
-
Returns: DataFrame with platform_name, minutes_per_week, variety_score
|
| 14 |
-
"""
|
| 15 |
-
rows = []
|
| 16 |
-
for name, minutes, variety in values:
|
| 17 |
-
minutes = 0.0 if minutes is None else float(minutes)
|
| 18 |
-
variety = None if variety is None else float(variety)
|
| 19 |
-
if minutes > 0 or (variety is not None and not np.isnan(variety)):
|
| 20 |
-
rows.append(
|
| 21 |
-
{
|
| 22 |
-
"platform_name": name,
|
| 23 |
-
"minutes_per_week": minutes,
|
| 24 |
-
"variety_score": variety,
|
| 25 |
-
}
|
| 26 |
-
)
|
| 27 |
-
if not rows:
|
| 28 |
-
return pd.DataFrame(columns=["platform_name", "minutes_per_week", "variety_score"])
|
| 29 |
-
return pd.DataFrame(rows)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def analyze_user_data(
|
| 33 |
-
tiktok_minutes,
|
| 34 |
-
tiktok_variety,
|
| 35 |
-
insta_minutes,
|
| 36 |
-
insta_variety,
|
| 37 |
-
youtube_minutes,
|
| 38 |
-
youtube_variety,
|
| 39 |
-
twitter_minutes,
|
| 40 |
-
twitter_variety,
|
| 41 |
-
reddit_minutes,
|
| 42 |
-
reddit_variety,
|
| 43 |
-
facebook_minutes,
|
| 44 |
-
facebook_variety,
|
| 45 |
-
other_minutes,
|
| 46 |
-
other_variety,
|
| 47 |
-
feed_satisfaction,
|
| 48 |
-
fomo_level,
|
| 49 |
-
):
|
| 50 |
-
df = build_dataframe_from_inputs(
|
| 51 |
-
[
|
| 52 |
-
("tiktok", tiktok_minutes, tiktok_variety),
|
| 53 |
-
("instagram", insta_minutes, insta_variety),
|
| 54 |
-
("youtube", youtube_minutes, youtube_variety),
|
| 55 |
-
("twitter", twitter_minutes, twitter_variety),
|
| 56 |
-
("reddit", reddit_minutes, reddit_variety),
|
| 57 |
-
("facebook", facebook_minutes, facebook_variety),
|
| 58 |
-
("other", other_minutes, other_variety),
|
| 59 |
-
]
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
if df.empty:
|
| 63 |
-
return "Please enter at least one platform with some weekly minutes.", pd.DataFrame()
|
| 64 |
-
|
| 65 |
-
scores = cear_analyzer.calculate_scores(
|
| 66 |
-
df, satisfaction=feed_satisfaction, fomo=fomo_level
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
c = float(scores.get("C_Score", 0.0))
|
| 70 |
-
a = float(scores.get("A_Risk", 0.0))
|
| 71 |
-
d = float(scores.get("D_Index", 0.0))
|
| 72 |
-
avg_variety = scores.get("Avg_Variety", None)
|
| 73 |
-
satisfaction = scores.get("Satisfaction", None)
|
| 74 |
-
fomo = scores.get("FOMO", None)
|
| 75 |
-
per_eff = scores.get("Per_Platform_Efficiency", [])
|
| 76 |
-
|
| 77 |
-
# --- profile based on C & A --- #
|
| 78 |
-
if c >= 70 and a >= 70:
|
| 79 |
-
profile = (
|
| 80 |
-
"You are highly plugged into online culture, but that comes with high "
|
| 81 |
-
"algorithmic risk and a heavy concentration of attention on a small set of feeds."
|
| 82 |
-
)
|
| 83 |
-
elif c >= 70 and a < 70:
|
| 84 |
-
profile = (
|
| 85 |
-
"You are well-connected to online culture without extreme algorithmic concentration. "
|
| 86 |
-
"Your usage is relatively efficient for staying up to date."
|
| 87 |
-
)
|
| 88 |
-
elif c < 40 and a >= 70:
|
| 89 |
-
profile = (
|
| 90 |
-
"You give a lot of attention to a narrow set of feeds without gaining much cultural exposure. "
|
| 91 |
-
"This is a high-risk, low-benefit pattern."
|
| 92 |
-
)
|
| 93 |
-
else:
|
| 94 |
-
profile = (
|
| 95 |
-
"You currently have relatively low exposure to viral trends and also keep algorithmic risk low. "
|
| 96 |
-
"You are either deliberately detached from viral culture or simply under-invested in trend-dense platforms."
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
# --- variety interpretation --- #
|
| 100 |
-
if avg_variety is None:
|
| 101 |
-
variety_text = (
|
| 102 |
-
"You did not provide variety ratings, so this analysis focuses only on time and platform mix."
|
| 103 |
-
)
|
| 104 |
-
elif avg_variety < 4:
|
| 105 |
-
variety_text = (
|
| 106 |
-
f"Your average variety rating is **{avg_variety:.1f} / 10**, which suggests that your feeds feel "
|
| 107 |
-
"quite repetitive and reinforce a narrow slice of content."
|
| 108 |
-
)
|
| 109 |
-
elif avg_variety > 7:
|
| 110 |
-
variety_text = (
|
| 111 |
-
f"Your average variety rating is **{avg_variety:.1f} / 10**, which suggests that you see a wide range "
|
| 112 |
-
"of topics and styles. This broadens your exposure and slightly offsets some algorithmic risk."
|
| 113 |
-
)
|
| 114 |
-
else:
|
| 115 |
-
variety_text = (
|
| 116 |
-
f"Your average variety rating is **{avg_variety:.1f} / 10**, indicating a moderate mix of content types."
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
# --- satisfaction & FOMO interpretation --- #
|
| 120 |
-
satisfaction_text = ""
|
| 121 |
-
if satisfaction is not None:
|
| 122 |
-
if satisfaction <= 3:
|
| 123 |
-
satisfaction_text = (
|
| 124 |
-
"You report low satisfaction with your feed, which suggests your current pattern might not "
|
| 125 |
-
"match what you actually want from social media."
|
| 126 |
-
)
|
| 127 |
-
elif satisfaction >= 8:
|
| 128 |
-
satisfaction_text = (
|
| 129 |
-
"You report high satisfaction with your feed, indicating your current usage largely aligns "
|
| 130 |
-
"with what you want out of these platforms."
|
| 131 |
-
)
|
| 132 |
-
else:
|
| 133 |
-
satisfaction_text = (
|
| 134 |
-
"Your satisfaction is in the mid range, which suggests your feed is 'fine' but not fully optimized "
|
| 135 |
-
"for how you would like to spend your attention."
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
fomo_text = ""
|
| 139 |
-
if fomo is not None:
|
| 140 |
-
if fomo >= 7 and c < 50:
|
| 141 |
-
fomo_text = (
|
| 142 |
-
"You feel out of the loop and your relatively low C-Score supports that feeling. "
|
| 143 |
-
"If staying current matters to you, a bit more time on trend-dense platforms could help."
|
| 144 |
-
)
|
| 145 |
-
elif fomo <= 3 and c < 40:
|
| 146 |
-
fomo_text = (
|
| 147 |
-
"You have limited exposure to trends but do not feel much FOMO, which suggests a comfortable "
|
| 148 |
-
"distance from viral culture."
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
# --- summary header --- #
|
| 152 |
-
summary_lines = [
|
| 153 |
-
"## 📊 CEAR Analysis Summary",
|
| 154 |
-
"",
|
| 155 |
-
f"- **Cultural Connectedness Score (C-Score):** **{c:.2f}**",
|
| 156 |
-
f"- **Algorithmic Risk Score (A-Risk):** **{a:.2f}**",
|
| 157 |
-
f"- **Platform Diversity Index (D-Index):** **{d:.2f}**",
|
| 158 |
-
]
|
| 159 |
-
if avg_variety is not None:
|
| 160 |
-
summary_lines.append(f"- **Average Variety Rating (0–10):** **{avg_variety:.2f}**")
|
| 161 |
-
if satisfaction is not None:
|
| 162 |
-
summary_lines.append(f"- **Feed Satisfaction (0–10):** **{satisfaction:.1f}**")
|
| 163 |
-
if fomo is not None:
|
| 164 |
-
summary_lines.append(f"- **FOMO / Out-of-the-loop (0–10):** **{fomo:.1f}**")
|
| 165 |
-
|
| 166 |
-
# --- interpretation section --- #
|
| 167 |
-
summary_lines.extend(
|
| 168 |
-
[
|
| 169 |
-
"",
|
| 170 |
-
"### 📝 Interpretation",
|
| 171 |
-
"",
|
| 172 |
-
profile,
|
| 173 |
-
"",
|
| 174 |
-
variety_text,
|
| 175 |
-
]
|
| 176 |
-
)
|
| 177 |
-
if satisfaction_text:
|
| 178 |
-
summary_lines.append("")
|
| 179 |
-
summary_lines.append(satisfaction_text)
|
| 180 |
-
if fomo_text:
|
| 181 |
-
summary_lines.append("")
|
| 182 |
-
summary_lines.append(fomo_text)
|
| 183 |
-
|
| 184 |
-
# --- explanation of how survey inputs are used --- #
|
| 185 |
-
survey_explainer = """
|
| 186 |
-
### ℹ️ How your answers are used
|
| 187 |
-
|
| 188 |
-
- **Minutes per week** drive the core scores. More time on high-weight platforms increases both C-Score and A-Risk, with diminishing returns for C-Score.
|
| 189 |
-
- **Per-platform variety (0–10)** is combined into a minutes-weighted average. Low variety means you mainly see one type of content; high variety means you see a wider mix of topics and styles.
|
| 190 |
-
- **Feed satisfaction (0–10)** does not change the scores; it is used to interpret whether your current pattern feels good or frustrating to you.
|
| 191 |
-
- **FOMO (0–10)** is compared with your C-Score: high FOMO with low C-Score means you feel out of the loop, while low FOMO with low C-Score means you are detached by choice.
|
| 192 |
-
"""
|
| 193 |
-
summary_lines.append("")
|
| 194 |
-
summary_lines.append(survey_explainer.strip())
|
| 195 |
-
|
| 196 |
-
summary_lines.append(
|
| 197 |
-
"\nThe C-Score uses a logarithmic transform of your weekly minutes, encoding diminishing returns as time increases. "
|
| 198 |
-
"A-Risk reflects your raw time investment and how concentrated it is on a small set of high-weight platforms. "
|
| 199 |
-
"D-Index captures how many platforms you use in a meaningful way (higher values mean your time is spread across more platforms)."
|
| 200 |
-
)
|
| 201 |
-
|
| 202 |
-
summary = "\n".join(summary_lines).strip()
|
| 203 |
-
|
| 204 |
-
# --- per-platform efficiency table --- #
|
| 205 |
if isinstance(per_eff, list) and per_eff:
|
| 206 |
eff_df = pd.DataFrame(per_eff)
|
| 207 |
if "platform_name" in eff_df.columns:
|
|
@@ -214,88 +10,3 @@ def analyze_user_data(
|
|
| 214 |
eff_df = pd.DataFrame(columns=["platform", "efficiency_score"])
|
| 215 |
|
| 216 |
return summary, eff_df
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
# ---------------- Gradio UI ---------------- #
|
| 220 |
-
|
| 221 |
-
with gr.Blocks() as demo:
|
| 222 |
-
gr.Markdown(
|
| 223 |
-
"# CEAR – Cultural Exposure & Algorithmic Risk Analyzer\n"
|
| 224 |
-
"Enter your weekly screen time per platform, rate the variety of each feed, and optionally report how satisfied "
|
| 225 |
-
"you are with your feed and how much FOMO you feel."
|
| 226 |
-
)
|
| 227 |
-
|
| 228 |
-
with gr.Row():
|
| 229 |
-
with gr.Column():
|
| 230 |
-
gr.Markdown("### Weekly minutes & per-platform variety (0–10)")
|
| 231 |
-
|
| 232 |
-
tiktok_minutes = gr.Number(label="TikTok minutes/week", value=240, precision=0)
|
| 233 |
-
tiktok_variety = gr.Slider(label="TikTok variety (0–10)", minimum=0, maximum=10, step=1, value=4)
|
| 234 |
-
|
| 235 |
-
insta_minutes = gr.Number(label="Instagram minutes/week", value=180, precision=0)
|
| 236 |
-
insta_variety = gr.Slider(label="Instagram variety (0–10)", minimum=0, maximum=10, step=1, value=5)
|
| 237 |
-
|
| 238 |
-
youtube_minutes = gr.Number(label="YouTube minutes/week", value=120, precision=0)
|
| 239 |
-
youtube_variety = gr.Slider(label="YouTube variety (0–10)", minimum=0, maximum=10, step=1, value=7)
|
| 240 |
-
|
| 241 |
-
twitter_minutes = gr.Number(label="Twitter/X minutes/week", value=60, precision=0)
|
| 242 |
-
twitter_variety = gr.Slider(label="Twitter/X variety (0–10)", minimum=0, maximum=10, step=1, value=6)
|
| 243 |
-
|
| 244 |
-
reddit_minutes = gr.Number(label="Reddit minutes/week", value=90, precision=0)
|
| 245 |
-
reddit_variety = gr.Slider(label="Reddit variety (0–10)", minimum=0, maximum=10, step=1, value=8)
|
| 246 |
-
|
| 247 |
-
facebook_minutes = gr.Number(label="Facebook minutes/week", value=45, precision=0)
|
| 248 |
-
facebook_variety = gr.Slider(label="Facebook variety (0–10)", minimum=0, maximum=10, step=1, value=3)
|
| 249 |
-
|
| 250 |
-
other_minutes = gr.Number(label="Other platforms minutes/week", value=30, precision=0)
|
| 251 |
-
other_variety = gr.Slider(label="Other platforms variety (0–10)", minimum=0, maximum=10, step=1, value=5)
|
| 252 |
-
|
| 253 |
-
with gr.Column():
|
| 254 |
-
gr.Markdown("### Self-report (global)")
|
| 255 |
-
|
| 256 |
-
feed_satisfaction = gr.Slider(
|
| 257 |
-
label="Feed satisfaction (0 = miserable, 10 = very happy)",
|
| 258 |
-
minimum=0,
|
| 259 |
-
maximum=10,
|
| 260 |
-
step=1,
|
| 261 |
-
value=6,
|
| 262 |
-
)
|
| 263 |
-
fomo_level = gr.Slider(
|
| 264 |
-
label="FOMO / out-of-the-loop feeling (0 = none, 10 = extreme)",
|
| 265 |
-
minimum=0,
|
| 266 |
-
maximum=10,
|
| 267 |
-
step=1,
|
| 268 |
-
value=4,
|
| 269 |
-
)
|
| 270 |
-
|
| 271 |
-
run_btn = gr.Button("Analyze")
|
| 272 |
-
|
| 273 |
-
summary_out = gr.Markdown(label="Score Results")
|
| 274 |
-
eff_out = gr.Dataframe(label="Per-platform Cultural Efficiency")
|
| 275 |
-
|
| 276 |
-
run_btn.click(
|
| 277 |
-
fn=analyze_user_data,
|
| 278 |
-
inputs=[
|
| 279 |
-
tiktok_minutes,
|
| 280 |
-
tiktok_variety,
|
| 281 |
-
insta_minutes,
|
| 282 |
-
insta_variety,
|
| 283 |
-
youtube_minutes,
|
| 284 |
-
youtube_variety,
|
| 285 |
-
twitter_minutes,
|
| 286 |
-
twitter_variety,
|
| 287 |
-
reddit_minutes,
|
| 288 |
-
reddit_variety,
|
| 289 |
-
facebook_minutes,
|
| 290 |
-
facebook_variety,
|
| 291 |
-
other_minutes,
|
| 292 |
-
other_variety,
|
| 293 |
-
feed_satisfaction,
|
| 294 |
-
fomo_level,
|
| 295 |
-
],
|
| 296 |
-
outputs=[summary_out, eff_out],
|
| 297 |
-
)
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
if __name__ == "__main__":
|
| 301 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
if isinstance(per_eff, list) and per_eff:
|
| 2 |
eff_df = pd.DataFrame(per_eff)
|
| 3 |
if "platform_name" in eff_df.columns:
|
|
|
|
| 10 |
eff_df = pd.DataFrame(columns=["platform", "efficiency_score"])
|
| 11 |
|
| 12 |
return summary, eff_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|