karthick965938 commited on
Commit
a61750b
·
1 Parent(s): b9cc6e8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import streamlit as st
3
+
4
+ st.title("ChatGPT Demo")
5
+
6
+ openai.api_key = os.environ["OPENAI_API_KEY"]
7
+
8
+ if "openai_model" not in st.session_state:
9
+ st.session_state["openai_model"] = "gpt-3.5-turbo"
10
+
11
+ if "messages" not in st.session_state:
12
+ st.session_state.messages = []
13
+
14
+ for message in st.session_state.messages:
15
+ with st.chat_message(message["role"]):
16
+ st.markdown(message["content"])
17
+
18
+ if prompt := st.chat_input("What is up?"):
19
+ st.session_state.messages.append({"role": "user", "content": prompt})
20
+ with st.chat_message("user"):
21
+ st.markdown(prompt)
22
+
23
+ with st.chat_message("assistant"):
24
+ message_placeholder = st.empty()
25
+ full_response = ""
26
+ for response in openai.ChatCompletion.create(
27
+ model=st.session_state["openai_model"],
28
+ messages=[
29
+ {"role": m["role"], "content": m["content"]}
30
+ for m in st.session_state.messages
31
+ ],
32
+ stream=True,
33
+ ):
34
+ full_response += response.choices[0].delta.get("content", "")
35
+ message_placeholder.markdown(full_response + "▌")
36
+ message_placeholder.markdown(full_response)
37
+ st.session_state.messages.append({"role": "assistant", "content": full_response})