Chuan Hu commited on
Commit
5b344e4
1 Parent(s): 9df8412

Chatbot支持换行,气泡颜色优化

Browse files
Files changed (1) hide show
  1. ChuanhuChatbot.py +7 -5
ChuanhuChatbot.py CHANGED
@@ -1,7 +1,8 @@
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:
@@ -16,6 +17,7 @@ class ChatGPT:
16
  messages=[self.system, *messages],
17
  )
18
  response = response["choices"][0]["message"]["content"]
 
19
  return response
20
 
21
  def predict(self, chatbot, input_sentence, context):
@@ -26,7 +28,7 @@ class ChatGPT:
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
@@ -36,7 +38,7 @@ class ChatGPT:
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
 
@@ -51,7 +53,7 @@ mychatGPT = ChatGPT(my_api_key)
51
 
52
 
53
  with gr.Blocks() as demo:
54
- chatbot = gr.Chatbot()
55
  state = gr.State([])
56
 
57
  with gr.Column():
@@ -60,7 +62,7 @@ with gr.Blocks() as demo:
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)
 
1
  import gradio as gr
2
  import openai
3
+ import markdown
4
 
5
+ my_api_key = "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # input your api_key
6
  initial_prompt = "You are a helpful assistant."
7
 
8
  class ChatGPT:
 
17
  messages=[self.system, *messages],
18
  )
19
  response = response["choices"][0]["message"]["content"]
20
+ response = markdown.markdown(response)
21
  return response
22
 
23
  def predict(self, chatbot, input_sentence, context):
 
28
  response = self.get_response(context)
29
 
30
  context.append({"role": "assistant", "content": response})
31
+
32
  chatbot.append((input_sentence, response))
33
 
34
  return chatbot, context
 
38
  return [], []
39
  response = self.get_response(context[:-1])
40
  context[-1] = {"role": "assistant", "content": response}
41
+
42
  chatbot[-1] = (context[-2]["content"], response)
43
  return chatbot, context
44
 
 
53
 
54
 
55
  with gr.Blocks() as demo:
56
+ chatbot = gr.Chatbot().style(color_map=("#1D51EE", "#585A5B"))
57
  state = gr.State([])
58
 
59
  with gr.Column():
 
62
  emptyBth = gr.Button("重置")
63
  retryBth = gr.Button("再试一次")
64
 
65
+ system = gr.Textbox(show_label=True, placeholder=f"在这里输入新的System Prompt...", label="更改 System prompt").style(container=False)
66
  syspromptTxt = gr.Textbox(show_label=True, placeholder=initial_prompt, interactive=False, label="目前的 System prompt").style(container=False)
67
 
68
  txt.submit(mychatGPT.predict, [chatbot, txt, state], [chatbot, state], show_progress=True)