Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ import pandas as pd
|
|
7 |
import os
|
8 |
|
9 |
# OpenAI API ํค ์ค์
|
10 |
-
openai.api_key = os.getenv("OPENAI_API_KEY", "
|
11 |
|
12 |
# ๋ค์ด๋ฒ ๋ด์ค API๋ฅผ ํตํด ์ค์ ๋ด์ค ๊ธฐ์ฌ ๊ฐ์ ธ์ค๊ธฐ
|
13 |
def fetch_naver_news(query, display=5):
|
@@ -31,11 +31,10 @@ def fetch_naver_news(query, display=5):
|
|
31 |
news_data = response.json()
|
32 |
return news_data['items'] # ๋ด์ค ๊ธฐ์ฌ ๋ฆฌ์คํธ ๋ฐํ
|
33 |
else:
|
34 |
-
st.error(
|
35 |
return []
|
36 |
|
37 |
# ์ ์น ์ฑํฅ ๋ถ์ ๋ชจ๋ธ ๋ก๋
|
38 |
-
@st.cache_resource
|
39 |
def load_sentiment_model():
|
40 |
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
41 |
return classifier
|
@@ -43,8 +42,7 @@ def load_sentiment_model():
|
|
43 |
# GPT-4๋ฅผ ์ด์ฉํด ๋ฐ๋ ๊ด์ ๊ธฐ์ฌ ์์ฑ
|
44 |
def generate_article_gpt4(prompt):
|
45 |
try:
|
46 |
-
|
47 |
-
response = client.chat.completions.create(
|
48 |
model="gpt-4",
|
49 |
messages=[
|
50 |
{"role": "system", "content": "You are a helpful assistant that generates articles."},
|
@@ -80,7 +78,7 @@ def analyze_news_political_viewpoint(query):
|
|
80 |
|
81 |
classifier = load_sentiment_model()
|
82 |
results = []
|
83 |
-
sentiment_counts = {"์ง๋ณด": 0, "๋ณด์": 0, "์ค๋ฆฝ": 0}
|
84 |
|
85 |
for item in news_items:
|
86 |
title = item["title"]
|
@@ -89,7 +87,7 @@ def analyze_news_political_viewpoint(query):
|
|
89 |
|
90 |
# ๊ธฐ์ฌ ์ฑํฅ ๋ถ์
|
91 |
sentiment, score = analyze_article_sentiment(combined_text, classifier)
|
92 |
-
sentiment_counts[sentiment] += 1
|
93 |
|
94 |
# ๋ฐ๋ ๊ด์ ๊ธฐ์ฌ ์์ฑ
|
95 |
opposite_perspective = "๋ณด์์ " if sentiment == "์ง๋ณด" else "์ง๋ณด์ "
|
@@ -106,6 +104,7 @@ def analyze_news_political_viewpoint(query):
|
|
106 |
|
107 |
return results, sentiment_counts
|
108 |
|
|
|
109 |
# ์ฑํฅ ๋ถํฌ ์๊ฐํ
|
110 |
def visualize_sentiment_distribution(sentiment_counts):
|
111 |
fig, ax = plt.subplots()
|
@@ -141,3 +140,4 @@ if st.button("๋ถ์ ์์"):
|
|
141 |
st.write("---")
|
142 |
else:
|
143 |
st.error("๋ถ์๋ ๋ด์ค ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.")
|
|
|
|
7 |
import os
|
8 |
|
9 |
# OpenAI API ํค ์ค์
|
10 |
+
openai.api_key = os.getenv("OPENAI_API_KEY", "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") # ํ๊ฒฝ ๋ณ์ ๋๋ ์ง์ ํค ์
๋ ฅ
|
11 |
|
12 |
# ๋ค์ด๋ฒ ๋ด์ค API๋ฅผ ํตํด ์ค์ ๋ด์ค ๊ธฐ์ฌ ๊ฐ์ ธ์ค๊ธฐ
|
13 |
def fetch_naver_news(query, display=5):
|
|
|
31 |
news_data = response.json()
|
32 |
return news_data['items'] # ๋ด์ค ๊ธฐ์ฌ ๋ฆฌ์คํธ ๋ฐํ
|
33 |
else:
|
34 |
+
st.error("๋ด์ค ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ๋ฐ ์คํจํ์ต๋๋ค.")
|
35 |
return []
|
36 |
|
37 |
# ์ ์น ์ฑํฅ ๋ถ์ ๋ชจ๋ธ ๋ก๋
|
|
|
38 |
def load_sentiment_model():
|
39 |
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
40 |
return classifier
|
|
|
42 |
# GPT-4๋ฅผ ์ด์ฉํด ๋ฐ๋ ๊ด์ ๊ธฐ์ฌ ์์ฑ
|
43 |
def generate_article_gpt4(prompt):
|
44 |
try:
|
45 |
+
response = openai.ChatCompletion.create(
|
|
|
46 |
model="gpt-4",
|
47 |
messages=[
|
48 |
{"role": "system", "content": "You are a helpful assistant that generates articles."},
|
|
|
78 |
|
79 |
classifier = load_sentiment_model()
|
80 |
results = []
|
81 |
+
sentiment_counts = {"์ง๋ณด": 0, "๋ณด์": 0, "์ค๋ฆฝ": 0} # ๋งคํ๋ ๋ผ๋ฒจ์ ๋ง๊ฒ ์ด๊ธฐํ
|
82 |
|
83 |
for item in news_items:
|
84 |
title = item["title"]
|
|
|
87 |
|
88 |
# ๊ธฐ์ฌ ์ฑํฅ ๋ถ์
|
89 |
sentiment, score = analyze_article_sentiment(combined_text, classifier)
|
90 |
+
sentiment_counts[sentiment] += 1 # ๋งคํ๋ ํค๋ก ์นด์ดํธ ์ฆ๊ฐ
|
91 |
|
92 |
# ๋ฐ๋ ๊ด์ ๊ธฐ์ฌ ์์ฑ
|
93 |
opposite_perspective = "๋ณด์์ " if sentiment == "์ง๋ณด" else "์ง๋ณด์ "
|
|
|
104 |
|
105 |
return results, sentiment_counts
|
106 |
|
107 |
+
|
108 |
# ์ฑํฅ ๋ถํฌ ์๊ฐํ
|
109 |
def visualize_sentiment_distribution(sentiment_counts):
|
110 |
fig, ax = plt.subplots()
|
|
|
140 |
st.write("---")
|
141 |
else:
|
142 |
st.error("๋ถ์๋ ๋ด์ค ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.")
|
143 |
+
|