import pandas as pd import matplotlib.pyplot as plt import io import base64 import os import traceback import random import networkx as nx from datetime import datetime, timedelta import gradio as gr import logging from jinja2 import Template from matplotlib import font_manager # ===== Fonts and Styles ===== # Load local SimHei font if available simhei_path = 'SimHei.ttf' # Assuming it's .ttf; change to .tiff if needed (though .ttf is standard) if os.path.exists(simhei_path): font_prop = font_manager.FontProperties(fname=simhei_path) plt.rcParams['font.family'] = 'sans-serif' plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft JhengHei', 'Noto Sans TC', 'Arial Unicode MS'] else: plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Noto Sans TC', 'SimHei', 'Arial Unicode MS'] plt.rcParams['axes.unicode_minus'] = False plt.style.use("seaborn-v0_8") # ===== Logging ===== logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') # ===== Parameters ===== candidates = ["Hsu Chih-chieh", "Chiu Yi-ying", "Lai Jui-lung", "Lin Dai-hua", "Ko Chih-en"] days_back = 7 max_tweets_per_candidate = 20 news_file = "news_sample.csv" history_file = "history_sentiment.csv" # ===== Sentiment Analysis ===== try: from transformers import pipeline sentiment_pipeline = pipeline( "sentiment-analysis", model="lxyuan/distilbert-base-multilingual-cased-sentiments-student" ) def sentiment(text): return sentiment_pipeline(text)[0] except: def sentiment(text): return { "label": random.choice(["positive", "negative", "neutral"]), "score": random.uniform(0.3, 0.9) } # ===== Simulate Post Fetching ===== def fetch_tweets(candidate): sample_texts = { "Hsu Chih-chieh": ["Hsu Chih-chieh actively participates in local activities", "Hsu Chih-chieh criticized for empty policies", "Support Hsu Chih-chieh, build a new future for Kaohsiung!"], "Chiu Yi-ying": ["Chiu Yi-ying strongly states intention to run for mayor", "Chiu Yi-ying criticizes Lin Dai-hua", "Chiu Yi-ying promotes Hakka culture"], "Lai Jui-lung": ["Lai Jui-lung promotes marine economy", "Lai Jui-lung leads in polls", "Lai Jui-lung questioned for lack of experience"], "Lin Dai-hua": ["Lin Dai-hua actively engages with grassroots", "Lin Dai-hua involved in assistant fee controversy", "Lin Dai-hua receives support from Zheng Guohui"], "Ko Chih-en": ["Ko Chih-en leads significantly in polls", "Ko Chih-en criticized for missing disaster inspection", "Ko Chih-en promotes youth policies"] } return pd.DataFrame([ { "Date": datetime.now() - timedelta(days=random.randint(0, days_back - 1)), "User": f"user{random.randint(1, 100)}", "Content": random.choice(sample_texts.get(candidate, [f"{candidate}'s post {i}"])), "Candidate": candidate } for i in range(random.randint(5, max_tweets_per_candidate)) ]) # ===== Tool: Matplotlib to base64 ===== def fig_to_base64(): buf = io.BytesIO() plt.savefig(buf, format="png", dpi=120, bbox_inches="tight") buf.seek(0) img_b64 = base64.b64encode(buf.read()).decode("utf-8") buf.close() plt.close() return img_b64 # ===== Multi-Chart Generator ===== def generate_charts(all_df, summary, df_hist): results = {} # 1. Daily Sentiment Ratios fig = plt.figure(figsize=(8, 5)) summary[['Positive Ratio', 'Negative Ratio', 'Neutral Ratio']].plot( kind='bar', stacked=True, colormap='coolwarm', ax=fig.gca() ) plt.title("Candidates' Daily Social Sentiment Ratios") plt.ylabel("Ratio") plt.xlabel("Candidate") plt.legend(["Positive", "Negative", "Neutral"]) results["img_b64_today"] = fig_to_base64() # 2. Historical Sentiment Trends fig = plt.figure(figsize=(10, 5)) for c in candidates: temp = df_hist[df_hist['Candidate'] == c] if not temp.empty: plt.plot(temp['Date'], temp['Positive Ratio'], marker='o', label=f"{c} Positive") plt.plot(temp['Date'], temp['Negative Ratio'], marker='x', label=f"{c} Negative") plt.plot(temp['Date'], temp['Neutral Ratio'], marker='s', label=f"{c} Neutral") plt.title("Candidates' Historical Sentiment Trends") plt.xticks(rotation=45) plt.ylabel("Ratio") plt.xlabel("Date") plt.legend() results["img_b64_trend"] = fig_to_base64() # 3. Social Sentiment Trends sentiment_trend = all_df.groupby([pd.Grouper(key='Date', freq='D'), 'Sentiment']).size().unstack(fill_value=0) sentiment_trend = sentiment_trend.div(sentiment_trend.sum(axis=1), axis=0).fillna(0) fig = plt.figure(figsize=(8, 5)) for s in ['positive', 'negative', 'neutral']: if s in sentiment_trend.columns: plt.plot(sentiment_trend.index, sentiment_trend[s], marker='o', label={'positive':'Positive', 'negative':'Negative', 'neutral':'Neutral'}[s]) plt.title("Social Sentiment Trends") plt.xlabel("Date") plt.ylabel("Ratio") plt.legend() results["img_social_sentiment"] = fig_to_base64() # 4. Platform Performance platforms = ["X", "Facebook", "Instagram", "PTT", "Line"] platform_counts = pd.Series({p: random.randint(10, 100) for p in platforms}) fig = plt.figure(figsize=(8, 5)) plt.bar(platforms, platform_counts, color='skyblue') plt.title("Platform Post Volumes") plt.xlabel("Platform") plt.ylabel("Post Count") results["img_platform_performance"] = fig_to_base64() # 5. Candidates' Volume Trends candidate_trend = all_df.groupby([pd.Grouper(key='Date', freq='D'), 'Candidate']).size().unstack(fill_value=0) fig = plt.figure(figsize=(8, 5)) for c in candidates: if c in candidate_trend.columns: plt.plot(candidate_trend.index, candidate_trend[c], marker='o', label=c) plt.title("Candidates' Post Volume Trends") plt.xlabel("Date") plt.ylabel("Post Count") plt.legend() results["img_candidate_volume"] = fig_to_base64() # 6. Candidates' Sentiment Analysis fig = plt.figure(figsize=(8, 5)) summary[['Positive Ratio', 'Negative Ratio', 'Neutral Ratio']].plot( kind='bar', stacked=True, colormap='coolwarm', ax=fig.gca() ) plt.title("Candidates' Post Sentiment Analysis (Positive/Negative/Neutral)") plt.ylabel("Ratio") plt.xlabel("Candidate") plt.legend(["Positive", "Negative", "Neutral"]) results["img_candidate_sentiment"] = fig_to_base64() # 7. Knowledge Graph fig, ax = plt.subplots(figsize=(8, 6)) G = nx.Graph() for c in candidates: G.add_node(c) for i in range(len(candidates) - 1): G.add_edge(candidates[i], candidates[i + 1]) nx.draw(G, nx.spring_layout(G), with_labels=True, node_color='lightgreen', font_size=12, ax=ax) plt.title("Candidates' Knowledge Graph") results["img_knowledge_graph"] = fig_to_base64() return results # ===== Main Analysis Function ===== def run_analysis(): try: # Embed the template as a string to avoid file dependency and ensure syntax is correct html_template = """ 2026 Kaohsiung Mayoral Election Public Opinion Analysis Report

2026 Kaohsiung Mayoral Election Public Opinion Analysis Report

Report Date: {{ report_date }}

Engagement Summary

{{ engagement_table | safe }}

News Summary

News Details

{{ news_table | safe }}

Today's Sentiment Ratios

Today's Sentiment Ratios

Historical Sentiment Trends

Historical Sentiment Trends

Social Sentiment Trends

Social Sentiment Trends

Platform Performance

Platform Performance

Candidates' Volume Trends

Candidates' Volume Trends

Candidates' Sentiment Analysis

Candidates' Sentiment Analysis

Knowledge Graph

Knowledge Graph
""" # --- Posts & Sentiment Analysis --- all_df = pd.concat([fetch_tweets(c) for c in candidates], ignore_index=True) all_df['Sentiment'] = all_df['Content'].apply(lambda x: sentiment(x)['label']) all_df['Confidence'] = all_df['Content'].apply(lambda x: sentiment(x)['score']) # --- Statistics --- summary = all_df.groupby(['Candidate', 'Sentiment']).size().unstack(fill_value=0) summary['Total Posts'] = summary.sum(axis=1) summary['Positive Ratio'] = summary.get('positive', 0) / summary['Total Posts'].replace(0, 1) summary['Negative Ratio'] = summary.get('negative', 0) / summary['Total Posts'].replace(0, 1) summary['Neutral Ratio'] = summary.get('neutral', 0) / summary['Total Posts'].replace(0, 1) # --- Historical Data --- today_str = datetime.now().strftime('%Y-%m-%d') hist_row = summary[['Positive Ratio', 'Negative Ratio', 'Neutral Ratio']].copy() hist_row['Date'] = today_str hist_row['Candidate'] = summary.index df_hist = pd.concat( [pd.read_csv(history_file), hist_row], ignore_index=True ) if os.path.exists(history_file) else hist_row df_hist.to_csv(history_file, index=False) # --- Charts --- charts = generate_charts(all_df, summary, df_hist) # --- News --- if os.path.exists(news_file): df_news = pd.read_csv(news_file) news_summary = df_news.groupby('Category').size().to_dict() news_table = df_news.to_html(index=False, classes="min-w-full border border-gray-200") else: news_summary = { "Polls": "Ko Chih-en leads Green Camp candidates in multiple polls.", "Intra-party Competition": "Intense competition in the DPP primary.", "Controversy": "Lin Dai-hua involved in assistant fee controversy." } news_table = "

No news data available

" # Convert news_summary to list of tuples to support iteration in template news_summary = list(news_summary.items()) # --- Engagement Table --- engagement_table = f"""
Total Engagement {len(all_df)} Positive Sentiment Ratio {all_df['Sentiment'].value_counts(normalize=True).get('positive', 0):.1%} Average Interaction Rate 3.9% Active Platforms {5}
""" # --- HTML Rendering --- template = Template(html_template) html_content = template.render( report_date=datetime.now().strftime('%Y-%m-%d %H:%M'), engagement_table=engagement_table if engagement_table else "

No engagement data provided

", news_summary=news_summary if news_summary else "

No news summary provided

", news_table=news_table if news_table else "

No news data provided

", **charts ) return html_content except Exception: return f"
❌ Analysis failed:\n{traceback.format_exc()}
" # ===== Gradio Frontend ===== if __name__ == "__main__": iface = gr.Interface( fn=run_analysis, inputs=[], outputs=gr.HTML(), title="2026 Kaohsiung Mayoral Election Public Opinion Analysis" ) iface.launch(server_name="0.0.0.0", server_port=7860)