File size: 4,624 Bytes
384f5e4 bfd4ab7 384f5e4 717fe8c 384f5e4 717fe8c bfd4ab7 717fe8c bfd4ab7 60f9cc3 717fe8c b45b655 bfd4ab7 02d5bf8 717fe8c 0d5a75b ad7b4a8 465e649 717fe8c 0d5a75b 717fe8c 384f5e4 ad7b4a8 b45b655 465e649 0d5a75b ad7b4a8 a8c6507 0d5a75b bfd4ab7 717fe8c 8cadfb7 0d5a75b bfd4ab7 384f5e4 db118f0 384f5e4 bfd4ab7 4e47ea7 384f5e4 74f08a5 384f5e4 74f08a5 384f5e4 2dd33a7 02d5bf8 2dd33a7 02d5bf8 74f08a5 02d5bf8 74f08a5 0d5a75b 02d5bf8 74f08a5 02d5bf8 74f08a5 02d5bf8 74f08a5 02d5bf8 74f08a5 02d5bf8 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 |
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:
st.error(f"Error: {response.status_code}, {response.text}")
return None
# Step 2: Hugging Face ์ ์น ์ฑํฅ ๋ถ์ ๋ชจ๋ธ ๋ก๋
def load_huggingface_model():
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
return classifier
# Step 3: ์ ์น ์ฑํฅ ๋ถ๋ฅ ํจ์
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 4: ๋ด์ค ๋ถ์ ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
def analyze_news_political_orientation(news_items, classifier):
results = {"์ง๋ณด": 0, "๋ณด์": 0, "์ค๋ฆฝ": 0}
detailed_results = []
for item in news_items:
title = item["title"]
description = item["description"]
combined_text = f"{title}. {description}"
# ์ ์น ์ฑํฅ ๋ถ๋ฅ
orientation, score = classify_political_sentiment(combined_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 news_data is None:
st.error("๋ด์ค ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ๋ฐ ์คํจํ์ต๋๋ค.")
else:
news_items = news_data["items"]
# Hugging Face ๋ชจ๋ธ ๋ก๋
classifier = load_huggingface_model()
# ๋ด์ค ๋ฐ์ดํฐ ๋ถ์
results, detailed_results = analyze_news_political_orientation(news_items, classifier)
# ๋ถ์ ๊ฒฐ๊ณผ ์๊ฐํ
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}")
|