Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +41 -12
src/streamlit_app.py
CHANGED
|
@@ -19,6 +19,9 @@ import schedule
|
|
| 19 |
import threading
|
| 20 |
import matplotlib.pyplot as plt
|
| 21 |
import kss # KoNLPy ๋์ KSS ์ฌ์ฉ
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# ์๋ํด๋ผ์ฐ๋ ์ถ๊ฐ
|
| 24 |
try:
|
|
@@ -357,7 +360,7 @@ def generate_article(original_content, prompt_text):
|
|
| 357 |
return "OpenAI API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
| 358 |
|
| 359 |
response = st.session_state.openai_client.chat.completions.create(
|
| 360 |
-
model="gpt-4
|
| 361 |
messages=[
|
| 362 |
{"role": "system", "content": "๋น์ ์ ์ ๋ฌธ์ ์ธ ๋ด์ค ๊ธฐ์์
๋๋ค. ์ฃผ์ด์ง ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์๋ก์ด ๊ธฐ์ฌ๋ฅผ ์์ฑํด์ฃผ์ธ์."},
|
| 363 |
{"role": "user", "content": f"๋ค์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก {prompt_text}\n\n{original_content[:1000]}"}
|
|
@@ -374,13 +377,31 @@ def generate_image(prompt):
|
|
| 374 |
if not st.session_state.openai_client:
|
| 375 |
return "OpenAI API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
| 376 |
|
| 377 |
-
|
| 378 |
-
|
|
|
|
| 379 |
prompt=prompt,
|
| 380 |
-
n=1,
|
| 381 |
size="1024x1024"
|
| 382 |
)
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
except Exception as e:
|
| 385 |
return f"์ด๋ฏธ์ง ์์ฑ ์ค๋ฅ: {str(e)}"
|
| 386 |
|
|
@@ -662,9 +683,18 @@ elif menu == "๊ธฐ์ฌ ๋ถ์ํ๊ธฐ":
|
|
| 662 |
with st.spinner("๊ธฐ์ฌ์ ๊ฐ์ ์ ๋ถ์ ์ค์
๋๋ค..."):
|
| 663 |
try:
|
| 664 |
response = st.session_state.openai_client.chat.completions.create(
|
| 665 |
-
model="gpt-4",
|
| 666 |
messages=[
|
| 667 |
-
{"role": "system", "content": "๋น์ ์ ํ
์คํธ์ ๊ฐ์ ๊ณผ ๋
ผ์กฐ๋ฅผ ๋ถ์ํ๋ ์ ๋ฌธ๊ฐ์
๋๋ค.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
{"role": "user", "content": f"๋ค์ ๋ด์ค ๊ธฐ์ฌ๋ฅผ ๋ถ์ํด ์ฃผ์ธ์:\n\n์ ๋ชฉ: {selected_article['title']}\n\n๋ด์ฉ: {selected_article['content'][:1500]}"}
|
| 669 |
],
|
| 670 |
max_tokens=800
|
|
@@ -867,7 +897,6 @@ elif menu == "์ ๊ธฐ์ฌ ์์ฑํ๊ธฐ":
|
|
| 867 |
# ์ด๋ฏธ์ง ์์ฑํ๊ธฐ (์ต์
์ด ์ ํ๋ ๊ฒฝ์ฐ)
|
| 868 |
if generate_image_too:
|
| 869 |
with st.spinner("๊ธฐ์ฌ ๊ด๋ จ ์ด๋ฏธ์ง๋ฅผ ์์ฑ ์ค์
๋๋ค..."):
|
| 870 |
-
# ์ด๋ฏธ์ง ์์ฑ ํ๋กฌํํธ ์ค๋น
|
| 871 |
image_prompt = f"""์ ๋ฌธ๊ธฐ์ฌ ์ ๋ชฉ "{selected_article['title']}" ์ ๋ณด๊ณ ์ด๋ฏธ์ง๋ฅผ ๋ง๋ค์ด์ค
|
| 872 |
์ด๋ฏธ์ง์๋ ๋ค์ ์์๊ฐ ํฌํจ๋์ด์ผ ํฉ๋๋ค:
|
| 873 |
- ๊ธฐ์ฌ๋ฅผ ์ดํดํ ์ ์๋ ๋์
|
|
@@ -876,13 +905,13 @@ elif menu == "์ ๊ธฐ์ฌ ์์ฑํ๊ธฐ":
|
|
| 876 |
"""
|
| 877 |
|
| 878 |
# ์ด๋ฏธ์ง ์์ฑ
|
| 879 |
-
|
| 880 |
|
| 881 |
-
if
|
| 882 |
st.subheader("์์ฑ๋ ์ด๋ฏธ์ง:")
|
| 883 |
-
st.image(
|
| 884 |
else:
|
| 885 |
-
st.error(
|
| 886 |
|
| 887 |
# ์์ฑ๋ ๊ธฐ์ฌ ์ ์ฅ ์ต์
|
| 888 |
if st.button("์์ฑ๋ ๊ธฐ์ฌ ์ ์ฅ"):
|
|
|
|
| 19 |
import threading
|
| 20 |
import matplotlib.pyplot as plt
|
| 21 |
import kss # KoNLPy ๋์ KSS ์ฌ์ฉ
|
| 22 |
+
from PIL import Image
|
| 23 |
+
import base64
|
| 24 |
+
from io import BytesIO
|
| 25 |
|
| 26 |
# ์๋ํด๋ผ์ฐ๋ ์ถ๊ฐ
|
| 27 |
try:
|
|
|
|
| 360 |
return "OpenAI API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
| 361 |
|
| 362 |
response = st.session_state.openai_client.chat.completions.create(
|
| 363 |
+
model="gpt-4", # ๋๋ ์ฌ์ฉ ๊ฐ๋ฅํ ์ ์ ํ ๋ชจ๋ธ
|
| 364 |
messages=[
|
| 365 |
{"role": "system", "content": "๋น์ ์ ์ ๋ฌธ์ ์ธ ๋ด์ค ๊ธฐ์์
๋๋ค. ์ฃผ์ด์ง ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์๋ก์ด ๊ธฐ์ฌ๋ฅผ ์์ฑํด์ฃผ์ธ์."},
|
| 366 |
{"role": "user", "content": f"๋ค์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก {prompt_text}\n\n{original_content[:1000]}"}
|
|
|
|
| 377 |
if not st.session_state.openai_client:
|
| 378 |
return "OpenAI API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
| 379 |
|
| 380 |
+
# GPT Image 1 ๋ชจ๋ธ๋ก ์ด๋ฏธ์ง ์์ฑ
|
| 381 |
+
result = st.session_state.openai_client.images.generate(
|
| 382 |
+
model="gpt-image-1", # ์๋ก์ด ๋ชจ๋ธ๋ช
์ฌ์ฉ
|
| 383 |
prompt=prompt,
|
|
|
|
| 384 |
size="1024x1024"
|
| 385 |
)
|
| 386 |
+
|
| 387 |
+
# base64 ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ ๋์ฝ๋ฉ
|
| 388 |
+
image_base64 = result.data[0].b64_json
|
| 389 |
+
image_bytes = base64.b64decode(image_base64)
|
| 390 |
+
|
| 391 |
+
# BytesIO ๊ฐ์ฒด๋ก ๋ณํ
|
| 392 |
+
image = BytesIO(image_bytes)
|
| 393 |
+
|
| 394 |
+
# PIL Image๋ก ๋ณํํ์ฌ ํฌ๊ธฐ ์กฐ์ (์ ํ์ฌํญ)
|
| 395 |
+
pil_image = Image.open(image)
|
| 396 |
+
pil_image = pil_image.resize((800, 800), Image.LANCZOS) # ํฌ๊ธฐ ์กฐ์
|
| 397 |
+
|
| 398 |
+
# ๋ค์ BytesIO๋ก ๋ณํ
|
| 399 |
+
output = BytesIO()
|
| 400 |
+
pil_image.save(output, format="JPEG", quality=80, optimize=True)
|
| 401 |
+
output.seek(0)
|
| 402 |
+
|
| 403 |
+
return output
|
| 404 |
+
|
| 405 |
except Exception as e:
|
| 406 |
return f"์ด๋ฏธ์ง ์์ฑ ์ค๋ฅ: {str(e)}"
|
| 407 |
|
|
|
|
| 683 |
with st.spinner("๊ธฐ์ฌ์ ๊ฐ์ ์ ๋ถ์ ์ค์
๋๋ค..."):
|
| 684 |
try:
|
| 685 |
response = st.session_state.openai_client.chat.completions.create(
|
| 686 |
+
model="gpt-4",
|
| 687 |
messages=[
|
| 688 |
+
{"role": "system", "content": """๋น์ ์ ํ
์คํธ์ ๊ฐ์ ๊ณผ ๋
ผ์กฐ๋ฅผ ๋ถ์ํ๋ ์ ๋ฌธ๊ฐ์
๋๋ค.
|
| 689 |
+
๋ค์ ๋ด์ค ๊ธฐ์ฌ์ ๊ฐ์ ๊ณผ ๋
ผ์กฐ๋ฅผ ๋ถ์ํ๊ณ , ๋ฐ๋์ ์๋ ํ์์ JSON์ผ๋ก ์๋ตํด์ฃผ์ธ์:
|
| 690 |
+
{
|
| 691 |
+
"sentiment": "๊ธ์ ์ /๋ถ์ ์ /์ค๋ฆฝ์ ",
|
| 692 |
+
"reason": "์ด์ ์ค๋ช
...",
|
| 693 |
+
"keywords": [
|
| 694 |
+
{"word": "ํค์๋1", "score": 8},
|
| 695 |
+
{"word": "ํค์๋2", "score": 7}
|
| 696 |
+
]
|
| 697 |
+
}"""},
|
| 698 |
{"role": "user", "content": f"๋ค์ ๋ด์ค ๊ธฐ์ฌ๋ฅผ ๋ถ์ํด ์ฃผ์ธ์:\n\n์ ๋ชฉ: {selected_article['title']}\n\n๋ด์ฉ: {selected_article['content'][:1500]}"}
|
| 699 |
],
|
| 700 |
max_tokens=800
|
|
|
|
| 897 |
# ์ด๋ฏธ์ง ์์ฑํ๊ธฐ (์ต์
์ด ์ ํ๋ ๊ฒฝ์ฐ)
|
| 898 |
if generate_image_too:
|
| 899 |
with st.spinner("๊ธฐ์ฌ ๊ด๋ จ ์ด๋ฏธ์ง๋ฅผ ์์ฑ ์ค์
๋๋ค..."):
|
|
|
|
| 900 |
image_prompt = f"""์ ๋ฌธ๊ธฐ์ฌ ์ ๋ชฉ "{selected_article['title']}" ์ ๋ณด๊ณ ์ด๋ฏธ์ง๋ฅผ ๋ง๋ค์ด์ค
|
| 901 |
์ด๋ฏธ์ง์๋ ๋ค์ ์์๊ฐ ํฌํจ๋์ด์ผ ํฉ๋๋ค:
|
| 902 |
- ๊ธฐ์ฌ๋ฅผ ์ดํดํ ์ ์๋ ๋์
|
|
|
|
| 905 |
"""
|
| 906 |
|
| 907 |
# ์ด๋ฏธ์ง ์์ฑ
|
| 908 |
+
image = generate_image(image_prompt)
|
| 909 |
|
| 910 |
+
if isinstance(image, BytesIO):
|
| 911 |
st.subheader("์์ฑ๋ ์ด๋ฏธ์ง:")
|
| 912 |
+
st.image(image, use_column_width=True)
|
| 913 |
else:
|
| 914 |
+
st.error(image)
|
| 915 |
|
| 916 |
# ์์ฑ๋ ๊ธฐ์ฌ ์ ์ฅ ์ต์
|
| 917 |
if st.button("์์ฑ๋ ๊ธฐ์ฌ ์ ์ฅ"):
|