Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
import json
|
|
@@ -7,8 +10,12 @@ import anthropic
|
|
| 7 |
api_key = os.environ.get("API_KEY")
|
| 8 |
client = anthropic.Anthropic(api_key=api_key)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
def get_system_prompt():
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
[๋ชฉ์ ๊ณผ ์ ๋ฌธ์ฑ]
|
| 14 |
- AI ์๋น์ค ๊ฐ๋ฐ์ ํนํ๋ ์ ๋ฌธ ์ด์์คํดํธ๋ก์, Python๊ณผ Gradio ๊ธฐ๋ฐ AI ์๋น์ค ๊ฐ๋ฐ์ ์ง์ํฉ๋๋ค
|
|
@@ -67,75 +74,99 @@ def get_system_prompt():
|
|
| 67 |
|
| 68 |
์ ๋ ํญ์ Mouse-II๋ก์ ์ ๋ฌธ์ ์ด๊ณ ์ ํํ AI ์๋น์ค ๊ฐ๋ฐ ๊ฐ์ด๋๋ฅผ ์ ๊ณตํ๊ฒ ์ต๋๋ค."""
|
| 69 |
|
|
|
|
|
|
|
| 70 |
def chatbot_interface():
|
| 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 |
def main():
|
| 138 |
-
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|
| 141 |
-
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
|
| 4 |
import os
|
| 5 |
import streamlit as st
|
| 6 |
import json
|
|
|
|
| 10 |
api_key = os.environ.get("API_KEY")
|
| 11 |
client = anthropic.Anthropic(api_key=api_key)
|
| 12 |
|
| 13 |
+
# ์ต๋ ํ ํฐ ์ ์ค์ (Claude-3 Sonnet์ ์ต๋ ํ ํฐ ์)
|
| 14 |
+
MAX_TOKENS = 200000
|
| 15 |
+
|
| 16 |
def get_system_prompt():
|
| 17 |
+
|
| 18 |
+
return """์ ์ด๋ฆ์ Mouse-II์
๋๋ค.
|
| 19 |
|
| 20 |
[๋ชฉ์ ๊ณผ ์ ๋ฌธ์ฑ]
|
| 21 |
- AI ์๋น์ค ๊ฐ๋ฐ์ ํนํ๋ ์ ๋ฌธ ์ด์์คํดํธ๋ก์, Python๊ณผ Gradio ๊ธฐ๋ฐ AI ์๋น์ค ๊ฐ๋ฐ์ ์ง์ํฉ๋๋ค
|
|
|
|
| 74 |
|
| 75 |
์ ๋ ํญ์ Mouse-II๋ก์ ์ ๋ฌธ์ ์ด๊ณ ์ ํํ AI ์๋น์ค ๊ฐ๋ฐ ๊ฐ์ด๋๋ฅผ ์ ๊ณตํ๊ฒ ์ต๋๋ค."""
|
| 76 |
|
| 77 |
+
|
| 78 |
+
|
| 79 |
def chatbot_interface():
|
| 80 |
+
st.title("Mouse-II์ ๋ํํ๊ธฐ")
|
| 81 |
+
|
| 82 |
+
# ๋ชจ๋ธ ๊ณ ์ ์ค์
|
| 83 |
+
if "ai_model" not in st.session_state:
|
| 84 |
+
st.session_state["ai_model"] = "claude-3-5-sonnet-20241022"
|
| 85 |
+
# ์ธ์
์ํ ์ด๊ธฐํ
|
| 86 |
+
if "messages" not in st.session_state:
|
| 87 |
+
st.session_state.messages = []
|
| 88 |
+
|
| 89 |
+
# ์๋ ์ ์ฅ ๊ธฐ๋ฅ
|
| 90 |
+
if "auto_save" not in st.session_state:
|
| 91 |
+
st.session_state.auto_save = True
|
| 92 |
+
|
| 93 |
+
# ๋ํ ๊ธฐ๋ก ๊ด๋ฆฌ (์ฌ์ด๋๋ฐ)
|
| 94 |
+
st.sidebar.title("๋ํ ๊ธฐ๋ก ๊ด๋ฆฌ")
|
| 95 |
+
|
| 96 |
+
# ์๋ ์ ์ฅ ํ ๊ธ
|
| 97 |
+
st.session_state.auto_save = st.sidebar.toggle("์๋ ์ ์ฅ", value=st.session_state.auto_save)
|
| 98 |
+
|
| 99 |
+
# ๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ
|
| 100 |
+
uploaded_file = st.sidebar.file_uploader("๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ", type=['json'])
|
| 101 |
+
if uploaded_file is not None:
|
| 102 |
+
try:
|
| 103 |
+
content = uploaded_file.getvalue().decode()
|
| 104 |
+
if content.strip():
|
| 105 |
+
st.session_state.messages = json.loads(content)
|
| 106 |
+
st.sidebar.success("๋ํ ๊ธฐ๋ก์ ์ฑ๊ณต์ ์ผ๋ก ๋ถ๋ฌ์์ต๋๋ค!")
|
| 107 |
+
else:
|
| 108 |
+
st.sidebar.warning("์
๋ก๋๋ ํ์ผ์ด ๋น์ด ์์ต๋๋ค.")
|
| 109 |
+
except json.JSONDecodeError:
|
| 110 |
+
st.sidebar.error("์ฌ๋ฐ๋ฅธ JSON ํ์์ ํ์ผ์ด ์๋๋๋ค.")
|
| 111 |
+
except Exception as e:
|
| 112 |
+
st.sidebar.error(f"ํ์ผ ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
|
| 113 |
+
|
| 114 |
+
# ๋ํ ๊ธฐ๋ก ์ด๊ธฐํ ๋ฒํผ
|
| 115 |
+
if st.sidebar.button("๋ํ ๊ธฐ๋ก ์ด๊ธฐํ"):
|
| 116 |
+
st.session_state.messages = []
|
| 117 |
+
st.sidebar.success("๋ํ ๊ธฐ๋ก์ด ์ด๊ธฐํ๋์์ต๋๋ค.")
|
| 118 |
+
|
| 119 |
+
# ๋ฉ์์ง ํ์
|
| 120 |
+
for message in st.session_state.messages:
|
| 121 |
+
with st.chat_message(message["role"]):
|
| 122 |
+
st.markdown(message["content"])
|
| 123 |
+
|
| 124 |
+
# ์ฌ์ฉ์ ์
๋ ฅ
|
| 125 |
+
if prompt := st.chat_input("๋ฌด์์ ๋์๋๋ฆด๊น์?"):
|
| 126 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 127 |
+
with st.chat_message("user"):
|
| 128 |
+
st.markdown(prompt)
|
| 129 |
+
|
| 130 |
+
# AI ์๋ต ์์ฑ
|
| 131 |
+
with st.chat_message("assistant"):
|
| 132 |
+
message_placeholder = st.empty()
|
| 133 |
+
full_response = ""
|
| 134 |
+
|
| 135 |
+
# API ํธ์ถ
|
| 136 |
+
with client.messages.stream(
|
| 137 |
+
max_tokens=MAX_TOKENS,
|
| 138 |
+
system=get_system_prompt(),
|
| 139 |
+
messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
|
| 140 |
+
model=st.session_state["ai_model"]
|
| 141 |
+
) as stream:
|
| 142 |
+
for text in stream.text_stream:
|
| 143 |
+
full_response += str(text) if text is not None else ""
|
| 144 |
+
message_placeholder.markdown(full_response + "โ")
|
| 145 |
+
|
| 146 |
+
message_placeholder.markdown(full_response)
|
| 147 |
+
|
| 148 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 149 |
+
|
| 150 |
+
# ์๋ ์ ์ฅ ๊ธฐ๋ฅ
|
| 151 |
+
if st.session_state.auto_save:
|
| 152 |
+
try:
|
| 153 |
+
with open('chat_history_auto_save.json', 'w', encoding='utf-8') as f:
|
| 154 |
+
json.dump(st.session_state.messages, f, ensure_ascii=False, indent=4)
|
| 155 |
+
except Exception as e:
|
| 156 |
+
st.sidebar.error(f"์๋ ์ ์ฅ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 157 |
+
|
| 158 |
+
# ๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋
|
| 159 |
+
if st.sidebar.button("๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋"):
|
| 160 |
+
json_history = json.dumps(st.session_state.messages, indent=4, ensure_ascii=False)
|
| 161 |
+
st.sidebar.download_button(
|
| 162 |
+
label="๋ํ ๊ธฐ๋ก ์ ์ฅํ๊ธฐ",
|
| 163 |
+
data=json_history,
|
| 164 |
+
file_name="chat_history.json",
|
| 165 |
+
mime="application/json"
|
| 166 |
+
)
|
| 167 |
|
| 168 |
def main():
|
| 169 |
+
chatbot_interface()
|
| 170 |
|
| 171 |
if __name__ == "__main__":
|
| 172 |
+
main()
|