Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ import openai
|
|
6 |
import pandas as pd
|
7 |
|
8 |
# OpenAI API ํค ์ค์
|
9 |
-
openai.api_key = "
|
10 |
|
11 |
# ๋ค์ด๋ฒ ๋ด์ค API๋ฅผ ํตํด ์ค์ ๋ด์ค ๊ธฐ์ฌ ๊ฐ์ ธ์ค๊ธฐ
|
12 |
def fetch_naver_news(query, display=5):
|
@@ -30,10 +30,11 @@ def fetch_naver_news(query, display=5):
|
|
30 |
news_data = response.json()
|
31 |
return news_data['items'] # ๋ด์ค ๊ธฐ์ฌ ๋ฆฌ์คํธ ๋ฐํ
|
32 |
else:
|
33 |
-
st.error("๋ด์ค ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ๋ฐ ์คํจํ์ต๋๋ค.")
|
34 |
return []
|
35 |
|
36 |
# ์ ์น ์ฑํฅ ๋ถ์ ๋ชจ๋ธ ๋ก๋
|
|
|
37 |
def load_sentiment_model():
|
38 |
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
39 |
return classifier
|
@@ -42,7 +43,7 @@ def load_sentiment_model():
|
|
42 |
def generate_article_gpt4(prompt):
|
43 |
try:
|
44 |
response = openai.ChatCompletion.create(
|
45 |
-
model="gpt-4",
|
46 |
messages=[
|
47 |
{"role": "system", "content": "You are a helpful assistant that generates articles."},
|
48 |
{"role": "user", "content": prompt}
|
@@ -50,7 +51,6 @@ def generate_article_gpt4(prompt):
|
|
50 |
max_tokens=512,
|
51 |
temperature=0.7
|
52 |
)
|
53 |
-
# ์๋ต์์ ํ
์คํธ๋ง ์ถ์ถ
|
54 |
return response['choices'][0]['message']['content']
|
55 |
except Exception as e:
|
56 |
return f"Error generating text: {e}"
|
@@ -78,7 +78,7 @@ def analyze_news_political_viewpoint(query):
|
|
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,7 +87,7 @@ def analyze_news_political_viewpoint(query):
|
|
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,7 +104,6 @@ def analyze_news_political_viewpoint(query):
|
|
104 |
|
105 |
return results, sentiment_counts
|
106 |
|
107 |
-
|
108 |
# ์ฑํฅ ๋ถํฌ ์๊ฐํ
|
109 |
def visualize_sentiment_distribution(sentiment_counts):
|
110 |
fig, ax = plt.subplots()
|
|
|
6 |
import pandas as pd
|
7 |
|
8 |
# OpenAI API ํค ์ค์
|
9 |
+
openai.api_key = "your_openai_api_key"
|
10 |
|
11 |
# ๋ค์ด๋ฒ ๋ด์ค API๋ฅผ ํตํด ์ค์ ๋ด์ค ๊ธฐ์ฌ ๊ฐ์ ธ์ค๊ธฐ
|
12 |
def fetch_naver_news(query, display=5):
|
|
|
30 |
news_data = response.json()
|
31 |
return news_data['items'] # ๋ด์ค ๊ธฐ์ฌ ๋ฆฌ์คํธ ๋ฐํ
|
32 |
else:
|
33 |
+
st.error(f"๋ด์ค ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ๋ฐ ์คํจํ์ต๋๋ค. (์ฝ๋: {response.status_code})")
|
34 |
return []
|
35 |
|
36 |
# ์ ์น ์ฑํฅ ๋ถ์ ๋ชจ๋ธ ๋ก๋
|
37 |
+
@st.cache_resource
|
38 |
def load_sentiment_model():
|
39 |
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
40 |
return classifier
|
|
|
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."},
|
49 |
{"role": "user", "content": prompt}
|
|
|
51 |
max_tokens=512,
|
52 |
temperature=0.7
|
53 |
)
|
|
|
54 |
return response['choices'][0]['message']['content']
|
55 |
except Exception as e:
|
56 |
return f"Error generating text: {e}"
|
|
|
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 |
def visualize_sentiment_distribution(sentiment_counts):
|
109 |
fig, ax = plt.subplots()
|