Update app.py
Browse files
app.py
CHANGED
@@ -2,11 +2,12 @@ import streamlit as st
|
|
2 |
import requests
|
3 |
import matplotlib.pyplot as plt
|
4 |
from transformers import pipeline
|
5 |
-
import
|
6 |
import os
|
7 |
|
8 |
# OpenAI API 키 설정
|
9 |
-
|
|
|
10 |
|
11 |
# 네이버 뉴스 API를 통해 실제 뉴스 기사 가져오기
|
12 |
def fetch_naver_news(query, display=5):
|
@@ -38,18 +39,19 @@ def load_sentiment_model():
|
|
38 |
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
39 |
return classifier
|
40 |
|
41 |
-
# GPT-4를 이용해 반대 관점 기사 생성
|
42 |
def generate_article_gpt4(prompt):
|
43 |
try:
|
44 |
-
|
|
|
45 |
|
46 |
-
#
|
47 |
response = client.chat.completions.create(
|
48 |
-
model="gpt-4", # GPT-4 모델 사용
|
49 |
messages=[
|
50 |
{"role": "system", "content": "You are a helpful assistant that generates articles."},
|
51 |
{"role": "user", "content": prompt}
|
52 |
],
|
|
|
53 |
max_tokens=512,
|
54 |
temperature=0.7
|
55 |
)
|
|
|
2 |
import requests
|
3 |
import matplotlib.pyplot as plt
|
4 |
from transformers import pipeline
|
5 |
+
from openai import OpenAI # 1.0.0+에서 새로 변경된 임포트 방식
|
6 |
import os
|
7 |
|
8 |
# OpenAI API 키 설정
|
9 |
+
# 환경변수에서 API 키를 가져오기 위해 os.getenv 사용
|
10 |
+
openai.api_key = os.getenv("OPENAI_API_KEY") # Hugging Face Secrets에 API 키를 설정한 경우
|
11 |
|
12 |
# 네이버 뉴스 API를 통해 실제 뉴스 기사 가져오기
|
13 |
def fetch_naver_news(query, display=5):
|
|
|
39 |
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
40 |
return classifier
|
41 |
|
42 |
+
# GPT-4를 이용해 반대 관점 기사 생성
|
43 |
def generate_article_gpt4(prompt):
|
44 |
try:
|
45 |
+
# OpenAI 클라이언트 인스턴스 생성
|
46 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
47 |
|
48 |
+
# GPT-4로 반대 관점 기사 생성
|
49 |
response = client.chat.completions.create(
|
|
|
50 |
messages=[
|
51 |
{"role": "system", "content": "You are a helpful assistant that generates articles."},
|
52 |
{"role": "user", "content": prompt}
|
53 |
],
|
54 |
+
model="gpt-4",
|
55 |
max_tokens=512,
|
56 |
temperature=0.7
|
57 |
)
|