Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
#######################################
|
6 |
+
# 1) 모델 준비
|
7 |
+
#######################################
|
8 |
+
|
9 |
+
# (A) Zero-Shot 분류 파이프라인 (다국어 모델)
|
10 |
+
topic_classifier = pipeline(
|
11 |
+
"zero-shot-classification",
|
12 |
+
model="joeddav/xlm-roberta-large-xnli"
|
13 |
+
)
|
14 |
+
|
15 |
+
# (B) 감성분석 파이프라인 (koelectra-nsmc 예시)
|
16 |
+
sentiment_analyzer = pipeline(
|
17 |
+
"sentiment-analysis",
|
18 |
+
model="monologg/koelectra-base-finetuned-nsmc"
|
19 |
+
)
|
20 |
+
|
21 |
+
# 분류할 주제 라벨 (HR 이슈 예시)
|
22 |
+
topic_labels = ["근무환경", "복리후생", "급여", "평가보상", "제도", "생산라인", "조직문화", "인원충원", "기타"]
|
23 |
+
|
24 |
+
#######################################
|
25 |
+
# 2) Streamlit UI
|
26 |
+
#######################################
|
27 |
+
|
28 |
+
st.title("Employee Feedback Analyzer")
|
29 |
+
st.write("""
|
30 |
+
**직원 익명게시판** 등 텍스트를 업로드하여,
|
31 |
+
- **이슈 주제**(Zero-Shot)
|
32 |
+
- **감성분석**(긍정/부정)
|
33 |
+
결과를 확인할 수 있는 데모입니다.
|
34 |
+
""")
|
35 |
+
|
36 |
+
uploaded_file = st.file_uploader("CSV 또는 XLSX 파일을 업로드하세요", type=["csv", "xlsx"])
|
37 |
+
|
38 |
+
if uploaded_file is not None:
|
39 |
+
# 2-1) 파일 로드
|
40 |
+
if uploaded_file.name.endswith(".csv"):
|
41 |
+
df = pd.read_csv(uploaded_file)
|
42 |
+
else:
|
43 |
+
df = pd.read_excel(uploaded_file)
|
44 |
+
|
45 |
+
st.write("업로드된 데이터 (최대 5행 미리보기):")
|
46 |
+
st.dataframe(df.head())
|
47 |
+
|
48 |
+
# 'content'라는 열에 게시글이 있다고 가정
|
49 |
+
text_column = st.selectbox("분석할 텍스트 컬럼 선택", df.columns)
|
50 |
+
|
51 |
+
if st.button("분석 시작"):
|
52 |
+
# 결과 저장 리스트
|
53 |
+
topics = []
|
54 |
+
topic_scores = []
|
55 |
+
sentiments = []
|
56 |
+
sentiment_scores = []
|
57 |
+
|
58 |
+
for text in df[text_column]:
|
59 |
+
# Zero-Shot 분류
|
60 |
+
# truncation=True로 글이 길어도 최대 토큰 초과 에러 방지
|
61 |
+
topic_result = topic_classifier(text, topic_labels, truncation=True)
|
62 |
+
# 가장 확률 높은 라벨
|
63 |
+
pred_label = topic_result["labels"][0]
|
64 |
+
pred_score = topic_result["scores"][0]
|
65 |
+
|
66 |
+
# 감성분석
|
67 |
+
sentiment_result = sentiment_analyzer(text, truncation=True)
|
68 |
+
sent_label = sentiment_result[0]["label"]
|
69 |
+
sent_score = sentiment_result[0]["score"]
|
70 |
+
|
71 |
+
topics.append(pred_label)
|
72 |
+
topic_scores.append(round(pred_score, 4))
|
73 |
+
sentiments.append(sent_label)
|
74 |
+
sentiment_scores.append(round(sent_score, 4))
|
75 |
+
|
76 |
+
# 결과를 데이터프레임에 추가
|
77 |
+
df["pred_topic"] = topics
|
78 |
+
df["topic_score"] = topic_scores
|
79 |
+
df["sentiment_label"] = sentiments
|
80 |
+
df["sentiment_score"] = sentiment_scores
|
81 |
+
|
82 |
+
st.write("분석 결과")
|
83 |
+
st.dataframe(df)
|
84 |
+
|
85 |
+
# 다운로드 버튼
|
86 |
+
csv_data = df.to_csv(index=False).encode("utf-8-sig")
|
87 |
+
st.download_button(
|
88 |
+
label="결과 CSV 다운로드",
|
89 |
+
data=csv_data,
|
90 |
+
file_name="employee_feedback_analysis.csv",
|
91 |
+
mime="text/csv"
|
92 |
+
)
|