Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +53 -52
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
|
@@ -39,27 +39,29 @@ class SchedulerState:
|
|
39 |
# ์ ์ญ ์ค์ผ์ค๋ฌ ์ํ ๊ฐ์ฒด ์์ฑ (์ค๋ ๋ ์์์ ์ฌ์ฉ)
|
40 |
global_scheduler_state = SchedulerState()
|
41 |
|
42 |
-
# API ํค
|
43 |
if 'openai_api_key' not in st.session_state:
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
# # API ํค ๊ด๋ฆฌ๋ฅผ ์ํ ์ธ์
์ํ ์ด๊ธฐํ
|
54 |
-
# if 'openai_api_key' not in st.session_state:
|
55 |
-
# st.session_state.openai_api_key = None
|
56 |
|
57 |
-
#
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
# ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋๋ก NLTK ๋ฐ์ดํฐ ๊ฒฝ๋ก ์ค์
|
65 |
nltk_data_dir = '/tmp/nltk_data'
|
@@ -77,23 +79,23 @@ try:
|
|
77 |
except LookupError:
|
78 |
nltk.download('stopwords', download_dir=nltk_data_dir)
|
79 |
|
80 |
-
# OpenAI API ํค ์ค์ (์ค์ ์ฌ์ฉ ์ ํ๊ฒฝ ๋ณ์๋ Streamlit secrets์์ ๊ฐ์ ธ์ค๋ ๊ฒ์ด ์ข์ต๋๋ค)
|
81 |
-
if 'OPENAI_API_KEY' in os.environ:
|
82 |
-
openai.api_key = os.environ['OPENAI_API_KEY']
|
83 |
-
elif 'OPENAI_API_KEY' in st.secrets:
|
84 |
-
openai.api_key = st.secrets['OPENAI_API_KEY']
|
85 |
-
elif os.getenv('OPENAI_API_KEY'):
|
86 |
-
openai.api_key = os.getenv('OPENAI_API_KEY')
|
87 |
-
|
88 |
# ํ์ด์ง ์ค์
|
89 |
st.set_page_config(page_title="๋ด์ค ๊ธฐ์ฌ ๋๊ตฌ", page_icon="๐ฐ", layout="wide")
|
90 |
|
91 |
-
#
|
92 |
-
st.sidebar
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
# ์ ์ฅ๋ ๊ธฐ์ฌ๋ฅผ ๋ถ๋ฌ์ค๋ ํจ์
|
99 |
def load_saved_articles():
|
@@ -260,7 +262,6 @@ def extract_keywords_for_wordcloud(text, top_n=50):
|
|
260 |
|
261 |
|
262 |
# ์๋ ํด๋ผ์ฐ๋ ์์ฑ ํจ์
|
263 |
-
|
264 |
def generate_wordcloud(keywords_dict):
|
265 |
if not WordCloud:
|
266 |
st.warning("์๋ํด๋ผ์ฐ๋ ์ค์น์๋์ด ์์ต๋๋ค.")
|
@@ -330,6 +331,9 @@ def analyze_news_content(news_df):
|
|
330 |
# OpenAI API๋ฅผ ์ด์ฉํ ์ ๊ธฐ์ฌ ์์ฑ
|
331 |
def generate_article(original_content, prompt_text):
|
332 |
try:
|
|
|
|
|
|
|
333 |
response = st.session_state.openai_client.chat.completions.create(
|
334 |
model="gpt-4.1-mini",
|
335 |
messages=[
|
@@ -345,12 +349,19 @@ def generate_article(original_content, prompt_text):
|
|
345 |
# OpenAI API๋ฅผ ์ด์ฉํ ์ด๋ฏธ์ง ์์ฑ
|
346 |
def generate_image(prompt):
|
347 |
try:
|
348 |
-
|
|
|
|
|
|
|
349 |
model="gpt-image-1",
|
350 |
prompt=prompt
|
351 |
)
|
352 |
-
|
353 |
-
|
|
|
|
|
|
|
|
|
354 |
except Exception as e:
|
355 |
return f"์ด๋ฏธ์ง ์์ฑ ์ค๋ฅ: {str(e)}"
|
356 |
|
@@ -522,7 +533,7 @@ if menu == "๋ด์ค ๊ธฐ์ฌ ํฌ๋กค๋ง":
|
|
522 |
st.write(f"**์์ฝ:** {article['description']}")
|
523 |
st.write(f"**๋งํฌ:** {article['link']}")
|
524 |
st.write("**๋ณธ๋ฌธ ๋ฏธ๋ฆฌ๋ณด๊ธฐ:**")
|
525 |
-
st.write(article['content'][:300] + "...")
|
526 |
|
527 |
elif menu == "๊ธฐ์ฌ ๋ถ์ํ๊ธฐ":
|
528 |
st.header("๊ธฐ์ฌ ๋ถ์ํ๊ธฐ")
|
@@ -557,7 +568,6 @@ elif menu == "๊ธฐ์ฌ ๋ถ์ํ๊ธฐ":
|
|
557 |
keyword_tab1, keyword_tab2 = st.tabs(["ํค์๋ ๋น๋", "์๋ํด๋ผ์ฐ๋"])
|
558 |
|
559 |
with keyword_tab1:
|
560 |
-
|
561 |
keywords = analyze_keywords(selected_article['content'])
|
562 |
|
563 |
# ์๊ฐํ
|
@@ -633,13 +643,7 @@ elif menu == "๊ธฐ์ฌ ๋ถ์ํ๊ธฐ":
|
|
633 |
try:
|
634 |
nltk.data.find('taggers/averaged_perceptron_tagger')
|
635 |
except LookupError:
|
636 |
-
nltk.download('averaged_perceptron_tagger')
|
637 |
-
|
638 |
-
# Try using the correct resource name as shown in the error message
|
639 |
-
try:
|
640 |
-
nltk.data.find('averaged_perceptron_tagger_eng')
|
641 |
-
except LookupError:
|
642 |
-
nltk.download('averaged_perceptron_tagger_eng')
|
643 |
|
644 |
# ์ธ์ด ๊ฐ์ง (๊ฐ๋จํ ๋ฐฉ์)
|
645 |
is_korean = bool(re.search(r'[๊ฐ-ํฃ]', content))
|
@@ -710,11 +714,9 @@ elif menu == "๊ธฐ์ฌ ๋ถ์ํ๊ธฐ":
|
|
710 |
|
711 |
elif analysis_type == "๊ฐ์ ๋ถ์":
|
712 |
if st.button("๊ฐ์ ๋ถ์ํ๊ธฐ"):
|
713 |
-
if st.session_state.
|
714 |
with st.spinner("๊ธฐ์ฌ์ ๊ฐ์ ์ ๋ถ์ ์ค์
๋๋ค..."):
|
715 |
try:
|
716 |
-
openai.api_key = st.session_state.openai_api_key
|
717 |
-
|
718 |
# ๊ฐ์ ๋ถ์ ํ๋กฌํํธ ์ค์
|
719 |
response = st.session_state.openai_client.chat.completions.create(
|
720 |
model="gpt-4.1-mini",
|
@@ -913,8 +915,7 @@ elif menu == "์ ๊ธฐ์ฌ ์์ฑํ๊ธฐ":
|
|
913 |
generate_image_too = st.checkbox("๊ธฐ์ฌ ์์ฑ ํ ์ด๋ฏธ์ง๋ ํจ๊ป ์์ฑํ๊ธฐ", value=True)
|
914 |
|
915 |
if st.button("์ ๊ธฐ์ฌ ์์ฑํ๊ธฐ"):
|
916 |
-
if st.session_state.
|
917 |
-
openai.api_key = st.session_state.openai_api_key
|
918 |
with st.spinner("๊ธฐ์ฌ๋ฅผ ์์ฑ ์ค์
๋๋ค..."):
|
919 |
new_article = generate_article(selected_article['content'], prompt_text)
|
920 |
|
|
|
11 |
import json
|
12 |
import os
|
13 |
from datetime import datetime, timedelta
|
14 |
+
from openai import OpenAI
|
15 |
from dotenv import load_dotenv
|
16 |
import traceback
|
17 |
import plotly.graph_objects as go
|
|
|
39 |
# ์ ์ญ ์ค์ผ์ค๋ฌ ์ํ ๊ฐ์ฒด ์์ฑ (์ค๋ ๋ ์์์ ์ฌ์ฉ)
|
40 |
global_scheduler_state = SchedulerState()
|
41 |
|
42 |
+
# API ํค ๊ด๋ฆฌ๋ฅผ ์ํ ์ธ์
์ํ ์ด๊ธฐํ
|
43 |
if 'openai_api_key' not in st.session_state:
|
44 |
+
st.session_state.openai_api_key = None
|
45 |
+
st.session_state.openai_client = None
|
46 |
+
|
47 |
+
# ์ฌ๋ฌ ๋ฐฉ๋ฒ์ผ๋ก API ํค ๋ก๋ ์๋
|
48 |
+
load_dotenv() # .env ํ์ผ์์ ๋ก๋ ์๋
|
49 |
+
|
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 |
+
|
62 |
+
# 3. API ํค๊ฐ ์์ผ๋ฉด ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
63 |
+
if st.session_state.openai_api_key:
|
64 |
+
st.session_state.openai_client = OpenAI(api_key=st.session_state.openai_api_key)
|
65 |
|
66 |
# ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋๋ก NLTK ๋ฐ์ดํฐ ๊ฒฝ๋ก ์ค์
|
67 |
nltk_data_dir = '/tmp/nltk_data'
|
|
|
79 |
except LookupError:
|
80 |
nltk.download('stopwords', download_dir=nltk_data_dir)
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
# ํ์ด์ง ์ค์
|
83 |
st.set_page_config(page_title="๋ด์ค ๊ธฐ์ฌ ๋๊ตฌ", page_icon="๐ฐ", layout="wide")
|
84 |
|
85 |
+
# ์ฌ์ด๋๋ฐ์ API ํค ์
๋ ฅ ํ๋ ์ถ๊ฐ
|
86 |
+
with st.sidebar:
|
87 |
+
st.title("๋ด์ค ๊ธฐ์ฌ ๋๊ตฌ")
|
88 |
+
menu = st.radio(
|
89 |
+
"๋ฉ๋ด ์ ํ",
|
90 |
+
["๋ด์ค ๊ธฐ์ฌ ํฌ๋กค๋ง", "๊ธฐ์ฌ ๋ถ์ํ๊ธฐ", "์ ๊ธฐ์ฌ ์์ฑํ๊ธฐ", "๋ด์ค ๊ธฐ์ฌ ์์ฝํ๊ธฐ"]
|
91 |
+
)
|
92 |
+
|
93 |
+
st.divider()
|
94 |
+
api_key = st.text_input("OpenAI API ํค ์
๋ ฅ", type="password")
|
95 |
+
if api_key:
|
96 |
+
st.session_state.openai_api_key = api_key
|
97 |
+
st.session_state.openai_client = OpenAI(api_key=api_key)
|
98 |
+
st.success("API ํค๊ฐ ์ค์ ๋์์ต๋๋ค!")
|
99 |
|
100 |
# ์ ์ฅ๋ ๊ธฐ์ฌ๋ฅผ ๋ถ๋ฌ์ค๋ ํจ์
|
101 |
def load_saved_articles():
|
|
|
262 |
|
263 |
|
264 |
# ์๋ ํด๋ผ์ฐ๋ ์์ฑ ํจ์
|
|
|
265 |
def generate_wordcloud(keywords_dict):
|
266 |
if not WordCloud:
|
267 |
st.warning("์๋ํด๋ผ์ฐ๋ ์ค์น์๋์ด ์์ต๋๋ค.")
|
|
|
331 |
# OpenAI API๋ฅผ ์ด์ฉํ ์ ๊ธฐ์ฌ ์์ฑ
|
332 |
def generate_article(original_content, prompt_text):
|
333 |
try:
|
334 |
+
if not st.session_state.openai_client:
|
335 |
+
return "OpenAI API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
336 |
+
|
337 |
response = st.session_state.openai_client.chat.completions.create(
|
338 |
model="gpt-4.1-mini",
|
339 |
messages=[
|
|
|
349 |
# OpenAI API๋ฅผ ์ด์ฉํ ์ด๋ฏธ์ง ์์ฑ
|
350 |
def generate_image(prompt):
|
351 |
try:
|
352 |
+
if not st.session_state.openai_client:
|
353 |
+
return "OpenAI API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
354 |
+
|
355 |
+
response = st.session_state.openai_client.images.generate(
|
356 |
model="gpt-image-1",
|
357 |
prompt=prompt
|
358 |
)
|
359 |
+
try:
|
360 |
+
image_base64 = response.data[0].b64_json
|
361 |
+
return f"data:image/png;base64,{image_base64}"
|
362 |
+
except AttributeError:
|
363 |
+
# b64_json๊ฐ ์์ ๊ฒฝ์ฐ URL ์ฌ์ฉ
|
364 |
+
return response.data[0].url
|
365 |
except Exception as e:
|
366 |
return f"์ด๋ฏธ์ง ์์ฑ ์ค๋ฅ: {str(e)}"
|
367 |
|
|
|
533 |
st.write(f"**์์ฝ:** {article['description']}")
|
534 |
st.write(f"**๋งํฌ:** {article['link']}")
|
535 |
st.write("**๋ณธ๋ฌธ ๋ฏธ๋ฆฌ๋ณด๊ธฐ:**")
|
536 |
+
st.write(article['content'][:300] + "..." if len(article['content']) > 300 else article['content'])
|
537 |
|
538 |
elif menu == "๊ธฐ์ฌ ๋ถ์ํ๊ธฐ":
|
539 |
st.header("๊ธฐ์ฌ ๋ถ์ํ๊ธฐ")
|
|
|
568 |
keyword_tab1, keyword_tab2 = st.tabs(["ํค์๋ ๋น๋", "์๋ํด๋ผ์ฐ๋"])
|
569 |
|
570 |
with keyword_tab1:
|
|
|
571 |
keywords = analyze_keywords(selected_article['content'])
|
572 |
|
573 |
# ์๊ฐํ
|
|
|
643 |
try:
|
644 |
nltk.data.find('taggers/averaged_perceptron_tagger')
|
645 |
except LookupError:
|
646 |
+
nltk.download('averaged_perceptron_tagger', download_dir=nltk_data_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
647 |
|
648 |
# ์ธ์ด ๊ฐ์ง (๊ฐ๋จํ ๋ฐฉ์)
|
649 |
is_korean = bool(re.search(r'[๊ฐ-ํฃ]', content))
|
|
|
714 |
|
715 |
elif analysis_type == "๊ฐ์ ๋ถ์":
|
716 |
if st.button("๊ฐ์ ๋ถ์ํ๊ธฐ"):
|
717 |
+
if st.session_state.openai_client:
|
718 |
with st.spinner("๊ธฐ์ฌ์ ๊ฐ์ ์ ๋ถ์ ์ค์
๋๋ค..."):
|
719 |
try:
|
|
|
|
|
720 |
# ๊ฐ์ ๋ถ์ ํ๋กฌํํธ ์ค์
|
721 |
response = st.session_state.openai_client.chat.completions.create(
|
722 |
model="gpt-4.1-mini",
|
|
|
915 |
generate_image_too = st.checkbox("๊ธฐ์ฌ ์์ฑ ํ ์ด๋ฏธ์ง๋ ํจ๊ป ์์ฑํ๊ธฐ", value=True)
|
916 |
|
917 |
if st.button("์ ๊ธฐ์ฌ ์์ฑํ๊ธฐ"):
|
918 |
+
if st.session_state.openai_client:
|
|
|
919 |
with st.spinner("๊ธฐ์ฌ๋ฅผ ์์ฑ ์ค์
๋๋ค..."):
|
920 |
new_article = generate_article(selected_article['content'], prompt_text)
|
921 |
|