Update app.py
Browse files
app.py
CHANGED
@@ -34,7 +34,18 @@ def load_huggingface_model():
|
|
34 |
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
35 |
return classifier
|
36 |
|
37 |
-
# Step 3:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
def classify_political_sentiment(text, classifier):
|
39 |
result = classifier(text[:512]) # 입력이 너무 길면 잘라서 분석
|
40 |
sentiment = result[0]
|
@@ -53,7 +64,7 @@ def classify_political_sentiment(text, classifier):
|
|
53 |
else:
|
54 |
return "중립", sentiment_score
|
55 |
|
56 |
-
# Step
|
57 |
def analyze_news_political_orientation(news_items, classifier):
|
58 |
results = {"진보": 0, "보수": 0, "중립": 0}
|
59 |
detailed_results = []
|
@@ -71,12 +82,14 @@ def analyze_news_political_orientation(news_items, classifier):
|
|
71 |
"요약": description,
|
72 |
"성향": orientation,
|
73 |
"점수": score,
|
74 |
-
"링크": item["link"]
|
|
|
|
|
75 |
})
|
76 |
|
77 |
return results, detailed_results
|
78 |
|
79 |
-
# Step
|
80 |
st.title("정치 성향 분석 대시보드")
|
81 |
st.markdown("### 네이버 뉴스 데이터를 실시간으로 수집하고 정치 성향을 분석합니다.")
|
82 |
|
@@ -110,15 +123,6 @@ if st.button("분석 시작"):
|
|
110 |
st.subheader("성향 분포 차트")
|
111 |
st.bar_chart(pd.DataFrame.from_dict(results, orient='index', columns=["건수"]))
|
112 |
|
113 |
-
# 성향 분포 차트 (matplotlib로 추가된 시각화)
|
114 |
-
st.subheader("성향 분포 (세부 차트)")
|
115 |
-
fig, ax = plt.subplots()
|
116 |
-
ax.bar(results.keys(), results.values())
|
117 |
-
ax.set_xlabel("성향")
|
118 |
-
ax.set_ylabel("건수")
|
119 |
-
ax.set_title("성향 분포 차트")
|
120 |
-
st.pyplot(fig)
|
121 |
-
|
122 |
# 세부 결과 출력
|
123 |
st.subheader("세부 결과")
|
124 |
df = pd.DataFrame(detailed_results)
|
@@ -128,6 +132,8 @@ if st.button("분석 시작"):
|
|
128 |
st.subheader("뉴스 링크")
|
129 |
for index, row in df.iterrows():
|
130 |
st.write(f"- [{row['제목']}]({row['링크']}) (성향: {row['성향']}, 점수: {row['점수']:.2f})")
|
|
|
|
|
131 |
|
132 |
except Exception as e:
|
133 |
st.error(f"오류 발생: {e}")
|
|
|
34 |
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
35 |
return classifier
|
36 |
|
37 |
+
# Step 3: 기사 생성 모델 (진보적/보수적 기사를 생성하는 모델)
|
38 |
+
def generate_article_by_perspective(text, perspective="진보"):
|
39 |
+
# 진보적, 보수적 기사를 생성하는 템플릿
|
40 |
+
if perspective == "진보":
|
41 |
+
# 진보적 관점으로 기사 재구성
|
42 |
+
return f"진보적 관점에서 다룬 기사: {text} ... (진보적 시각으로 변경됨)"
|
43 |
+
elif perspective == "보수":
|
44 |
+
# 보수적 관점으로 기사 재구성
|
45 |
+
return f"보수적 관점에서 다룬 기사: {text} ... (보수적 시각으로 변경됨)"
|
46 |
+
return text
|
47 |
+
|
48 |
+
# Step 4: 정치 성향 분류 함수
|
49 |
def classify_political_sentiment(text, classifier):
|
50 |
result = classifier(text[:512]) # 입력이 너무 길면 잘라서 분석
|
51 |
sentiment = result[0]
|
|
|
64 |
else:
|
65 |
return "중립", sentiment_score
|
66 |
|
67 |
+
# Step 5: 뉴스 분석 및 결과 출력
|
68 |
def analyze_news_political_orientation(news_items, classifier):
|
69 |
results = {"진보": 0, "보수": 0, "중립": 0}
|
70 |
detailed_results = []
|
|
|
82 |
"요약": description,
|
83 |
"성향": orientation,
|
84 |
"점수": score,
|
85 |
+
"링크": item["link"],
|
86 |
+
"진보적 기사": generate_article_by_perspective(combined_text, "진보"),
|
87 |
+
"보수적 기사": generate_article_by_perspective(combined_text, "보수"),
|
88 |
})
|
89 |
|
90 |
return results, detailed_results
|
91 |
|
92 |
+
# Step 6: Streamlit 앱 시작
|
93 |
st.title("정치 성향 분석 대시보드")
|
94 |
st.markdown("### 네이버 뉴스 데이터를 실시간으로 수집하고 정치 성향을 분석합니다.")
|
95 |
|
|
|
123 |
st.subheader("성향 분포 차트")
|
124 |
st.bar_chart(pd.DataFrame.from_dict(results, orient='index', columns=["건수"]))
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
# 세부 결과 출력
|
127 |
st.subheader("세부 결과")
|
128 |
df = pd.DataFrame(detailed_results)
|
|
|
132 |
st.subheader("뉴스 링크")
|
133 |
for index, row in df.iterrows():
|
134 |
st.write(f"- [{row['제목']}]({row['링크']}) (성향: {row['성향']}, 점수: {row['점수']:.2f})")
|
135 |
+
st.write(f"**진보적 기사**: {row['진보적 기사']}")
|
136 |
+
st.write(f"**보수적 기사**: {row['보수적 기사']}")
|
137 |
|
138 |
except Exception as e:
|
139 |
st.error(f"오류 발생: {e}")
|