Spaces:
Running
Running
File size: 11,761 Bytes
eca6fb0 7e13425 eca6fb0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# import gradio as gr
import gradio
# import lmdb
# import base64
# import io
# import random
# import time
import json
import copy
# import sqlite3
from urllib.parse import urljoin
import openai
DEFAULT_PROMPT = [
["system", "你(assistant)是一名疯狂的摇滚乐手,用户(user)是你的粉丝。"],
["user", "我们来玩一个角色扮演游戏吧!请你扮演一名疯狂的摇滚乐手,而我将扮演你的粉丝。"],
["assistant", "真是个有趣的游戏!我将扮演一名疯狂的摇滚乐手,而你是我的粉丝。听起来真不错!让我们开始吧!"],
]
# def get_settings(old_state):
# db_path = './my_app_state'
# env = lmdb.open(db_path, max_dbs=2*1024*1024)
# # print(env.stat())
# txn = env.begin()
# saved_api_key = txn.get(key=b'api_key').decode('utf-8') or ''
# txn.commit()
# env.close()
# new_state = copy.deepcopy(old_state) or {}
# new_state['api_key'] = saved_api_key
# return new_state, saved_api_key
# def save_settings(old_state, api_key_text):
# db_path = './my_app_state'
# env = lmdb.open(db_path, max_dbs=2*1024*1024)
# # print(env.stat())
# txn = env.begin(write=True)
# txn.put(key=b'api_key', value=api_key_text.encode('utf-8'))
# # 提交事务
# txn.commit()
# return get_settings(old_state)
def on_click_send_btn(
old_state, api_key_text, chat_input_role, chat_input, prompt_table, chat_use_prompt, chat_use_history, chat_log,
temperature, top_p, choices_num, stream, max_tokens, presence_penalty, frequency_penalty, logit_bias,
):
print(prompt_table)
prompt_table = prompt_table or []
chat_log = chat_log or []
chat_log_md = ''
if chat_use_prompt:
chat_log_md += '<center>(prompt)</center>\n\n'
chat_log_md += "\n".join([xx for xx in map(lambda it: f"##### `{it[0]}`\n\n{it[1]}\n\n", prompt_table)])
chat_log_md += '\n---\n'
if True:
chat_log_md += '<center>(history)</center>\n\n' if chat_use_history else '<center>(not used history)</center>\n\n'
chat_log_md += "\n".join([xx for xx in map(lambda it: f"##### `{it[0]}`\n\n{it[1]}\n\n", chat_log)])
chat_log_md += '\n---\n'
# if chat_input=='':
# return old_state, chat_log, chat_log_md, None, None, chat_input
try:
logit_bias_json = json.dumps(logit_bias) if logit_bias else None
except:
return old_state, chat_log, chat_log_md, None, None, chat_input
new_state = copy.deepcopy(old_state) or {}
req_hist = copy.deepcopy(prompt_table) if chat_use_prompt else []
if chat_use_history:
for hh in (chat_log or []):
req_hist.append(hh)
if chat_input and chat_input!="":
req_hist.append([(chat_input_role or 'user'), chat_input])
openai.api_key = api_key_text
props = {
'model': "gpt-3.5-turbo",
'messages': [xx for xx in map(lambda it: {'role':it[0], 'content':it[1]}, req_hist)],
'temperature': temperature,
'top_p': top_p,
'n': choices_num,
'stream': stream,
'presence_penalty': presence_penalty,
'frequency_penalty': frequency_penalty,
}
if max_tokens>0:
props['max_tokens'] = max_tokens
if logit_bias_json is not None:
props['logit_bias'] = logit_bias_json
props_json = json.dumps(props)
try:
completion = openai.ChatCompletion.create(**props)
print('')
print(completion.choices)
the_response_role = completion.choices[0].message.role
the_response = completion.choices[0].message.content
print(the_response)
print('')
chat_last_resp = json.dumps(completion.__dict__)
chat_last_resp_dict = json.loads(chat_last_resp)
chat_last_resp_dict['api_key'] = "hidden by UI"
chat_last_resp_dict['organization'] = "hidden by UI"
chat_last_resp = json.dumps(chat_last_resp_dict)
chat_log_md = ''
if chat_use_prompt:
chat_log_md += '<center>(prompt)</center>\n\n'
chat_log_md += "\n".join([xx for xx in map(lambda it: f"##### `{it[0]}`\n\n{it[1]}\n\n", prompt_table)])
chat_log_md += '\n---\n'
if True:
chat_log_md += '<center>(history)</center>\n\n' if chat_use_history else '<center>(not used history)</center>\n\n'
chat_log_md += "\n".join([xx for xx in map(lambda it: f"##### `{it[0]}`\n\n{it[1]}\n\n", chat_log)])
chat_log_md += '\n---\n'
if chat_input and chat_input!="":
chat_log.append([(chat_input_role or 'user'), chat_input])
chat_log_md += f"##### `{(chat_input_role or 'user')}`\n\n{chat_input}\n\n"
chat_log.append([the_response_role, the_response])
chat_log_md += f"##### `{the_response_role}`\n\n{the_response}\n\n"
return new_state, chat_log, chat_log_md, chat_last_resp, props_json, ''
except Exception as error:
print(error)
chat_log_md = ''
if chat_use_prompt:
chat_log_md += '<center>(prompt)</center>\n\n'
chat_log_md += "\n".join([xx for xx in map(lambda it: f"##### `{it[0]}`\n\n{it[1]}\n\n", prompt_table)])
chat_log_md += '\n---\n'
if True:
chat_log_md += '<center>(history)</center>\n\n' if chat_use_history else '<center>(not used history)</center>\n\n'
chat_log_md += "\n".join([xx for xx in map(lambda it: f"##### `{it[0]}`\n\n{it[1]}\n\n", chat_log)])
chat_log_md += '\n---\n'
# chat_log_md = ''
# chat_log_md = "\n".join([xx for xx in map(lambda it: f"##### `{it[0]}`\n\n{it[1]}\n\n", prompt_table)]) if chat_use_prompt else ''
# chat_log_md += "\n".join([xx for xx in map(lambda it: f"##### `{it[0]}`\n\n{it[1]}\n\n", hist)])
chat_log_md += "\n"
chat_log_md += str(error)
return new_state, chat_log, chat_log_md, None, props_json, chat_input
def clear_history():
return [], ""
css = """
.table-wrap .cell-wrap input {min-width:80%}
#api-key-textbox textarea {filter:blur(8px); transition: filter 0.25s}
#api-key-textbox textarea:focus {filter:none}
"""
with gradio.Blocks(title="ChatGPT", css=css) as demo:
global_state = gradio.State(value={})
# https://gradio.app/docs
# https://platform.openai.com/docs/api-reference/chat/create
with gradio.Tab("ChatGPT"):
with gradio.Row():
with gradio.Column(scale=10):
gradio.Markdown("Go to https://platform.openai.com/account/api-keys to get your API key.")
api_key_text = gradio.Textbox(label="Your API key", elem_id="api-key-textbox")
with gradio.Row():
with gradio.Column(scale=2):
api_key_refresh_btn = gradio.Button("🔄 Load from browser storage")
api_key_refresh_btn.click(
# get_settings,
None,
inputs=[global_state],
outputs=[global_state, api_key_text],
api_name="load-settings",
_js="""(global_state, api_key_text)=>{
global_state=(global_state??{});
global_state['api_key_text']=localStorage?.getItem?.('[gradio][chat-gpt-ui][api_key_text]');
return [global_state, global_state['api_key_text']];
}""",
)
with gradio.Column(scale=2):
api_key_save_btn = gradio.Button("💾 Save to browser storage")
api_key_save_btn.click(
# save_settings,
None,
inputs=[global_state, api_key_text],
outputs=[global_state, api_key_text],
api_name="save-settings",
_js="""(global_state, api_key_text)=>{
localStorage.setItem('[gradio][chat-gpt-ui][api_key_text]', api_key_text);
global_state=(global_state??{});
global_state['api_key_text']=localStorage?.getItem?.('[gradio][chat-gpt-ui][api_key_text]');
return [global_state, global_state['api_key_text']];
}""",
)
with gradio.Row():
with gradio.Column(scale=10):
with gradio.Box():
prompt_table = gradio.Dataframe(
type='array',
label='Prompt', col_count=(2, 'fixed'), max_cols=2,
value=DEFAULT_PROMPT, headers=['role', 'content'], interactive=True,
)
gradio.Markdown("Will be added to the beginning of the conversation. See https://platform.openai.com/docs/guides/chat/introduction .")
with gradio.Row():
with gradio.Column(scale=4):
with gradio.Box():
gradio.Markdown("See https://platform.openai.com/docs/api-reference/chat/create .")
chat_temperature = gradio.Slider(label="temperature", value=1, minimum=0, maximum=2)
chat_top_p = gradio.Slider(label="top_p", value=1, minimum=0, maximum=1)
chat_choices_num = gradio.Slider(label="choices num(n)", value=1, minimum=1, maximum=20)
chat_stream = gradio.Checkbox(label="stream", value=False, visible=False)
chat_max_tokens = gradio.Slider(label="max_tokens", value=-1, minimum=-1, maximum=4096)
chat_presence_penalty = gradio.Slider(label="presence_penalty", value=0, minimum=-2, maximum=2)
chat_frequency_penalty = gradio.Slider(label="frequency_penalty", value=0, minimum=-2, maximum=2)
chat_logit_bias = gradio.Textbox(label="logit_bias", visible=False)
pass
with gradio.Column(scale=8):
with gradio.Row():
with gradio.Column(scale=10):
chat_log = gradio.State()
with gradio.Box():
chat_log_box = gradio.Markdown(label='chat history')
chat_input_role = gradio.Textbox(lines=1, label='role', value='user')
chat_input = gradio.Textbox(lines=4, label='input')
with gradio.Row():
chat_clear_history_btn = gradio.Button("clear history")
chat_clear_history_btn.click(clear_history, inputs=[], outputs=[chat_log, chat_log_box])
chat_use_prompt = gradio.Checkbox(label='send with prompt', value=True)
chat_use_history = gradio.Checkbox(label='send with history', value=True)
chat_send_btn = gradio.Button("send")
pass
with gradio.Row():
chat_last_req = gradio.JSON(label='last request')
chat_last_resp = gradio.JSON(label='last response')
chat_send_btn.click(
on_click_send_btn,
inputs=[
global_state, api_key_text, chat_input_role, chat_input, prompt_table, chat_use_prompt, chat_use_history, chat_log,
chat_temperature, chat_top_p, chat_choices_num, chat_stream, chat_max_tokens, chat_presence_penalty, chat_frequency_penalty, chat_logit_bias,
],
outputs=[global_state, chat_log, chat_log_box, chat_last_resp, chat_last_req, chat_input],
api_name="click-send-btn",
)
pass
with gradio.Tab("Settings"):
gradio.Markdown('Currently nothing.')
pass
if __name__ == "__main__":
demo.queue(concurrency_count=20).launch()
|