Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,8 +13,8 @@ import colorsys
|
|
| 13 |
# Streamlit 페이지 설정 (반드시 첫 번째 st 명령어여야 함)
|
| 14 |
st.set_page_config(
|
| 15 |
layout="wide",
|
| 16 |
-
page_title="토픽모델링 Tool for SK",
|
| 17 |
-
page_icon="
|
| 18 |
)
|
| 19 |
|
| 20 |
# KoNLPy 형태소 분석기 초기화
|
|
@@ -86,27 +86,19 @@ st.markdown("""
|
|
| 86 |
</div>
|
| 87 |
""", unsafe_allow_html=True)
|
| 88 |
|
| 89 |
-
# 사이드바 접기/펼치기 기능
|
| 90 |
-
if 'sidebar_state' not in st.session_state:
|
| 91 |
-
st.session_state.sidebar_state = 'expanded'
|
| 92 |
-
|
| 93 |
-
def toggle_sidebar():
|
| 94 |
-
if st.session_state.sidebar_state == 'expanded':
|
| 95 |
-
st.session_state.sidebar_state = 'collapsed'
|
| 96 |
-
else:
|
| 97 |
-
st.session_state.sidebar_state = 'expanded'
|
| 98 |
-
|
| 99 |
# 메인 컨텐츠
|
| 100 |
st.markdown('<div class="main-content">', unsafe_allow_html=True)
|
| 101 |
st.title('토픽모델링 Tool for SK')
|
| 102 |
|
| 103 |
-
# 사이드바 토글 버튼
|
| 104 |
-
st.button("사이드바 접기/펼치기", on_click=toggle_sidebar)
|
| 105 |
-
|
| 106 |
# 사이드바 내용
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
# Claude API 키 설정
|
| 111 |
api_key = st.text_input("Claude API 키를 입력하세요", type="password")
|
| 112 |
if not api_key:
|
|
@@ -119,19 +111,26 @@ if st.session_state.sidebar_state == 'expanded':
|
|
| 119 |
# 파일 업로드
|
| 120 |
uploaded_file = st.file_uploader("CSV 파일을 업로드하세요", type="csv")
|
| 121 |
|
| 122 |
-
if uploaded_file is not None:
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
st.write(df.head())
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
if st.sidebar.button("토픽 모델링 실행"):
|
| 135 |
# 텍스트 전처리
|
| 136 |
with st.spinner("텍스트 전처리 중..."):
|
| 137 |
df['processed_text'] = df[text_column].apply(lambda x: preprocess_text(x, stop_words))
|
|
@@ -226,39 +225,46 @@ if uploaded_file is not None:
|
|
| 226 |
|
| 227 |
st.altair_chart(chart, use_container_width=True)
|
| 228 |
|
|
|
|
| 229 |
# Claude API를 사용하여 토픽 해석
|
| 230 |
if api_key:
|
| 231 |
anthropic = Anthropic(api_key=api_key)
|
| 232 |
|
| 233 |
st.header("Claude의 토픽 해석")
|
| 234 |
with st.spinner("토픽 해석 중..."):
|
| 235 |
-
prompt = f"""{HUMAN_PROMPT} 다음은 토픽 모델링 결과로 나온 각 토픽의 정보입니다. 이를 바탕으로 전체 토픽을 종합적으로 해석해주세요:
|
| 236 |
-
|
| 237 |
-
{", ".join([f"토픽 {info['topic_num']} (비중: {info['weight']:.1f}%)" for info in topic_results])}
|
| 238 |
-
|
| 239 |
-
각 토픽의 주요 단어:
|
| 240 |
-
"""
|
| 241 |
for info in topic_results:
|
| 242 |
prompt += f"""
|
| 243 |
-
토픽 {info['topic_num']} (비중: {info['weight']:.1f}%):
|
| 244 |
-
LDA 상위 단어: {', '.join(info['lda_words'])}
|
| 245 |
-
TF-IDF 상위 단어: {', '.join(info['tfidf_words'])}
|
| 246 |
-
"""
|
| 247 |
|
| 248 |
prompt += """
|
| 249 |
-
위 정보를 바탕으로 다음 형식에 맞춰 답변해주세요:
|
| 250 |
-
|
| 251 |
-
1. 전체 문서의 주제 요약 (3-4문장):
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
2. 각 토픽 요약:
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
response = anthropic.completions.create(
|
| 263 |
model="claude-2.1",
|
| 264 |
max_tokens_to_sample=3000,
|
|
|
|
| 13 |
# Streamlit 페이지 설정 (반드시 첫 번째 st 명령어여야 함)
|
| 14 |
st.set_page_config(
|
| 15 |
layout="wide",
|
| 16 |
+
page_title="📊 토픽모델링 Tool for SK",
|
| 17 |
+
page_icon="📊"
|
| 18 |
)
|
| 19 |
|
| 20 |
# KoNLPy 형태소 분석기 초기화
|
|
|
|
| 86 |
</div>
|
| 87 |
""", unsafe_allow_html=True)
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
# 메인 컨텐츠
|
| 90 |
st.markdown('<div class="main-content">', unsafe_allow_html=True)
|
| 91 |
st.title('토픽모델링 Tool for SK')
|
| 92 |
|
|
|
|
|
|
|
|
|
|
| 93 |
# 사이드바 내용
|
| 94 |
+
with st.sidebar:
|
| 95 |
+
st.header('설정')
|
| 96 |
+
|
| 97 |
+
# 최소화된 사이드바 상태에서도 보이는 정보
|
| 98 |
+
st.markdown("⚙️ 설정을 조정하려면 사이드바를 확장하세요.")
|
| 99 |
+
|
| 100 |
+
# 확장된 사이드바 상태에서 보이는 정보
|
| 101 |
+
with st.expander("상세 설정", expanded=True):
|
| 102 |
# Claude API 키 설정
|
| 103 |
api_key = st.text_input("Claude API 키를 입력하세요", type="password")
|
| 104 |
if not api_key:
|
|
|
|
| 111 |
# 파일 업로드
|
| 112 |
uploaded_file = st.file_uploader("CSV 파일을 업로드하세요", type="csv")
|
| 113 |
|
| 114 |
+
if uploaded_file is not None:
|
| 115 |
+
# 텍스트 컬럼 선택
|
| 116 |
+
df = pd.read_csv(uploaded_file)
|
| 117 |
+
text_column = st.selectbox("텍스트 컬럼을 선택하세요", df.columns)
|
|
|
|
| 118 |
|
| 119 |
+
# 토픽 수 선택
|
| 120 |
+
num_topics = st.slider("토픽 수를 선택하세요", 2, 20, 5)
|
| 121 |
|
| 122 |
+
if st.button("토픽 모델링 실행"):
|
| 123 |
+
st.session_state.run_analysis = True
|
| 124 |
+
else:
|
| 125 |
+
st.session_state.run_analysis = False
|
| 126 |
+
|
| 127 |
+
if 'run_analysis' in st.session_state and st.session_state.run_analysis:
|
| 128 |
+
if uploaded_file is not None:
|
| 129 |
+
# 데이터 읽기
|
| 130 |
+
df = pd.read_csv(uploaded_file)
|
| 131 |
+
st.write("데이터 미리보기:")
|
| 132 |
+
st.write(df.head())
|
| 133 |
|
|
|
|
| 134 |
# 텍스트 전처리
|
| 135 |
with st.spinner("텍스트 전처리 중..."):
|
| 136 |
df['processed_text'] = df[text_column].apply(lambda x: preprocess_text(x, stop_words))
|
|
|
|
| 225 |
|
| 226 |
st.altair_chart(chart, use_container_width=True)
|
| 227 |
|
| 228 |
+
|
| 229 |
# Claude API를 사용하여 토픽 해석
|
| 230 |
if api_key:
|
| 231 |
anthropic = Anthropic(api_key=api_key)
|
| 232 |
|
| 233 |
st.header("Claude의 토픽 해석")
|
| 234 |
with st.spinner("토픽 해석 중..."):
|
| 235 |
+
prompt = f"""{HUMAN_PROMPT} 다음은 LDA 토픽 모델링 결과로 나온 각 토픽의 정보입니다. 이를 바탕으로 전체 토픽을 종합적으로 해석해주세요:
|
| 236 |
+
|
| 237 |
+
{", ".join([f"토픽 {info['topic_num']} (비중: {info['weight']:.1f}%)" for info in topic_results])}
|
| 238 |
+
|
| 239 |
+
각 토픽의 주요 단어:
|
| 240 |
+
"""
|
| 241 |
for info in topic_results:
|
| 242 |
prompt += f"""
|
| 243 |
+
토픽 {info['topic_num']} (비중: {info['weight']:.1f}%):
|
| 244 |
+
LDA 상위 단어: {', '.join(info['lda_words'])}
|
| 245 |
+
TF-IDF 상위 단어: {', '.join(info['tfidf_words'])}
|
| 246 |
+
"""
|
| 247 |
|
| 248 |
prompt += """
|
| 249 |
+
위 정보를 바탕으로 다음 형식에 맞춰 답변해주세요:
|
| 250 |
+
|
| 251 |
+
1. 전체 문서의 주제 요약 (3-4문장):
|
| 252 |
+
[여기에 전체 문서의 주제를 종합적으로 설명해주세요. 각 토픽의 비중을 고려하여 중요도를 반영해주세요.]
|
| 253 |
+
|
| 254 |
+
2. 각 토픽 요약:
|
| 255 |
+
[각 토픽에 대해 다음 형식으로 요약해주세요]
|
| 256 |
+
- 토픽 [번호] ([LDA 기반 토픽명]): [비중]%
|
| 257 |
+
• 토픽명 설명: [토픽명이 이렇게 지어진 이유를 1-2문장으로 설명해주세요. LDA와 TF-IDF 상위 단어들이 어떻게 이 토픽명과 연관되는지 설명하세요.]
|
| 258 |
+
• 토픽 설명: [1-2문장으로 토픽의 전반적인 내용을 설명해주세요.]
|
| 259 |
+
• 가상의 예시 응답: "[이 토픽과 관련된 가상의 구성원 발언 예시를 넣어주세요. -한다 체를 지켜주세요]"
|
| 260 |
+
|
| 261 |
+
주의사항:
|
| 262 |
+
1. 토픽명은 "LDA 기반 [구체적인 토픽명]" 형식으로 작성해주세요. 예를 들어, "구성원들의 성장과 개인적인 역량개발 노력" 또는 "리더들의 노력과 조직의 전폭적인 지원" 등입니다.
|
| 263 |
+
2. 토픽명은 단순히 단어를 나열하는 것이 아니라, 토픽의 핵심 주제나 의미를 잘 나타내는 구체적인 문구로 만들어주세요.
|
| 264 |
+
3. 토픽명 설명에서는 왜 그러한 토픽명이 선택되었는지, LDA와 TF-IDF 상위 단어들과의 연관성을 설명해주세요.
|
| 265 |
+
|
| 266 |
+
위 형식에 맞춰 답변해주세요. 사용자가 쉽게 복사하여 사용할 수 있도록 간결하고 명확하게 작성해주세요."""
|
| 267 |
+
|
| 268 |
response = anthropic.completions.create(
|
| 269 |
model="claude-2.1",
|
| 270 |
max_tokens_to_sample=3000,
|