"""
app.py
HIFFI Artist Intelligence - Multi-Platform Executive Dashboard
Dual-Engine Architecture:
1. HIFFI Hip-Hop 500: Multi-platform evidence tiers, YouTube/Spotify/IG telemetry, BVI, CPI engine.
2. Global Master Catalog (17k): Empirical registry verification (CSPC, RIAA, Last.fm) with ordinal tier ranking.
"""
from pathlib import Path
import sqlite3
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import streamlit as st
try:
from core.cpi_engine import CommercialPowerEngine
except ImportError:
CommercialPowerEngine = None
try:
from exporters.excel_workbook import generate_executive_sales_workbook
except ImportError:
generate_executive_sales_workbook = None
try:
from core.ranking_engine import HIFFIRankingEngine
except ImportError:
HIFFIRankingEngine = None
st.set_page_config(
page_title="HIFFI | Multi-Platform Intelligence",
page_icon="โก",
layout="wide",
initial_sidebar_state="expanded",
)
PATH_DB = Path("data/hiffi_catalog.db")
PATH_500 = Path("data/hiffi_multiplatform_500.csv")
PATH_17K = Path("data/master_artists_intelligence_17k_final.csv")
FALLBACK_PATH = Path("enriched_artists.csv")
# Global CSS Styling
st.markdown(
"""
""",
unsafe_allow_html=True,
)
@st.cache_data(ttl=60)
def load_hiffi_500():
df = pd.DataFrame()
if PATH_DB.exists():
try:
conn = sqlite3.connect(PATH_DB)
query = """
WITH latest_telemetry AS (
SELECT t.*, ROW_NUMBER() OVER (
PARTITION BY t.artist_id
ORDER BY t.last_synced_at DESC, t.telemetry_id DESC
) as rn
FROM telemetry_metrics t
),
latest_rankings AS (
SELECT r.*, ROW_NUMBER() OVER (
PARTITION BY r.artist_id
ORDER BY r.ranking_id DESC
) as rn
FROM rankings r
),
latest_commercial AS (
SELECT c.*, ROW_NUMBER() OVER (
PARTITION BY c.artist_id
ORDER BY c.commercial_id DESC
) as rn
FROM commercial_sales c
)
SELECT
a.artist_id,
a.artist_name as name,
a.image_verified_flag,
r.catalog_rank as hiffi_xp_published_rank,
r.tier_rank,
r.catalog_intelligence_tier,
t.subscribers_count as subscriber_count,
t.total_views as lifetime_views,
t.spotify_monthly_listeners,
t.instagram_followers,
t.daily_view_velocity,
t.growth_velocity_7d as momentum_7d,
t.growth_velocity_30d as momentum_30d,
t.spotify_daily_change,
t.momentum_score,
c.pure_album_sales,
c.physical_singles_sales,
c.digital_singles_sales,
c.streaming_eas,
c.total_eas_cspc,
c.sales_data_status
FROM artists a
LEFT JOIN latest_telemetry t ON a.artist_id = t.artist_id AND t.rn = 1
LEFT JOIN latest_rankings r ON a.artist_id = r.artist_id AND r.rn = 1
LEFT JOIN latest_commercial c ON a.artist_id = c.artist_id AND c.rn = 1
WHERE a.image_verified_flag = 1
"""
df = pd.read_sql_query(query, conn)
conn.close()
except Exception:
df = pd.DataFrame()
if df.empty:
target = PATH_500 if PATH_500.exists() else FALLBACK_PATH
if target.exists():
df = pd.read_csv(target)
if df.empty:
return pd.DataFrame()
if "name" not in df.columns and "artist_name" in df.columns:
df["name"] = df["artist_name"]
if "subscriber_count" not in df.columns and "subscribers_count" in df.columns:
df["subscriber_count"] = df["subscribers_count"]
if "lifetime_views" not in df.columns and "total_views" in df.columns:
df["lifetime_views"] = df["total_views"]
if "hiffi_xp_published_rank" not in df.columns:
df["hiffi_xp_published_rank"] = df.get("catalog_rank", df.get("hiffi_xp_rank", df.get("hiffi_rank", df.index + 1)))
has_yt = df.get("subscriber_count", pd.Series(index=df.index)).fillna(0).gt(0).astype(int)
has_sp = df.get("spotify_monthly_listeners", pd.Series(index=df.index)).fillna(0).gt(0).astype(int)
has_ig = df.get("instagram_followers", pd.Series(index=df.index)).fillna(0).gt(0).astype(int)
df["platform_evidence_tier"] = has_yt + has_sp + has_ig
numeric_cols = [
"hiffi_xp_published_rank", "hiffi_xp_rank", "hiffi_xp_score", "production_hiffi_rank",
"production_hiffi_score", "platform_evidence_tier", "breakout_velocity_index",
"subscriber_count", "daily_view_velocity", "momentum_30d", "spotify_monthly_listeners",
"spotify_daily_change", "instagram_followers", "confidence_score", "breakout_rank",
"chartmasters_sales_rank", "cohort_sales_rank", "pure_album_sales",
"physical_singles_sales", "digital_singles_sales", "streaming_eas", "total_eas_cspc",
"riaa_certified_album_units", "riaa_certified_singles_units"
]
for col in numeric_cols:
if col in df.columns:
df[col] = pd.to_numeric(df[col], errors="coerce")
# Dynamic BVI computation
if HIFFIRankingEngine is not None and ("breakout_velocity_index" not in df.columns or df["breakout_velocity_index"].isna().all()):
try:
eng_rank = HIFFIRankingEngine()
temp_df = df.copy()
if "status_flag" not in temp_df.columns:
temp_df["status_flag"] = "COMPLETE"
df["breakout_velocity_index"] = eng_rank.calculate_breakout_velocity(temp_df)
except Exception:
df["breakout_velocity_index"] = 0.0
if "breakout_velocity_index" not in df.columns:
df["breakout_velocity_index"] = 0.0
# Calculate HIFFI-XP composite if not present
if "hiffi_xp_score" not in df.columns or df["hiffi_xp_score"].isna().all():
if HIFFIRankingEngine is not None:
try:
eng_rank = HIFFIRankingEngine()
temp_df = df.copy()
if "status_flag" not in temp_df.columns:
temp_df["status_flag"] = "COMPLETE"
temp_df = eng_rank.score_and_rank(temp_df)
temp_df = eng_rank.calculate_hiffi_xp(temp_df)
df["hiffi_xp_score"] = temp_df["hiffi_xp_score"]
df["effective_weights"] = temp_df.get("effective_weights", "yt:50.0%,sp_monthly:25.0%,sp_velocity:5.0%,ig:20.0%")
if "production_hiffi_score" not in df.columns:
df["production_hiffi_score"] = temp_df.get("production_hiffi_score", 0.0)
except Exception:
df["hiffi_xp_score"] = 50.0
else:
df["hiffi_xp_score"] = 50.0
if "effective_weights" not in df.columns:
df["effective_weights"] = "yt:50.0%,sp_monthly:25.0%,sp_velocity:5.0%,ig:20.0%"
if "anomaly_flags" not in df.columns:
df["anomaly_flags"] = "CLEAN"
if CommercialPowerEngine is not None:
try:
cpi_eng = CommercialPowerEngine()
df["commercial_power_index"] = cpi_eng.calculate_cpi(df)
except Exception:
df["commercial_power_index"] = None
if "subgenre" not in df.columns:
def infer_subgenre(r):
subs = r.get("subscriber_count", 0) or 0
bvi = r.get("breakout_velocity_index", 0) or 0
if bvi >= 70:
return "Breakout / Emerging"
elif subs >= 15000000:
return "Legends"
else:
return "Trap & Street"
df["subgenre"] = df.apply(infer_subgenre, axis=1)
return df
@st.cache_data(ttl=60)
def load_master_17k():
if not PATH_17K.exists():
return pd.DataFrame()
df = pd.read_csv(PATH_17K)
if "name" not in df.columns and "artist_name" in df.columns:
df["name"] = df["artist_name"]
return df
df_500 = load_hiffi_500()
df_17k = load_master_17k()
engine = CommercialPowerEngine() if CommercialPowerEngine is not None else None
def get_provenance_badge(status: str) -> str:
s = str(status).strip()
if "CSPC" in s or s == "VERIFIED_CSPC":
return "โ
VERIFIED (CSPC)"
elif "RIAA" in s or "COMMERCIAL_CORE" in s:
return "๐
VERIFIED (RIAA)"
elif "LASTFM" in s:
return "๐ป VERIFIED (Last.fm)"
elif "ESTIMATED_SEA" in s:
return "๐ ESTIMATED (SEA)"
return "โณ UNAUDITED / PENDING"
# ----------------- Sidebar Controls -----------------
with st.sidebar:
st.markdown("### โก HIFFI Intelligence Suite")
st.caption("Hip-Hop 500 & Master Catalog Intelligence")
st.markdown("
", unsafe_allow_html=True)
catalog_source = st.selectbox(
"๐๏ธ Active Data Scope:",
["โก Hip-Hop 500 (Executive Telemetry)", "๐ Global Master Catalog (17,000+ Acts)"],
index=0,
)
st.markdown("", unsafe_allow_html=True)
# =====================================================================
# MODULE 1: GLOBAL MASTER CATALOG (17,000+ ACTS)
# =====================================================================
if "Global Master Catalog" in catalog_source:
with st.sidebar:
st.markdown("##### ๐ 17k Catalog Filters")
if df_17k.empty:
st.error("Master 17k dataset not found at data/master_artists_intelligence_17k_final.csv")
st.stop()
tier_list = ["All Tiers"] + sorted(df_17k["catalog_intelligence_tier"].dropna().unique().tolist())
sel_tier = st.selectbox("Intelligence Tier:", tier_list, index=0)
search_17k = st.text_input("Search Artist Name:", placeholder="e.g. Eminem, The Roots...")
filt_17k = df_17k.copy()
if sel_tier != "All Tiers":
filt_17k = filt_17k[filt_17k["catalog_intelligence_tier"] == sel_tier]
if search_17k:
filt_17k = filt_17k[filt_17k["name"].str.contains(search_17k, case=False, na=False)]
head1, head2 = st.columns([3, 1])
with head1:
st.markdown("๐ Global Master Artist Catalog (17k)
", unsafe_allow_html=True)
st.markdown("Empirical registry validation via CSPC sales, RIAA certifications, and verified Last.fm global scrobbles.
", unsafe_allow_html=True)
with head2:
st.markdown(f"Filtered: {len(filt_17k):,} / {len(df_17k):,} acts
", unsafe_allow_html=True)
# 17k Macro KPIs
mk1, mk2, mk3, mk4, mk5 = st.columns(5)
with mk1:
st.markdown(f'Universe Size
{len(df_17k):,}
Total Cataloged
', unsafe_allow_html=True)
with mk2:
ver_cnt = df_17k["catalog_rank"].notna().sum()
st.markdown(f'Verified Entities
{ver_cnt:,}
Registry Authenticated
', unsafe_allow_html=True)
with mk3:
t1_cnt = (df_17k["catalog_intelligence_tier"] == "TIER_1_GLOBAL_SUPERSTAR_CSPC").sum()
st.markdown(f'Tier 1 Superstars
{t1_cnt:,}
Audited CSPC EAS
', unsafe_allow_html=True)
with mk4:
lfm_cnt = (df_17k["catalog_intelligence_tier"] == "TIER_3_LASTFM_VERIFIED_ENTITY").sum()
st.markdown(f'Last.fm Verified
{lfm_cnt:,}
Active Platform Entities
', unsafe_allow_html=True)
with mk5:
unver_cnt = len(df_17k) - ver_cnt
st.markdown(f'Unaudited
{unver_cnt:,}
Zero-Registry Matches
', unsafe_allow_html=True)
st.markdown("", unsafe_allow_html=True)
render_cols = [
"catalog_rank", "tier_rank", "name", "catalog_intelligence_tier",
"audited_sales_units", "lastfm_listeners", "lastfm_playcount", "lastfm_url"
]
disp_cols = [c for c in render_cols if c in filt_17k.columns]
st.dataframe(
filt_17k[disp_cols].reset_index(drop=True),
use_container_width=True,
height=560,
column_config={
"catalog_rank": st.column_config.NumberColumn("Global Rank", format="#%d", width="small"),
"tier_rank": st.column_config.NumberColumn("Tier Rank", format="#%d", width="small"),
"name": st.column_config.TextColumn("Artist Name", width="medium"),
"catalog_intelligence_tier": st.column_config.TextColumn("Registry Tier", width="medium"),
"audited_sales_units": st.column_config.NumberColumn("Audited Sales Units", format="%,d", width="medium"),
"lastfm_listeners": st.column_config.NumberColumn("Verified Listeners", format="%,d", width="medium"),
"lastfm_playcount": st.column_config.NumberColumn("Global Scrobbles", format="%,d", width="medium"),
"lastfm_url": st.column_config.LinkColumn("Last.fm Profile", width="small"),
},
)
with st.sidebar:
st.markdown("##### ๐พ 17k Data Export")
csv_17k = filt_17k.to_csv(index=False).encode("utf-8")
st.download_button("๐ฅ Export 17k Table CSV", data=csv_17k, file_name="master_17k_catalog.csv", mime="text/csv", use_container_width=True)
# =====================================================================
# MODULE 2: HIFFI HIP-HOP 500 (EXECUTIVE DASHBOARD)
# =====================================================================
else:
df = df_500
if df.empty:
st.error("Hip-Hop 500 dataset not found. Please verify data/hiffi_catalog.db exists.")
st.stop()
with st.sidebar:
view_mode = st.radio(
"Leaderboard Perspective:",
[
"๐ Published Multi-Platform Rank",
"๐ Breakout Velocity (BVI)",
"๐ฟ Record Sales (CSPC Methodology)",
"๐งช Commercial Simulation Engine (CPI)",
"๐ Full Cohort Audit Grid",
],
index=0,
)
st.markdown("", unsafe_allow_html=True)
catalog_filter = "All Monitored Artists (500)"
if view_mode in ("๐ฟ Record Sales (CSPC Methodology)", "๐งช Commercial Simulation Engine (CPI)"):
st.markdown("##### ๐ Sales Catalog Scope")
catalog_filter = st.radio(
"Filter Rows by Sales Telemetry:",
["All Monitored Artists (500)", "Verified Only (321)", "Pure CSPC Only (62)"],
index=0,
)
st.markdown("", unsafe_allow_html=True)
sim_weight = 0.15
sim_mode = "matched_only"
if view_mode == "๐งช Commercial Simulation Engine (CPI)" and engine is not None:
st.markdown("##### ๐งช Simulation Parameters")
sim_mode_choice = st.radio(
"Evaluation Scope:",
["๐ฏ Matched Cohort Only", "๐ Global Cohort (N=500, Weight Redistribution)"],
index=1 if catalog_filter == "All Monitored Artists (500)" else 0,
)
sim_mode = "matched_only" if "Matched" in sim_mode_choice else "global_redistribution"
sim_weight = st.select_slider(
"Commercial Signal Weight:",
options=[0.0, 0.05, 0.10, 0.15, 0.20],
value=0.15,
format_func=lambda x: f"{int(x*100)}% Commercial",
)
st.caption(f"Baseline Score Weight: **{int((1.0 - sim_weight)*100)}%**")
st.markdown("", unsafe_allow_html=True)
st.markdown("##### ๐ก๏ธ Cohort Filters")
all_verticals = sorted(df["subgenre"].dropna().unique().tolist())
selected_verticals = st.multiselect("Vertical", options=all_verticals, default=all_verticals)
tier_options = sorted(df["platform_evidence_tier"].dropna().unique().astype(int).tolist(), reverse=True)
selected_tiers = st.multiselect("Evidence Tier", options=tier_options, default=tier_options)
min_xp = st.slider("Min HIFFI-XP Score", 0.0, 100.0, 0.0, 5.0)
filtered_df = df[
(df["subgenre"].isin(selected_verticals))
& (df["platform_evidence_tier"].isin(selected_tiers))
& (df["hiffi_xp_score"].fillna(0.0) >= min_xp)
].copy()
if "sales_data_status" in filtered_df.columns:
if catalog_filter == "Pure CSPC Only (62)":
filtered_df = filtered_df[filtered_df["sales_data_status"] == "VERIFIED_CSPC"].copy()
elif catalog_filter == "Verified Only (321)":
filtered_df = filtered_df[filtered_df["sales_data_status"].isin(["VERIFIED_CSPC", "VERIFIED_RIAA"])].copy()
sim_metrics = {}
if view_mode == "๐งช Commercial Simulation Engine (CPI)" and engine is not None:
try:
filtered_df = engine.simulate_ranking(filtered_df, commercial_weight=sim_weight, mode=sim_mode)
sim_metrics = engine.compute_simulation_metrics(filtered_df)
except Exception:
pass
with st.sidebar:
st.markdown("", unsafe_allow_html=True)
st.markdown("##### ๐พ Unified Audit Exports")
if generate_executive_sales_workbook is not None:
try:
excel_workbook_bytes = generate_executive_sales_workbook(filtered_df, commercial_weight=sim_weight, mode=sim_mode)
st.download_button(
"๐ Multi-Tab Audit Excel (.xlsx)",
data=excel_workbook_bytes,
file_name="hiffi_executive_sales_audit.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
use_container_width=True,
)
except Exception:
pass
csv_payload = filtered_df.to_csv(index=False).encode("utf-8")
st.download_button(
"๐ฅ Filtered Table CSV",
data=csv_payload,
file_name="hiffi_multiplatform_filtered.csv",
mime="text/csv",
use_container_width=True,
)
st.markdown("", unsafe_allow_html=True)
st.caption("Governance: CSPC Methodology โข EAS Units โข Zero-Imputation Compliant")
head_col1, head_col2 = st.columns([3, 1])
with head_col1:
st.markdown("HIFFI Hip-Hop 500 Intelligence Hub
", unsafe_allow_html=True)
st.markdown("Multi-platform composite rankings with zero-imputation policy, dynamic weight redistribution, and verified record sales.
", unsafe_allow_html=True)
with head_col2:
st.markdown(
f"Filtered Cohort: {len(filtered_df)} / {len(df)} artists
",
unsafe_allow_html=True,
)
# 500 Macro KPI Cards (6 Columns with YouTube Telemetry)
k1, k2, k3, k4, k5, k6 = st.columns(6)
with k1:
st.markdown(f'Tracked Artists
{len(df)}
Monitored Cohort
', unsafe_allow_html=True)
with k2:
t3_count = len(df[df["platform_evidence_tier"] == 3])
st.markdown(f'Tier 3 (Complete)
{t3_count}
YouTube + Spotify + Instagram
', unsafe_allow_html=True)
with k3:
yt_count = df["subscriber_count"].fillna(0).gt(0).sum() if "subscriber_count" in df.columns else len(df)
st.markdown(f'YouTube Telemetry
{yt_count}
Verified video channels
', unsafe_allow_html=True)
with k4:
sp_count = df["spotify_monthly_listeners"].fillna(0).gt(0).sum() if "spotify_monthly_listeners" in df.columns else len(df)
st.markdown(f'Spotify Telemetry
{sp_count}
Verified monthly listeners
', unsafe_allow_html=True)
with k5:
ig_count = df["instagram_followers"].fillna(0).gt(0).sum() if "instagram_followers" in df.columns else len(df)
st.markdown(f'Instagram Audience
{ig_count}
Verified public followings
', unsafe_allow_html=True)
with k6:
cataloged_count = df["total_eas_cspc"].fillna(0).gt(0).sum() if "total_eas_cspc" in df.columns else 0
st.markdown(f'Catalog Coverage
{cataloged_count}
Commercial Units Indexed
', unsafe_allow_html=True)
st.markdown("", unsafe_allow_html=True)
if view_mode in ("๐ฟ Record Sales (CSPC Methodology)", "๐งช Commercial Simulation Engine (CPI)") and "sales_data_status" in df.columns:
verified_cspc_cnt = int((df["sales_data_status"] == "VERIFIED_CSPC").sum())
verified_riaa_cnt = int((df["sales_data_status"] == "VERIFIED_RIAA").sum())
estimated_sea_cnt = int((df["sales_data_status"] == "ESTIMATED_SEA").sum())
total_active_coverage = verified_cspc_cnt + verified_riaa_cnt + estimated_sea_cnt
coverage_pct = (total_active_coverage / len(df)) * 100.0 if len(df) > 0 else 0.0
st.markdown(
f"""
๐ก Commercial Telemetry Coverage Progress:
{verified_cspc_cnt} CSPC Verified
โข
{verified_riaa_cnt} RIAA Certified
โข
{estimated_sea_cnt} Streaming SEA
Active Commercial Coverage: {coverage_pct:.1f}% ({total_active_coverage}/{len(df)})
""",
unsafe_allow_html=True,
)
if view_mode == "๐ฟ Record Sales (CSPC Methodology)":
st.markdown("### ๐ฟ Global Record Sales & Lifetime Commercial Ranking")
st.caption("Standardized on ChartMasters CSPC, verified RIAA certifications, and video/audio streaming equivalent units.")
sc1, sc2, sc3, sc4 = st.columns(4)
total_sales_volume = filtered_df["total_eas_cspc"].fillna(0).sum() if "total_eas_cspc" in filtered_df.columns else 0
cspc_count = (filtered_df["sales_data_status"] == "VERIFIED_CSPC").sum() if "sales_data_status" in filtered_df.columns else 0
riaa_count = (filtered_df["sales_data_status"] == "VERIFIED_RIAA").sum() if "sales_data_status" in filtered_df.columns else 0
sea_count = (filtered_df["sales_data_status"] == "ESTIMATED_SEA").sum() if "sales_data_status" in filtered_df.columns else 0
with sc1:
st.markdown(f'Tracked Catalog Volume
{total_sales_volume/1e6:.1f}M
Total Equivalent Album Sales (EAS)
', unsafe_allow_html=True)
with sc2:
st.markdown(f'CSPC Verified
{cspc_count}
ChartMasters Full Dossiers
', unsafe_allow_html=True)
with sc3:
st.markdown(f'RIAA Certified
{riaa_count}
Domestic Certifications
', unsafe_allow_html=True)
with sc4:
st.markdown(f'Streaming SEA
{sea_count}
Luminate/Billboard Conversion
', unsafe_allow_html=True)
st.markdown("#### ๐ All-Time Global Sales Leaderboard")
sales_table_df = filtered_df.sort_values(by="total_eas_cspc", ascending=False).copy() if "total_eas_cspc" in filtered_df.columns else filtered_df.copy()
if "sales_data_status" in sales_table_df.columns:
sales_table_df["provenance_badge"] = sales_table_df["sales_data_status"].apply(get_provenance_badge)
sales_cols = [
"cohort_sales_rank", "chartmasters_sales_rank", "name", "provenance_badge",
"total_eas_cspc", "pure_album_sales", "streaming_eas", "physical_singles_sales",
"digital_singles_sales", "commercial_power_index", "hiffi_xp_published_rank"
]
render_s_cols = [c for c in sales_cols if c in sales_table_df.columns]
st.dataframe(
sales_table_df[render_s_cols].reset_index(drop=True),
use_container_width=True,
height=380,
column_config={
"cohort_sales_rank": st.column_config.NumberColumn("Cohort Sales Rank", format="#%d", width="small"),
"chartmasters_sales_rank": st.column_config.NumberColumn("All-Time Global Rank", format="#%d", width="small"),
"name": st.column_config.TextColumn("Artist", width="medium"),
"provenance_badge": st.column_config.TextColumn("Audit Provenance", width="medium"),
"total_eas_cspc": st.column_config.NumberColumn("Total EAS Units", format="%,d", width="medium"),
"pure_album_sales": st.column_config.NumberColumn("Pure Studio Albums", format="%,d", width="medium"),
"streaming_eas": st.column_config.NumberColumn("Streaming EAS", format="%,d", width="medium"),
"physical_singles_sales": st.column_config.NumberColumn("Physical Singles", format="%,d", width="small"),
"digital_singles_sales": st.column_config.NumberColumn("Digital Downloads", format="%,d", width="small"),
"commercial_power_index": st.column_config.ProgressColumn("CPI Score", min_value=0, max_value=100, format="%.1f", width="small"),
"hiffi_xp_published_rank": st.column_config.NumberColumn("HIFFI Base Rank", format="#%d", width="small"),
},
)
st.markdown("", unsafe_allow_html=True)
c_s1, c_s2 = st.columns(2)
with c_s1:
if "total_eas_cspc" in sales_table_df.columns:
top_sellers = sales_table_df[sales_table_df["total_eas_cspc"].fillna(0) > 0].head(12)
fig_format = go.Figure()
fig_format.add_trace(go.Bar(name="Traditional Albums", x=top_sellers["name"], y=top_sellers.get("pure_album_sales", 0), marker_color="#8b5cf6"))
fig_format.add_trace(go.Bar(name="Digital Singles", x=top_sellers["name"], y=top_sellers.get("digital_singles_sales", 0), marker_color="#38bdf8"))
fig_format.add_trace(go.Bar(name="Streaming EAS", x=top_sellers["name"], y=top_sellers.get("streaming_eas", 0), marker_color="#10b981"))
fig_format.update_layout(
barmode="stack", title="Format Contribution: Pure Albums vs Downloads vs Streaming EAS",
template="plotly_dark", paper_bgcolor="#111827", plot_bgcolor="#111827",
font=dict(color="#9ca3af"), height=360, margin=dict(l=20, r=20, t=50, b=20),
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1.0),
)
st.plotly_chart(fig_format, use_container_width=True)
with c_s2:
if "total_eas_cspc" in sales_table_df.columns and "hiffi_xp_score" in sales_table_df.columns:
scatter_data = sales_table_df.dropna(subset=["total_eas_cspc", "hiffi_xp_score"]).copy()
if not scatter_data.empty:
fig_quad = px.scatter(
scatter_data, x="hiffi_xp_score", y="total_eas_cspc", size="total_eas_cspc",
color="provenance_badge" if "provenance_badge" in scatter_data.columns else None,
hover_name="name", log_y=True,
title="Legacy vs. Velocity Quadrant (All-Time Sales vs HIFFI-XP)",
labels={"hiffi_xp_score": "Current Velocity (HIFFI-XP)", "total_eas_cspc": "Total Lifetime EAS Units (Log Scale)"},
template="plotly_dark",
)
fig_quad.update_layout(paper_bgcolor="#111827", plot_bgcolor="#111827", font=dict(color="#9ca3af"), height=360, margin=dict(l=20, r=20, t=50, b=20))
st.plotly_chart(fig_quad, use_container_width=True)
else:
top3 = filtered_df.sort_values(by="hiffi_xp_published_rank", ascending=True).head(3)
if len(top3) >= 3 and view_mode != "๐งช Commercial Simulation Engine (CPI)":
st.markdown("#### ๐ Market Leaders (Published Baseline Rankings)")
p1, p2, p3 = st.columns(3)
podium_data = [
(p1, top3.iloc[0], "๐ฅ PUBLISHED #1", "podium-gold", "#f59e0b"),
(p2, top3.iloc[1], "๐ฅ PUBLISHED #2", "podium-silver", "#94a3b8"),
(p3, top3.iloc[2], "๐ฅ PUBLISHED #3", "podium-bronze", "#f97316"),
]
for col, row_data, rank_label, css_class, accent_color in podium_data:
tier_val = int(row_data.get("platform_evidence_tier", 1))
yt_str = f"{int(row_data['subscriber_count']):,}" if pd.notna(row_data.get("subscriber_count")) else "NaN"
sp_str = f"{int(row_data['spotify_monthly_listeners']):,}" if pd.notna(row_data.get("spotify_monthly_listeners")) else "NaN"
ig_str = f"{int(row_data['instagram_followers']):,}" if pd.notna(row_data.get("instagram_followers")) else "NaN"
with col:
st.markdown(
f"""
{rank_label}
Tier {tier_val} Evidence
{row_data['name']}
{row_data.get('channel_title', '')}
HIFFI-XP
{row_data.get('hiffi_xp_score', 0.0):.2f}
""",
unsafe_allow_html=True,
)
st.markdown("", unsafe_allow_html=True)
if view_mode == "๐งช Commercial Simulation Engine (CPI)":
st.markdown("#### ๐งช Simulation Impact & Decision Summary")
sm1, sm2, sm3, sm4 = st.columns(4)
g_delta = f"+{sim_metrics.get('top_gainer_delta', 0)}" if sim_metrics.get('top_gainer_delta', 0) > 0 else f"{sim_metrics.get('top_gainer_delta', 0)}"
l_delta = f"{sim_metrics.get('top_loser_delta', 0)}"
with sm1:
st.markdown(f'Top Gainer
{sim_metrics.get("top_gainer_name", "None")} (โฒ {g_delta})
', unsafe_allow_html=True)
with sm2:
st.markdown(f'Top Loser
{sim_metrics.get("top_loser_name", "None")} (โผ {l_delta})
', unsafe_allow_html=True)
with sm3:
st.markdown(f'Mean Absolute Shift
ยฑ {sim_metrics.get("mean_abs_delta", 0.0)} positions
', unsafe_allow_html=True)
with sm4:
st.markdown(f'Simulation Scope
{sim_metrics.get("matched_coverage", 0)} / {len(filtered_df)} ({sim_mode})
', unsafe_allow_html=True)
st.caption(f"๐ก **Executive Takeaway:** At **{int(sim_weight*100)}% commercial weight**, the average cataloged artist moves **ยฑ{sim_metrics.get('mean_abs_delta', 0.0)} positions**.")
st.markdown("", unsafe_allow_html=True)
st.markdown(f"#### ๐ {view_mode}")
if view_mode == "๐ Published Multi-Platform Rank":
table_df = filtered_df.sort_values(by="hiffi_xp_published_rank", ascending=True).copy()
# Explicitly placing subscriber_count right after HIFFI-XP Score
cols = [
"hiffi_xp_published_rank", "hiffi_xp_rank", "name",
"platform_evidence_tier", "hiffi_xp_score", "subscriber_count",
"spotify_monthly_listeners", "instagram_followers",
"effective_weights", "anomaly_flags"
]
elif view_mode == "๐ Breakout Velocity (BVI)":
bvi_series = filtered_df.get("breakout_velocity_index", pd.Series(0.0, index=filtered_df.index)).fillna(0.0)
table_df = filtered_df[bvi_series.notna()].copy()
table_df["breakout_velocity_index"] = bvi_series
table_df = table_df.sort_values(by="breakout_velocity_index", ascending=False)
cols = ["name", "breakout_velocity_index", "hiffi_xp_published_rank", "daily_view_velocity", "momentum_30d", "subscriber_count", "subgenre"]
elif view_mode == "๐งช Commercial Simulation Engine (CPI)":
table_df = filtered_df.sort_values(by="simulated_rank", ascending=True).copy() if "simulated_rank" in filtered_df.columns else filtered_df.copy()
def format_rank_delta(val):
if pd.isna(val):
return "N/A"
ival = int(val)
if ival > 0:
return f"โฒ +{ival} (Up)"
elif ival < 0:
return f"โผ {ival} (Down)"
else:
return "โ 0 (Equal)"
if "simulation_rank_delta" in table_df.columns:
table_df["rank_shift_indicator"] = table_df["simulation_rank_delta"].apply(format_rank_delta)
else:
table_df["rank_shift_indicator"] = "โ 0"
comp_rank = "baseline_matched_rank" if sim_mode == "matched_only" else "hiffi_xp_published_rank"
cols = [
"simulated_rank", comp_rank, "rank_shift_indicator", "name",
"simulated_hiffi_score", "hiffi_xp_score", "commercial_power_index",
"total_eas_cspc", "sales_data_status",
]
else:
table_df = filtered_df.sort_values(by="hiffi_xp_published_rank", ascending=True).copy()
cols = ["hiffi_xp_published_rank", "name", "production_hiffi_rank", "hiffi_xp_rank", "rank_delta", "platform_evidence_tier", "hiffi_xp_score", "subscriber_count", "spotify_monthly_listeners", "instagram_followers", "total_eas_cspc", "sales_data_status", "evidence_exclusions", "anomaly_flags"]
display_table = table_df.copy()
if "sales_data_status" in display_table.columns:
display_table["sales_data_status"] = display_table["sales_data_status"].apply(get_provenance_badge)
render_cols = [c for c in cols if c in display_table.columns]
st.dataframe(
display_table[render_cols].reset_index(drop=True),
use_container_width=True,
height=340,
column_config={
"hiffi_xp_published_rank": st.column_config.NumberColumn("Published Rank", format="%d", width="small"),
"hiffi_xp_rank": st.column_config.NumberColumn("Raw Rank", format="%d", width="small"),
"name": st.column_config.TextColumn("Artist", width="medium"),
"platform_evidence_tier": st.column_config.NumberColumn("Tier", format="Tier %d", width="small"),
"baseline_matched_rank": st.column_config.NumberColumn("Baseline Match Rank", format="%d", width="small"),
"simulated_rank": st.column_config.NumberColumn("Simulated Rank", format="%d", width="small"),
"rank_shift_indicator": st.column_config.TextColumn("Simulated ฮ (Movement)", width="medium"),
"simulated_hiffi_score": st.column_config.ProgressColumn("Simulated Score", format="%.2f", min_value=0, max_value=100, width="medium"),
"hiffi_xp_score": st.column_config.ProgressColumn("HIFFI-XP Score", format="%.2f", min_value=0, max_value=100, width="medium"),
"subscriber_count": st.column_config.NumberColumn("YouTube Subscribers", format="%,d", width="medium"),
"spotify_monthly_listeners": st.column_config.NumberColumn("Spotify Monthly Listeners", format="%,d", width="medium"),
"instagram_followers": st.column_config.NumberColumn("Instagram Followers", format="%,d", width="medium"),
"breakout_velocity_index": st.column_config.ProgressColumn("Breakout Velocity Index", format="%.2f", min_value=0, max_value=100, width="medium"),
"commercial_power_index": st.column_config.ProgressColumn("Commercial Power Index", format="%.1f", min_value=0, max_value=100, width="medium"),
"total_eas_cspc": st.column_config.NumberColumn("Total Equivalent Album Sales", format="%,d", width="medium"),
"pure_album_sales": st.column_config.NumberColumn("Traditional Album Sales", format="%,d", width="medium"),
"streaming_eas": st.column_config.NumberColumn("Streaming-Equivalent Album Sales", format="%,d", width="medium"),
"sales_data_status": st.column_config.TextColumn("Sales Audit Status", width="medium"),
},
)
st.markdown("", unsafe_allow_html=True)
c_left, c_right = st.columns(2)
with c_left:
chart_df = filtered_df.dropna(subset=["spotify_monthly_listeners", "instagram_followers", "hiffi_xp_score"]).copy() if all(c in filtered_df.columns for c in ["spotify_monthly_listeners", "instagram_followers", "hiffi_xp_score"]) else pd.DataFrame()
if not chart_df.empty:
fig_scatter = px.scatter(
chart_df, x="spotify_monthly_listeners", y="instagram_followers",
size="hiffi_xp_score", color="platform_evidence_tier", hover_name="name",
log_x=True, log_y=True, title="Spotify Monthly Listeners vs. Instagram Followers",
labels={"spotify_monthly_listeners": "Spotify Monthly Listeners (Log Scale)", "instagram_followers": "Instagram Followers (Log Scale)", "platform_evidence_tier": "Tier"},
template="plotly_dark",
)
fig_scatter.update_layout(paper_bgcolor="#111827", plot_bgcolor="#111827", font=dict(color="#9ca3af"), height=320, margin=dict(l=20, r=20, t=40, b=20))
st.plotly_chart(fig_scatter, use_container_width=True)
with c_right:
if "platform_evidence_tier" in filtered_df.columns:
tier_counts = filtered_df["platform_evidence_tier"].value_counts().reset_index()
tier_counts.columns = ["Tier", "Count"]
fig_pie = px.pie(tier_counts, values="Count", names="Tier", title="Evidence Tier Cohort Distribution", template="plotly_dark", color="Tier", color_discrete_map={3: "#10b981", 2: "#38bdf8", 1: "#f59e0b", 0: "#ef4444"})
fig_pie.update_layout(paper_bgcolor="#111827", plot_bgcolor="#111827", font=dict(color="#9ca3af"), height=320, margin=dict(l=20, r=20, t=40, b=20))
st.plotly_chart(fig_pie, use_container_width=True)
# Dossier for 500
st.markdown("", unsafe_allow_html=True)
st.markdown("#### ๐ Artist Dossier")
selected_name = st.selectbox("Inspect Artist Details:", df["name"].unique(), label_visibility="collapsed")
row = df[df["name"] == selected_name].iloc[0]
d1, d2, d3, d4, d5, d6 = st.columns(6)
with d1:
h_val = row.get("hiffi_xp_score")
st.metric("HIFFI-XP Score", f"{h_val:.2f}" if pd.notna(h_val) else "N/A")
with d2:
pub_rank = row.get("hiffi_xp_published_rank")
st.metric("Published Rank", f"#{int(pub_rank)}" if pd.notna(pub_rank) else "Unranked")
with d3:
yt_s = row.get("subscriber_count")
st.metric("YouTube Subscribers", f"{int(yt_s):,}" if pd.notna(yt_s) else "Unindexed (NaN)")
with d4:
sp_l = row.get("spotify_monthly_listeners")
st.metric("Spotify Listeners", f"{int(sp_l):,}" if pd.notna(sp_l) else "Unindexed (NaN)")
with d5:
ig_f = row.get("instagram_followers")
st.metric("Instagram Followers", f"{int(ig_f):,}" if pd.notna(ig_f) else "Unindexed (NaN)")
with d6:
sales_val = row.get("total_eas_cspc")
st.metric("Total EAS Sales", f"{int(sales_val):,}" if pd.notna(sales_val) else "Awaiting Ingestion")
st.markdown(
f"""
Artist: {row['name']}
Evidence Tier: Tier {int(row.get('platform_evidence_tier', 1))}
YouTube Subscribers: {f"{int(row.get('subscriber_count')):,}" if pd.notna(row.get('subscriber_count')) else "N/A"}
Global Sales Rank: {f"#{int(row.get('chartmasters_sales_rank'))}" if pd.notna(row.get('chartmasters_sales_rank')) else "Unranked"}
Cohort Sales Rank: {f"#{int(row.get('cohort_sales_rank'))}" if pd.notna(row.get('cohort_sales_rank')) else "Uncataloged"}
Commercial Power Index: {f"{row.get('commercial_power_index'):.1f}" if pd.notna(row.get('commercial_power_index')) else "Awaiting Audit"}
Traditional Albums: {f"{int(row.get('pure_album_sales')):,}" if pd.notna(row.get('pure_album_sales')) else "N/A"}
Streaming-Equivalent EAS: {f"{int(row.get('streaming_eas')):,}" if pd.notna(row.get('streaming_eas')) else "N/A"}
Sales Status: {get_provenance_badge(row.get('sales_data_status', ''))}
""",
unsafe_allow_html=True,
)