|
import streamlit as st |
|
import requests |
|
import openai |
|
from transformers import pipeline |
|
import matplotlib.pyplot as plt |
|
import pandas as pd |
|
|
|
|
|
openai.api_key = "YOUR_OPENAI_API_KEY" |
|
|
|
|
|
def fetch_news(query, display=5): |
|
client_id = "YOUR_NAVER_CLIENT_ID" |
|
client_secret = "YOUR_NAVER_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": 1, "sort": "date"} |
|
response = requests.get(url, headers=headers, params=params) |
|
return response.json()["items"] if response.status_code == 200 else [] |
|
|
|
|
|
def load_sentiment_model(): |
|
return pipeline("text-classification", model="bucketresearch/politicalBiasBERT") |
|
|
|
|
|
def generate_article_gpt4(prompt): |
|
try: |
|
response = openai.ChatCompletion.create( |
|
model="gpt-4", |
|
messages=[{"role": "user", "content": prompt}], |
|
max_tokens=512, |
|
temperature=0.7, |
|
) |
|
return response['choices'][0]['message']['content'] |
|
except Exception as e: |
|
return f"Error generating text: {e}" |
|
|
|
|
|
def analyze_news_political_viewpoint(query, sentiment_model): |
|
news_data = fetch_news(query) |
|
if not news_data: |
|
return "๋ด์ค ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ๋ฐ ์คํจํ์ต๋๋ค.", None, None |
|
|
|
results = [] |
|
sentiment_counts = {"์ง๋ณด": 0, "๋ณด์": 0} |
|
|
|
for item in news_data: |
|
title = item["title"] |
|
description = item["description"] |
|
combined_text = f"{title}. {description}" |
|
|
|
|
|
sentiment = sentiment_model(combined_text[:512])[0] |
|
sentiment_label = sentiment["label"] |
|
sentiment_score = sentiment["score"] |
|
|
|
|
|
if sentiment_label == "์ง๋ณด": |
|
prompt = f"๋ค์ ๊ธฐ์ฌ๋ฅผ ๋ณด์์ ๊ด์ ์์ ์์ฑํด์ฃผ์ธ์:\n{combined_text}" |
|
elif sentiment_label == "๋ณด์": |
|
prompt = f"๋ค์ ๊ธฐ์ฌ๋ฅผ ์ง๋ณด์ ๊ด์ ์์ ์์ฑํด์ฃผ์ธ์:\n{combined_text}" |
|
else: |
|
continue |
|
|
|
generated_article = generate_article_gpt4(prompt) |
|
sentiment_counts[sentiment_label] += 1 |
|
|
|
|
|
results.append({ |
|
"์ ๋ชฉ": title, |
|
"์๋ณธ ๊ธฐ์ฌ": description, |
|
"์ฑํฅ": sentiment_label, |
|
"์ฑํฅ ์ ์": sentiment_score, |
|
"๋์กฐ ๊ด์ ๊ธฐ์ฌ": generated_article, |
|
}) |
|
|
|
return "๋ด์ค ๋ถ์์ด ์๋ฃ๋์์ต๋๋ค.", results, sentiment_counts |
|
|
|
|
|
def visualize_sentiment_distribution(sentiment_counts): |
|
labels = list(sentiment_counts.keys()) |
|
values = list(sentiment_counts.values()) |
|
fig, ax = plt.subplots() |
|
ax.bar(labels, values, color=['blue', 'red']) |
|
ax.set_title("์ง๋ณด vs ๋ณด์ ๊ธฐ์ฌ ์") |
|
ax.set_ylabel("๊ธฐ์ฌ ์") |
|
st.pyplot(fig) |
|
|
|
|
|
st.title("์ ์น์ ๊ด์ ๋น๊ต ๋ถ์ ๋๊ตฌ") |
|
st.markdown("### ๋ด์ค ๊ธฐ์ฌ์ ์ ์น ์ฑํฅ ๋ถ์๊ณผ ๋ฐ๋ ๊ด์ ๊ธฐ์ฌ ์์ฑ") |
|
|
|
query = st.text_input("๊ฒ์ ํค์๋๋ฅผ ์
๋ ฅํ์ธ์", value="์ ์น") |
|
if st.button("๋ถ์ ์์"): |
|
with st.spinner("๋ถ์ ์ค์
๋๋ค..."): |
|
sentiment_model = load_sentiment_model() |
|
status_message, analysis_results, sentiment_counts = analyze_news_political_viewpoint(query, sentiment_model) |
|
|
|
|
|
st.subheader(status_message) |
|
if analysis_results: |
|
st.write("### ์ฑํฅ ๋ถํฌ ์๊ฐํ") |
|
visualize_sentiment_distribution(sentiment_counts) |
|
|
|
st.write("### ๋ถ์ ๊ฒฐ๊ณผ") |
|
for result in analysis_results: |
|
st.write(f"#### ์ ๋ชฉ: {result['์ ๋ชฉ']}") |
|
st.write(f"- **์๋ณธ ๊ธฐ์ฌ**: {result['์๋ณธ ๊ธฐ์ฌ']}") |
|
st.write(f"- **์ฑํฅ**: {result['์ฑํฅ']} (์ ์: {result['์ฑํฅ ์ ์']:.2f})") |
|
st.write(f"- **๋์กฐ ๊ด์ ๊ธฐ์ฌ**: {result['๋์กฐ ๊ด์ ๊ธฐ์ฌ']}") |
|
st.write("---") |
|
|