Hugo Garcia-Cotte commited on
Commit
b7524fd
1 Parent(s): 66d92e7

Add application file

Browse files
Files changed (1) hide show
  1. app.py +55 -2
app.py CHANGED
@@ -1,4 +1,57 @@
 
 
 
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
1
+ import os
2
+
3
+ import openai
4
  import streamlit as st
5
+ from streamlit_chat import message
6
+
7
+ st.set_page_config(
8
+ page_title="ChatGPT",
9
+ page_icon=":robot:"
10
+ )
11
+
12
+ st.header("ChatGPT")
13
+
14
+ if 'generated' not in st.session_state:
15
+ st.session_state['generated'] = []
16
+ if 'past' not in st.session_state:
17
+ st.session_state['past'] = []
18
+ if 'messages' not in st.session_state:
19
+ st.session_state['messages'] = [
20
+ {"role": "system", "content": "You are a helpful assistant that translates English to Chinese."}]
21
+
22
+
23
+ def query(question):
24
+ st.session_state['messages'].append({"role": "user", "content": question})
25
+
26
+ openai.api_key = os.environ['OPENAI_API_KEY']
27
+ response = openai.ChatCompletion.create(
28
+ model="gpt-3.5-turbo",
29
+ temperature=1.2,
30
+ messages=st.session_state['messages'],
31
+ max_tokens=2000
32
+ )
33
+
34
+ answer = response['choices'][0]['message']['content']
35
+ st.session_state['messages'].append({"role": "assistant", "content": answer})
36
+ return answer
37
+
38
+
39
+ with st.form("my_form"):
40
+ # Create a text input for the first field
41
+ input_text = st.text_input("You: ", "Hello, how are you?", key="input")
42
+
43
+ # Every form must have a submit button.
44
+ # c1, c2 = st.columns([2, 2])
45
+ submitted = st.form_submit_button("🤖 Submit")
46
+
47
+ if submitted and input_text:
48
+ output = query(input_text, )
49
+ if output:
50
+ st.session_state.past.append(input_text)
51
+ st.session_state.generated.append(output)
52
+
53
+ if st.session_state['generated']:
54
 
55
+ for i in range(len(st.session_state['generated']) - 1, -1, -1):
56
+ message(st.session_state["generated"][i], key=str(i))
57
+ message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')