Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ from konlpy.tag import Okt
|
|
| 8 |
import re
|
| 9 |
import os
|
| 10 |
import altair as alt
|
|
|
|
| 11 |
|
| 12 |
# KoNLPy 형태소 분석기 초기화
|
| 13 |
@st.cache_resource
|
|
@@ -17,7 +18,7 @@ def load_okt():
|
|
| 17 |
okt = load_okt()
|
| 18 |
|
| 19 |
# 기본 불용어 목록
|
| 20 |
-
default_stop_words = ['이', '그', '저', '것', '수', '등', '들', '및', '에서', '그리고', '그래서', '또는', '그런데']
|
| 21 |
|
| 22 |
@st.cache_data
|
| 23 |
def preprocess_text(text, stop_words):
|
|
@@ -32,6 +33,11 @@ def preprocess_text(text, stop_words):
|
|
| 32 |
|
| 33 |
return ' '.join(processed)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Streamlit 앱 설정
|
| 36 |
st.title('한국어 토픽 모델링 앱')
|
| 37 |
|
|
@@ -126,16 +132,48 @@ if uploaded_file is not None:
|
|
| 126 |
# 토픽 비중 그래프
|
| 127 |
st.header("토픽 비중 그래프")
|
| 128 |
topic_weights = lda_output.mean(axis=0)
|
| 129 |
-
df_weights = pd.DataFrame({
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
width=600,
|
| 136 |
height=400,
|
| 137 |
-
title='문서 내 토픽 비중'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
)
|
|
|
|
| 139 |
st.altair_chart(chart, use_container_width=True)
|
| 140 |
|
| 141 |
# Claude API를 사용하여 토픽 해석
|
|
@@ -145,11 +183,27 @@ if uploaded_file is not None:
|
|
| 145 |
st.header("Claude의 토픽 해석")
|
| 146 |
for idx, topic in enumerate(lda.components_):
|
| 147 |
with st.spinner(f"토픽 {idx + 1} 해석 중..."):
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
response = anthropic.completions.create(
|
| 151 |
model="claude-2.1",
|
| 152 |
-
max_tokens_to_sample=
|
| 153 |
prompt=f"{prompt}\n\n{AI_PROMPT}",
|
| 154 |
)
|
| 155 |
st.subheader(f"토픽 {idx + 1} 해석:")
|
|
|
|
| 8 |
import re
|
| 9 |
import os
|
| 10 |
import altair as alt
|
| 11 |
+
import colorsys
|
| 12 |
|
| 13 |
# KoNLPy 형태소 분석기 초기화
|
| 14 |
@st.cache_resource
|
|
|
|
| 18 |
okt = load_okt()
|
| 19 |
|
| 20 |
# 기본 불용어 목록
|
| 21 |
+
default_stop_words = ['이', '그', '저', '것', '수', '등', '들', '및', '에서', '그리고', '그래서', '또는', '그런데', '의', '대한', '간의']
|
| 22 |
|
| 23 |
@st.cache_data
|
| 24 |
def preprocess_text(text, stop_words):
|
|
|
|
| 33 |
|
| 34 |
return ' '.join(processed)
|
| 35 |
|
| 36 |
+
# HSL 색상 생성
|
| 37 |
+
def generate_colors(n):
|
| 38 |
+
HSV_tuples = [(x * 1.0 / n, 0.5, 0.9) for x in range(n)]
|
| 39 |
+
return ['#%02x%02x%02x' % tuple(int(x*255) for x in colorsys.hsv_to_rgb(*hsv)) for hsv in HSV_tuples]
|
| 40 |
+
|
| 41 |
# Streamlit 앱 설정
|
| 42 |
st.title('한국어 토픽 모델링 앱')
|
| 43 |
|
|
|
|
| 132 |
# 토픽 비중 그래프
|
| 133 |
st.header("토픽 비중 그래프")
|
| 134 |
topic_weights = lda_output.mean(axis=0)
|
| 135 |
+
df_weights = pd.DataFrame({
|
| 136 |
+
'토픽': [f'토픽 {i+1}' for i in range(num_topics)],
|
| 137 |
+
'비중': topic_weights
|
| 138 |
+
})
|
| 139 |
+
|
| 140 |
+
# 퍼센트로 변환
|
| 141 |
+
df_weights['퍼센트'] = df_weights['비중'] / df_weights['비중'].sum() * 100
|
| 142 |
+
|
| 143 |
+
colors = generate_colors(num_topics)
|
| 144 |
+
|
| 145 |
+
# 차트 생성
|
| 146 |
+
base = alt.Chart(df_weights).encode(
|
| 147 |
+
x=alt.X('토픽:N', axis=alt.Axis(labelAngle=0)),
|
| 148 |
+
y=alt.Y('퍼센트:Q', axis=alt.Axis(format=',.1f'))
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
bars = base.mark_bar().encode(
|
| 152 |
+
color=alt.Color('토픽:N', scale=alt.Scale(range=colors))
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
text = base.mark_text(
|
| 156 |
+
align='center',
|
| 157 |
+
baseline='middle',
|
| 158 |
+
dy=-10 # 텍스트를 약간 위로 이동
|
| 159 |
+
).encode(
|
| 160 |
+
text=alt.Text('퍼센트:Q', format='.1f')
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
chart = (bars + text).properties(
|
| 164 |
width=600,
|
| 165 |
height=400,
|
| 166 |
+
title='문서 내 토픽 비중 (%)'
|
| 167 |
+
).configure_axis(
|
| 168 |
+
labelFontSize=12,
|
| 169 |
+
titleFontSize=14
|
| 170 |
+
).configure_title(
|
| 171 |
+
fontSize=16,
|
| 172 |
+
font='Arial',
|
| 173 |
+
anchor='middle',
|
| 174 |
+
color='gray'
|
| 175 |
)
|
| 176 |
+
|
| 177 |
st.altair_chart(chart, use_container_width=True)
|
| 178 |
|
| 179 |
# Claude API를 사용하여 토픽 해석
|
|
|
|
| 183 |
st.header("Claude의 토픽 해석")
|
| 184 |
for idx, topic in enumerate(lda.components_):
|
| 185 |
with st.spinner(f"토픽 {idx + 1} 해석 중..."):
|
| 186 |
+
lda_top_words = [feature_names[i] for i in topic.argsort()[:-11:-1]]
|
| 187 |
+
topic_docs = lda_output[:, idx].argsort()[::-1][:100]
|
| 188 |
+
topic_tfidf = tfidf_matrix[topic_docs].mean(axis=0).A1
|
| 189 |
+
tfidf_top_words = [feature_names[i] for i in topic_tfidf.argsort()[:-11:-1]]
|
| 190 |
+
|
| 191 |
+
prompt = f"{HUMAN_PROMPT} 다음은 토픽 모델링 결과로 나온 단어들입니다. 이를 바탕으로 아래 형식에 맞춰 토픽을 해석해주세요:
|
| 192 |
+
|
| 193 |
+
{{LDA 상위 단어}}: {', '.join(lda_top_words)}
|
| 194 |
+
{{TF-IDF 상위 단어}}: {', '.join(tfidf_top_words)}
|
| 195 |
+
|
| 196 |
+
1. 토픽 의미 해석: (위 단어들을 바탕으로 이 토픽이 어떤 의미를 나타내는지 2-3문장으로 설명해주세요)
|
| 197 |
+
|
| 198 |
+
2. 토픽명 제안: (이 토픽을 잘 나타낼 수 있는 간단한 제목을 제안해주세요)
|
| 199 |
+
|
| 200 |
+
3. 대표적인 예시 응답: (이 토픽과 관련된 대표적인 발언이나 문장 예시를 3가지 제시해주세요)
|
| 201 |
+
|
| 202 |
+
위 형식에 맞춰 답변해주세요."
|
| 203 |
+
|
| 204 |
response = anthropic.completions.create(
|
| 205 |
model="claude-2.1",
|
| 206 |
+
max_tokens_to_sample=1000,
|
| 207 |
prompt=f"{prompt}\n\n{AI_PROMPT}",
|
| 208 |
)
|
| 209 |
st.subheader(f"토픽 {idx + 1} 해석:")
|