shiue2000 commited on
Commit
f4feaac
·
verified ·
1 Parent(s): bc1d766

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -4
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # ==========================================
2
- # 高雄市長選戰輕量化輿情分析 (HF Spaces)
3
  # ==========================================
4
 
5
  import snscrape.modules.twitter as sntwitter
@@ -10,6 +10,7 @@ import matplotlib.pyplot as plt
10
  import io
11
  import base64
12
  import gradio as gr
 
13
 
14
  # -----------------------------
15
  # 參數設定
@@ -17,6 +18,7 @@ import gradio as gr
17
  candidates = ["許智傑", "邱議瑩", "賴瑞隆", "林岱樺", "柯志恩"]
18
  days_back = 7
19
  max_tweets_per_candidate = 100 # HF部署建議少量
 
20
 
21
  # -----------------------------
22
  # 中文情緒分析模型
@@ -64,12 +66,27 @@ def run_analysis():
64
  img_b64 = base64.b64encode(buf.read()).decode("utf-8")
65
  buf.close()
66
 
67
- # 4. 即時輿情報告 (HTML)
 
 
 
 
 
 
 
 
 
68
  html_report = f"""
69
  <h2>高雄市長選戰輿情摘要 ({datetime.now().strftime('%Y-%m-%d')})</h2>
 
70
  <h3>1. 社群貼文聲量與情緒</h3>
71
  {summary.to_html()}
72
- <h3>2. 情緒圖表</h3>
 
 
 
 
 
73
  <img src='data:image/png;base64,{img_b64}' width='600'>
74
  """
75
  return html_report
@@ -83,7 +100,7 @@ iface = gr.Interface(
83
  outputs=gr.HTML,
84
  live=False,
85
  title="高雄市長選戰輿情分析",
86
- description="抓取 X(Twitter)貼文、分析情緒比例,並生成即時輿情報告"
87
  )
88
 
89
  iface.launch()
 
1
  # ==========================================
2
+ # 高雄市長選戰輕量化輿情分析 (HF Spaces + 新聞整合)
3
  # ==========================================
4
 
5
  import snscrape.modules.twitter as sntwitter
 
10
  import io
11
  import base64
12
  import gradio as gr
13
+ import os
14
 
15
  # -----------------------------
16
  # 參數設定
 
18
  candidates = ["許智傑", "邱議瑩", "賴瑞隆", "林岱樺", "柯志恩"]
19
  days_back = 7
20
  max_tweets_per_candidate = 100 # HF部署建議少量
21
+ news_file = "news_sample.csv" # CSV 欄位: 日期, 標題, 摘要, 類別
22
 
23
  # -----------------------------
24
  # 中文情緒分析模型
 
66
  img_b64 = base64.b64encode(buf.read()).decode("utf-8")
67
  buf.close()
68
 
69
+ # 4. 新聞線索整合
70
+ if os.path.exists(news_file):
71
+ df_news = pd.read_csv(news_file)
72
+ news_summary = df_news.groupby('類別').size().to_dict()
73
+ news_table = df_news.to_html(index=False)
74
+ else:
75
+ news_summary = {}
76
+ news_table = "<p>未提供新聞資料</p>"
77
+
78
+ # 5. 生成即時輿情報告 (HTML)
79
  html_report = f"""
80
  <h2>高雄市長選戰輿情摘要 ({datetime.now().strftime('%Y-%m-%d')})</h2>
81
+
82
  <h3>1. 社群貼文聲量與情緒</h3>
83
  {summary.to_html()}
84
+
85
+ <h3>2. 新聞議題統計</h3>
86
+ <p>各類別新聞數量:{news_summary}</p>
87
+ {news_table}
88
+
89
+ <h3>3. 情緒圖表</h3>
90
  <img src='data:image/png;base64,{img_b64}' width='600'>
91
  """
92
  return html_report
 
100
  outputs=gr.HTML,
101
  live=False,
102
  title="高雄市長選戰輿情分析",
103
+ description="抓取 X(Twitter)貼文 + 新聞議題分析,生成即時輿情報告"
104
  )
105
 
106
  iface.launch()