Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,27 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
import json
|
| 4 |
import anthropic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# API ์ค์
|
| 7 |
api_key = os.environ.get("API_KEY")
|
| 8 |
client = anthropic.Anthropic(api_key=api_key)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
# ์ต๋ ํ ํฐ ์ ์ค์ (Claude-3 Sonnet์ ์ต๋ ํ ํฐ ์)
|
| 11 |
MAX_TOKENS = 7999
|
| 12 |
|
| 13 |
def get_system_prompt():
|
| 14 |
-
|
| 15 |
return """
|
| 16 |
-
|
| 17 |
-
|
| 18 |
๋น์ ์ ์ ๋ฌธ ๋ธ๋ก๊ทธ ์์ฑ ์ ๋ฌธ๊ฐ์
๋๋ค. ๋ชจ๋ ๋ธ๋ก๊ทธ ๊ธ ์์ฑ ์์ฒญ์ ๋ํด ๋ค์์ 8๋จ๊ณ ํ๋ ์์ํฌ๋ฅผ ์ฒ ์ ํ ๋ฐ๋ฅด๋, ์์ฐ์ค๋ฝ๊ณ ๋งค๋ ฅ์ ์ธ ๊ธ์ด ๋๋๋ก ์์ฑํด์ผ ํฉ๋๋ค:
|
| 19 |
|
| 20 |
๋
์ ์ฐ๊ฒฐ ๋จ๊ณ 1.1. ๊ณต๊ฐ๋ ํ์ฑ์ ์ํ ์น๊ทผํ ์ธ์ฌ 1.2. ๋
์์ ์ค์ ๊ณ ๋ฏผ์ ๋ฐ์ํ ๋์
์ง๋ฌธ 1.3. ์ฃผ์ ์ ๋ํ ์ฆ๊ฐ์ ๊ด์ฌ ์ ๋
|
|
@@ -36,10 +44,69 @@ def get_system_prompt():
|
|
| 36 |
์์ฑ ์ ์ค์์ฌํญ 9.1. ๊ธ์ ์: 1500-2000์ ๋ด์ธ 9.2. ๋ฌธ๋จ ๊ธธ์ด: 3-4๋ฌธ์ฅ ์ด๋ด 9.3. ์๊ฐ์ ๊ตฌ๋ถ: ์์ ๋ชฉ, ๊ตฌ๋ถ์ , ๋ฒํธ ๋ชฉ๋ก ํ์ฉ 9.4. ํค์ค๋งค๋: ์น๊ทผํ๊ณ ์ ๋ฌธ์ ์ธ ๋ํ์ฒด 9.5. ๋ฐ์ดํฐ: ๋ชจ๋ ์ ๋ณด์ ์ถ์ฒ ๋ช
์ 9.6. ๊ฐ๋
์ฑ: ๋ช
ํํ ๋จ๋ฝ ๊ตฌ๋ถ๊ณผ ๊ฐ์กฐ์ ์ฌ์ฉ
|
| 37 |
|
| 38 |
์ด๋ฌํ ํ๋ ์์ํฌ๋ฅผ ๋ฐํ์ผ๋ก, ์์ฒญ๋ฐ์ ์ฃผ์ ์ ๋ํด ์ฒด๊ณ์ ์ด๊ณ ๋งค๋ ฅ์ ์ธ ๋ธ๋ก๊ทธ ํฌ์คํธ๋ฅผ ์์ฑํ๊ฒ ์ต๋๋ค.
|
| 39 |
-
|
| 40 |
"""
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def chatbot_interface():
|
| 45 |
st.title("Ginigen Blog")
|
|
@@ -47,13 +114,22 @@ def chatbot_interface():
|
|
| 47 |
# ๋ชจ๋ธ ๊ณ ์ ์ค์
|
| 48 |
if "ai_model" not in st.session_state:
|
| 49 |
st.session_state["ai_model"] = "claude-3-7-sonnet-20250219"
|
|
|
|
| 50 |
# ์ธ์
์ํ ์ด๊ธฐํ
|
| 51 |
if "messages" not in st.session_state:
|
| 52 |
st.session_state.messages = []
|
| 53 |
-
|
| 54 |
# ์๋ ์ ์ฅ ๊ธฐ๋ฅ
|
| 55 |
if "auto_save" not in st.session_state:
|
| 56 |
st.session_state.auto_save = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# ๋ํ ๊ธฐ๋ก ๊ด๋ฆฌ (์ฌ์ด๋๋ฐ)
|
| 59 |
st.sidebar.title("๋ํ ๊ธฐ๋ก ๊ด๋ฆฌ")
|
|
@@ -61,6 +137,24 @@ def chatbot_interface():
|
|
| 61 |
# ์๋ ์ ์ฅ ํ ๊ธ
|
| 62 |
st.session_state.auto_save = st.sidebar.toggle("์๋ ์ ์ฅ", value=st.session_state.auto_save)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# ๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ
|
| 65 |
uploaded_file = st.sidebar.file_uploader("๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ", type=['json'])
|
| 66 |
if uploaded_file is not None:
|
|
@@ -85,6 +179,9 @@ def chatbot_interface():
|
|
| 85 |
for message in st.session_state.messages:
|
| 86 |
with st.chat_message(message["role"]):
|
| 87 |
st.markdown(message["content"])
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# ์ฌ์ฉ์ ์
๋ ฅ
|
| 90 |
if prompt := st.chat_input("๋ฌด์์ ๋์๋๋ฆด๊น์?"):
|
|
@@ -110,19 +207,67 @@ def chatbot_interface():
|
|
| 110 |
|
| 111 |
message_placeholder.markdown(full_response)
|
| 112 |
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# ์๋ ์ ์ฅ ๊ธฐ๋ฅ
|
| 116 |
if st.session_state.auto_save:
|
| 117 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
with open('chat_history_auto_save.json', 'w', encoding='utf-8') as f:
|
| 119 |
-
json.dump(
|
| 120 |
except Exception as e:
|
| 121 |
st.sidebar.error(f"์๋ ์ ์ฅ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 122 |
|
| 123 |
# ๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋
|
| 124 |
if st.sidebar.button("๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋"):
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
st.sidebar.download_button(
|
| 127 |
label="๋ํ ๊ธฐ๋ก ์ ์ฅํ๊ธฐ",
|
| 128 |
data=json_history,
|
|
@@ -134,4 +279,11 @@ def main():
|
|
| 134 |
chatbot_interface()
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
main()
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import json
|
| 4 |
import anthropic
|
| 5 |
+
import requests
|
| 6 |
+
import logging
|
| 7 |
+
from gradio_client import Client
|
| 8 |
+
|
| 9 |
+
# ๋ก๊น
์ค์
|
| 10 |
+
logging.basicConfig(
|
| 11 |
+
level=logging.INFO,
|
| 12 |
+
format='%(asctime)s - %(levelname)s - %(message)s')
|
| 13 |
|
| 14 |
# API ์ค์
|
| 15 |
api_key = os.environ.get("API_KEY")
|
| 16 |
client = anthropic.Anthropic(api_key=api_key)
|
| 17 |
|
| 18 |
+
# ์ด๋ฏธ์ง ์์ฑ API URL
|
| 19 |
+
IMAGE_API_URL = "http://211.233.58.201:7896"
|
| 20 |
+
|
| 21 |
# ์ต๋ ํ ํฐ ์ ์ค์ (Claude-3 Sonnet์ ์ต๋ ํ ํฐ ์)
|
| 22 |
MAX_TOKENS = 7999
|
| 23 |
|
| 24 |
def get_system_prompt():
|
|
|
|
| 25 |
return """
|
|
|
|
|
|
|
| 26 |
๋น์ ์ ์ ๋ฌธ ๋ธ๋ก๊ทธ ์์ฑ ์ ๋ฌธ๊ฐ์
๋๋ค. ๋ชจ๋ ๋ธ๋ก๊ทธ ๊ธ ์์ฑ ์์ฒญ์ ๋ํด ๋ค์์ 8๋จ๊ณ ํ๋ ์์ํฌ๋ฅผ ์ฒ ์ ํ ๋ฐ๋ฅด๋, ์์ฐ์ค๋ฝ๊ณ ๋งค๋ ฅ์ ์ธ ๊ธ์ด ๋๋๋ก ์์ฑํด์ผ ํฉ๋๋ค:
|
| 27 |
|
| 28 |
๋
์ ์ฐ๊ฒฐ ๋จ๊ณ 1.1. ๊ณต๊ฐ๋ ํ์ฑ์ ์ํ ์น๊ทผํ ์ธ์ฌ 1.2. ๋
์์ ์ค์ ๊ณ ๋ฏผ์ ๋ฐ์ํ ๋์
์ง๋ฌธ 1.3. ์ฃผ์ ์ ๋ํ ์ฆ๊ฐ์ ๊ด์ฌ ์ ๋
|
|
|
|
| 44 |
์์ฑ ์ ์ค์์ฌํญ 9.1. ๊ธ์ ์: 1500-2000์ ๋ด์ธ 9.2. ๋ฌธ๋จ ๊ธธ์ด: 3-4๋ฌธ์ฅ ์ด๋ด 9.3. ์๊ฐ์ ๊ตฌ๋ถ: ์์ ๋ชฉ, ๊ตฌ๋ถ์ , ๋ฒํธ ๋ชฉ๋ก ํ์ฉ 9.4. ํค์ค๋งค๋: ์น๊ทผํ๊ณ ์ ๋ฌธ์ ์ธ ๋ํ์ฒด 9.5. ๋ฐ์ดํฐ: ๋ชจ๋ ์ ๋ณด์ ์ถ์ฒ ๋ช
์ 9.6. ๊ฐ๋
์ฑ: ๋ช
ํํ ๋จ๋ฝ ๊ตฌ๋ถ๊ณผ ๊ฐ์กฐ์ ์ฌ์ฉ
|
| 45 |
|
| 46 |
์ด๋ฌํ ํ๋ ์์ํฌ๋ฅผ ๋ฐํ์ผ๋ก, ์์ฒญ๋ฐ์ ์ฃผ์ ์ ๋ํด ์ฒด๊ณ์ ์ด๊ณ ๋งค๋ ฅ์ ์ธ ๋ธ๋ก๊ทธ ํฌ์คํธ๋ฅผ ์์ฑํ๊ฒ ์ต๋๋ค.
|
|
|
|
| 47 |
"""
|
| 48 |
|
| 49 |
+
def test_image_api_connection():
|
| 50 |
+
"""์ด๋ฏธ์ง API ์๋ฒ ์ฐ๊ฒฐ ํ
์คํธ"""
|
| 51 |
+
try:
|
| 52 |
+
client = Client(IMAGE_API_URL)
|
| 53 |
+
return "์ด๋ฏธ์ง API ์ฐ๊ฒฐ ์ฑ๊ณต: ์ ์ ์๋ ์ค"
|
| 54 |
+
except Exception as e:
|
| 55 |
+
logging.error(f"์ด๋ฏธ์ง API ์ฐ๊ฒฐ ํ
์คํธ ์คํจ: {e}")
|
| 56 |
+
return f"์ด๋ฏธ์ง API ์ฐ๊ฒฐ ์คํจ: {e}"
|
| 57 |
|
| 58 |
+
def generate_image(prompt, width=768, height=768, guidance=3.5, inference_steps=30, seed=3):
|
| 59 |
+
"""์ด๋ฏธ์ง ์์ฑ ํจ์"""
|
| 60 |
+
if not prompt:
|
| 61 |
+
return None, "์ค๋ฅ: ํ๋กฌํํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
client = Client(IMAGE_API_URL)
|
| 65 |
+
result = client.predict(
|
| 66 |
+
prompt=prompt,
|
| 67 |
+
width=int(width),
|
| 68 |
+
height=int(height),
|
| 69 |
+
guidance=float(guidance),
|
| 70 |
+
inference_steps=int(inference_steps),
|
| 71 |
+
seed=int(seed),
|
| 72 |
+
do_img2img=False,
|
| 73 |
+
init_image=None,
|
| 74 |
+
image2image_strength=0.8,
|
| 75 |
+
resize_img=True,
|
| 76 |
+
api_name="/generate_image"
|
| 77 |
+
)
|
| 78 |
+
logging.info(f"์ด๋ฏธ์ง ์์ฑ ์ฑ๊ณต: {result[1]}")
|
| 79 |
+
return result[0], f"์ฌ์ฉ๋ ์๋: {result[1]}"
|
| 80 |
+
except Exception as e:
|
| 81 |
+
logging.error(f"์ด๋ฏธ์ง ์์ฑ ์คํจ: {str(e)}")
|
| 82 |
+
return None, f"์ค๋ฅ: {str(e)}"
|
| 83 |
+
|
| 84 |
+
def extract_image_prompt(blog_content, blog_topic):
|
| 85 |
+
"""๋ธ๋ก๊ทธ ๋ด์ฉ์์ ์ด๋ฏธ์ง ์์ฑ์ ์ํ ํ๋กฌํํธ ์ถ์ถ"""
|
| 86 |
+
image_prompt_system = f"""
|
| 87 |
+
๋ค์์ '{blog_topic}'์ ๊ดํ ๋ธ๋ก๊ทธ ๊ธ์
๋๋ค. ์ด ๋ธ๋ก๊ทธ ๊ธ์ ๋ด์ฉ์ ๊ธฐ๋ฐ์ผ๋ก ์ ์ ํ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ธฐ ์ํ
|
| 88 |
+
ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์. ํ๋กฌํํธ๋ ์์ด๋ก ์์ฑํ๊ณ , ๊ตฌ์ฒด์ ์ธ ์๊ฐ์ ์์๋ฅผ ๋ด์์ผ ํฉ๋๋ค.
|
| 89 |
+
ํ๋กฌํํธ๋ง ๋ฐํํ์ธ์(๋ค๋ฅธ ์ค๋ช
์์ด).
|
| 90 |
+
|
| 91 |
+
์์ ํ์:
|
| 92 |
+
"A professional photo of [subject], [specific details], [atmosphere], [lighting], [perspective], high quality, detailed"
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
response = client.messages.create(
|
| 97 |
+
model="claude-3-7-sonnet-20250219",
|
| 98 |
+
max_tokens=150,
|
| 99 |
+
system=image_prompt_system,
|
| 100 |
+
messages=[{"role": "user", "content": blog_content}]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# ์๋ต์์ ํ๋กฌํํธ ์ถ์ถ
|
| 104 |
+
image_prompt = response.content[0].text.strip()
|
| 105 |
+
logging.info(f"์์ฑ๋ ์ด๋ฏธ์ง ํ๋กฌํํธ: {image_prompt}")
|
| 106 |
+
return image_prompt
|
| 107 |
+
except Exception as e:
|
| 108 |
+
logging.error(f"์ด๋ฏธ์ง ํ๋กฌํํธ ์์ฑ ์ค๋ฅ: {e}")
|
| 109 |
+
return f"A professional photo related to {blog_topic}, detailed, high quality"
|
| 110 |
|
| 111 |
def chatbot_interface():
|
| 112 |
st.title("Ginigen Blog")
|
|
|
|
| 114 |
# ๋ชจ๋ธ ๊ณ ์ ์ค์
|
| 115 |
if "ai_model" not in st.session_state:
|
| 116 |
st.session_state["ai_model"] = "claude-3-7-sonnet-20250219"
|
| 117 |
+
|
| 118 |
# ์ธ์
์ํ ์ด๊ธฐํ
|
| 119 |
if "messages" not in st.session_state:
|
| 120 |
st.session_state.messages = []
|
| 121 |
+
|
| 122 |
# ์๋ ์ ์ฅ ๊ธฐ๋ฅ
|
| 123 |
if "auto_save" not in st.session_state:
|
| 124 |
st.session_state.auto_save = True
|
| 125 |
+
|
| 126 |
+
# ์ด๋ฏธ์ง ์์ฑ ํ ๊ธ
|
| 127 |
+
if "generate_image" not in st.session_state:
|
| 128 |
+
st.session_state.generate_image = False
|
| 129 |
+
|
| 130 |
+
# ์ด๋ฏธ์ง API ์ํ
|
| 131 |
+
if "image_api_status" not in st.session_state:
|
| 132 |
+
st.session_state.image_api_status = test_image_api_connection()
|
| 133 |
|
| 134 |
# ๋ํ ๊ธฐ๋ก ๊ด๋ฆฌ (์ฌ์ด๋๋ฐ)
|
| 135 |
st.sidebar.title("๋ํ ๊ธฐ๋ก ๊ด๋ฆฌ")
|
|
|
|
| 137 |
# ์๋ ์ ์ฅ ํ ๊ธ
|
| 138 |
st.session_state.auto_save = st.sidebar.toggle("์๋ ์ ์ฅ", value=st.session_state.auto_save)
|
| 139 |
|
| 140 |
+
# ์ด๋ฏธ์ง ์์ฑ ํ ๊ธ
|
| 141 |
+
st.session_state.generate_image = st.sidebar.toggle("๋ธ๋ก๊ทธ ๊ธ ์์ฑ ํ ์ด๋ฏธ์ง ์๋ ์์ฑ", value=st.session_state.generate_image)
|
| 142 |
+
|
| 143 |
+
# ์ด๋ฏธ์ง API ์ํ ํ์
|
| 144 |
+
st.sidebar.text(st.session_state.image_api_status)
|
| 145 |
+
|
| 146 |
+
# ์ด๋ฏธ์ง ์์ฑ ์ค์ (ํ ๊ธ์ด ์ผ์ ธ ์์ ๋๋ง ํ์)
|
| 147 |
+
if st.session_state.generate_image:
|
| 148 |
+
st.sidebar.subheader("์ด๋ฏธ์ง ์์ฑ ์ค์ ")
|
| 149 |
+
width = st.sidebar.slider("๋๋น", 256, 1024, 768, 64)
|
| 150 |
+
height = st.sidebar.slider("๋์ด", 256, 1024, 768, 64)
|
| 151 |
+
guidance = st.sidebar.slider("๊ฐ์ด๋์ค ์ค์ผ์ผ", 1.0, 20.0, 3.5, 0.1)
|
| 152 |
+
inference_steps = st.sidebar.slider("์ธํผ๋ฐ์ค ์คํ
", 1, 50, 30, 1)
|
| 153 |
+
seed = st.sidebar.number_input("์๋", value=3, min_value=0, step=1)
|
| 154 |
+
else:
|
| 155 |
+
# ๊ธฐ๋ณธ๊ฐ ์ค์
|
| 156 |
+
width, height, guidance, inference_steps, seed = 768, 768, 3.5, 30, 3
|
| 157 |
+
|
| 158 |
# ๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ
|
| 159 |
uploaded_file = st.sidebar.file_uploader("๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ", type=['json'])
|
| 160 |
if uploaded_file is not None:
|
|
|
|
| 179 |
for message in st.session_state.messages:
|
| 180 |
with st.chat_message(message["role"]):
|
| 181 |
st.markdown(message["content"])
|
| 182 |
+
# ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ํ์
|
| 183 |
+
if "image" in message:
|
| 184 |
+
st.image(message["image"], caption=message.get("image_caption", "์์ฑ๋ ์ด๋ฏธ์ง"))
|
| 185 |
|
| 186 |
# ์ฌ์ฉ์ ์
๋ ฅ
|
| 187 |
if prompt := st.chat_input("๋ฌด์์ ๋์๋๋ฆด๊น์?"):
|
|
|
|
| 207 |
|
| 208 |
message_placeholder.markdown(full_response)
|
| 209 |
|
| 210 |
+
# ์ด๋ฏธ์ง ์์ฑ ์ต์
์ด ์ผ์ ธ ์๋ ๊ฒฝ์ฐ
|
| 211 |
+
if st.session_state.generate_image:
|
| 212 |
+
with st.spinner("๋ธ๋ก๊ทธ์ ๋ง๋ ์ด๋ฏธ์ง ์์ฑ ์ค..."):
|
| 213 |
+
# ์ด๋ฏธ์ง ํ๋กฌํํธ ์์ฑ
|
| 214 |
+
image_prompt = extract_image_prompt(full_response, prompt)
|
| 215 |
+
|
| 216 |
+
# ์ด๋ฏธ์ง ์์ฑ
|
| 217 |
+
image, image_caption = generate_image(
|
| 218 |
+
image_prompt,
|
| 219 |
+
width=width,
|
| 220 |
+
height=height,
|
| 221 |
+
guidance=guidance,
|
| 222 |
+
inference_steps=inference_steps,
|
| 223 |
+
seed=seed
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
if image:
|
| 227 |
+
st.image(image, caption=image_caption)
|
| 228 |
+
# ์ด๋ฏธ์ง ์ ๋ณด๋ฅผ ์๋ต์ ํฌํจ
|
| 229 |
+
st.session_state.messages.append({
|
| 230 |
+
"role": "assistant",
|
| 231 |
+
"content": full_response,
|
| 232 |
+
"image": image,
|
| 233 |
+
"image_caption": image_caption
|
| 234 |
+
})
|
| 235 |
+
else:
|
| 236 |
+
st.error(f"์ด๋ฏธ์ง ์์ฑ ์คํจ: {image_caption}")
|
| 237 |
+
st.session_state.messages.append({
|
| 238 |
+
"role": "assistant",
|
| 239 |
+
"content": full_response
|
| 240 |
+
})
|
| 241 |
+
else:
|
| 242 |
+
# ์ด๋ฏธ์ง ์์ฑ ์์ด ์๋ต๋ง ์ ์ฅ
|
| 243 |
+
st.session_state.messages.append({
|
| 244 |
+
"role": "assistant",
|
| 245 |
+
"content": full_response
|
| 246 |
+
})
|
| 247 |
|
| 248 |
# ์๋ ์ ์ฅ ๊ธฐ๋ฅ
|
| 249 |
if st.session_state.auto_save:
|
| 250 |
try:
|
| 251 |
+
# ์ด๋ฏธ์ง ์ ๋ณด๋ ์ ์ฅํ์ง ์์ (JSON์๋ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ์ง์ ์ ์ฅํ ์ ์์)
|
| 252 |
+
save_messages = []
|
| 253 |
+
for msg in st.session_state.messages:
|
| 254 |
+
save_msg = {"role": msg["role"], "content": msg["content"]}
|
| 255 |
+
save_messages.append(save_msg)
|
| 256 |
+
|
| 257 |
with open('chat_history_auto_save.json', 'w', encoding='utf-8') as f:
|
| 258 |
+
json.dump(save_messages, f, ensure_ascii=False, indent=4)
|
| 259 |
except Exception as e:
|
| 260 |
st.sidebar.error(f"์๋ ์ ์ฅ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 261 |
|
| 262 |
# ๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋
|
| 263 |
if st.sidebar.button("๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋"):
|
| 264 |
+
# ์ด๋ฏธ์ง ์ ๋ณด๋ ์ ์ฅํ์ง ์์
|
| 265 |
+
save_messages = []
|
| 266 |
+
for msg in st.session_state.messages:
|
| 267 |
+
save_msg = {"role": msg["role"], "content": msg["content"]}
|
| 268 |
+
save_messages.append(save_msg)
|
| 269 |
+
|
| 270 |
+
json_history = json.dumps(save_messages, indent=4, ensure_ascii=False)
|
| 271 |
st.sidebar.download_button(
|
| 272 |
label="๋ํ ๊ธฐ๋ก ์ ์ฅํ๊ธฐ",
|
| 273 |
data=json_history,
|
|
|
|
| 279 |
chatbot_interface()
|
| 280 |
|
| 281 |
if __name__ == "__main__":
|
| 282 |
+
# requirements.txt ํ์ผ ์์ฑ
|
| 283 |
+
with open("requirements.txt", "w") as f:
|
| 284 |
+
f.write("streamlit>=1.31.0\n")
|
| 285 |
+
f.write("anthropic>=0.18.1\n")
|
| 286 |
+
f.write("gradio-client>=1.8.0\n")
|
| 287 |
+
f.write("requests>=2.32.3\n")
|
| 288 |
+
|
| 289 |
main()
|