seawolf2357 commited on
Commit
db9828e
·
verified ·
1 Parent(s): c8a1f82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -30
app.py CHANGED
@@ -3,79 +3,95 @@ import streamlit as st
3
  import json
4
  import anthropic
5
 
6
- # Configuration de l'API
7
  api_key = os.environ.get("API_KEY")
8
  client = anthropic.Anthropic(api_key=api_key)
9
 
10
- def chatbot_interface():
11
- st.title("")
 
 
 
 
 
 
 
12
 
13
- # Choix du modèle
 
 
 
14
  model_options = [
15
- "claude-3-opus-20240229",
16
- "claude-3-sonnet-20240229",
17
- "claude-3-haiku-20240307"
18
  ]
19
- model_selected = st.sidebar.selectbox("Choisissez un modèle de Claude :", model_options)
 
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
- # Initialisation de l'état de session
24
  if "messages" not in st.session_state:
25
  st.session_state.messages = []
26
 
27
- # Charger l'historique
28
- st.sidebar.title("Historique")
29
- uploaded_file = st.sidebar.file_uploader("Chargez un fichier historique JSON")
30
  if uploaded_file is not None:
31
  st.session_state.messages = json.load(uploaded_file)
32
 
33
- # Afficher les messages
34
  for message in st.session_state.messages:
35
  with st.chat_message(message["role"]):
36
  st.markdown(message["content"])
37
 
38
- # Entrée utilisateur
39
- if prompt := st.chat_input("What is up?"):
40
  st.session_state.messages.append({"role": "user", "content": prompt})
41
  with st.chat_message("user"):
42
  st.markdown(prompt)
43
 
44
- # Appel API pour obtenir la réponse de l'assistant
45
  with st.chat_message("assistant"):
46
- message_placeholder = st.empty() # Placeholder pour les réponses progressives
47
  full_response = ""
 
 
 
 
48
 
49
- # Préparation de la requête à envoyer à Claude
50
  with client.messages.stream(
51
  max_tokens=1024,
52
- messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.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 + "▌") # Afficher la réponse progressive avec un curseur
58
-
59
- message_placeholder.markdown(full_response) # Afficher la réponse finale
 
60
  st.session_state.messages.append({"role": "assistant", "content": full_response})
61
 
62
- # Télécharger l'historique
63
- if st.button("Télécharger l'historique"):
64
  json_history = json.dumps(st.session_state.messages, indent=4)
65
  st.download_button(
66
- label="Télécharger l'historique",
67
  data=json_history,
68
  file_name="chat_history.json",
69
  mime="application/json"
70
  )
71
 
72
  def main():
73
- logo_path = "logo.png" # Remplacez 'logo.png' par le chemin réel vers votre image
74
- st.sidebar.image(logo_path, width=150) # Ajustez la largeur selon vos besoins
75
- logo_path = "logo_transparent_background.png" # Remplacez 'logo.png' par le chemin réel vers votre image
76
- st.sidebar.image(logo_path, width=250) # Ajustez la largeur selon vos besoins
 
 
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()