File size: 4,723 Bytes
384f5e4 bfd4ab7 384f5e4 717fe8c 384f5e4 717fe8c bfd4ab7 717fe8c bfd4ab7 60f9cc3 717fe8c 2e4d46c bfd4ab7 717fe8c 4f28bae 384f5e4 717fe8c 4f28bae 717fe8c 384f5e4 717fe8c bb49491 717fe8c bfd4ab7 4f28bae bfd4ab7 717fe8c 4f28bae bfd4ab7 4f28bae bfd4ab7 384f5e4 bfd4ab7 384f5e4 4f28bae 384f5e4 4f28bae 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 |
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:
return response.json()
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("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
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,
})
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)
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['์ ์']:.2f})")
except Exception as e:
st.error(f"์ค๋ฅ ๋ฐ์: {e}")
|