zjrwtx commited on
Commit
2fd3784
·
1 Parent(s): 3d26e68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -24
app.py CHANGED
@@ -1,29 +1,63 @@
1
- import openai
2
  import streamlit as st
 
 
 
3
 
4
- openai.api_key ="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" # Replace this with your API key: https://beta.openai.com/docs/quickstart/add-your-api-key
 
 
5
 
6
- def openai_chat(prompt):
7
- completions = openai.Completion.create(
8
- engine="text-davinci-003",
9
- prompt="请给出这道题的答案与解释:"+prompt,
10
- max_tokens=1024,
11
- n=1,
12
- temperature=0.5,
 
 
13
  )
14
 
15
- message = completions.choices[0].text
16
- return message.strip()
17
-
18
- def chatbot(history = []):
19
-
20
- input_text = st.text_input("Enter your question:")
21
- if st.button('Submit'):
22
- output = openai_chat(input_text)
23
- history.append((input_text, output))
24
- st.write("Robot: ",output)
25
- st.write("History:",history)
26
-
27
-
28
- if __name__ == "__main__":
29
- chatbot()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from streamlit_chat import message
3
+
4
+ import openai
5
 
6
+ openai.api_key = "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"
7
+ # openAI code
8
+ def openai_create(prompt):
9
 
10
+ response = openai.Completion.create(
11
+ model="text-davinci-003",
12
+ prompt=prompt,
13
+ temperature=0.9,
14
+ max_tokens=150,
15
+ top_p=1,
16
+ frequency_penalty=0,
17
+ presence_penalty=0.6,
18
+ stop=[" Human:", " AI:"]
19
  )
20
 
21
+ return response.choices[0].text
22
+
23
+
24
+ def chatgpt_clone(input):
25
+ output = openai_create(input)
26
+ return output
27
+
28
+ # Streamlit App
29
+ st.set_page_config(
30
+ page_title="Streamlit Chat - Demo",
31
+ page_icon=":robot:"
32
+ )
33
+
34
+ st.header("🏅和小译学长聊聊天")
35
+
36
+ if 'generated' not in st.session_state:
37
+ st.session_state['generated'] = []
38
+
39
+ if 'past' not in st.session_state:
40
+ st.session_state['past'] = []
41
+
42
+
43
+ def get_text():
44
+ input_text = st.text_input("🏅请告诉小译学长你想说的吧: ", key="input")
45
+ if st.button("发送"):
46
+ return input_text
47
+ return None
48
+
49
+
50
+
51
+ user_input = get_text()
52
+
53
+ if user_input:
54
+
55
+ output = chatgpt_clone(user_input)
56
+ st.session_state.past.append(user_input)
57
+ st.session_state.generated.append(output)
58
+
59
+ if st.session_state['generated']:
60
+
61
+ for i in range(len(st.session_state['generated'])-1, -1, -1):
62
+ message(st.session_state["generated"][i], key=str(i))
63
+ message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')