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