william1324 commited on
Commit
e4fff6a
·
verified ·
1 Parent(s): 578a287

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -17
app.py CHANGED
@@ -6,7 +6,6 @@ from prophet import Prophet
6
  import matplotlib.pyplot as plt
7
  import gradio as gr
8
 
9
- # 1️⃣ 模型初始化
10
  embedder = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
11
  sentiment_model = pipeline(
12
  "text-classification",
@@ -14,26 +13,20 @@ sentiment_model = pipeline(
14
  tokenizer="uer/roberta-base-finetuned-dianping-chinese"
15
  )
16
 
17
- # 2️⃣ 主處理流程
18
  def full_pipeline(file, num_clusters):
19
- try:
20
- df = pd.read_csv(file, encoding="utf-8")
21
- except UnicodeDecodeError:
22
- try:
23
- df = pd.read_csv(file, encoding="utf-8-sig")
24
- except UnicodeDecodeError:
25
- try:
26
- df = pd.read_csv(file, encoding="big5")
27
- except Exception as e:
28
- return f"❌ 無法讀取 CSV 檔案,錯誤原因:{str(e)}"
29
 
30
- # 向量化並聚類
31
  texts = df["text"].astype(str).tolist()
32
  embeddings = embedder.encode(texts, show_progress_bar=True)
33
  kmeans = KMeans(n_clusters=num_clusters, random_state=42)
34
  df["topic"] = kmeans.fit_predict(embeddings)
35
 
36
- # 情緒分析
37
  sentiments = []
38
  for text in texts:
39
  try:
@@ -52,7 +45,7 @@ except UnicodeDecodeError:
52
  sentiments.append(sentiment)
53
  df["sentiment"] = sentiments
54
 
55
- # 熱度預測(以 topic=0 為例)
56
  df["timestamp"] = pd.to_datetime(df["timestamp"])
57
  topic0 = df[df["topic"] == 0]
58
  daily_counts = topic0.groupby(df["timestamp"].dt.date).size().reset_index(name="count")
@@ -86,6 +79,6 @@ gr.Interface(
86
  gr.File(label="結果 CSV(含 topic, sentiment)"),
87
  gr.Image(label="topic=0 熱度預測圖(Prophet)")
88
  ],
89
- title="話題雷達",
90
- description="自動分群、分析情緒,並預測熱度走勢"
91
  ).launch()
 
6
  import matplotlib.pyplot as plt
7
  import gradio as gr
8
 
 
9
  embedder = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
10
  sentiment_model = pipeline(
11
  "text-classification",
 
13
  tokenizer="uer/roberta-base-finetuned-dianping-chinese"
14
  )
15
 
 
16
  def full_pipeline(file, num_clusters):
17
+ df = pd.read_csv(file)
18
+
19
+ if "text" not in df.columns:
20
+ return "❌ 錯誤:CSV 檔案需包含 text 欄位"
21
+ if "timestamp" not in df.columns:
22
+ return "❌ 錯誤:CSV 檔案需包含 timestamp 欄位(例如新聞時間)"
 
 
 
 
23
 
 
24
  texts = df["text"].astype(str).tolist()
25
  embeddings = embedder.encode(texts, show_progress_bar=True)
26
  kmeans = KMeans(n_clusters=num_clusters, random_state=42)
27
  df["topic"] = kmeans.fit_predict(embeddings)
28
 
29
+
30
  sentiments = []
31
  for text in texts:
32
  try:
 
45
  sentiments.append(sentiment)
46
  df["sentiment"] = sentiments
47
 
48
+
49
  df["timestamp"] = pd.to_datetime(df["timestamp"])
50
  topic0 = df[df["topic"] == 0]
51
  daily_counts = topic0.groupby(df["timestamp"].dt.date).size().reset_index(name="count")
 
79
  gr.File(label="結果 CSV(含 topic, sentiment)"),
80
  gr.Image(label="topic=0 熱度預測圖(Prophet)")
81
  ],
82
+ title=,"話題雷達"
83
+ description="自動分群、分析情緒,並預測熱度走勢(topic=0 為例)"
84
  ).launch()