820nam commited on
Commit
8b3a49e
ยท
verified ยท
1 Parent(s): febef4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
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", "your_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(f"๋‰ด์Šค ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. (์ฝ”๋“œ: {response.status_code})")
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
- client = openai.OpenAI()
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
+