Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,9 @@
|
|
|
|
1 |
import requests
|
2 |
-
from transformers import pipeline
|
|
|
3 |
|
4 |
-
# Step 1: ๋ค์ด๋ฒ ๋ด์ค API
|
5 |
def fetch_naver_news(query, display=10, start=1, sort="date"):
|
6 |
client_id = "I_8koTJh3R5l4wLurQbG" # ๋ค์ด๋ฒ ๊ฐ๋ฐ์ ์ผํฐ์์ ๋ฐ๊ธ๋ฐ์ Client ID
|
7 |
client_secret = "W5oWYlAgur" # ๋ค์ด๋ฒ ๊ฐ๋ฐ์ ์ผํฐ์์ ๋ฐ๊ธ๋ฐ์ Client Secret
|
@@ -24,27 +26,15 @@ def fetch_naver_news(query, display=10, start=1, sort="date"):
|
|
24 |
else:
|
25 |
raise Exception(f"Error: {response.status_code}, {response.text}")
|
26 |
|
27 |
-
# Step 2:
|
28 |
-
def
|
29 |
-
|
30 |
-
model = MarianMTModel.from_pretrained(model_name)
|
31 |
-
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
32 |
-
return model, tokenizer
|
33 |
-
|
34 |
-
# ํ๊ตญ์ด ํ
์คํธ๋ฅผ ์์ด๋ก ๋ฒ์ญํ๋ ํจ์
|
35 |
-
def translate_to_english(text, model, tokenizer):
|
36 |
-
translated = tokenizer.encode(text, return_tensors="pt", padding=True)
|
37 |
-
translated_text = model.generate(translated, max_length=512)
|
38 |
-
return tokenizer.decode(translated_text[0], skip_special_tokens=True)
|
39 |
-
|
40 |
-
# Step 3: ์ ์น ์ฑํฅ ๋ถ์ ๋ชจ๋ธ ๋ก๋ (PoliticalBiasBERT)
|
41 |
-
def load_political_bias_model():
|
42 |
-
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
43 |
return classifier
|
44 |
|
45 |
-
# Step
|
46 |
def classify_political_sentiment(text, classifier):
|
47 |
-
|
|
|
48 |
sentiment = result[0]
|
49 |
label = sentiment["label"]
|
50 |
score = sentiment["score"]
|
@@ -63,52 +53,67 @@ def classify_political_sentiment(text, classifier):
|
|
63 |
else:
|
64 |
return "์ค๋ฆฝ", sentiment_score
|
65 |
|
66 |
-
# Step
|
67 |
-
def analyze_news_political_orientation(news_items, classifier
|
68 |
results = {"์ง๋ณด": 0, "๋ณด์": 0, "์ค๋ฆฝ": 0}
|
69 |
detailed_results = []
|
70 |
|
71 |
for item in news_items:
|
72 |
title = item["title"]
|
73 |
description = item["description"]
|
74 |
-
|
75 |
-
# ํ๊ตญ์ด ๊ธฐ์ฌ ํ
์คํธ๋ฅผ ์์ด๋ก ๋ฒ์ญ
|
76 |
combined_text = f"{title}. {description}"
|
77 |
-
translated_text = translate_to_english(combined_text, translation_model, tokenizer)
|
78 |
|
79 |
# ์ ์น ์ฑํฅ ๋ถ๋ฅ
|
80 |
-
orientation, score = classify_political_sentiment(
|
81 |
results[orientation] += 1
|
82 |
-
detailed_results.append(
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
print("-" * 80)
|
89 |
|
90 |
return results, detailed_results
|
91 |
|
92 |
-
#
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
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"):
|
8 |
client_id = "I_8koTJh3R5l4wLurQbG" # ๋ค์ด๋ฒ ๊ฐ๋ฐ์ ์ผํฐ์์ ๋ฐ๊ธ๋ฐ์ Client ID
|
9 |
client_secret = "W5oWYlAgur" # ๋ค์ด๋ฒ ๊ฐ๋ฐ์ ์ผํฐ์์ ๋ฐ๊ธ๋ฐ์ Client Secret
|
|
|
26 |
else:
|
27 |
raise Exception(f"Error: {response.status_code}, {response.text}")
|
28 |
|
29 |
+
# Step 2: Hugging Face ๊ฐ์ฑ ๋ถ์ ๋ชจ๋ธ ๋ก๋
|
30 |
+
def load_huggingface_model():
|
31 |
+
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
return classifier
|
33 |
|
34 |
+
# Step 3: ์ ์น ์ฑํฅ ๋ถ๋ฅ ํจ์
|
35 |
def classify_political_sentiment(text, classifier):
|
36 |
+
# ๊ฐ์ฑ ๋ถ์ ์คํ
|
37 |
+
result = classifier(text[:512]) # ์
๋ ฅ์ด ๋๋ฌด ๊ธธ๋ฉด ์๋ผ์ ๋ถ์
|
38 |
sentiment = result[0]
|
39 |
label = sentiment["label"]
|
40 |
score = sentiment["score"]
|
|
|
53 |
else:
|
54 |
return "์ค๋ฆฝ", sentiment_score
|
55 |
|
56 |
+
# Step 4: ๋ด์ค ๋ถ์ ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
57 |
+
def analyze_news_political_orientation(news_items, classifier):
|
58 |
results = {"์ง๋ณด": 0, "๋ณด์": 0, "์ค๋ฆฝ": 0}
|
59 |
detailed_results = []
|
60 |
|
61 |
for item in news_items:
|
62 |
title = item["title"]
|
63 |
description = item["description"]
|
|
|
|
|
64 |
combined_text = f"{title}. {description}"
|
|
|
65 |
|
66 |
# ์ ์น ์ฑํฅ ๋ถ๋ฅ
|
67 |
+
orientation, score = classify_political_sentiment(combined_text, classifier)
|
68 |
results[orientation] += 1
|
69 |
+
detailed_results.append({
|
70 |
+
"์ ๋ชฉ": title,
|
71 |
+
"์์ฝ": description,
|
72 |
+
"์ฑํฅ": orientation,
|
73 |
+
"์ ์": score,
|
74 |
+
})
|
|
|
75 |
|
76 |
return results, detailed_results
|
77 |
|
78 |
+
# Streamlit ์ฑ ์์
|
79 |
+
st.title("์ ์น ์ฑํฅ ๋ถ์ ๋์๋ณด๋")
|
80 |
+
st.markdown("### ๋ค์ด๋ฒ ๋ด์ค ๋ฐ์ดํฐ๋ฅผ ์ค์๊ฐ์ผ๋ก ์์งํ๊ณ ์ ์น ์ฑํฅ์ ๋ถ์ํฉ๋๋ค.")
|
81 |
+
|
82 |
+
# ๊ฒ์ ํค์๋ ์
๋ ฅ
|
83 |
+
query = st.text_input("๊ฒ์ ํค์๋๋ฅผ ์
๋ ฅํ์ธ์", value="์ ์น")
|
84 |
+
|
85 |
+
if st.button("๋ถ์ ์์"):
|
86 |
+
with st.spinner("๋ฐ์ดํฐ๋ฅผ ๋ถ์ ์ค์
๋๋ค..."):
|
87 |
+
try:
|
88 |
+
# ๋ค์ด๋ฒ ๋ด์ค ๋ฐ์ดํฐ ์์ง
|
89 |
+
news_data = fetch_naver_news(query, display=10)
|
90 |
+
news_items = news_data["items"]
|
91 |
+
|
92 |
+
# Hugging Face ๋ชจ๋ธ ๋ก๋
|
93 |
+
classifier = load_huggingface_model()
|
94 |
+
|
95 |
+
# ๋ด์ค ๋ฐ์ดํฐ ๋ถ์
|
96 |
+
results, detailed_results = analyze_news_political_orientation(news_items, classifier)
|
97 |
+
|
98 |
+
# ๋ถ์ ๊ฒฐ๊ณผ ์๊ฐํ
|
99 |
+
st.subheader("๋ถ์ ๊ฒฐ๊ณผ ์์ฝ")
|
100 |
+
st.write(f"์ง๋ณด: {results['์ง๋ณด']}๊ฑด")
|
101 |
+
st.write(f"๋ณด์: {results['๋ณด์']}๊ฑด")
|
102 |
+
st.write(f"์ค๋ฆฝ: {results['์ค๋ฆฝ']}๊ฑด")
|
103 |
+
|
104 |
+
# ํ์ด ์ฐจํธ
|
105 |
+
st.subheader("์ฑํฅ ๋ถํฌ ์ฐจํธ")
|
106 |
+
st.bar_chart(pd.DataFrame.from_dict(results, orient='index', columns=["๊ฑด์"]))
|
107 |
+
|
108 |
+
# ์ธ๋ถ ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
109 |
+
st.subheader("์ธ๋ถ ๊ฒฐ๊ณผ")
|
110 |
+
df = pd.DataFrame(detailed_results)
|
111 |
+
st.dataframe(df)
|
112 |
+
|
113 |
+
# ๋งํฌ ํฌํจํ ๋ด์ค ์ถ๋ ฅ
|
114 |
+
st.subheader("๋ด์ค ๋งํฌ")
|
115 |
+
for index, row in df.iterrows():
|
116 |
+
st.write(f"- [{row['์ ๋ชฉ']}] (์ฑํฅ: {row['์ฑํฅ']}, ์ ์: {row['์ ์']:.2f})")
|
117 |
+
|
118 |
+
except Exception as e:
|
119 |
+
st.error(f"์ค๋ฅ ๋ฐ์: {e}")
|