dkwak commited on
Commit
424048b
1 Parent(s): 3e34094

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +60 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import random
4
+ import time
5
+
6
+
7
+ if __name__ == '__main__':
8
+ # Set up OpenAI API key
9
+ openai.api_key = "API"
10
+
11
+ system_message = {"role": "system", "content": "You are ChatGPT, a large language model trained by OpenAI, based on the GPT-3.5 architecture. Your answer should be as detailed as possible."}
12
+
13
+ with gr.Blocks(css="footer {visibility: hidden}", title = 'Hello, Yoda.') as demo:
14
+ gr.Markdown("""<h1><center>Hello, Yoda.</center></h1>""")
15
+ chatbot = gr.Chatbot()
16
+ msg = gr.Textbox(show_label=False, placeholder="Ask me a question and press enter.")
17
+ btn=gr.Button("submit")
18
+ clear = gr.Button("clear")
19
+ # messages_history = [system_message]
20
+
21
+ state = gr.State([])
22
+
23
+ def user(user_message, history):
24
+ return "", history + [[user_message, None]]
25
+
26
+ def bot(history, messages_history):
27
+ user_message = history[-1][0]
28
+ bot_message, messages_history = ask_gpt(user_message, messages_history)
29
+ messages_history += [{"role": "assistant", "content": bot_message}]
30
+ history[-1][1] = bot_message
31
+ time.sleep(1)
32
+ return history, messages_history
33
+
34
+ def ask_gpt(message, messages_history):
35
+ if len(messages_history) == 0:
36
+ messages_history = [system_message]
37
+ messages_history += [{"role": "user", "content": message}]
38
+ response = openai.ChatCompletion.create(
39
+ model="gpt-3.5-turbo",
40
+ messages=messages_history
41
+ )
42
+ return response['choices'][0]['message']['content'], messages_history
43
+
44
+ def init_history(messages_history):
45
+ messages_history = []
46
+ messages_history += [system_message]
47
+ return messages_history
48
+
49
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
50
+ bot, [chatbot, state], [chatbot, state]
51
+ )
52
+
53
+ btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
54
+ bot, [chatbot, state], [chatbot, state]
55
+ )
56
+
57
+ clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state], [state])
58
+
59
+ demo.launch(show_api = False, auth = ('ID', 'PWD'))
60
+ #demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai
2
+ gradio