Spaces:
Sleeping
Sleeping
Merge pull request #1 from MZhao-ouo/main
Browse files- ChuanhuChatbot.py +17 -18
ChuanhuChatbot.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
|
4 |
-
my_api_key = ""
|
5 |
initial_prompt = "You are a helpful assistant."
|
6 |
|
7 |
class ChatGPT:
|
8 |
-
|
9 |
def __init__(self, apikey) -> None:
|
10 |
openai.api_key = apikey
|
11 |
self.system = {"role": "system", "content": initial_prompt}
|
@@ -19,27 +18,27 @@ class ChatGPT:
|
|
19 |
response = response["choices"][0]["message"]["content"]
|
20 |
return response
|
21 |
|
22 |
-
def predict(self, input_sentence, context):
|
|
|
|
|
23 |
context.append({"role": "user", "content": f"{input_sentence}"})
|
24 |
|
25 |
response = self.get_response(context)
|
26 |
|
27 |
context.append({"role": "assistant", "content": response})
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
for i in range(0, len(context), 2):
|
32 |
-
response.append((context[i]["content"], context[i+1]["content"]))
|
33 |
-
|
34 |
-
return response, context
|
35 |
|
36 |
-
def retry(self, context):
|
|
|
|
|
37 |
response = self.get_response(context[:-1])
|
38 |
context[-1] = {"role": "assistant", "content": response}
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
return response, context
|
43 |
|
44 |
def update_system(self, new_system_prompt):
|
45 |
self.system = {"role": "system", "content": new_system_prompt}
|
@@ -61,14 +60,14 @@ with gr.Blocks() as demo:
|
|
61 |
emptyBth = gr.Button("重置")
|
62 |
retryBth = gr.Button("再试一次")
|
63 |
|
64 |
-
system = gr.Textbox(show_label=True, placeholder="
|
65 |
-
syspromptTxt = gr.Textbox(show_label=
|
66 |
|
67 |
-
txt.submit(mychatGPT.predict, [txt, state], [chatbot, state], show_progress=True)
|
68 |
txt.submit(lambda :"", None, txt)
|
69 |
emptyBth.click(reset_state, outputs=[chatbot, state])
|
70 |
system.submit(mychatGPT.update_system, system, syspromptTxt)
|
71 |
system.submit(lambda :"", None, system)
|
72 |
-
retryBth.click(mychatGPT.retry, [state], [chatbot, state], show_progress=True)
|
73 |
|
74 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
|
4 |
+
my_api_key = "" # input your api_key
|
5 |
initial_prompt = "You are a helpful assistant."
|
6 |
|
7 |
class ChatGPT:
|
|
|
8 |
def __init__(self, apikey) -> None:
|
9 |
openai.api_key = apikey
|
10 |
self.system = {"role": "system", "content": initial_prompt}
|
|
|
18 |
response = response["choices"][0]["message"]["content"]
|
19 |
return response
|
20 |
|
21 |
+
def predict(self, chatbot, input_sentence, context):
|
22 |
+
if len(input_sentence) == 0:
|
23 |
+
return [], context
|
24 |
context.append({"role": "user", "content": f"{input_sentence}"})
|
25 |
|
26 |
response = self.get_response(context)
|
27 |
|
28 |
context.append({"role": "assistant", "content": response})
|
29 |
+
|
30 |
+
chatbot.append((input_sentence, response))
|
31 |
|
32 |
+
return chatbot, context
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
def retry(self, chatbot, context):
|
35 |
+
if len(context) == 0:
|
36 |
+
return [], []
|
37 |
response = self.get_response(context[:-1])
|
38 |
context[-1] = {"role": "assistant", "content": response}
|
39 |
+
|
40 |
+
chatbot[-1] = (context[-2]["content"], response)
|
41 |
+
return chatbot, context
|
|
|
42 |
|
43 |
def update_system(self, new_system_prompt):
|
44 |
self.system = {"role": "system", "content": new_system_prompt}
|
|
|
60 |
emptyBth = gr.Button("重置")
|
61 |
retryBth = gr.Button("再试一次")
|
62 |
|
63 |
+
system = gr.Textbox(show_label=True, placeholder=f"Current System prompt: {initial_prompt}", label="更改 System prompt").style(container=False)
|
64 |
+
syspromptTxt = gr.Textbox(show_label=True, placeholder=initial_prompt, interactive=False, label="目前的 System prompt").style(container=False)
|
65 |
|
66 |
+
txt.submit(mychatGPT.predict, [chatbot, txt, state], [chatbot, state], show_progress=True)
|
67 |
txt.submit(lambda :"", None, txt)
|
68 |
emptyBth.click(reset_state, outputs=[chatbot, state])
|
69 |
system.submit(mychatGPT.update_system, system, syspromptTxt)
|
70 |
system.submit(lambda :"", None, system)
|
71 |
+
retryBth.click(mychatGPT.retry, [chatbot, state], [chatbot, state], show_progress=True)
|
72 |
|
73 |
demo.launch()
|