Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,11 @@ import streamlit as st
|
|
| 2 |
import requests
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
from transformers import pipeline
|
| 5 |
-
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
api_key = os.getenv("OPENAI_API_KEY") # Secrets์์ API ํค ๊ฐ์ ธ์ค๊ธฐ
|
| 11 |
-
|
| 12 |
-
# OpenAI ํด๋ผ์ด์ธํธ ์ธ์คํด์ค ์์ฑ
|
| 13 |
-
client = OpenAI(api_key=api_key) # API ํค๋ฅผ ํด๋ผ์ด์ธํธ์ ์ ๋ฌ
|
| 14 |
-
|
| 15 |
-
# ํด๋ผ์ด์ธํธ๋ฅผ ์ฌ์ฉํ์ฌ OpenAI API ํธ์ถ
|
| 16 |
-
def test_openai_api():
|
| 17 |
-
try:
|
| 18 |
-
completion = client.completions.create(
|
| 19 |
-
model="gpt-4",
|
| 20 |
-
messages=[{"role": "user", "content": "Hello world"}]
|
| 21 |
-
)
|
| 22 |
-
return completion.choices[0].text
|
| 23 |
-
except Exception as e:
|
| 24 |
-
return f"Error: {e}"
|
| 25 |
|
| 26 |
# ๋ค์ด๋ฒ ๋ด์ค API๋ฅผ ํตํด ์ค์ ๋ด์ค ๊ธฐ์ฌ ๊ฐ์ ธ์ค๊ธฐ
|
| 27 |
def fetch_naver_news(query, display=5):
|
|
@@ -53,21 +38,20 @@ def load_sentiment_model():
|
|
| 53 |
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
| 54 |
return classifier
|
| 55 |
|
| 56 |
-
# GPT-4๋ฅผ ์ด์ฉํด ๋ฐ๋ ๊ด์ ๊ธฐ์ฌ ์์ฑ
|
| 57 |
# GPT-4๋ฅผ ์ด์ฉํด ๋ฐ๋ ๊ด์ ๊ธฐ์ฌ ์์ฑ
|
| 58 |
def generate_article_gpt4(prompt):
|
| 59 |
try:
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
max_tokens=512, # ์์ฑํ ํ
์คํธ์ ์ต๋ ๊ธธ์ด
|
| 68 |
-
temperature=0.7 #
|
| 69 |
)
|
| 70 |
-
return response
|
| 71 |
except Exception as e:
|
| 72 |
return f"Error generating text: {e}"
|
| 73 |
|
|
|
|
| 2 |
import requests
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
from transformers import pipeline
|
| 5 |
+
import openai
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# OpenAI API ํค ์ค์ (ํ๊ฒฝ๋ณ์์์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ)
|
| 9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# ๋ค์ด๋ฒ ๋ด์ค API๋ฅผ ํตํด ์ค์ ๋ด์ค ๊ธฐ์ฌ ๊ฐ์ ธ์ค๊ธฐ
|
| 12 |
def fetch_naver_news(query, display=5):
|
|
|
|
| 38 |
classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
|
| 39 |
return classifier
|
| 40 |
|
|
|
|
| 41 |
# GPT-4๋ฅผ ์ด์ฉํด ๋ฐ๋ ๊ด์ ๊ธฐ์ฌ ์์ฑ
|
| 42 |
def generate_article_gpt4(prompt):
|
| 43 |
try:
|
| 44 |
+
# GPT-4 ๋ชจ๋ธ์ ์ด์ฉํด ๋ฐ๋ ๊ด์ ๊ธฐ์ฌ๋ฅผ ์์ฑ
|
| 45 |
+
response = openai.ChatCompletion.create(
|
| 46 |
+
model="gpt-4", # GPT-4 ๋ชจ๋ธ์ ์ฌ์ฉ
|
| 47 |
+
messages=[
|
| 48 |
+
{"role": "system", "content": "You are a helpful assistant that generates articles."},
|
| 49 |
+
{"role": "user", "content": prompt} # ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ํ๋กฌํํธ
|
| 50 |
+
],
|
| 51 |
max_tokens=512, # ์์ฑํ ํ
์คํธ์ ์ต๋ ๊ธธ์ด
|
| 52 |
+
temperature=0.7 # ์ฐฝ์์ฑ ์ ๋
|
| 53 |
)
|
| 54 |
+
return response['choices'][0]['message']['content'] # GPT์ ์๋ต ํ
์คํธ ๋ฐํ
|
| 55 |
except Exception as e:
|
| 56 |
return f"Error generating text: {e}"
|
| 57 |
|