Spaces:
Sleeping
Sleeping
dragonSwing
commited on
Commit
•
4430327
1
Parent(s):
4088500
Reformat code
Browse files
app.py
CHANGED
@@ -14,12 +14,14 @@ prompt_templates = {
|
|
14 |
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
15 |
|
16 |
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
|
17 |
-
"""
|
18 |
}
|
19 |
|
|
|
20 |
def get_empty_state():
|
21 |
return {"total_tokens": 0, "messages": []}
|
22 |
|
|
|
23 |
def download_prompt_templates():
|
24 |
url = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"
|
25 |
try:
|
@@ -40,55 +42,81 @@ def download_prompt_templates():
|
|
40 |
choices = choices[:1] + sorted(choices[1:])
|
41 |
return gr.update(value=choices[0], choices=choices)
|
42 |
|
|
|
43 |
def on_token_change(user_token):
|
44 |
openai.api_key = user_token
|
45 |
|
|
|
46 |
def on_prompt_template_change(prompt_template):
|
47 |
-
if not isinstance(prompt_template, str):
|
|
|
48 |
return prompt_templates[prompt_template]
|
49 |
|
50 |
-
def submit_message(user_token, prompt, prompt_template, temperature, max_tokens, context_length, state):
|
51 |
|
52 |
-
|
|
|
|
|
|
|
53 |
|
54 |
if not prompt:
|
55 |
-
return
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
prompt_template = prompt_templates[prompt_template]
|
58 |
|
59 |
system_prompt = []
|
60 |
if prompt_template:
|
61 |
-
system_prompt = [{
|
62 |
|
63 |
-
prompt_msg = {
|
64 |
|
65 |
if not user_token:
|
66 |
history.append(prompt_msg)
|
67 |
-
history.append(
|
68 |
-
"role": "system",
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
try:
|
74 |
-
completion = openai.ChatCompletion.create(
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
history.append(prompt_msg)
|
77 |
history.append(completion.choices[0].message.to_dict())
|
78 |
|
79 |
-
state[
|
80 |
-
|
81 |
except Exception as e:
|
82 |
history.append(prompt_msg)
|
83 |
-
history.append({
|
84 |
-
"role": "system",
|
85 |
-
"content": f"Error: {e}"
|
86 |
-
})
|
87 |
|
88 |
total_tokens_used_msg = f"Total tokens used: {state['total_tokens']}"
|
89 |
-
chat_messages = [
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
-
return '', chat_messages, total_tokens_used_msg, state
|
92 |
|
93 |
def clear_conversation():
|
94 |
return gr.update(value=None, visible=True), None, "", get_empty_state()
|
@@ -105,45 +133,113 @@ css = """
|
|
105 |
"""
|
106 |
|
107 |
with gr.Blocks(css=css) as demo:
|
108 |
-
|
109 |
state = gr.State(get_empty_state())
|
110 |
|
111 |
-
|
112 |
with gr.Column(elem_id="col-container"):
|
113 |
-
gr.Markdown(
|
|
|
114 |
Using the ofiicial API (gpt-3.5-turbo model)
|
115 |
Prompt templates from [awesome-chatgpt-prompts](https://github.com/f/awesome-chatgpt-prompts).""",
|
116 |
-
|
|
|
117 |
|
118 |
with gr.Row():
|
119 |
with gr.Column(scale=0.3):
|
120 |
-
gr.Markdown(
|
121 |
-
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
prompt_template_preview = gr.Markdown(elem_id="prompt_template_preview")
|
124 |
with gr.Accordion("Advanced parameters", open=False):
|
125 |
-
temperature = gr.Slider(
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
with gr.Column(scale=0.7):
|
129 |
chatbot = gr.Chatbot(elem_id="chatbox")
|
130 |
-
input_message = gr.Textbox(
|
|
|
|
|
|
|
|
|
131 |
btn_submit = gr.Button("Submit")
|
132 |
total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
|
133 |
btn_clear_conversation = gr.Button("🔃 Start New Conversation")
|
134 |
|
135 |
-
gr.HTML(
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
user_token.change(on_token_change, inputs=[user_token], outputs=[])
|
143 |
|
144 |
-
|
145 |
-
|
|
|
146 |
|
147 |
|
148 |
demo.queue(concurrency_count=10)
|
149 |
-
demo.launch(height=
|
|
|
14 |
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
|
15 |
|
16 |
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
|
17 |
+
""",
|
18 |
}
|
19 |
|
20 |
+
|
21 |
def get_empty_state():
|
22 |
return {"total_tokens": 0, "messages": []}
|
23 |
|
24 |
+
|
25 |
def download_prompt_templates():
|
26 |
url = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"
|
27 |
try:
|
|
|
42 |
choices = choices[:1] + sorted(choices[1:])
|
43 |
return gr.update(value=choices[0], choices=choices)
|
44 |
|
45 |
+
|
46 |
def on_token_change(user_token):
|
47 |
openai.api_key = user_token
|
48 |
|
49 |
+
|
50 |
def on_prompt_template_change(prompt_template):
|
51 |
+
if not isinstance(prompt_template, str):
|
52 |
+
return
|
53 |
return prompt_templates[prompt_template]
|
54 |
|
|
|
55 |
|
56 |
+
def submit_message(
|
57 |
+
user_token, prompt, prompt_template, temperature, max_tokens, context_length, state
|
58 |
+
):
|
59 |
+
history = state["messages"]
|
60 |
|
61 |
if not prompt:
|
62 |
+
return (
|
63 |
+
gr.update(value=""),
|
64 |
+
[
|
65 |
+
(history[i]["content"], history[i + 1]["content"])
|
66 |
+
for i in range(0, len(history) - 1, 2)
|
67 |
+
],
|
68 |
+
f"Total tokens used: {state['total_tokens']}",
|
69 |
+
state,
|
70 |
+
)
|
71 |
+
|
72 |
prompt_template = prompt_templates[prompt_template]
|
73 |
|
74 |
system_prompt = []
|
75 |
if prompt_template:
|
76 |
+
system_prompt = [{"role": "system", "content": prompt_template}]
|
77 |
|
78 |
+
prompt_msg = {"role": "user", "content": prompt}
|
79 |
|
80 |
if not user_token:
|
81 |
history.append(prompt_msg)
|
82 |
+
history.append(
|
83 |
+
{"role": "system", "content": "Error: OpenAI API Key is not set."}
|
84 |
+
)
|
85 |
+
return (
|
86 |
+
"",
|
87 |
+
[
|
88 |
+
(history[i]["content"], history[i + 1]["content"])
|
89 |
+
for i in range(0, len(history) - 1, 2)
|
90 |
+
],
|
91 |
+
f"Total tokens used: 0",
|
92 |
+
state,
|
93 |
+
)
|
94 |
+
|
95 |
try:
|
96 |
+
completion = openai.ChatCompletion.create(
|
97 |
+
model="gpt-3.5-turbo",
|
98 |
+
messages=system_prompt + history[-context_length * 2 :] + [prompt_msg],
|
99 |
+
temperature=temperature,
|
100 |
+
max_tokens=max_tokens,
|
101 |
+
)
|
102 |
|
103 |
history.append(prompt_msg)
|
104 |
history.append(completion.choices[0].message.to_dict())
|
105 |
|
106 |
+
state["total_tokens"] += completion["usage"]["total_tokens"]
|
107 |
+
|
108 |
except Exception as e:
|
109 |
history.append(prompt_msg)
|
110 |
+
history.append({"role": "system", "content": f"Error: {e}"})
|
|
|
|
|
|
|
111 |
|
112 |
total_tokens_used_msg = f"Total tokens used: {state['total_tokens']}"
|
113 |
+
chat_messages = [
|
114 |
+
(history[i]["content"], history[i + 1]["content"])
|
115 |
+
for i in range(0, len(history) - 1, 2)
|
116 |
+
]
|
117 |
+
|
118 |
+
return "", chat_messages, total_tokens_used_msg, state
|
119 |
|
|
|
120 |
|
121 |
def clear_conversation():
|
122 |
return gr.update(value=None, visible=True), None, "", get_empty_state()
|
|
|
133 |
"""
|
134 |
|
135 |
with gr.Blocks(css=css) as demo:
|
|
|
136 |
state = gr.State(get_empty_state())
|
137 |
|
|
|
138 |
with gr.Column(elem_id="col-container"):
|
139 |
+
gr.Markdown(
|
140 |
+
"""## OpenAI ChatGPT Demo
|
141 |
Using the ofiicial API (gpt-3.5-turbo model)
|
142 |
Prompt templates from [awesome-chatgpt-prompts](https://github.com/f/awesome-chatgpt-prompts).""",
|
143 |
+
elem_id="header",
|
144 |
+
)
|
145 |
|
146 |
with gr.Row():
|
147 |
with gr.Column(scale=0.3):
|
148 |
+
gr.Markdown(
|
149 |
+
"Enter your OpenAI API Key. You can get one [here](https://platform.openai.com/account/api-keys).",
|
150 |
+
elem_id="label",
|
151 |
+
)
|
152 |
+
user_token = gr.Textbox(
|
153 |
+
value="",
|
154 |
+
placeholder="OpenAI API Key",
|
155 |
+
type="password",
|
156 |
+
show_label=False,
|
157 |
+
)
|
158 |
+
prompt_template = gr.Dropdown(
|
159 |
+
label="Set a custom insruction for the chatbot:",
|
160 |
+
choices=list(prompt_templates.keys()),
|
161 |
+
)
|
162 |
prompt_template_preview = gr.Markdown(elem_id="prompt_template_preview")
|
163 |
with gr.Accordion("Advanced parameters", open=False):
|
164 |
+
temperature = gr.Slider(
|
165 |
+
minimum=0,
|
166 |
+
maximum=2.0,
|
167 |
+
value=0.7,
|
168 |
+
step=0.1,
|
169 |
+
label="Temperature",
|
170 |
+
info="Higher = more creative/chaotic",
|
171 |
+
)
|
172 |
+
max_tokens = gr.Slider(
|
173 |
+
minimum=100,
|
174 |
+
maximum=4096,
|
175 |
+
value=1000,
|
176 |
+
step=1,
|
177 |
+
label="Max tokens per response",
|
178 |
+
)
|
179 |
+
context_length = gr.Slider(
|
180 |
+
minimum=1,
|
181 |
+
maximum=10,
|
182 |
+
value=2,
|
183 |
+
step=1,
|
184 |
+
label="Context length",
|
185 |
+
info="Number of previous messages to send to the chatbot. Be careful with high values, it can blow up the token budget quickly.",
|
186 |
+
)
|
187 |
with gr.Column(scale=0.7):
|
188 |
chatbot = gr.Chatbot(elem_id="chatbox")
|
189 |
+
input_message = gr.Textbox(
|
190 |
+
show_label=False,
|
191 |
+
placeholder="Enter text and press enter",
|
192 |
+
visible=True,
|
193 |
+
).style(container=False)
|
194 |
btn_submit = gr.Button("Submit")
|
195 |
total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
|
196 |
btn_clear_conversation = gr.Button("🔃 Start New Conversation")
|
197 |
|
198 |
+
gr.HTML(
|
199 |
+
"""<br><br><br><center>You can duplicate this Space to skip the queue:<a href="https://huggingface.co/spaces/dragonSwing/chatgpt-app?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a><br>
|
200 |
+
<p><img src="https://visitor-badge.glitch.me/badge?page_id=dragonswing.chatgpt-app" alt="visitors"></p></center>"""
|
201 |
+
)
|
202 |
+
|
203 |
+
btn_submit.click(
|
204 |
+
submit_message,
|
205 |
+
[
|
206 |
+
user_token,
|
207 |
+
input_message,
|
208 |
+
prompt_template,
|
209 |
+
temperature,
|
210 |
+
max_tokens,
|
211 |
+
context_length,
|
212 |
+
state,
|
213 |
+
],
|
214 |
+
[input_message, chatbot, total_tokens_str, state],
|
215 |
+
)
|
216 |
+
input_message.submit(
|
217 |
+
submit_message,
|
218 |
+
[
|
219 |
+
user_token,
|
220 |
+
input_message,
|
221 |
+
prompt_template,
|
222 |
+
temperature,
|
223 |
+
max_tokens,
|
224 |
+
context_length,
|
225 |
+
state,
|
226 |
+
],
|
227 |
+
[input_message, chatbot, total_tokens_str, state],
|
228 |
+
)
|
229 |
+
btn_clear_conversation.click(
|
230 |
+
clear_conversation, [], [input_message, chatbot, total_tokens_str, state]
|
231 |
+
)
|
232 |
+
prompt_template.change(
|
233 |
+
on_prompt_template_change,
|
234 |
+
inputs=[prompt_template],
|
235 |
+
outputs=[prompt_template_preview],
|
236 |
+
)
|
237 |
user_token.change(on_token_change, inputs=[user_token], outputs=[])
|
238 |
|
239 |
+
demo.load(
|
240 |
+
download_prompt_templates, inputs=None, outputs=[prompt_template], queue=False
|
241 |
+
)
|
242 |
|
243 |
|
244 |
demo.queue(concurrency_count=10)
|
245 |
+
demo.launch(height="800px")
|