Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import streamlit as st
|
|
| 2 |
import requests
|
| 3 |
from transformers import pipeline
|
| 4 |
import pandas as pd
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
|
| 7 |
# Step 1: ๋ค์ด๋ฒ ๋ด์ค API ํธ์ถ ํจ์
|
| 8 |
def fetch_naver_news(query, display=10, start=1, sort="date"):
|
|
@@ -29,21 +28,26 @@ def fetch_naver_news(query, display=10, start=1, sort="date"):
|
|
| 29 |
st.error(f"Error: {response.status_code}, {response.text}")
|
| 30 |
return None
|
| 31 |
|
| 32 |
-
# Step 2:
|
| 33 |
-
def
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
# Step 3:
|
| 38 |
def generate_article_by_perspective(text, perspective="์ง๋ณด"):
|
| 39 |
-
#
|
| 40 |
if perspective == "์ง๋ณด":
|
| 41 |
-
|
| 42 |
-
|
| 43 |
elif perspective == "๋ณด์":
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Step 4: ์ ์น ์ฑํฅ ๋ถ๋ฅ ํจ์
|
| 49 |
def classify_political_sentiment(text, classifier):
|
|
@@ -65,7 +69,7 @@ def classify_political_sentiment(text, classifier):
|
|
| 65 |
return "์ค๋ฆฝ", sentiment_score
|
| 66 |
|
| 67 |
# Step 5: ๋ด์ค ๋ถ์ ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
| 68 |
-
def analyze_news_political_orientation(news_items, classifier):
|
| 69 |
results = {"์ง๋ณด": 0, "๋ณด์": 0, "์ค๋ฆฝ": 0}
|
| 70 |
detailed_results = []
|
| 71 |
|
|
@@ -77,18 +81,24 @@ def analyze_news_political_orientation(news_items, classifier):
|
|
| 77 |
# ์ ์น ์ฑํฅ ๋ถ๋ฅ
|
| 78 |
orientation, score = classify_political_sentiment(combined_text, classifier)
|
| 79 |
results[orientation] += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
detailed_results.append({
|
| 81 |
"์ ๋ชฉ": title,
|
| 82 |
"์์ฝ": description,
|
| 83 |
"์ฑํฅ": orientation,
|
| 84 |
"์ ์": score,
|
| 85 |
"๋งํฌ": item["link"],
|
| 86 |
-
"์ง๋ณด์ ๊ธฐ์ฌ":
|
| 87 |
-
"๋ณด์์ ๊ธฐ์ฌ":
|
| 88 |
})
|
| 89 |
|
| 90 |
return results, detailed_results
|
| 91 |
|
|
|
|
| 92 |
# Step 6: Streamlit ์ฑ ์์
|
| 93 |
st.title("์ ์น ์ฑํฅ ๋ถ์ ๋์๋ณด๋")
|
| 94 |
st.markdown("### ๋ค์ด๋ฒ ๋ด์ค ๋ฐ์ดํฐ๋ฅผ ์ค์๊ฐ์ผ๋ก ์์งํ๊ณ ์ ์น ์ฑํฅ์ ๋ถ์ํฉ๋๋ค.")
|
|
@@ -109,9 +119,10 @@ if st.button("๋ถ์ ์์"):
|
|
| 109 |
|
| 110 |
# Hugging Face ๋ชจ๋ธ ๋ก๋
|
| 111 |
classifier = load_huggingface_model()
|
|
|
|
| 112 |
|
| 113 |
# ๋ด์ค ๋ฐ์ดํฐ ๋ถ์
|
| 114 |
-
results, detailed_results = analyze_news_political_orientation(news_items, classifier)
|
| 115 |
|
| 116 |
# ๋ถ์ ๊ฒฐ๊ณผ ์๊ฐํ
|
| 117 |
st.subheader("๋ถ์ ๊ฒฐ๊ณผ ์์ฝ")
|
|
|
|
| 2 |
import requests
|
| 3 |
from transformers import pipeline
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
|
| 6 |
# Step 1: ๋ค์ด๋ฒ ๋ด์ค API ํธ์ถ ํจ์
|
| 7 |
def fetch_naver_news(query, display=10, start=1, sort="date"):
|
|
|
|
| 28 |
st.error(f"Error: {response.status_code}, {response.text}")
|
| 29 |
return None
|
| 30 |
|
| 31 |
+
# Step 2: GPT ๋ชจ๋ธ ๋ก๋ (์ง๋ณด์ , ๋ณด์์ ๊ธฐ์ฌ ์์ฑ)
|
| 32 |
+
def load_gpt_model():
|
| 33 |
+
# GPT ๋ชจ๋ธ ๋ก๋ (์ด ๋ชจ๋ธ์ ํ
์คํธ ์์ฑ์ ์ฌ์ฉ๋ฉ๋๋ค)
|
| 34 |
+
gpt_model = pipeline("text-generation", model="gpt2") # Hugging Face์์ ์ฌ์ฉํ ์ ์๋ GPT ๋ชจ๋ธ
|
| 35 |
+
return gpt_model
|
| 36 |
|
| 37 |
+
# Step 3: ๊ธฐ์ฌ๋ฅผ ์ง๋ณด์ ๋ฐ ๋ณด์์ ๊ด์ ์์ ์์ฑํ๋ ํจ์
|
| 38 |
def generate_article_by_perspective(text, perspective="์ง๋ณด"):
|
| 39 |
+
# ์ง๋ณด์ ๊ด์ ์์ฑ
|
| 40 |
if perspective == "์ง๋ณด":
|
| 41 |
+
prompt = f"์ง๋ณด์ ๊ด์ ์์ ๋ค์ ๊ธฐ์ฌ๋ฅผ ์์ฑํด ์ฃผ์ธ์: {text}. ํ๊ฒฝ๊ณผ ์ฌํ์ ์ฑ
์์ ๊ฐ์กฐํ๋ฉฐ..."
|
| 42 |
+
# ๋ณด์์ ๊ด์ ์์ฑ
|
| 43 |
elif perspective == "๋ณด์":
|
| 44 |
+
prompt = f"๋ณด์์ ๊ด์ ์์ ๋ค์ ๊ธฐ์ฌ๋ฅผ ์์ฑํด ์ฃผ์ธ์: {text}. ๊ฒฝ์ ์ฑ์ฅ๊ณผ ๊ตญ๊ฐ ์๋ณด๋ฅผ ๊ฐ์กฐํ๋ฉฐ..."
|
| 45 |
+
else:
|
| 46 |
+
prompt = text
|
| 47 |
+
|
| 48 |
+
# GPT ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ํ
์คํธ ์์ฑ
|
| 49 |
+
generated_article = gpt_model(prompt, max_length=512, num_return_sequences=1)[0]['generated_text']
|
| 50 |
+
return generated_article
|
| 51 |
|
| 52 |
# Step 4: ์ ์น ์ฑํฅ ๋ถ๋ฅ ํจ์
|
| 53 |
def classify_political_sentiment(text, classifier):
|
|
|
|
| 69 |
return "์ค๋ฆฝ", sentiment_score
|
| 70 |
|
| 71 |
# Step 5: ๋ด์ค ๋ถ์ ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
| 72 |
+
def analyze_news_political_orientation(news_items, classifier, gpt_model):
|
| 73 |
results = {"์ง๋ณด": 0, "๋ณด์": 0, "์ค๋ฆฝ": 0}
|
| 74 |
detailed_results = []
|
| 75 |
|
|
|
|
| 81 |
# ์ ์น ์ฑํฅ ๋ถ๋ฅ
|
| 82 |
orientation, score = classify_political_sentiment(combined_text, classifier)
|
| 83 |
results[orientation] += 1
|
| 84 |
+
|
| 85 |
+
# ์ง๋ณด์ /๋ณด์์ ๊ธฐ์ฌ ์์ฑ
|
| 86 |
+
progressive_article = generate_article_by_perspective(combined_text, "์ง๋ณด")
|
| 87 |
+
conservative_article = generate_article_by_perspective(combined_text, "๋ณด์")
|
| 88 |
+
|
| 89 |
detailed_results.append({
|
| 90 |
"์ ๋ชฉ": title,
|
| 91 |
"์์ฝ": description,
|
| 92 |
"์ฑํฅ": orientation,
|
| 93 |
"์ ์": score,
|
| 94 |
"๋งํฌ": item["link"],
|
| 95 |
+
"์ง๋ณด์ ๊ธฐ์ฌ": progressive_article,
|
| 96 |
+
"๋ณด์์ ๊ธฐ์ฌ": conservative_article
|
| 97 |
})
|
| 98 |
|
| 99 |
return results, detailed_results
|
| 100 |
|
| 101 |
+
|
| 102 |
# Step 6: Streamlit ์ฑ ์์
|
| 103 |
st.title("์ ์น ์ฑํฅ ๋ถ์ ๋์๋ณด๋")
|
| 104 |
st.markdown("### ๋ค์ด๋ฒ ๋ด์ค ๋ฐ์ดํฐ๋ฅผ ์ค์๊ฐ์ผ๋ก ์์งํ๊ณ ์ ์น ์ฑํฅ์ ๋ถ์ํฉ๋๋ค.")
|
|
|
|
| 119 |
|
| 120 |
# Hugging Face ๋ชจ๋ธ ๋ก๋
|
| 121 |
classifier = load_huggingface_model()
|
| 122 |
+
gpt_model = load_gpt_model()
|
| 123 |
|
| 124 |
# ๋ด์ค ๋ฐ์ดํฐ ๋ถ์
|
| 125 |
+
results, detailed_results = analyze_news_political_orientation(news_items, classifier, gpt_model)
|
| 126 |
|
| 127 |
# ๋ถ์ ๊ฒฐ๊ณผ ์๊ฐํ
|
| 128 |
st.subheader("๋ถ์ ๊ฒฐ๊ณผ ์์ฝ")
|