Spaces:
Sleeping
Sleeping
Chuan Hu
commited on
Initial Commit
Browse files- ChuanhuChatbot.py +74 -0
- requirements.txt +2 -0
ChuanhuChatbot.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def get_response(self, messages):
|
| 15 |
+
response = openai.ChatCompletion.create(
|
| 16 |
+
model="gpt-3.5-turbo",
|
| 17 |
+
messages=[self.system, *messages],
|
| 18 |
+
)
|
| 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 |
+
response = []
|
| 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.append({"role": "assistant", "content": response})
|
| 39 |
+
response = []
|
| 40 |
+
for i in range(0, len(context), 2):
|
| 41 |
+
response.append((context[i]["content"], context[i+1]["content"]))
|
| 42 |
+
return response, context
|
| 43 |
+
|
| 44 |
+
def update_system(self, new_system_prompt):
|
| 45 |
+
self.system = {"role": "system", "content": new_system_prompt}
|
| 46 |
+
return new_system_prompt
|
| 47 |
+
|
| 48 |
+
def reset_state():
|
| 49 |
+
return [], []
|
| 50 |
+
|
| 51 |
+
mychatGPT = ChatGPT(my_api_key)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
chatbot = gr.Chatbot()
|
| 56 |
+
state = gr.State([])
|
| 57 |
+
|
| 58 |
+
with gr.Column():
|
| 59 |
+
txt = gr.Textbox(show_label=False, placeholder="💬 在这里输入").style(container=False)
|
| 60 |
+
with gr.Row():
|
| 61 |
+
emptyBth = gr.Button("重置")
|
| 62 |
+
retryBth = gr.Button("再试一次")
|
| 63 |
+
|
| 64 |
+
system = gr.Textbox(show_label=True, placeholder="New system prompts here...", label="System Prompt").style(container=False)
|
| 65 |
+
syspromptTxt = gr.Textbox(show_label=False, placeholder=initial_prompt, interactive=False).style(container=False)
|
| 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()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gradio
|