File size: 4,333 Bytes
384f5e4 bfd4ab7 ebf358a 384f5e4 ebf358a 384f5e4 717fe8c ebf358a 717fe8c ebf358a bfd4ab7 ebf358a 717fe8c ebf358a 717fe8c ebf358a d10e98d ebf358a d10e98d ebf358a bfd4ab7 717fe8c ebf358a 384f5e4 ebf358a 384f5e4 bfd4ab7 ebf358a bfd4ab7 ebf358a 95eccad ebf358a 384f5e4 ebf358a |
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 |
import streamlit as st
import requests
import openai
from transformers import pipeline
import matplotlib.pyplot as plt
import pandas as pd
# OpenAI API ํค ์ค์
openai.api_key = "YOUR_OPENAI_API_KEY"
# Step 1: ๋ค์ด๋ฒ ๋ด์ค ๋ฐ์ดํฐ ์์ง
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 []
# Step 2: ์ ์น ์ฑํฅ ๋ถ์ ๋ชจ๋ธ ๋ก๋
def load_sentiment_model():
return pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
# Step 3: GPT-4๋ฅผ ์ฌ์ฉํด ๋์กฐ ๊ด์ ๊ธฐ์ฌ ์์ฑ
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}"
# Step 4: ๋ด์ค ๋ฐ์ดํฐ ๋ถ์ ๋ฐ ๊ฒฐ๊ณผ ์์ฑ
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
# Step 5: ์๊ฐํ ํจ์
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)
# Step 6: Streamlit UI
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("---")
|