Spaces:
Running
Running
File size: 5,924 Bytes
e82962e db9828e e82962e 55ec4cb 9a73ea8 55ec4cb db9828e 55ec4cb 78f19de 9aaa4bf 78f19de 9aaa4bf 78f19de 9aaa4bf 78f19de 9aaa4bf 78f19de 9aaa4bf 78f19de 9aaa4bf 78f19de 9aaa4bf 78f19de 9aaa4bf 78f19de 9aaa4bf 78f19de 9aaa4bf 78f19de 9aaa4bf 78f19de e82962e 55ec4cb db9828e 0228286 55ec4cb 75bce88 55ec4cb e82962e 55ec4cb e82962e 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 |
import os
import streamlit as st
import json
import anthropic
# API ์ค์
api_key = os.environ.get("API_KEY")
client = anthropic.Anthropic(api_key=api_key)
# ์ต๋ ํ ํฐ ์ ์ค์ (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 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
# ๋ํ ๊ธฐ๋ก ๊ด๋ฆฌ (์ฌ์ด๋๋ฐ)
st.sidebar.title("๋ํ ๊ธฐ๋ก ๊ด๋ฆฌ")
# ์๋ ์ ์ฅ ํ ๊ธ
st.session_state.auto_save = st.sidebar.toggle("์๋ ์ ์ฅ", value=st.session_state.auto_save)
# ๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ
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 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)
st.session_state.messages.append({"role": "assistant", "content": full_response})
# ์๋ ์ ์ฅ ๊ธฐ๋ฅ
if st.session_state.auto_save:
try:
with open('chat_history_auto_save.json', 'w', encoding='utf-8') as f:
json.dump(st.session_state.messages, f, ensure_ascii=False, indent=4)
except Exception as e:
st.sidebar.error(f"์๋ ์ ์ฅ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
# ๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋
if st.sidebar.button("๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋"):
json_history = json.dumps(st.session_state.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__":
main() |