820nam commited on
Commit
ad7b4a8
·
verified ·
1 Parent(s): 465e649

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -17
app.py CHANGED
@@ -26,28 +26,39 @@ 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 정치 성향 분석 모델 로드 (bucketresearch/politicalBiasBERT)
30
- def load_political_bias_model():
 
 
 
 
 
31
  classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
32
  return classifier
33
 
34
- # Step 3: 정치 성향 분류 함수
35
  def classify_political_sentiment(text, classifier):
 
36
  result = classifier(text[:512]) # 입력이 너무 길면 잘라서 분석
37
- label = result[0]['label']
38
- score = result[0]['score']
39
-
40
- # 'LABEL_0' -> 'left', 'LABEL_1' -> 'center', 'LABEL_2' -> 'right'
41
- if label == "LABEL_0":
42
- orientation = "진보"
43
- elif label == "LABEL_1":
44
- orientation = "중립"
45
- else:
46
- orientation = "보수"
47
 
48
- return orientation, score
 
 
 
 
 
 
 
 
 
49
 
50
- # Step 4: 뉴스 분석 및 결과 출력
51
  def analyze_news_political_orientation(news_items, classifier, translator):
52
  results = {"진보": 0, "보수": 0, "중립": 0}
53
  detailed_results = []
@@ -59,7 +70,6 @@ def analyze_news_political_orientation(news_items, classifier, translator):
59
 
60
  # 번역: 한국어 -> 영어
61
  translated_text = translator(combined_text)[0]['translation_text']
62
- st.write(f"번역된 텍스트: {translated_text}") # 번역 결과 확인
63
 
64
  # 정치 성향 분류
65
  orientation, score = classify_political_sentiment(translated_text, classifier)
@@ -88,7 +98,7 @@ if st.button("분석 시작"):
88
  news_items = news_data["items"]
89
 
90
  # Hugging Face 모델 로드
91
- classifier = load_political_bias_model()
92
  translator = load_translation_model()
93
 
94
  # 뉴스 데이터 분석
 
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("text-classification", model="bucketresearch/politicalBiasBERT")
37
  return classifier
38
 
39
+ # Step 4: 정치 성향 분류 함수
40
  def classify_political_sentiment(text, classifier):
41
+ # 감성 분석 실행
42
  result = classifier(text[:512]) # 입력이 너무 길면 잘라서 분석
43
+ sentiment = result[0]
44
+ label = sentiment["label"]
45
+ score = sentiment["score"]
46
+
47
+ # 점수화
48
+ sentiment_score = score if label == "LABEL_1" else -score
 
 
 
 
49
 
50
+ # 키워드 기반 분류 (진보/보수)
51
+ progressive_keywords = ["복지", "평등", "민주", "환경", "사회적 책임"]
52
+ conservative_keywords = ["안보", "전통", "경제", "성장", "질서", "국방"]
53
+
54
+ if any(keyword in text for keyword in progressive_keywords):
55
+ return "진보", sentiment_score
56
+ elif any(keyword in text for keyword in conservative_keywords):
57
+ return "보수", sentiment_score
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 = []
 
70
 
71
  # 번역: 한국어 -> 영어
72
  translated_text = translator(combined_text)[0]['translation_text']
 
73
 
74
  # 정치 성향 분류
75
  orientation, score = classify_political_sentiment(translated_text, classifier)
 
98
  news_items = news_data["items"]
99
 
100
  # Hugging Face 모델 로드
101
+ classifier = load_huggingface_model()
102
  translator = load_translation_model()
103
 
104
  # 뉴스 데이터 분석