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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -30,24 +30,23 @@ def fetch_naver_news(query, display=10, start=1, sort="date"):
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):
@@ -83,8 +82,11 @@ def analyze_news_political_orientation(news_items, classifier, gpt_model):
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,
 
30
 
31
  # Step 2: GPT ๋ชจ๋ธ ๋กœ๋“œ (์ง„๋ณด์ , ๋ณด์ˆ˜์  ๊ธฐ์‚ฌ ์ƒ์„ฑ)
32
  def load_gpt_model():
33
+ try:
34
+ gpt_model = pipeline("text-generation", model="gpt2") # Hugging Face์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” GPT ๋ชจ๋ธ
35
+ st.write("GPT model loaded successfully.")
36
+ return gpt_model
37
+ except Exception as e:
38
+ st.error(f"Error loading GPT model: {e}")
39
+ return None
40
+
41
+ # Step 3: Hugging Face ์ •์น˜ ์„ฑํ–ฅ ๋ถ„์„ ๋ชจ๋ธ ๋กœ๋“œ
42
+ def load_huggingface_model():
43
+ try:
44
+ classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
45
+ st.write("Political bias model loaded successfully.")
46
+ return classifier
47
+ except Exception as e:
48
+ st.error(f"Error loading political bias model: {e}")
49
+ return None
 
50
 
51
  # Step 4: ์ •์น˜ ์„ฑํ–ฅ ๋ถ„๋ฅ˜ ํ•จ์ˆ˜
52
  def classify_political_sentiment(text, classifier):
 
82
  results[orientation] += 1
83
 
84
  # ์ง„๋ณด์ /๋ณด์ˆ˜์  ๊ธฐ์‚ฌ ์ƒ์„ฑ
85
+ prompt = f"์ง„๋ณด์  ๊ด€์ ์—์„œ ๊ธฐ์‚ฌ๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”: {combined_text}"
86
+ progressive_article = gpt_model(prompt, max_length=512, num_return_sequences=1)[0]['generated_text']
87
+
88
+ prompt = f"๋ณด์ˆ˜์  ๊ด€์ ์—์„œ ๊ธฐ์‚ฌ๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”: {combined_text}"
89
+ conservative_article = gpt_model(prompt, max_length=512, num_return_sequences=1)[0]['generated_text']
90
 
91
  detailed_results.append({
92
  "์ œ๋ชฉ": title,