M-lai commited on
Commit
efde302
·
verified ·
1 Parent(s): 1c843e4

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +95 -0
  2. logo.png +0 -0
  3. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+ import openai
4
+ import os
5
+
6
+ # API Key and Client Initialization
7
+ api_key = st.secrets["OPENAI_API_KEY"]
8
+ client = openai.OpenAI(api_key=api_key)
9
+
10
+ # Environment variables for username and password
11
+ USER = os.environ.get("MY_APP_USER")
12
+ PASSWORD = os.environ.get("MY_APP_PASSWORD")
13
+
14
+ def check_credentials(username, password):
15
+ return username == USER and password == PASSWORD
16
+
17
+ def login_form():
18
+ with st.form("login_form"):
19
+ username = st.text_input("Username")
20
+ password = st.text_input("Password", type="password")
21
+ submitted = st.form_submit_button("Login")
22
+ if submitted and check_credentials(username, password):
23
+ return True
24
+ if submitted:
25
+ st.error("Incorrect Username or Password")
26
+ return False
27
+
28
+ def chatbot_interface():
29
+ st.title("")
30
+
31
+ model_options = ["gpt-4-turbo", "gpt-3.5-turbo"]
32
+ model_selection = st.sidebar.selectbox("Choose your model:", model_options)
33
+
34
+ # Initialize the openai_model in session state
35
+ if "openai_model" not in st.session_state:
36
+ st.session_state.openai_model = model_selection
37
+
38
+ # API Key and Client Initialization
39
+ api_key = st.secrets["OPENAI_API_KEY"]
40
+ client = openai.OpenAI(api_key=api_key)
41
+
42
+ if "messages" not in st.session_state:
43
+ st.session_state.messages = []
44
+
45
+ # Load history
46
+ uploaded_file = st.file_uploader("Upload a JSON history file")
47
+ if uploaded_file is not None:
48
+ st.session_state.messages = json.load(uploaded_file)
49
+
50
+ # Display messages
51
+ for message in st.session_state.messages:
52
+ with st.chat_message(message["role"]):
53
+ st.markdown(message["content"])
54
+
55
+ # User input
56
+ if prompt := st.chat_input("What is up?"):
57
+ st.session_state.messages.append({"role": "user", "content": prompt})
58
+ with st.chat_message("user"):
59
+ st.markdown(prompt)
60
+ with st.chat_message("assistant"):
61
+ stream = client.chat.completions.create(
62
+ model=st.session_state.openai_model,
63
+ messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
64
+ stream=True,
65
+ )
66
+ response = st.write_stream(stream)
67
+ st.session_state.messages.append({"role": "assistant", "content": response})
68
+
69
+ # Download history
70
+ if st.button("Download History"):
71
+ json_history = json.dumps(st.session_state.messages, indent=4)
72
+ st.download_button(
73
+ label="Download History",
74
+ data=json_history,
75
+ file_name="chat_history.json",
76
+ mime="application/json"
77
+ )
78
+
79
+
80
+ def main():
81
+ st.sidebar.title("Login")
82
+ logo_path = "logo.png" # Remplacez 'logo.png' par le chemin réel vers votre image
83
+ st.sidebar.image(logo_path, width=150) # Ajustez la largeur selon vos besoins
84
+
85
+ if 'logged_in' not in st.session_state:
86
+ st.session_state['logged_in'] = False
87
+
88
+ if st.session_state['logged_in']:
89
+ chatbot_interface()
90
+ else:
91
+ if login_form():
92
+ st.session_state['logged_in'] = True
93
+
94
+ if __name__ == "__main__":
95
+ main()
logo.png ADDED
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai
2
+ streamlit