820nam commited on
Commit
4f28bae
ยท
verified ยท
1 Parent(s): 384f5e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -6
app.py CHANGED
@@ -26,12 +26,17 @@ def fetch_naver_news(query, display=10, start=1, sort="date"):
26
  else:
27
  raise Exception(f"Error: {response.status_code}, {response.text}")
28
 
29
- # Step 2: Hugging Face ๊ฐ์„ฑ ๋ถ„์„ ๋ชจ๋ธ ๋กœ๋“œ
 
 
 
 
 
30
  def load_huggingface_model():
31
  classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
32
  return classifier
33
 
34
- # Step 3: ์ •์น˜ ์„ฑํ–ฅ ๋ถ„๋ฅ˜ ํ•จ์ˆ˜
35
  def classify_political_sentiment(text, classifier):
36
  # ๊ฐ์„ฑ ๋ถ„์„ ์‹คํ–‰
37
  result = classifier(text[:512]) # ์ž…๋ ฅ์ด ๋„ˆ๋ฌด ๊ธธ๋ฉด ์ž˜๋ผ์„œ ๋ถ„์„
@@ -53,8 +58,8 @@ def classify_political_sentiment(text, classifier):
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,8 +68,11 @@ def analyze_news_political_orientation(news_items, classifier):
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,
@@ -91,9 +99,10 @@ if st.button("๋ถ„์„ ์‹œ์ž‘"):
91
 
92
  # Hugging Face ๋ชจ๋ธ ๋กœ๋“œ
93
  classifier = load_huggingface_model()
 
94
 
95
  # ๋‰ด์Šค ๋ฐ์ดํ„ฐ ๋ถ„์„
96
- results, detailed_results = analyze_news_political_orientation(news_items, classifier)
97
 
98
  # ๋ถ„์„ ๊ฒฐ๊ณผ ์‹œ๊ฐํ™”
99
  st.subheader("๋ถ„์„ ๊ฒฐ๊ณผ ์š”์•ฝ")
 
26
  else:
27
  raise Exception(f"Error: {response.status_code}, {response.text}")
28
 
29
+ # Step 2: Hugging Face ๋ฒˆ์—ญ ๋ชจ๋ธ ๋กœ๋“œ (ํ•œ๊ตญ์–ด -> ์˜์–ด)
30
+ def load_translation_model():
31
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
32
+ return translator
33
+
34
+ # Step 3: Hugging Face ๊ฐ์„ฑ ๋ถ„์„ ๋ชจ๋ธ ๋กœ๋“œ
35
  def load_huggingface_model():
36
  classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
37
  return classifier
38
 
39
+ # Step 4: ์ •์น˜ ์„ฑํ–ฅ ๋ถ„๋ฅ˜ ํ•จ์ˆ˜
40
  def classify_political_sentiment(text, classifier):
41
  # ๊ฐ์„ฑ ๋ถ„์„ ์‹คํ–‰
42
  result = classifier(text[:512]) # ์ž…๋ ฅ์ด ๋„ˆ๋ฌด ๊ธธ๋ฉด ์ž˜๋ผ์„œ ๋ถ„์„
 
58
  else:
59
  return "์ค‘๋ฆฝ", sentiment_score
60
 
61
+ # Step 5: ๋‰ด์Šค ๋ถ„์„ ๋ฐ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
62
+ def analyze_news_political_orientation(news_items, classifier, translator):
63
  results = {"์ง„๋ณด": 0, "๋ณด์ˆ˜": 0, "์ค‘๋ฆฝ": 0}
64
  detailed_results = []
65
 
 
68
  description = item["description"]
69
  combined_text = f"{title}. {description}"
70
 
71
+ # ๋ฒˆ์—ญ: ํ•œ๊ตญ์–ด -> ์˜์–ด
72
+ translated_text = translator(combined_text)[0]['translation_text']
73
+
74
  # ์ •์น˜ ์„ฑํ–ฅ ๋ถ„๋ฅ˜
75
+ orientation, score = classify_political_sentiment(translated_text, classifier)
76
  results[orientation] += 1
77
  detailed_results.append({
78
  "์ œ๋ชฉ": title,
 
99
 
100
  # Hugging Face ๋ชจ๋ธ ๋กœ๋“œ
101
  classifier = load_huggingface_model()
102
+ translator = load_translation_model()
103
 
104
  # ๋‰ด์Šค ๋ฐ์ดํ„ฐ ๋ถ„์„
105
+ results, detailed_results = analyze_news_political_orientation(news_items, classifier, translator)
106
 
107
  # ๋ถ„์„ ๊ฒฐ๊ณผ ์‹œ๊ฐํ™”
108
  st.subheader("๋ถ„์„ ๊ฒฐ๊ณผ ์š”์•ฝ")