Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""keyword_extraction"""
|
| 3 |
+
|
| 4 |
+
import requests
|
| 5 |
+
import jieba
|
| 6 |
+
from keybert import KeyBERT
|
| 7 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 8 |
+
import streamlit as st
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
from matplotlib.font_manager import FontProperties
|
| 11 |
+
|
| 12 |
+
# 下載字體
|
| 13 |
+
def download_font(url, save_path):
|
| 14 |
+
response = requests.get(url)
|
| 15 |
+
with open(save_path, 'wb') as f:
|
| 16 |
+
f.write(response.content)
|
| 17 |
+
|
| 18 |
+
# 字體URL和保存路徑
|
| 19 |
+
font_url = 'https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download'
|
| 20 |
+
font_path = 'TaipeiSansTCBeta-Regular.ttf'
|
| 21 |
+
|
| 22 |
+
# 下載字體
|
| 23 |
+
download_font(font_url, font_path)
|
| 24 |
+
|
| 25 |
+
# 設置字體
|
| 26 |
+
font_prop = FontProperties(fname=font_path)
|
| 27 |
+
|
| 28 |
+
# 讀取繁體中文詞典
|
| 29 |
+
# jieba.set_dictionary('path_to_your_dict.txt') # 繁體中文詞典的實際路徑,若需要繁體字典請取消註解並設置正確路徑
|
| 30 |
+
|
| 31 |
+
# 2. 定義斷詞函數
|
| 32 |
+
def jieba_tokenizer(text):
|
| 33 |
+
return jieba.lcut(text)
|
| 34 |
+
|
| 35 |
+
# 3. 初始化CountVectorizer並定義KeyBERT模型
|
| 36 |
+
vectorizer = CountVectorizer(tokenizer=jieba_tokenizer)
|
| 37 |
+
kw_model = KeyBERT()
|
| 38 |
+
|
| 39 |
+
# 4. 提取關鍵詞的函數
|
| 40 |
+
def extract_keywords(doc):
|
| 41 |
+
keywords = kw_model.extract_keywords(doc, vectorizer=vectorizer)
|
| 42 |
+
return keywords
|
| 43 |
+
|
| 44 |
+
# 5. 畫圖函數
|
| 45 |
+
def plot_keywords(keywords, title):
|
| 46 |
+
words = [kw[0] for kw in keywords]
|
| 47 |
+
scores = [kw[1] for kw in keywords]
|
| 48 |
+
|
| 49 |
+
plt.figure(figsize=(10, 6))
|
| 50 |
+
plt.barh(words, scores, color='skyblue')
|
| 51 |
+
plt.xlabel('分數', fontproperties=font_prop)
|
| 52 |
+
plt.title(title, fontproperties=font_prop)
|
| 53 |
+
plt.gca().invert_yaxis() # 反轉Y軸,使得分數最高的關鍵詞在最上面
|
| 54 |
+
plt.xticks(fontproperties=font_prop)
|
| 55 |
+
plt.yticks(fontproperties=font_prop)
|
| 56 |
+
st.pyplot(plt)
|
| 57 |
+
|
| 58 |
+
# 6. 建立Streamlit網頁應用程式
|
| 59 |
+
st.title("中文關鍵詞提取工具")
|
| 60 |
+
doc = st.text_area("請輸入文章:")
|
| 61 |
+
|
| 62 |
+
if st.button("提取關鍵詞"):
|
| 63 |
+
if doc:
|
| 64 |
+
keywords = extract_keywords(doc)
|
| 65 |
+
st.write("關鍵詞提取結果:")
|
| 66 |
+
for keyword in keywords:
|
| 67 |
+
st.write(f"{keyword[0]}: {keyword[1]:.4f}")
|
| 68 |
+
|
| 69 |
+
plot_keywords(keywords, "關鍵詞提取結果")
|
| 70 |
+
|
| 71 |
+
# 使用另一個模型進行關鍵詞提取
|
| 72 |
+
kw_model_multilingual = KeyBERT(model='distiluse-base-multilingual-cased-v1')
|
| 73 |
+
keywords_multilingual = kw_model_multilingual.extract_keywords(doc, vectorizer=vectorizer)
|
| 74 |
+
st.write("多語言模型關鍵詞提取結果:")
|
| 75 |
+
for keyword in keywords_multilingual:
|
| 76 |
+
st.write(f"{keyword[0]}: {keyword[1]:.4f}")
|
| 77 |
+
|
| 78 |
+
plot_keywords(keywords_multilingual, "多語言模型關鍵詞提取結果")
|
| 79 |
+
else:
|
| 80 |
+
st.write("請輸入文章內容以進行關鍵詞提取。")
|