820nam commited on
Commit
1ae5cf1
ยท
verified ยท
1 Parent(s): 242b668

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -7
app.py CHANGED
@@ -6,7 +6,7 @@ import openai
6
  import pandas as pd
7
 
8
  # OpenAI API ํ‚ค ์„ค์ •
9
- openai.api_key = "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"
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", # 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()