BZ269 commited on
Commit
9d211d6
1 Parent(s): 4199b87

Add files via upload

Browse files
Files changed (1) hide show
  1. streamlit/app7.py +99 -0
streamlit/app7.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ import streamlit as st
3
+ import datetime
4
+
5
+ st.title("ChatGPT-like clone")
6
+
7
+ client = OpenAI(api_key=st.secrets['openai']["OPENAI_API_KEY"])
8
+
9
+ if "openai_model" not in st.session_state:
10
+ st.session_state["openai_model"] = "gpt-3.5-turbo"
11
+
12
+ if "messages_1" not in st.session_state:
13
+ st.session_state.messages_1 = []
14
+
15
+ if "messages_2" not in st.session_state:
16
+ st.session_state.messages_2 = []
17
+
18
+ if "start_time" not in st.session_state:
19
+ st.session_state.start_time = None
20
+
21
+ # Create two columns for the two chat interfaces
22
+ col1, col2 = st.columns(2)
23
+
24
+ # First chat interface
25
+ with col1:
26
+ st.subheader("Chat 1")
27
+ for message in st.session_state.messages_1:
28
+ with st.chat_message(message["role"]):
29
+ st.markdown(message["content"])
30
+
31
+ # Second chat interface
32
+ with col2:
33
+ st.subheader("Chat 2")
34
+ for message in st.session_state.messages_2:
35
+ with st.chat_message(message["role"]):
36
+ st.markdown(message["content"])
37
+
38
+ # Timer and Input
39
+ time_left = None
40
+ if st.session_state.start_time:
41
+ time_elapsed = datetime.datetime.now() - st.session_state.start_time
42
+ time_left = datetime.timedelta(minutes=10) - time_elapsed
43
+ st.write(f"Time left: {time_left}")
44
+
45
+ if time_left is None or time_left > datetime.timedelta(0):
46
+ # Chat 1 is active
47
+ prompt = st.text_input("Enter your message for Chat 1:")
48
+ active_chat = 1
49
+ messages = st.session_state.messages_1
50
+ elif time_left and time_left <= datetime.timedelta(0):
51
+ # Chat 2 is active
52
+ prompt = st.text_input("Enter your message for Chat 2:")
53
+ active_chat = 2
54
+ messages = st.session_state.messages_2
55
+
56
+ if prompt:
57
+ if st.session_state.start_time is None:
58
+ st.session_state.start_time = datetime.datetime.now()
59
+
60
+ messages.append({"role": "user", "content": prompt})
61
+
62
+ with (col1 if active_chat == 1 else col2):
63
+ with st.chat_message("user"):
64
+ st.markdown(prompt)
65
+
66
+ with (col1 if active_chat == 1 else col2):
67
+ with st.chat_message("assistant"):
68
+ message_placeholder = st.empty()
69
+ full_response = ""
70
+ for response in client.chat.completions.create(
71
+ model=st.session_state["openai_model"],
72
+ messages=messages,
73
+ stream=True,
74
+ ):
75
+ full_response += (response.choices[0].delta.content or "")
76
+ message_placeholder.markdown(full_response + "▌")
77
+ message_placeholder.markdown(full_response)
78
+ messages.append({"role": "assistant", "content": full_response})
79
+
80
+
81
+ # import streamlit as st
82
+ # import time
83
+ # def count_down(ts):
84
+ # with st.empty():
85
+ # while ts:
86
+ # mins, secs = divmod(ts, 60)
87
+ # time_now = '{:02d}:{:02d}'.format(mins, secs)
88
+ # st.header(f"{time_now}")
89
+ # time.sleep(1)
90
+ # ts -= 1
91
+ # st.write("Time Up!")
92
+ # def main():
93
+ # st.title("Pomodoro")
94
+ # time_minutes = st.number_input('Enter the time in minutes ', min_value=1, value=25)
95
+ # time_in_seconds = time_minutes * 60
96
+ # if st.button("START"):
97
+ # count_down(int(time_in_seconds))
98
+ # if __name__ == '__main__':
99
+ # main()