Spaces:
Build error
Build error
Commit
·
8c54a98
1
Parent(s):
ef7be1c
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
+
# OpenAI API key
|
| 5 |
+
openai.api_key = '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'
|
| 6 |
+
|
| 7 |
+
# Streamlit app
|
| 8 |
+
def main():
|
| 9 |
+
st.title("GPT-3.5 챗봇")
|
| 10 |
+
init_msgs = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 11 |
+
if st.button("대화 기록 삭제"):
|
| 12 |
+
st.session_state['messages'] = init_msgs
|
| 13 |
+
# Initialize Session State for messages
|
| 14 |
+
if 'messages' not in st.session_state:
|
| 15 |
+
st.session_state['messages'] = init_msgs
|
| 16 |
+
|
| 17 |
+
user_input = st.text_area("Ask something:", height=200)
|
| 18 |
+
submit_button = st.button("Submit")
|
| 19 |
+
conversation = st.empty()
|
| 20 |
+
|
| 21 |
+
if submit_button:
|
| 22 |
+
if user_input:
|
| 23 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 24 |
+
response = openai.ChatCompletion.create(
|
| 25 |
+
model="gpt-3.5-turbo",
|
| 26 |
+
messages=st.session_state.messages
|
| 27 |
+
)
|
| 28 |
+
new_msg = {"role": "assistant",
|
| 29 |
+
"content": response['choices'][0]['message']['content']}
|
| 30 |
+
st.session_state.messages.append(new_msg)
|
| 31 |
+
for message in st.session_state.messages:
|
| 32 |
+
if message["role"] == "user":
|
| 33 |
+
conversation.write(f"User: {message['content']}")
|
| 34 |
+
else:
|
| 35 |
+
conversation.write(f"Assistant: {message['content']}")
|
| 36 |
+
else:
|
| 37 |
+
st.write("Please enter a valid question.")
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
main()
|