Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,79 +3,95 @@ import streamlit as st
|
|
3 |
import json
|
4 |
import anthropic
|
5 |
|
6 |
-
#
|
7 |
api_key = os.environ.get("API_KEY")
|
8 |
client = anthropic.Anthropic(api_key=api_key)
|
9 |
|
10 |
-
def
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
|
|
|
|
|
|
14 |
model_options = [
|
15 |
-
"claude-3-
|
16 |
-
"claude-3-sonnet-20240229",
|
17 |
-
"claude-3-haiku-20240307"
|
18 |
]
|
19 |
-
model_selected = st.sidebar.selectbox("
|
|
|
20 |
if "ai_model" not in st.session_state or st.session_state["ai_model"] != model_selected:
|
21 |
st.session_state["ai_model"] = model_selected
|
22 |
|
23 |
-
#
|
24 |
if "messages" not in st.session_state:
|
25 |
st.session_state.messages = []
|
26 |
|
27 |
-
#
|
28 |
-
st.sidebar.title("
|
29 |
-
uploaded_file = st.sidebar.file_uploader("
|
30 |
if uploaded_file is not None:
|
31 |
st.session_state.messages = json.load(uploaded_file)
|
32 |
|
33 |
-
#
|
34 |
for message in st.session_state.messages:
|
35 |
with st.chat_message(message["role"]):
|
36 |
st.markdown(message["content"])
|
37 |
|
38 |
-
#
|
39 |
-
if prompt := st.chat_input("
|
40 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
41 |
with st.chat_message("user"):
|
42 |
st.markdown(prompt)
|
43 |
|
44 |
-
#
|
45 |
with st.chat_message("assistant"):
|
46 |
-
message_placeholder = st.empty()
|
47 |
full_response = ""
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
#
|
50 |
with client.messages.stream(
|
51 |
max_tokens=1024,
|
52 |
-
messages=
|
53 |
model=st.session_state["ai_model"]
|
54 |
) as stream:
|
55 |
for text in stream.text_stream:
|
56 |
full_response += str(text) if text is not None else ""
|
57 |
-
message_placeholder.markdown(full_response + "▌")
|
58 |
-
|
59 |
-
message_placeholder.markdown(full_response)
|
|
|
60 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
61 |
|
62 |
-
#
|
63 |
-
if st.button("
|
64 |
json_history = json.dumps(st.session_state.messages, indent=4)
|
65 |
st.download_button(
|
66 |
-
label="
|
67 |
data=json_history,
|
68 |
file_name="chat_history.json",
|
69 |
mime="application/json"
|
70 |
)
|
71 |
|
72 |
def main():
|
73 |
-
|
74 |
-
|
75 |
-
logo_path =
|
76 |
-
|
|
|
|
|
77 |
|
78 |
chatbot_interface()
|
79 |
|
80 |
if __name__ == "__main__":
|
81 |
-
main()
|
|
|
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 |
+
def get_system_prompt():
|
11 |
+
return """당신은 친절하고 전문적인 AI 어시스턴트입니다.
|
12 |
+
다음 가이드라인을 따라주세요:
|
13 |
+
- 정확하고 유용한 정보를 제공합니다
|
14 |
+
- 불확실한 경우 그 사실을 인정합니다
|
15 |
+
- 위험하거나 해로운 조언은 하지 않습니다
|
16 |
+
- 개인정보는 보호합니다
|
17 |
+
- 예의 바르고 공손하게 응답합니다
|
18 |
+
- 한국어로 자연스럽게 대화합니다"""
|
19 |
|
20 |
+
def chatbot_interface():
|
21 |
+
st.title("AI 어시스턴트와 대화하기")
|
22 |
+
|
23 |
+
# 모델 선택
|
24 |
model_options = [
|
25 |
+
"claude-3-5-sonnet-20241022"
|
|
|
|
|
26 |
]
|
27 |
+
model_selected = st.sidebar.selectbox("클로드 모델 선택:", model_options)
|
28 |
+
|
29 |
if "ai_model" not in st.session_state or st.session_state["ai_model"] != model_selected:
|
30 |
st.session_state["ai_model"] = model_selected
|
31 |
|
32 |
+
# 세션 상태 초기화
|
33 |
if "messages" not in st.session_state:
|
34 |
st.session_state.messages = []
|
35 |
|
36 |
+
# 대화 기록 불러오기
|
37 |
+
st.sidebar.title("대화 기록")
|
38 |
+
uploaded_file = st.sidebar.file_uploader("대화 기록 JSON 파일 불러오기")
|
39 |
if uploaded_file is not None:
|
40 |
st.session_state.messages = json.load(uploaded_file)
|
41 |
|
42 |
+
# 메시지 표시
|
43 |
for message in st.session_state.messages:
|
44 |
with st.chat_message(message["role"]):
|
45 |
st.markdown(message["content"])
|
46 |
|
47 |
+
# 사용자 입력
|
48 |
+
if prompt := st.chat_input("무엇을 도와드릴까요?"):
|
49 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
50 |
with st.chat_message("user"):
|
51 |
st.markdown(prompt)
|
52 |
|
53 |
+
# AI 응답 생성
|
54 |
with st.chat_message("assistant"):
|
55 |
+
message_placeholder = st.empty()
|
56 |
full_response = ""
|
57 |
+
|
58 |
+
# 시스템 프롬프트를 포함한 메시지 준비
|
59 |
+
messages = [{"role": "system", "content": get_system_prompt()}]
|
60 |
+
messages.extend([{"role": m["role"], "content": m["content"]} for m in st.session_state.messages])
|
61 |
|
62 |
+
# Claude API 호출
|
63 |
with client.messages.stream(
|
64 |
max_tokens=1024,
|
65 |
+
messages=messages,
|
66 |
model=st.session_state["ai_model"]
|
67 |
) as stream:
|
68 |
for text in stream.text_stream:
|
69 |
full_response += str(text) if text is not None else ""
|
70 |
+
message_placeholder.markdown(full_response + "▌")
|
71 |
+
|
72 |
+
message_placeholder.markdown(full_response)
|
73 |
+
|
74 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
75 |
|
76 |
+
# 대화 기록 다운로드
|
77 |
+
if st.button("대화 기록 다운로드"):
|
78 |
json_history = json.dumps(st.session_state.messages, indent=4)
|
79 |
st.download_button(
|
80 |
+
label="대화 기록 저장하기",
|
81 |
data=json_history,
|
82 |
file_name="chat_history.json",
|
83 |
mime="application/json"
|
84 |
)
|
85 |
|
86 |
def main():
|
87 |
+
# 로고 이미지 표시
|
88 |
+
logo_path = "logo.png"
|
89 |
+
st.sidebar.image(logo_path, width=150)
|
90 |
+
|
91 |
+
logo_path = "logo_transparent_background.png"
|
92 |
+
st.sidebar.image(logo_path, width=250)
|
93 |
|
94 |
chatbot_interface()
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
+
main()
|