Update app.py
Browse files
app.py
CHANGED
@@ -28,22 +28,12 @@ def fetch_naver_news(query, display=10, start=1, sort="date"):
|
|
28 |
st.error(f"Error: {response.status_code}, {response.text}")
|
29 |
return None
|
30 |
|
31 |
-
# Step 2: Hugging Face
|
32 |
-
def load_translation_model():
|
33 |
-
try:
|
34 |
-
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
35 |
-
st.write("Translation model loaded successfully.")
|
36 |
-
return translator
|
37 |
-
except Exception as e:
|
38 |
-
st.error(f"Error loading translation model: {e}")
|
39 |
-
return None
|
40 |
-
|
41 |
-
# Step 3: Hugging Face ์ ์น ์ฑํฅ ๋ถ์ ๋ชจ๋ธ ๋ก๋
|
42 |
def load_huggingface_model():
|
43 |
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
44 |
return classifier
|
45 |
|
46 |
-
# Step
|
47 |
def classify_political_sentiment(text, classifier):
|
48 |
result = classifier(text[:512]) # ์
๋ ฅ์ด ๋๋ฌด ๊ธธ๋ฉด ์๋ผ์ ๋ถ์
|
49 |
sentiment = result[0]
|
@@ -52,6 +42,7 @@ def classify_political_sentiment(text, classifier):
|
|
52 |
|
53 |
sentiment_score = score if label == "POSITIVE" else -score
|
54 |
|
|
|
55 |
progressive_keywords = ["๋ณต์ง", "ํ๋ฑ", "๋ฏผ์ฃผ", "ํ๊ฒฝ", "์ฌํ์ ์ฑ
์"]
|
56 |
conservative_keywords = ["์๋ณด", "์ ํต", "๊ฒฝ์ ", "์ฑ์ฅ", "์ง์", "๊ตญ๋ฐฉ"]
|
57 |
|
@@ -62,8 +53,8 @@ def classify_political_sentiment(text, classifier):
|
|
62 |
else:
|
63 |
return "์ค๋ฆฝ", sentiment_score
|
64 |
|
65 |
-
# Step
|
66 |
-
def analyze_news_political_orientation(news_items, classifier
|
67 |
results = {"์ง๋ณด": 0, "๋ณด์": 0, "์ค๋ฆฝ": 0}
|
68 |
detailed_results = []
|
69 |
|
@@ -72,12 +63,8 @@ def analyze_news_political_orientation(news_items, classifier, translator):
|
|
72 |
description = item["description"]
|
73 |
combined_text = f"{title}. {description}"
|
74 |
|
75 |
-
# ๋ฒ์ญ: ํ๊ตญ์ด -> ์์ด
|
76 |
-
translated_text = translator(combined_text)[0]['translation_text']
|
77 |
-
st.write("Translated Text:", translated_text) # ๋ฒ์ญ๋ ํ
์คํธ ํ์ธ
|
78 |
-
|
79 |
# ์ ์น ์ฑํฅ ๋ถ๋ฅ
|
80 |
-
orientation, score = classify_political_sentiment(
|
81 |
results[orientation] += 1
|
82 |
detailed_results.append({
|
83 |
"์ ๋ชฉ": title,
|
@@ -110,10 +97,9 @@ if st.button("๋ถ์ ์์"):
|
|
110 |
|
111 |
# Hugging Face ๋ชจ๋ธ ๋ก๋
|
112 |
classifier = load_huggingface_model()
|
113 |
-
translator = load_translation_model()
|
114 |
|
115 |
# ๋ด์ค ๋ฐ์ดํฐ ๋ถ์
|
116 |
-
results, detailed_results = analyze_news_political_orientation(news_items, classifier
|
117 |
|
118 |
# ๋ถ์ ๊ฒฐ๊ณผ ์๊ฐํ
|
119 |
st.subheader("๋ถ์ ๊ฒฐ๊ณผ ์์ฝ")
|
|
|
28 |
st.error(f"Error: {response.status_code}, {response.text}")
|
29 |
return None
|
30 |
|
31 |
+
# Step 2: Hugging Face ์ ์น ์ฑํฅ ๋ถ์ ๋ชจ๋ธ ๋ก๋
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
def load_huggingface_model():
|
33 |
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
34 |
return classifier
|
35 |
|
36 |
+
# Step 3: ์ ์น ์ฑํฅ ๋ถ๋ฅ ํจ์
|
37 |
def classify_political_sentiment(text, classifier):
|
38 |
result = classifier(text[:512]) # ์
๋ ฅ์ด ๋๋ฌด ๊ธธ๋ฉด ์๋ผ์ ๋ถ์
|
39 |
sentiment = result[0]
|
|
|
42 |
|
43 |
sentiment_score = score if label == "POSITIVE" else -score
|
44 |
|
45 |
+
# ํค์๋ ๊ธฐ๋ฐ ๋ถ๋ฅ (์ง๋ณด/๋ณด์)
|
46 |
progressive_keywords = ["๋ณต์ง", "ํ๋ฑ", "๋ฏผ์ฃผ", "ํ๊ฒฝ", "์ฌํ์ ์ฑ
์"]
|
47 |
conservative_keywords = ["์๋ณด", "์ ํต", "๊ฒฝ์ ", "์ฑ์ฅ", "์ง์", "๊ตญ๋ฐฉ"]
|
48 |
|
|
|
53 |
else:
|
54 |
return "์ค๋ฆฝ", sentiment_score
|
55 |
|
56 |
+
# Step 4: ๋ด์ค ๋ถ์ ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
57 |
+
def analyze_news_political_orientation(news_items, classifier):
|
58 |
results = {"์ง๋ณด": 0, "๋ณด์": 0, "์ค๋ฆฝ": 0}
|
59 |
detailed_results = []
|
60 |
|
|
|
63 |
description = item["description"]
|
64 |
combined_text = f"{title}. {description}"
|
65 |
|
|
|
|
|
|
|
|
|
66 |
# ์ ์น ์ฑํฅ ๋ถ๋ฅ
|
67 |
+
orientation, score = classify_political_sentiment(combined_text, classifier)
|
68 |
results[orientation] += 1
|
69 |
detailed_results.append({
|
70 |
"์ ๋ชฉ": title,
|
|
|
97 |
|
98 |
# Hugging Face ๋ชจ๋ธ ๋ก๋
|
99 |
classifier = load_huggingface_model()
|
|
|
100 |
|
101 |
# ๋ด์ค ๋ฐ์ดํฐ ๋ถ์
|
102 |
+
results, detailed_results = analyze_news_political_orientation(news_items, classifier)
|
103 |
|
104 |
# ๋ถ์ ๊ฒฐ๊ณผ ์๊ฐํ
|
105 |
st.subheader("๋ถ์ ๊ฒฐ๊ณผ ์์ฝ")
|