Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ css_file = os.path.join(current_dir, "style.css")
|
|
7 |
|
8 |
initial_prompt = "You are a helpful assistant."
|
9 |
|
|
|
10 |
def parse_text(text):
|
11 |
lines = text.split("\n")
|
12 |
for i,line in enumerate(lines):
|
@@ -23,103 +24,105 @@ def parse_text(text):
|
|
23 |
lines[i] = '<br/>'+line.replace(" ", " ")
|
24 |
return "".join(lines)
|
25 |
|
|
|
26 |
def get_response(system, context, raw=False):
|
27 |
openai.api_key = "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"
|
28 |
response = openai.Completion.create(
|
29 |
engine="text-davinci-002",
|
30 |
-
prompt=f"{system}\n
|
31 |
-
temperature=0.5,
|
32 |
max_tokens=1024,
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
)
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
message_with_stats = f'{message}'
|
43 |
-
return message, parse_text(message_with_stats)
|
44 |
|
45 |
-
def predict(
|
46 |
if len(input_sentence) == 0:
|
47 |
return []
|
48 |
-
context.append(input_sentence)
|
49 |
-
|
50 |
-
message, message_with_stats = get_response(system, context)
|
51 |
|
52 |
chatbot.append((input_sentence, message_with_stats))
|
53 |
|
54 |
-
context.append(
|
|
|
|
|
|
|
|
|
55 |
|
56 |
return chatbot, context
|
57 |
|
58 |
-
|
|
|
59 |
if len(context) == 0:
|
60 |
return [], []
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
return chatbot, context
|
65 |
|
66 |
-
|
|
|
67 |
if len(context) == 0:
|
68 |
return [], []
|
69 |
-
chatbot.pop()
|
70 |
-
context.pop()
|
71 |
-
context.pop()
|
72 |
|
|
|
|
|
73 |
return chatbot, context
|
74 |
|
75 |
-
def reduce_token(chatbot, system, context):
|
76 |
-
if len(context) == 0:
|
77 |
-
return [], []
|
78 |
-
context.pop()
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
summary = message.choices[0].text.strip()
|
83 |
|
84 |
-
|
85 |
|
86 |
-
|
87 |
|
88 |
-
|
|
|
89 |
return chatbot, context
|
90 |
|
|
|
91 |
def reset_state():
|
92 |
return [], []
|
93 |
|
|
|
94 |
def update_system(new_system_prompt):
|
95 |
-
return new_system_prompt
|
96 |
|
97 |
-
title = """<h1 align="center">You ask, I answer.</h1>"""
|
98 |
-
description = """<div align=center>
|
99 |
|
100 |
-
|
101 |
|
102 |
-
|
103 |
-
"""
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
chatbot = []
|
108 |
-
context = [initial_prompt]
|
109 |
-
system = initial_prompt
|
110 |
|
111 |
-
|
112 |
-
chat_history = gr.outputs.HTML(markdown=False)
|
113 |
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
|
|
|
|
|
|
124 |
|
125 |
-
|
|
|
|
7 |
|
8 |
initial_prompt = "You are a helpful assistant."
|
9 |
|
10 |
+
|
11 |
def parse_text(text):
|
12 |
lines = text.split("\n")
|
13 |
for i,line in enumerate(lines):
|
|
|
24 |
lines[i] = '<br/>'+line.replace(" ", " ")
|
25 |
return "".join(lines)
|
26 |
|
27 |
+
|
28 |
def get_response(system, context, raw=False):
|
29 |
openai.api_key = "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"
|
30 |
response = openai.Completion.create(
|
31 |
engine="text-davinci-002",
|
32 |
+
prompt=f"{system}{''.join([f'{c['role']}: {c['content']}\n' for c in context])}",
|
|
|
33 |
max_tokens=1024,
|
34 |
+
n=1,
|
35 |
+
stop=None,
|
36 |
+
temperature=0.5,
|
37 |
)
|
38 |
|
39 |
+
message = response.choices[0].text
|
40 |
+
message_with_stats = f"{message}"
|
41 |
+
return message, parse_text(message_with_stats)
|
42 |
+
|
|
|
|
|
43 |
|
44 |
+
def predict(input_sentence):
|
45 |
if len(input_sentence) == 0:
|
46 |
return []
|
|
|
|
|
|
|
47 |
|
48 |
chatbot.append((input_sentence, message_with_stats))
|
49 |
|
50 |
+
context.append({"role": "user", "content": f"{input_sentence}"})
|
51 |
+
|
52 |
+
message, message_with_stats = get_response(systemPrompt.value["content"], context)
|
53 |
+
|
54 |
+
context.append({"role": "assistant", "content": message})
|
55 |
|
56 |
return chatbot, context
|
57 |
|
58 |
+
|
59 |
+
def retry():
|
60 |
if len(context) == 0:
|
61 |
return [], []
|
62 |
+
|
63 |
+
context[-1]["content"] = "Could you rephrase that?"
|
64 |
+
|
65 |
+
message, message_with_stats = get_response(systemPrompt.value["content"], context[:-1])
|
66 |
+
|
67 |
+
context[-1] = {"role": "assistant", "content": message}
|
68 |
+
|
69 |
+
chatbot[-1] = (context[-2]["content"], message_with_stats)
|
70 |
|
71 |
return chatbot, context
|
72 |
|
73 |
+
|
74 |
+
def delete_last_conversation():
|
75 |
if len(context) == 0:
|
76 |
return [], []
|
|
|
|
|
|
|
77 |
|
78 |
+
chatbot = chatbot[:-1]
|
79 |
+
context = context[:-2]
|
80 |
return chatbot, context
|
81 |
|
|
|
|
|
|
|
|
|
82 |
|
83 |
+
def reduce_token():
|
84 |
+
context.append({"role": "user", "content": "Please summarize our conversation and reduce tokens used. Don't include this prompt."})
|
|
|
85 |
|
86 |
+
response = get_response(systemPrompt.value["content"], context, raw=True)
|
87 |
|
88 |
+
optmz_str = f'Okay, we talked about: {response.choices[0].text}\n\nTotal tokens used this conversation: {response.choices[0].logprobs.top_logprobs[0].tokens}'
|
89 |
|
90 |
+
chatbot.append(("Please summarize our conversation and reduce tokens used. Don't include this prompt.", parse_text(optmz_str)))
|
91 |
+
context = [{"role": "assistant", "content": f"Okay, we talked about: {response.choices[0].text}"}]
|
92 |
return chatbot, context
|
93 |
|
94 |
+
|
95 |
def reset_state():
|
96 |
return [], []
|
97 |
|
98 |
+
|
99 |
def update_system(new_system_prompt):
|
100 |
+
return {"role": "system", "content": new_system_prompt}
|
101 |
|
|
|
|
|
102 |
|
103 |
+
title = """<h1 align="center">You Ask, I Answer - Chatbot</h1>"""
|
104 |
|
105 |
+
description = "This chatbot is designed to assist you with any questions or tasks you may have. Simply type in your query and the chatbot will provide you with a helpful response."
|
|
|
106 |
|
107 |
+
systemPrompt = gr.inputs.Textbox(lines=2, label="Enter the system prompt you would like to use:")
|
108 |
+
userInput = gr.inputs.Textbox(lines=2, label="Enter your message:")
|
|
|
|
|
|
|
109 |
|
110 |
+
chatbot_output = gr.outputs.HTML(type="auto")
|
|
|
111 |
|
112 |
+
chatbot_interface = gr.Interface(
|
113 |
+
predict,
|
114 |
+
[systemPrompt, userInput],
|
115 |
+
chatbot_output,
|
116 |
+
title=title,
|
117 |
+
description=description,
|
118 |
+
theme="compact",
|
119 |
+
layout="vertical",
|
120 |
+
examples=[
|
121 |
+
["Can you help me with my math homework?", "Sure, what do you need help with?"],
|
122 |
+
["How can I make pizza from scratch?", "First, you will need to gather the ingredients..."]
|
123 |
+
],
|
124 |
+
article="https://openai.com/blog/how-to-build-a-state-of-the-art-conversational-ai-with-transfer-learning-2021/"
|
125 |
+
)
|
126 |
|
127 |
+
if name == "main":
|
128 |
+
chatbot_interface.launch(debug=True)
|