820nam commited on
Commit
95eccad
ยท
verified ยท
1 Parent(s): 926f5e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -16
app.py CHANGED
@@ -2,7 +2,6 @@ import streamlit as st
2
  import requests
3
  from transformers import pipeline
4
  import pandas as pd
5
- import matplotlib.pyplot as plt
6
 
7
  # Step 1: ๋„ค์ด๋ฒ„ ๋‰ด์Šค API ํ˜ธ์ถœ ํ•จ์ˆ˜
8
  def fetch_naver_news(query, display=10, start=1, sort="date"):
@@ -29,21 +28,26 @@ def fetch_naver_news(query, display=10, start=1, sort="date"):
29
  st.error(f"Error: {response.status_code}, {response.text}")
30
  return None
31
 
32
- # Step 2: Hugging Face ์ •์น˜ ์„ฑํ–ฅ ๋ถ„์„ ๋ชจ๋ธ ๋กœ๋“œ
33
- def load_huggingface_model():
34
- classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
35
- return classifier
 
36
 
37
- # Step 3: ๊ธฐ์‚ฌ ์ƒ์„ฑ ๋ชจ๋ธ (์ง„๋ณด์ /๋ณด์ˆ˜์  ๊ธฐ์‚ฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋ธ)
38
  def generate_article_by_perspective(text, perspective="์ง„๋ณด"):
39
- # ์ง„๋ณด์ , ๋ณด์ˆ˜์  ๊ธฐ์‚ฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ…œํ”Œ๋ฆฟ
40
  if perspective == "์ง„๋ณด":
41
- # ์ง„๋ณด์  ๊ด€์ ์œผ๋กœ ๊ธฐ์‚ฌ ์žฌ๊ตฌ์„ฑ
42
- return f"์ง„๋ณด์  ๊ด€์ ์—์„œ ๋‹ค๋ฃฌ ๊ธฐ์‚ฌ: {text} ... (์ง„๋ณด์  ์‹œ๊ฐ์œผ๋กœ ๋ณ€๊ฒฝ๋จ)"
43
  elif perspective == "๋ณด์ˆ˜":
44
- # ๋ณด์ˆ˜์  ๊ด€์ ์œผ๋กœ ๊ธฐ์‚ฌ ์žฌ๊ตฌ์„ฑ
45
- return f"๋ณด์ˆ˜์  ๊ด€์ ์—์„œ ๋‹ค๋ฃฌ ๊ธฐ์‚ฌ: {text} ... (๋ณด์ˆ˜์  ์‹œ๊ฐ์œผ๋กœ ๋ณ€๊ฒฝ๋จ)"
46
- return text
 
 
 
 
47
 
48
  # Step 4: ์ •์น˜ ์„ฑํ–ฅ ๋ถ„๋ฅ˜ ํ•จ์ˆ˜
49
  def classify_political_sentiment(text, classifier):
@@ -65,7 +69,7 @@ def classify_political_sentiment(text, classifier):
65
  return "์ค‘๋ฆฝ", sentiment_score
66
 
67
  # Step 5: ๋‰ด์Šค ๋ถ„์„ ๋ฐ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
68
- def analyze_news_political_orientation(news_items, classifier):
69
  results = {"์ง„๋ณด": 0, "๋ณด์ˆ˜": 0, "์ค‘๋ฆฝ": 0}
70
  detailed_results = []
71
 
@@ -77,18 +81,24 @@ def analyze_news_political_orientation(news_items, classifier):
77
  # ์ •์น˜ ์„ฑํ–ฅ ๋ถ„๋ฅ˜
78
  orientation, score = classify_political_sentiment(combined_text, classifier)
79
  results[orientation] += 1
 
 
 
 
 
80
  detailed_results.append({
81
  "์ œ๋ชฉ": title,
82
  "์š”์•ฝ": description,
83
  "์„ฑํ–ฅ": orientation,
84
  "์ ์ˆ˜": score,
85
  "๋งํฌ": item["link"],
86
- "์ง„๋ณด์  ๊ธฐ์‚ฌ": generate_article_by_perspective(combined_text, "์ง„๋ณด"),
87
- "๋ณด์ˆ˜์  ๊ธฐ์‚ฌ": generate_article_by_perspective(combined_text, "๋ณด์ˆ˜"),
88
  })
89
 
90
  return results, detailed_results
91
 
 
92
  # Step 6: Streamlit ์•ฑ ์‹œ์ž‘
93
  st.title("์ •์น˜ ์„ฑํ–ฅ ๋ถ„์„ ๋Œ€์‹œ๋ณด๋“œ")
94
  st.markdown("### ๋„ค์ด๋ฒ„ ๋‰ด์Šค ๋ฐ์ดํ„ฐ๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ˆ˜์ง‘ํ•˜๊ณ  ์ •์น˜ ์„ฑํ–ฅ์„ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค.")
@@ -109,9 +119,10 @@ if st.button("๋ถ„์„ ์‹œ์ž‘"):
109
 
110
  # Hugging Face ๋ชจ๋ธ ๋กœ๋“œ
111
  classifier = load_huggingface_model()
 
112
 
113
  # ๋‰ด์Šค ๋ฐ์ดํ„ฐ ๋ถ„์„
114
- results, detailed_results = analyze_news_political_orientation(news_items, classifier)
115
 
116
  # ๋ถ„์„ ๊ฒฐ๊ณผ ์‹œ๊ฐํ™”
117
  st.subheader("๋ถ„์„ ๊ฒฐ๊ณผ ์š”์•ฝ")
 
2
  import requests
3
  from transformers import pipeline
4
  import pandas as pd
 
5
 
6
  # Step 1: ๋„ค์ด๋ฒ„ ๋‰ด์Šค API ํ˜ธ์ถœ ํ•จ์ˆ˜
7
  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: GPT ๋ชจ๋ธ ๋กœ๋“œ (์ง„๋ณด์ , ๋ณด์ˆ˜์  ๊ธฐ์‚ฌ ์ƒ์„ฑ)
32
+ def load_gpt_model():
33
+ # GPT ๋ชจ๋ธ ๋กœ๋“œ (์ด ๋ชจ๋ธ์€ ํ…์ŠคํŠธ ์ƒ์„ฑ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค)
34
+ gpt_model = pipeline("text-generation", model="gpt2") # Hugging Face์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” GPT ๋ชจ๋ธ
35
+ return gpt_model
36
 
37
+ # Step 3: ๊ธฐ์‚ฌ๋ฅผ ์ง„๋ณด์  ๋ฐ ๋ณด์ˆ˜์  ๊ด€์ ์—์„œ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜
38
  def generate_article_by_perspective(text, perspective="์ง„๋ณด"):
39
+ # ์ง„๋ณด์  ๊ด€์  ์ƒ์„ฑ
40
  if perspective == "์ง„๋ณด":
41
+ prompt = f"์ง„๋ณด์  ๊ด€์ ์—์„œ ๋‹ค์Œ ๊ธฐ์‚ฌ๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”: {text}. ํ™˜๊ฒฝ๊ณผ ์‚ฌํšŒ์  ์ฑ…์ž„์„ ๊ฐ•์กฐํ•˜๋ฉฐ..."
42
+ # ๋ณด์ˆ˜์  ๊ด€์  ์ƒ์„ฑ
43
  elif perspective == "๋ณด์ˆ˜":
44
+ prompt = f"๋ณด์ˆ˜์  ๊ด€์ ์—์„œ ๋‹ค์Œ ๊ธฐ์‚ฌ๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”: {text}. ๊ฒฝ์ œ ์„ฑ์žฅ๊ณผ ๊ตญ๊ฐ€ ์•ˆ๋ณด๋ฅผ ๊ฐ•์กฐํ•˜๋ฉฐ..."
45
+ else:
46
+ prompt = text
47
+
48
+ # GPT ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ์ƒ์„ฑ
49
+ generated_article = gpt_model(prompt, max_length=512, num_return_sequences=1)[0]['generated_text']
50
+ return generated_article
51
 
52
  # Step 4: ์ •์น˜ ์„ฑํ–ฅ ๋ถ„๋ฅ˜ ํ•จ์ˆ˜
53
  def classify_political_sentiment(text, classifier):
 
69
  return "์ค‘๋ฆฝ", sentiment_score
70
 
71
  # Step 5: ๋‰ด์Šค ๋ถ„์„ ๋ฐ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
72
+ def analyze_news_political_orientation(news_items, classifier, gpt_model):
73
  results = {"์ง„๋ณด": 0, "๋ณด์ˆ˜": 0, "์ค‘๋ฆฝ": 0}
74
  detailed_results = []
75
 
 
81
  # ์ •์น˜ ์„ฑํ–ฅ ๋ถ„๋ฅ˜
82
  orientation, score = classify_political_sentiment(combined_text, classifier)
83
  results[orientation] += 1
84
+
85
+ # ์ง„๋ณด์ /๋ณด์ˆ˜์  ๊ธฐ์‚ฌ ์ƒ์„ฑ
86
+ progressive_article = generate_article_by_perspective(combined_text, "์ง„๋ณด")
87
+ conservative_article = generate_article_by_perspective(combined_text, "๋ณด์ˆ˜")
88
+
89
  detailed_results.append({
90
  "์ œ๋ชฉ": title,
91
  "์š”์•ฝ": description,
92
  "์„ฑํ–ฅ": orientation,
93
  "์ ์ˆ˜": score,
94
  "๋งํฌ": item["link"],
95
+ "์ง„๋ณด์  ๊ธฐ์‚ฌ": progressive_article,
96
+ "๋ณด์ˆ˜์  ๊ธฐ์‚ฌ": conservative_article
97
  })
98
 
99
  return results, detailed_results
100
 
101
+
102
  # Step 6: Streamlit ์•ฑ ์‹œ์ž‘
103
  st.title("์ •์น˜ ์„ฑํ–ฅ ๋ถ„์„ ๋Œ€์‹œ๋ณด๋“œ")
104
  st.markdown("### ๋„ค์ด๋ฒ„ ๋‰ด์Šค ๋ฐ์ดํ„ฐ๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ˆ˜์ง‘ํ•˜๊ณ  ์ •์น˜ ์„ฑํ–ฅ์„ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค.")
 
119
 
120
  # Hugging Face ๋ชจ๋ธ ๋กœ๋“œ
121
  classifier = load_huggingface_model()
122
+ gpt_model = load_gpt_model()
123
 
124
  # ๋‰ด์Šค ๋ฐ์ดํ„ฐ ๋ถ„์„
125
+ results, detailed_results = analyze_news_political_orientation(news_items, classifier, gpt_model)
126
 
127
  # ๋ถ„์„ ๊ฒฐ๊ณผ ์‹œ๊ฐํ™”
128
  st.subheader("๋ถ„์„ ๊ฒฐ๊ณผ ์š”์•ฝ")