File size: 5,223 Bytes
384f5e4 bfd4ab7 384f5e4 717fe8c 384f5e4 717fe8c bfd4ab7 717fe8c bfd4ab7 60f9cc3 717fe8c b45b655 bfd4ab7 717fe8c ad7b4a8 fd1df6b ad7b4a8 465e649 717fe8c ad7b4a8 717fe8c ad7b4a8 384f5e4 ad7b4a8 b45b655 465e649 ad7b4a8 bfd4ab7 ad7b4a8 4f28bae bfd4ab7 717fe8c 4f28bae bfd4ab7 4f28bae bfd4ab7 384f5e4 db118f0 384f5e4 bfd4ab7 384f5e4 12cb09e 40b493d 2dd33a7 384f5e4 2dd33a7 12cb09e 384f5e4 ad7b4a8 4f28bae 384f5e4 4f28bae 384f5e4 fd1df6b 384f5e4 db118f0 384f5e4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
import streamlit as st
import requests
from transformers import pipeline
import pandas as pd
# Step 1: ๋ค์ด๋ฒ ๋ด์ค API ํธ์ถ ํจ์
def fetch_naver_news(query, display=10, start=1, sort="date"):
client_id = "I_8koTJh3R5l4wLurQbG" # ๋ค์ด๋ฒ ๊ฐ๋ฐ์ ์ผํฐ์์ ๋ฐ๊ธ๋ฐ์ Client ID
client_secret = "W5oWYlAgur" # ๋ค์ด๋ฒ ๊ฐ๋ฐ์ ์ผํฐ์์ ๋ฐ๊ธ๋ฐ์ Client Secret
url = "https://openapi.naver.com/v1/search/news.json"
headers = {
"X-Naver-Client-Id": client_id,
"X-Naver-Client-Secret": client_secret,
}
params = {
"query": query,
"display": display,
"start": start,
"sort": sort,
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
news_data = response.json()
return news_data
else:
raise Exception(f"Error: {response.status_code}, {response.text}")
# Step 2: Hugging Face ๋ฒ์ญ ๋ชจ๋ธ ๋ก๋ (ํ๊ตญ์ด -> ์์ด)
def load_translation_model():
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
return translator
# Step 3: Hugging Face ์ ์น ์ฑํฅ ๋ถ์ ๋ชจ๋ธ ๋ก๋
def load_huggingface_model():
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
return classifier
# Step 4: ์ ์น ์ฑํฅ ๋ถ๋ฅ ํจ์
def classify_political_sentiment(text, classifier):
# ๊ฐ์ฑ ๋ถ์ ์คํ
result = classifier(text[:512]) # ์
๋ ฅ์ด ๋๋ฌด ๊ธธ๋ฉด ์๋ผ์ ๋ถ์
sentiment = result[0]
label = sentiment["label"]
score = sentiment["score"]
# ์ ์ํ
sentiment_score = score if label == "POSITIVE" else -score
# ํค์๋ ๊ธฐ๋ฐ ๋ถ๋ฅ (์ง๋ณด/๋ณด์)
progressive_keywords = ["๋ณต์ง", "ํ๋ฑ", "๋ฏผ์ฃผ", "ํ๊ฒฝ", "์ฌํ์ ์ฑ
์"]
conservative_keywords = ["์๋ณด", "์ ํต", "๊ฒฝ์ ", "์ฑ์ฅ", "์ง์", "๊ตญ๋ฐฉ"]
if any(keyword in text for keyword in progressive_keywords):
return "์ง๋ณด", sentiment_score
elif any(keyword in text for keyword in conservative_keywords):
return "๋ณด์", sentiment_score
else:
return "์ค๋ฆฝ", sentiment_score
# Step 5: ๋ด์ค ๋ถ์ ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
def analyze_news_political_orientation(news_items, classifier, translator):
results = {"์ง๋ณด": 0, "๋ณด์": 0, "์ค๋ฆฝ": 0}
detailed_results = []
for item in news_items:
title = item["title"]
description = item["description"]
combined_text = f"{title}. {description}"
# ๋ฒ์ญ: ํ๊ตญ์ด -> ์์ด
translated_text = translator(combined_text)[0]['translation_text']
# ์ ์น ์ฑํฅ ๋ถ๋ฅ
orientation, score = classify_political_sentiment(translated_text, classifier)
results[orientation] += 1
detailed_results.append({
"์ ๋ชฉ": title,
"์์ฝ": description,
"์ฑํฅ": orientation,
"์ ์": score,
"๋งํฌ": item["link"]
})
return results, detailed_results
# Streamlit ์ฑ ์์
st.title("์ ์น ์ฑํฅ ๋ถ์ ๋์๋ณด๋")
st.markdown("### ๋ค์ด๋ฒ ๋ด์ค ๋ฐ์ดํฐ๋ฅผ ์ค์๊ฐ์ผ๋ก ์์งํ๊ณ ์ ์น ์ฑํฅ์ ๋ถ์ํฉ๋๋ค.")
# ๊ฒ์ ํค์๋ ์
๋ ฅ
query = st.text_input("๊ฒ์ ํค์๋๋ฅผ ์
๋ ฅํ์ธ์", value="์ ์น")
# ๋ถ์ ์์ ๋ฒํผ ํด๋ฆญ ์ ๋์
# ๋ถ์ ์์ ๋ฒํผ ํด๋ฆญ ์ ๋์
# ๋ถ์ ์์ ๋ฒํผ ํด๋ฆญ ์ ๋์
if st.button("๋ถ์ ์์"):
with st.spinner("๋ฐ์ดํฐ๋ฅผ ๋ถ์ ์ค์
๋๋ค..."):
try:
# ๋ค์ด๋ฒ ๋ด์ค ๋ฐ์ดํฐ ์์ง
news_data = fetch_naver_news(query, display=10)
# ๋ฐ์ดํฐ ์์ง๋ ์ํ ํ์ธ
if not news_data or not news_data.get("items"):
st.error("๋ด์ค ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ๋ฐ ์คํจํ์ต๋๋ค.")
return # ์ด ์ค์ ์ญ์ ํ๊ณ , ์ค๋ฅ ๋ฉ์์ง๋ก ๋์ฒดํฉ๋๋ค.
news_items = news_data["items"]
# Hugging Face ๋ชจ๋ธ ๋ก๋
classifier = load_huggingface_model()
translator = load_translation_model()
# ๋ด์ค ๋ฐ์ดํฐ ๋ถ์
results, detailed_results = analyze_news_political_orientation(news_items, classifier, translator)
# ๋ถ์ ๊ฒฐ๊ณผ ์๊ฐํ
st.subheader("๋ถ์ ๊ฒฐ๊ณผ ์์ฝ")
st.write(f"์ง๋ณด: {results['์ง๋ณด']}๊ฑด")
st.write(f"๋ณด์: {results['๋ณด์']}๊ฑด")
st.write(f"์ค๋ฆฝ: {results['์ค๋ฆฝ']}๊ฑด")
# ์ฑํฅ ๋ถํฌ ์ฐจํธ
st.subheader("์ฑํฅ ๋ถํฌ ์ฐจํธ")
st.bar_chart(pd.DataFrame.from_dict(results, orient='index', columns=["๊ฑด์"]))
# ์ธ๋ถ ๊ฒฐ๊ณผ ์ถ๋ ฅ
st.subheader("์ธ๋ถ ๊ฒฐ๊ณผ")
df = pd.DataFrame(detailed_results)
st.dataframe(df)
# ๋งํฌ ํฌํจํ ๋ด์ค ์ถ๋ ฅ
st.subheader("๋ด์ค ๋งํฌ")
for index, row in df.iterrows():
st.write(f"- [{row['์ ๋ชฉ']}]({row['๋งํฌ']}) (์ฑํฅ: {row['์ฑํฅ']}, ์ ์: {row['์ ์']:.2f})")
except Exception as e:
st.error(f"์ค๋ฅ ๋ฐ์: {e}")
|