Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +22 -19
src/streamlit_app.py
CHANGED
|
@@ -11,7 +11,7 @@ from collections import Counter
|
|
| 11 |
import json
|
| 12 |
import os
|
| 13 |
from datetime import datetime, timedelta
|
| 14 |
-
import
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
import traceback
|
| 17 |
import plotly.graph_objects as go
|
|
@@ -50,14 +50,12 @@ load_dotenv() # .env ํ์ผ์์ ๋ก๋ ์๋
|
|
| 50 |
# 1. ํ๊ฒฝ ๋ณ์์์ API ํค ํ์ธ
|
| 51 |
if os.environ.get('OPENAI_API_KEY'):
|
| 52 |
st.session_state.openai_api_key = os.environ.get('OPENAI_API_KEY')
|
| 53 |
-
openai.api_key = st.session_state.openai_api_key
|
| 54 |
|
| 55 |
# 2. Streamlit secrets์์ API ํค ํ์ธ (try-except๋ก ์ค๋ฅ ๋ฐฉ์ง)
|
| 56 |
if not st.session_state.openai_api_key:
|
| 57 |
try:
|
| 58 |
if 'OPENAI_API_KEY' in st.secrets:
|
| 59 |
st.session_state.openai_api_key = st.secrets['OPENAI_API_KEY']
|
| 60 |
-
openai.api_key = st.session_state.openai_api_key
|
| 61 |
except Exception as e:
|
| 62 |
pass # secrets ํ์ผ์ด ์์ด๋ ์ค๋ฅ ๋ฐ์ํ์ง ์์
|
| 63 |
|
|
@@ -91,8 +89,12 @@ with st.sidebar:
|
|
| 91 |
api_key = st.text_input("OpenAI API ํค ์
๋ ฅ", type="password")
|
| 92 |
if api_key:
|
| 93 |
st.session_state.openai_api_key = api_key
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# ์ ์ฅ๋ ๊ธฐ์ฌ๋ฅผ ๋ถ๋ฌ์ค๋ ํจ์
|
| 98 |
def load_saved_articles():
|
|
@@ -335,36 +337,39 @@ def analyze_news_content(news_df):
|
|
| 335 |
results['top_keywords'] = []
|
| 336 |
return results
|
| 337 |
|
| 338 |
-
# OpenAI API๋ฅผ ์ด์ฉํ ์ ๊ธฐ์ฌ ์์ฑ (
|
| 339 |
def generate_article(original_content, prompt_text):
|
| 340 |
try:
|
| 341 |
if not st.session_state.openai_api_key:
|
| 342 |
return "OpenAI API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
| 343 |
|
| 344 |
-
|
| 345 |
-
|
|
|
|
| 346 |
messages=[
|
| 347 |
{"role": "system", "content": "๋น์ ์ ์ ๋ฌธ์ ์ธ ๋ด์ค ๊ธฐ์์
๋๋ค. ์ฃผ์ด์ง ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์๋ก์ด ๊ธฐ์ฌ๋ฅผ ์์ฑํด์ฃผ์ธ์."},
|
| 348 |
{"role": "user", "content": f"๋ค์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก {prompt_text}\n\n{original_content[:1000]}"}
|
| 349 |
],
|
| 350 |
max_tokens=2000
|
| 351 |
)
|
| 352 |
-
return response.choices[0].message
|
| 353 |
except Exception as e:
|
| 354 |
return f"๊ธฐ์ฌ ์์ฑ ์ค๋ฅ: {str(e)}"
|
| 355 |
|
| 356 |
-
# OpenAI API๋ฅผ ์ด์ฉํ ์ด๋ฏธ์ง ์์ฑ (
|
| 357 |
def generate_image(prompt):
|
| 358 |
try:
|
| 359 |
if not st.session_state.openai_api_key:
|
| 360 |
return "OpenAI API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
| 361 |
|
| 362 |
-
|
|
|
|
|
|
|
| 363 |
prompt=prompt,
|
| 364 |
n=1,
|
| 365 |
size="1024x1024"
|
| 366 |
)
|
| 367 |
-
return response
|
| 368 |
except Exception as e:
|
| 369 |
return f"์ด๋ฏธ์ง ์์ฑ ์ค๋ฅ: {str(e)}"
|
| 370 |
|
|
@@ -645,9 +650,9 @@ elif menu == "๊ธฐ์ฌ ๋ถ์ํ๊ธฐ":
|
|
| 645 |
if st.session_state.openai_api_key:
|
| 646 |
with st.spinner("๊ธฐ์ฌ์ ๊ฐ์ ์ ๋ถ์ ์ค์
๋๋ค..."):
|
| 647 |
try:
|
| 648 |
-
|
| 649 |
-
response =
|
| 650 |
-
model="gpt-4
|
| 651 |
messages=[
|
| 652 |
{"role": "system", "content": "๋น์ ์ ํ
์คํธ์ ๊ฐ์ ๊ณผ ๋
ผ์กฐ๋ฅผ ๋ถ์ํ๋ ์ ๋ฌธ๊ฐ์
๋๋ค. ๋ค์ ๋ด์ค ๊ธฐ์ฌ์ ๊ฐ์ ๊ณผ ๋
ผ์กฐ๋ฅผ ๋ถ์ํ๊ณ , '๊ธ์ ์ ', '๋ถ์ ์ ', '์ค๋ฆฝ์ ' ์ค ํ๋๋ก ๋ถ๋ฅํด ์ฃผ์ธ์. ๋ํ ๊ธฐ์ฌ์์ ๋๋ฌ๋๋ ํต์ฌ ๊ฐ์ ํค์๋๋ฅผ 5๊ฐ ์ถ์ถํ๊ณ , ๊ฐ ํค์๋๋ณ๋ก 1-10 ์ฌ์ด์ ๊ฐ๋ ์ ์๋ฅผ ๋งค๊ฒจ์ฃผ์ธ์. JSON ํ์์ผ๋ก ๋ค์๊ณผ ๊ฐ์ด ์๋ตํด์ฃผ์ธ์: {'sentiment': '๊ธ์ ์ /๋ถ์ ์ /์ค๋ฆฝ์ ', 'reason': '์ด์ ์ค๋ช
...', 'keywords': [{'word': 'ํค์๋1', 'score': 8}, {'word': 'ํค์๋2', 'score': 7}, ...]}"},
|
| 653 |
{"role": "user", "content": f"๋ค์ ๋ด์ค ๊ธฐ์ฌ๋ฅผ ๋ถ์ํด ์ฃผ์ธ์:\n\n์ ๋ชฉ: {selected_article['title']}\n\n๋ด์ฉ: {selected_article['content'][:1500]}"}
|
|
@@ -655,8 +660,8 @@ elif menu == "๊ธฐ์ฌ ๋ถ์ํ๊ธฐ":
|
|
| 655 |
max_tokens=800
|
| 656 |
)
|
| 657 |
|
| 658 |
-
# JSON ํ์ฑ
|
| 659 |
-
analysis_result = json.loads(response.choices[0].message
|
| 660 |
|
| 661 |
# ๊ฒฐ๊ณผ ์๊ฐํ
|
| 662 |
st.subheader("๊ฐ์ ๋ถ์ ๊ฒฐ๊ณผ")
|
|
@@ -885,8 +890,6 @@ elif menu == "์ ๊ธฐ์ฌ ์์ฑํ๊ธฐ":
|
|
| 885 |
else:
|
| 886 |
st.warning("OpenAI API ํค๋ฅผ ์ฌ์ด๋๋ฐ์์ ์ค์ ํด์ฃผ์ธ์.")
|
| 887 |
|
| 888 |
-
|
| 889 |
-
|
| 890 |
elif menu == "๋ด์ค ๊ธฐ์ฌ ์์ฝํ๊ธฐ":
|
| 891 |
st.header("๋ด์ค ๊ธฐ์ฌ ์์ฝํ๊ธฐ")
|
| 892 |
|
|
|
|
| 11 |
import json
|
| 12 |
import os
|
| 13 |
from datetime import datetime, timedelta
|
| 14 |
+
from openai import OpenAI # ์๋ก์ด import ๋ฐฉ์
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
import traceback
|
| 17 |
import plotly.graph_objects as go
|
|
|
|
| 50 |
# 1. ํ๊ฒฝ ๋ณ์์์ API ํค ํ์ธ
|
| 51 |
if os.environ.get('OPENAI_API_KEY'):
|
| 52 |
st.session_state.openai_api_key = os.environ.get('OPENAI_API_KEY')
|
|
|
|
| 53 |
|
| 54 |
# 2. Streamlit secrets์์ API ํค ํ์ธ (try-except๋ก ์ค๋ฅ ๋ฐฉ์ง)
|
| 55 |
if not st.session_state.openai_api_key:
|
| 56 |
try:
|
| 57 |
if 'OPENAI_API_KEY' in st.secrets:
|
| 58 |
st.session_state.openai_api_key = st.secrets['OPENAI_API_KEY']
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
pass # secrets ํ์ผ์ด ์์ด๋ ์ค๋ฅ ๋ฐ์ํ์ง ์์
|
| 61 |
|
|
|
|
| 89 |
api_key = st.text_input("OpenAI API ํค ์
๋ ฅ", type="password")
|
| 90 |
if api_key:
|
| 91 |
st.session_state.openai_api_key = api_key
|
| 92 |
+
|
| 93 |
+
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ๋ฅผ ์ํ ํจ์
|
| 94 |
+
def init_openai_client():
|
| 95 |
+
if st.session_state.openai_api_key:
|
| 96 |
+
return OpenAI(api_key=st.session_state.openai_api_key)
|
| 97 |
+
return None
|
| 98 |
|
| 99 |
# ์ ์ฅ๋ ๊ธฐ์ฌ๋ฅผ ๋ถ๋ฌ์ค๋ ํจ์
|
| 100 |
def load_saved_articles():
|
|
|
|
| 337 |
results['top_keywords'] = []
|
| 338 |
return results
|
| 339 |
|
| 340 |
+
# OpenAI API๋ฅผ ์ด์ฉํ ์ ๊ธฐ์ฌ ์์ฑ (์๋ก์ด ๋ฒ์ ๋ฐฉ์)
|
| 341 |
def generate_article(original_content, prompt_text):
|
| 342 |
try:
|
| 343 |
if not st.session_state.openai_api_key:
|
| 344 |
return "OpenAI API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
| 345 |
|
| 346 |
+
client = init_openai_client()
|
| 347 |
+
response = client.chat.completions.create(
|
| 348 |
+
model="gpt-4", # ๋๋ ์ฌ์ฉ ๊ฐ๋ฅํ ์ ์ ํ ๋ชจ๋ธ
|
| 349 |
messages=[
|
| 350 |
{"role": "system", "content": "๋น์ ์ ์ ๋ฌธ์ ์ธ ๋ด์ค ๊ธฐ์์
๋๋ค. ์ฃผ์ด์ง ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์๋ก์ด ๊ธฐ์ฌ๋ฅผ ์์ฑํด์ฃผ์ธ์."},
|
| 351 |
{"role": "user", "content": f"๋ค์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก {prompt_text}\n\n{original_content[:1000]}"}
|
| 352 |
],
|
| 353 |
max_tokens=2000
|
| 354 |
)
|
| 355 |
+
return response.choices[0].message.content
|
| 356 |
except Exception as e:
|
| 357 |
return f"๊ธฐ์ฌ ์์ฑ ์ค๋ฅ: {str(e)}"
|
| 358 |
|
| 359 |
+
# OpenAI API๋ฅผ ์ด์ฉํ ์ด๋ฏธ์ง ์์ฑ (์๋ก์ด ๋ฒ์ ๋ฐฉ์)
|
| 360 |
def generate_image(prompt):
|
| 361 |
try:
|
| 362 |
if not st.session_state.openai_api_key:
|
| 363 |
return "OpenAI API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
| 364 |
|
| 365 |
+
client = init_openai_client()
|
| 366 |
+
response = client.images.generate(
|
| 367 |
+
model="dall-e-3", # ๋๋ "dall-e-2"
|
| 368 |
prompt=prompt,
|
| 369 |
n=1,
|
| 370 |
size="1024x1024"
|
| 371 |
)
|
| 372 |
+
return response.data[0].url
|
| 373 |
except Exception as e:
|
| 374 |
return f"์ด๋ฏธ์ง ์์ฑ ์ค๋ฅ: {str(e)}"
|
| 375 |
|
|
|
|
| 650 |
if st.session_state.openai_api_key:
|
| 651 |
with st.spinner("๊ธฐ์ฌ์ ๊ฐ์ ์ ๋ถ์ ์ค์
๋๋ค..."):
|
| 652 |
try:
|
| 653 |
+
client = init_openai_client()
|
| 654 |
+
response = client.chat.completions.create(
|
| 655 |
+
model="gpt-4", # ๋๋ ์ฌ์ฉ ๊ฐ๋ฅํ ์ ์ ํ ๋ชจ๋ธ
|
| 656 |
messages=[
|
| 657 |
{"role": "system", "content": "๋น์ ์ ํ
์คํธ์ ๊ฐ์ ๊ณผ ๋
ผ์กฐ๋ฅผ ๋ถ์ํ๋ ์ ๋ฌธ๊ฐ์
๋๋ค. ๋ค์ ๋ด์ค ๊ธฐ์ฌ์ ๊ฐ์ ๊ณผ ๋
ผ์กฐ๋ฅผ ๋ถ์ํ๊ณ , '๊ธ์ ์ ', '๋ถ์ ์ ', '์ค๋ฆฝ์ ' ์ค ํ๋๋ก ๋ถ๋ฅํด ์ฃผ์ธ์. ๋ํ ๊ธฐ์ฌ์์ ๋๋ฌ๋๋ ํต์ฌ ๊ฐ์ ํค์๋๋ฅผ 5๊ฐ ์ถ์ถํ๊ณ , ๊ฐ ํค์๋๋ณ๋ก 1-10 ์ฌ์ด์ ๊ฐ๋ ์ ์๋ฅผ ๋งค๊ฒจ์ฃผ์ธ์. JSON ํ์์ผ๋ก ๋ค์๊ณผ ๊ฐ์ด ์๋ตํด์ฃผ์ธ์: {'sentiment': '๊ธ์ ์ /๋ถ์ ์ /์ค๋ฆฝ์ ', 'reason': '์ด์ ์ค๋ช
...', 'keywords': [{'word': 'ํค์๋1', 'score': 8}, {'word': 'ํค์๋2', 'score': 7}, ...]}"},
|
| 658 |
{"role": "user", "content": f"๋ค์ ๋ด์ค ๊ธฐ์ฌ๋ฅผ ๋ถ์ํด ์ฃผ์ธ์:\n\n์ ๋ชฉ: {selected_article['title']}\n\n๋ด์ฉ: {selected_article['content'][:1500]}"}
|
|
|
|
| 660 |
max_tokens=800
|
| 661 |
)
|
| 662 |
|
| 663 |
+
# JSON ํ์ฑ
|
| 664 |
+
analysis_result = json.loads(response.choices[0].message.content)
|
| 665 |
|
| 666 |
# ๊ฒฐ๊ณผ ์๊ฐํ
|
| 667 |
st.subheader("๊ฐ์ ๋ถ์ ๊ฒฐ๊ณผ")
|
|
|
|
| 890 |
else:
|
| 891 |
st.warning("OpenAI API ํค๋ฅผ ์ฌ์ด๋๋ฐ์์ ์ค์ ํด์ฃผ์ธ์.")
|
| 892 |
|
|
|
|
|
|
|
| 893 |
elif menu == "๋ด์ค ๊ธฐ์ฌ ์์ฝํ๊ธฐ":
|
| 894 |
st.header("๋ด์ค ๊ธฐ์ฌ ์์ฝํ๊ธฐ")
|
| 895 |
|