Update app.py
Browse files
app.py
CHANGED
@@ -26,12 +26,17 @@ def fetch_naver_news(query, display=10, start=1, sort="date"):
|
|
26 |
else:
|
27 |
raise Exception(f"Error: {response.status_code}, {response.text}")
|
28 |
|
29 |
-
# Step 2: Hugging Face
|
|
|
|
|
|
|
|
|
|
|
30 |
def load_huggingface_model():
|
31 |
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
32 |
return classifier
|
33 |
|
34 |
-
# Step
|
35 |
def classify_political_sentiment(text, classifier):
|
36 |
# ๊ฐ์ฑ ๋ถ์ ์คํ
|
37 |
result = classifier(text[:512]) # ์
๋ ฅ์ด ๋๋ฌด ๊ธธ๋ฉด ์๋ผ์ ๋ถ์
|
@@ -53,8 +58,8 @@ 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 = []
|
60 |
|
@@ -63,8 +68,11 @@ def analyze_news_political_orientation(news_items, classifier):
|
|
63 |
description = item["description"]
|
64 |
combined_text = f"{title}. {description}"
|
65 |
|
|
|
|
|
|
|
66 |
# ์ ์น ์ฑํฅ ๋ถ๋ฅ
|
67 |
-
orientation, score = classify_political_sentiment(
|
68 |
results[orientation] += 1
|
69 |
detailed_results.append({
|
70 |
"์ ๋ชฉ": title,
|
@@ -91,9 +99,10 @@ if st.button("๋ถ์ ์์"):
|
|
91 |
|
92 |
# Hugging Face ๋ชจ๋ธ ๋ก๋
|
93 |
classifier = load_huggingface_model()
|
|
|
94 |
|
95 |
# ๋ด์ค ๋ฐ์ดํฐ ๋ถ์
|
96 |
-
results, detailed_results = analyze_news_political_orientation(news_items, classifier)
|
97 |
|
98 |
# ๋ถ์ ๊ฒฐ๊ณผ ์๊ฐํ
|
99 |
st.subheader("๋ถ์ ๊ฒฐ๊ณผ ์์ฝ")
|
|
|
26 |
else:
|
27 |
raise Exception(f"Error: {response.status_code}, {response.text}")
|
28 |
|
29 |
+
# Step 2: Hugging Face ๋ฒ์ญ ๋ชจ๋ธ ๋ก๋ (ํ๊ตญ์ด -> ์์ด)
|
30 |
+
def load_translation_model():
|
31 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
32 |
+
return translator
|
33 |
+
|
34 |
+
# Step 3: Hugging Face ๊ฐ์ฑ ๋ถ์ ๋ชจ๋ธ ๋ก๋
|
35 |
def load_huggingface_model():
|
36 |
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
37 |
return classifier
|
38 |
|
39 |
+
# Step 4: ์ ์น ์ฑํฅ ๋ถ๋ฅ ํจ์
|
40 |
def classify_political_sentiment(text, classifier):
|
41 |
# ๊ฐ์ฑ ๋ถ์ ์คํ
|
42 |
result = classifier(text[:512]) # ์
๋ ฅ์ด ๋๋ฌด ๊ธธ๋ฉด ์๋ผ์ ๋ถ์
|
|
|
58 |
else:
|
59 |
return "์ค๋ฆฝ", sentiment_score
|
60 |
|
61 |
+
# Step 5: ๋ด์ค ๋ถ์ ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
62 |
+
def analyze_news_political_orientation(news_items, classifier, translator):
|
63 |
results = {"์ง๋ณด": 0, "๋ณด์": 0, "์ค๋ฆฝ": 0}
|
64 |
detailed_results = []
|
65 |
|
|
|
68 |
description = item["description"]
|
69 |
combined_text = f"{title}. {description}"
|
70 |
|
71 |
+
# ๋ฒ์ญ: ํ๊ตญ์ด -> ์์ด
|
72 |
+
translated_text = translator(combined_text)[0]['translation_text']
|
73 |
+
|
74 |
# ์ ์น ์ฑํฅ ๋ถ๋ฅ
|
75 |
+
orientation, score = classify_political_sentiment(translated_text, classifier)
|
76 |
results[orientation] += 1
|
77 |
detailed_results.append({
|
78 |
"์ ๋ชฉ": title,
|
|
|
99 |
|
100 |
# Hugging Face ๋ชจ๋ธ ๋ก๋
|
101 |
classifier = load_huggingface_model()
|
102 |
+
translator = load_translation_model()
|
103 |
|
104 |
# ๋ด์ค ๋ฐ์ดํฐ ๋ถ์
|
105 |
+
results, detailed_results = analyze_news_political_orientation(news_items, classifier, translator)
|
106 |
|
107 |
# ๋ถ์ ๊ฒฐ๊ณผ ์๊ฐํ
|
108 |
st.subheader("๋ถ์ ๊ฒฐ๊ณผ ์์ฝ")
|