rafif2/NewsTopicsBubbles
0
1import streamlit as st2import pandas as pd3from bertopic import BERTopic4 5st.set_page_config(page_title="Media Framing of UK Climate Change", layout="wide")6 7@st.cache_resource8def load_assets():9 # Load the final merged and labeled model10 model = BERTopic.load("bertopic_model_final")11 # Load the final dataframe12 df = pd.read_pickle("processed_dataframe_final (1).pkl")13 return model, df14 15topic_model, df = load_assets()16docs = df['clean_text'].tolist()17 18# Pre-calculate dynamic topic modeling to save time on toggles19@st.cache_data20def get_topics_per_class(_model, _docs, classes):21 return _model.topics_per_class(_docs, classes=classes)22 23@st.cache_data24def get_topics_over_time(_model, _docs, timestamps):25 # BERTopic handles datetime lists well26 return _model.topics_over_time(_docs, timestamps=timestamps)27 28 29st.sidebar.title("Dashboard Controls")30st.sidebar.markdown("Explore how the BBC and The Guardian frame climate change.")31 32search_query = st.sidebar.text_input("Search for a keyword/theme:")33 34st.sidebar.markdown("---")35st.sidebar.subheader("Advanced Views")36view_outlet = st.sidebar.checkbox("Compare by News Outlet", value=False)37view_time = st.sidebar.checkbox("View Timeline Evolution", value=False)38 39st.title("Media Framing of UK Climate Change & Renewable Energy")40 41if search_query:42 st.subheader(f"Topics matching: '{search_query}'")43 # Find the top 3 topics related to the user's search44 similar_topics, similarity = topic_model.find_topics(search_query, top_n=3)45 46 fig_search = topic_model.visualize_barchart(47 topics=similar_topics, 48 n_words=10, 49 custom_labels=True,50 title="Key Terms for Matching Topics"51 )52 st.plotly_chart(fig_search, use_container_width=True)53 54else:55 st.subheader("Global Topic Distribution")56 st.markdown("Hover over the topics on the left to see their relationships, and view the defining terms on the right.")57 58 col1, col2 = st.columns([1, 1])59 60 with col1:61 # Intertopic Distance Map62 fig_map = topic_model.visualize_topics(custom_labels=True)63 st.plotly_chart(fig_map, use_container_width=True)64 65 with col2:66 # Top Term Barcharts67 fig_bar = topic_model.visualize_barchart(top_n_topics=8, n_words=10, custom_labels=True)68 st.plotly_chart(fig_bar, use_container_width=True)69 70if view_outlet or view_time:71 st.markdown("---")72 73if view_outlet:74 st.subheader("Editorial Emphasis by News Outlet")75 # Generate the topics per class data using your 'outlet' column76 topics_per_class = get_topics_per_class(topic_model, docs, df['outlet'].tolist())77 78 # Visualize it79 fig_class = topic_model.visualize_topics_per_class(80 topics_per_class, 81 top_n_topics=10, 82 custom_labels=True,83 title="Topic Frequency: BBC vs The Guardian"84 )85 st.plotly_chart(fig_class, use_container_width=True)86 87if view_time:88 st.subheader("Topic Evolution Over Time")89 timestamps = df['published_date'].dt.strftime('%Y-%m-%d').tolist()90 topics_over_time = get_topics_over_time(topic_model, docs, timestamps)91 92 # Visualize it93 fig_time = topic_model.visualize_topics_over_time(94 topics_over_time, 95 top_n_topics=8, 96 custom_labels=True,97 title="Topic Prominence Over Time"98 )99 st.plotly_chart(fig_time, use_container_width=True)