Spbou4-hilma commited on
Commit
e82962e
·
verified ·
1 Parent(s): 0da2617

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ 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("Chatbot Interface")
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
+
76
+ chatbot_interface()
77
+
78
+ if __name__ == "__main__":
79
+ main()