820nam commited on
Commit
0d5a75b
ยท
verified ยท
1 Parent(s): 74f08a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -21
app.py CHANGED
@@ -28,22 +28,12 @@ def fetch_naver_news(query, display=10, start=1, sort="date"):
28
  st.error(f"Error: {response.status_code}, {response.text}")
29
  return None
30
 
31
- # Step 2: Hugging Face ๋ฒˆ์—ญ ๋ชจ๋ธ ๋กœ๋“œ (ํ•œ๊ตญ์–ด -> ์˜์–ด)
32
- def load_translation_model():
33
- try:
34
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
35
- st.write("Translation model loaded successfully.")
36
- return translator
37
- except Exception as e:
38
- st.error(f"Error loading translation model: {e}")
39
- return None
40
-
41
- # Step 3: Hugging Face ์ •์น˜ ์„ฑํ–ฅ ๋ถ„์„ ๋ชจ๋ธ ๋กœ๋“œ
42
  def load_huggingface_model():
43
  classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
44
  return classifier
45
 
46
- # Step 4: ์ •์น˜ ์„ฑํ–ฅ ๋ถ„๋ฅ˜ ํ•จ์ˆ˜
47
  def classify_political_sentiment(text, classifier):
48
  result = classifier(text[:512]) # ์ž…๋ ฅ์ด ๋„ˆ๋ฌด ๊ธธ๋ฉด ์ž˜๋ผ์„œ ๋ถ„์„
49
  sentiment = result[0]
@@ -52,6 +42,7 @@ def classify_political_sentiment(text, classifier):
52
 
53
  sentiment_score = score if label == "POSITIVE" else -score
54
 
 
55
  progressive_keywords = ["๋ณต์ง€", "ํ‰๋“ฑ", "๋ฏผ์ฃผ", "ํ™˜๊ฒฝ", "์‚ฌํšŒ์  ์ฑ…์ž„"]
56
  conservative_keywords = ["์•ˆ๋ณด", "์ „ํ†ต", "๊ฒฝ์ œ", "์„ฑ์žฅ", "์งˆ์„œ", "๊ตญ๋ฐฉ"]
57
 
@@ -62,8 +53,8 @@ def classify_political_sentiment(text, classifier):
62
  else:
63
  return "์ค‘๋ฆฝ", sentiment_score
64
 
65
- # Step 5: ๋‰ด์Šค ๋ถ„์„ ๋ฐ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
66
- def analyze_news_political_orientation(news_items, classifier, translator):
67
  results = {"์ง„๋ณด": 0, "๋ณด์ˆ˜": 0, "์ค‘๋ฆฝ": 0}
68
  detailed_results = []
69
 
@@ -72,12 +63,8 @@ def analyze_news_political_orientation(news_items, classifier, translator):
72
  description = item["description"]
73
  combined_text = f"{title}. {description}"
74
 
75
- # ๋ฒˆ์—ญ: ํ•œ๊ตญ์–ด -> ์˜์–ด
76
- translated_text = translator(combined_text)[0]['translation_text']
77
- st.write("Translated Text:", translated_text) # ๋ฒˆ์—ญ๋œ ํ…์ŠคํŠธ ํ™•์ธ
78
-
79
  # ์ •์น˜ ์„ฑํ–ฅ ๋ถ„๋ฅ˜
80
- orientation, score = classify_political_sentiment(translated_text, classifier)
81
  results[orientation] += 1
82
  detailed_results.append({
83
  "์ œ๋ชฉ": title,
@@ -110,10 +97,9 @@ if st.button("๋ถ„์„ ์‹œ์ž‘"):
110
 
111
  # Hugging Face ๋ชจ๋ธ ๋กœ๋“œ
112
  classifier = load_huggingface_model()
113
- translator = load_translation_model()
114
 
115
  # ๋‰ด์Šค ๋ฐ์ดํ„ฐ ๋ถ„์„
116
- results, detailed_results = analyze_news_political_orientation(news_items, classifier, translator)
117
 
118
  # ๋ถ„์„ ๊ฒฐ๊ณผ ์‹œ๊ฐํ™”
119
  st.subheader("๋ถ„์„ ๊ฒฐ๊ณผ ์š”์•ฝ")
 
28
  st.error(f"Error: {response.status_code}, {response.text}")
29
  return None
30
 
31
+ # Step 2: Hugging Face ์ •์น˜ ์„ฑํ–ฅ ๋ถ„์„ ๋ชจ๋ธ ๋กœ๋“œ
 
 
 
 
 
 
 
 
 
 
32
  def load_huggingface_model():
33
  classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
34
  return classifier
35
 
36
+ # Step 3: ์ •์น˜ ์„ฑํ–ฅ ๋ถ„๋ฅ˜ ํ•จ์ˆ˜
37
  def classify_political_sentiment(text, classifier):
38
  result = classifier(text[:512]) # ์ž…๋ ฅ์ด ๋„ˆ๋ฌด ๊ธธ๋ฉด ์ž˜๋ผ์„œ ๋ถ„์„
39
  sentiment = result[0]
 
42
 
43
  sentiment_score = score if label == "POSITIVE" else -score
44
 
45
+ # ํ‚ค์›Œ๋“œ ๊ธฐ๋ฐ˜ ๋ถ„๋ฅ˜ (์ง„๋ณด/๋ณด์ˆ˜)
46
  progressive_keywords = ["๋ณต์ง€", "ํ‰๋“ฑ", "๋ฏผ์ฃผ", "ํ™˜๊ฒฝ", "์‚ฌํšŒ์  ์ฑ…์ž„"]
47
  conservative_keywords = ["์•ˆ๋ณด", "์ „ํ†ต", "๊ฒฝ์ œ", "์„ฑ์žฅ", "์งˆ์„œ", "๊ตญ๋ฐฉ"]
48
 
 
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
 
 
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,
 
97
 
98
  # Hugging Face ๋ชจ๋ธ ๋กœ๋“œ
99
  classifier = load_huggingface_model()
 
100
 
101
  # ๋‰ด์Šค ๋ฐ์ดํ„ฐ ๋ถ„์„
102
+ results, detailed_results = analyze_news_political_orientation(news_items, classifier)
103
 
104
  # ๋ถ„์„ ๊ฒฐ๊ณผ ์‹œ๊ฐํ™”
105
  st.subheader("๋ถ„์„ ๊ฒฐ๊ณผ ์š”์•ฝ")