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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
import gradio
|
| 3 |
+
# import lmdb
|
| 4 |
+
# import base64
|
| 5 |
+
# import io
|
| 6 |
+
# import random
|
| 7 |
+
# import time
|
| 8 |
+
import json
|
| 9 |
+
import copy
|
| 10 |
+
# import sqlite3
|
| 11 |
+
from urllib.parse import urljoin
|
| 12 |
+
import openai
|
| 13 |
+
|
| 14 |
+
from app_js import api_key__get_from_browser, api_key__save_to_browser, saved_prompts_refresh_btn__click_js, selected_saved_prompt_title__change_js, saved_prompts_delete_btn__click_js, saved_prompts_save_btn__click_js, copy_prompt__click_js, paste_prompt__click_js, chat_copy_history_btn__click_js, chat_copy_history_md_btn__click_js, api_key_refresh_btn__click_js, api_key_save_btn__click_js
|
| 15 |
+
|
| 16 |
+
from functions import sequential_chat_fn, make_history_file_fn, on_click_send_btn, clear_history, copy_history, update_saved_prompt_titles, save_prompt, load_saved_prompt
|
| 17 |
+
|
| 18 |
+
DEFAULT_PROMPT = [
|
| 19 |
+
["system", "You(assistant) are a helpful AI assistant."],
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
introduction = """<center><h2>ChatGPT 批处理工具</h2></center>
|
| 24 |
+
|
| 25 |
+
您好。这是一个用于批量向 ChatGPT 发送消息的工具。
|
| 26 |
+
|
| 27 |
+
通过这个工具,您可以一次性计划好要给 ChatGPT 发送哪些消息,并依次发送。
|
| 28 |
+
|
| 29 |
+
请注意:
|
| 30 |
+
|
| 31 |
+
1. 为了使用本工具,您需要填写自己的 API Key ,并承担可能的费用。我们不会收集或存储您的 API Key 。您可访问 https://platform.openai.com/account/api-keys 来获取您的 API Key 。
|
| 32 |
+
2. 这个 demo 页面的 space 是公共的。出于研究和改善代码的需要,我们需要记录通过这个页面发送的聊天内容,因此,我们有能力在后台看到您与 ChatGPT 的聊天记录。 **<span style="color:#ff5656;">如果您继续在此页面使用本工具,意味着您同意我们查看、使用、传播您的聊天数据。</span>** 如果您希望避免这一情况,可以 [将此工具复制一份到自己专用的 space](https://huggingface.co/spaces/hugforziio/chat-gpt-batch?duplicate=true) ,同时也可以避免排队等候。
|
| 33 |
+
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
css = """
|
| 38 |
+
.table-wrap .cell-wrap input {min-width:80%}
|
| 39 |
+
#api-key-textbox textarea {filter:blur(8px); transition: filter 0.25s}
|
| 40 |
+
#api-key-textbox textarea:focus {filter:none}
|
| 41 |
+
#chat-log-md hr {margin-top: 1rem; margin-bottom: 1rem;}
|
| 42 |
+
#chat-markdown-wrap-box {max-height:80vh; overflow: auto !important;}
|
| 43 |
+
"""
|
| 44 |
+
with gradio.Blocks(title="ChatGPT 批处理", css=css) as demo:
|
| 45 |
+
|
| 46 |
+
with gradio.Accordion("说明", open=True):
|
| 47 |
+
gradio.Markdown(introduction)
|
| 48 |
+
|
| 49 |
+
with gradio.Accordion("基本设置", open=True):
|
| 50 |
+
system_prompt_enabled = gradio.Checkbox(label='是否使用系统全局提示语', info='是否要以“系统”身份,给 ChatGPT 描述任务?', value=True)
|
| 51 |
+
# 系统提示
|
| 52 |
+
system_prompt = gradio.Textbox(label='系统级全局提示语', info='以“系统”身份,给 ChatGPT 描述任务', value='你是一个词性分类器,用户将给你发送一个单词,请判断该单词的词性,如名词、动词等。!!请注意!!⚠️最高优先级!!:你只能直接返回词性,而不要返回任何多余的内容,🈲不要解释为什么是这个词性等等,否则用户所使用的程序将会出错,给用户带来严重的损失😱!!!')
|
| 53 |
+
# 用户消息模板
|
| 54 |
+
user_message_template = gradio.Textbox(label='用户消息模板', info='要批量发送的消息的模板', value='单词:```___```')
|
| 55 |
+
with gradio.Row():
|
| 56 |
+
# 用户消息模板中的替换区
|
| 57 |
+
user_message_template_mask = gradio.Textbox(label='模板占位符', info='消息模板中需要被替换的部分,可以是正则表达式', value='___')
|
| 58 |
+
# 用户消息模板中的替换区是正则吗
|
| 59 |
+
user_message_template_mask_is_regex = gradio.Checkbox(label='模板占位符是正则吗', info='模板占位符是不是正则表达式?', value=False)
|
| 60 |
+
# 用户消息替换区清单文本
|
| 61 |
+
user_message_list_text = gradio.Textbox(label='用户消息列表', info='所有待发送的消息', value='动物 火车 介于 的 置于 你在做什么')
|
| 62 |
+
with gradio.Row():
|
| 63 |
+
# 用户消息替换区清单分隔符
|
| 64 |
+
user_message_list_text_splitter = gradio.Textbox(label='用户消息分隔符', info='用于分割用户消息列表的分隔符,如逗号(`,`)、换行符(`\\n`)等,也可以是正则表达式', value='\\s+')
|
| 65 |
+
# 用户消息替换区清单分隔符是正则吗
|
| 66 |
+
user_message_list_text_splitter_is_regex = gradio.Checkbox(label='分隔符是正则吗', info='用户消息分隔符是不是正则表达式?', value=True)
|
| 67 |
+
# 历史记录条数
|
| 68 |
+
history_prompt_num = gradio.Slider(label="发送历史记录条数", info='每次发生消息时,同时携带多少条先前的历史记录(以便 ChatGPT 了解上下文)', value=0, minimum=0, maximum=8000)
|
| 69 |
+
|
| 70 |
+
# load_config_from_browser = gradio.Button("🔄 从浏览器加载配置")
|
| 71 |
+
# save_config_to_browser = gradio.Button("💾 将配置保存到浏览器")
|
| 72 |
+
# export_config_to_file = gradio.Button("📤 将配置导出为文件")
|
| 73 |
+
|
| 74 |
+
# 更多参数
|
| 75 |
+
with gradio.Accordion("更多参数", open=False):
|
| 76 |
+
# 时间间隔
|
| 77 |
+
sleep_base = gradio.Number(label='时间间隔 ms', value=700)
|
| 78 |
+
# 时间间隔浮动
|
| 79 |
+
sleep_rand = gradio.Number(label='时间间隔浮动 ms', value=200)
|
| 80 |
+
# 那些参数
|
| 81 |
+
prop_stream = gradio.Checkbox(label="流式传输 stream", value=True)
|
| 82 |
+
prop_model = gradio.Textbox(label="模型 model", value="gpt-3.5-turbo")
|
| 83 |
+
prop_temperature = gradio.Slider(label="temperature", value=1, minimum=0, maximum=2)
|
| 84 |
+
prop_top_p = gradio.Slider(label="top_p", value=1, minimum=0, maximum=1)
|
| 85 |
+
prop_choices_num = gradio.Slider(label="choices num(n)", value=1, minimum=1, maximum=20)
|
| 86 |
+
prop_max_tokens = gradio.Slider(label="max_tokens", value=-1, minimum=-1, maximum=4096)
|
| 87 |
+
prop_presence_penalty = gradio.Slider(label="presence_penalty", value=0, minimum=-2, maximum=2)
|
| 88 |
+
prop_frequency_penalty = gradio.Slider(label="frequency_penalty", value=0, minimum=-2, maximum=2)
|
| 89 |
+
prop_logit_bias = gradio.Textbox(label="logit_bias", visible=False)
|
| 90 |
+
pass
|
| 91 |
+
|
| 92 |
+
# 欸丕艾科易
|
| 93 |
+
token_text = gradio.Textbox(visible=False)
|
| 94 |
+
with gradio.Row():
|
| 95 |
+
with gradio.Column(scale=10, min_width=100):
|
| 96 |
+
api_key_text = gradio.Textbox(label="你的 API key", placeholder="sk-...", elem_id="api-key-textbox")
|
| 97 |
+
with gradio.Column(scale=1, min_width=100):
|
| 98 |
+
api_key_load_btn = gradio.Button("🔄 从浏览器本地存储加载")
|
| 99 |
+
api_key_load_btn.click(
|
| 100 |
+
None,
|
| 101 |
+
inputs=[],
|
| 102 |
+
outputs=[api_key_text, token_text],
|
| 103 |
+
_js=api_key__get_from_browser,
|
| 104 |
+
)
|
| 105 |
+
with gradio.Column(scale=1, min_width=100):
|
| 106 |
+
api_key_save_btn = gradio.Button("💾 保存到浏览器本地存储")
|
| 107 |
+
api_key_save_btn.click(
|
| 108 |
+
None,
|
| 109 |
+
inputs=[api_key_text, token_text],
|
| 110 |
+
outputs=[api_key_text, token_text],
|
| 111 |
+
_js=api_key__save_to_browser,
|
| 112 |
+
)
|
| 113 |
+
pass
|
| 114 |
+
pass
|
| 115 |
+
|
| 116 |
+
# 开始执行按钮
|
| 117 |
+
start_btn = gradio.Button(value='开始!')
|
| 118 |
+
|
| 119 |
+
with gradio.Accordion(label="聊天记录", elem_id='chat-markdown-wrap-box'):
|
| 120 |
+
# 输出区域(隐藏状态)
|
| 121 |
+
history = gradio.State(value=[])
|
| 122 |
+
# 输出区域(md渲染)
|
| 123 |
+
history_md_stable = gradio.Markdown(value="🙂")
|
| 124 |
+
history_md_stream = gradio.Markdown(value="🤖")
|
| 125 |
+
|
| 126 |
+
with gradio.Accordion("提示"):
|
| 127 |
+
tips = gradio.Markdown(value="待命")
|
| 128 |
+
|
| 129 |
+
# 中止执行按钮
|
| 130 |
+
stop_btn = gradio.Button(value='中止!')
|
| 131 |
+
|
| 132 |
+
with gradio.Accordion("下载聊天记录", open=False):
|
| 133 |
+
make_file_btn = gradio.Button(value='生成文件')
|
| 134 |
+
with gradio.Row(visible=False) as file_row:
|
| 135 |
+
# 下载区域(json文件)
|
| 136 |
+
history_file_json = gradio.File(label='Json 下载', interactive=False)
|
| 137 |
+
# 下载区域(md文件)
|
| 138 |
+
history_file_md = gradio.File(label='Markdown 下载', interactive=False)
|
| 139 |
+
pass
|
| 140 |
+
pass
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
make_file_btn.click(
|
| 144 |
+
fn=make_history_file_fn,
|
| 145 |
+
inputs=[history],
|
| 146 |
+
outputs=[history_file_json, history_file_md, file_row],
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
start_event = start_btn.click(
|
| 151 |
+
fn=sequential_chat_fn,
|
| 152 |
+
inputs=[
|
| 153 |
+
history,
|
| 154 |
+
|
| 155 |
+
system_prompt_enabled,
|
| 156 |
+
system_prompt,
|
| 157 |
+
user_message_template,
|
| 158 |
+
user_message_template_mask,
|
| 159 |
+
user_message_template_mask_is_regex,
|
| 160 |
+
user_message_list_text,
|
| 161 |
+
user_message_list_text_splitter,
|
| 162 |
+
user_message_list_text_splitter_is_regex,
|
| 163 |
+
history_prompt_num,
|
| 164 |
+
|
| 165 |
+
api_key_text, token_text,
|
| 166 |
+
|
| 167 |
+
sleep_base,
|
| 168 |
+
sleep_rand,
|
| 169 |
+
prop_stream,
|
| 170 |
+
prop_model,
|
| 171 |
+
prop_temperature,
|
| 172 |
+
prop_top_p,
|
| 173 |
+
prop_choices_num,
|
| 174 |
+
prop_max_tokens,
|
| 175 |
+
prop_presence_penalty,
|
| 176 |
+
prop_frequency_penalty,
|
| 177 |
+
prop_logit_bias,
|
| 178 |
+
],
|
| 179 |
+
outputs=[
|
| 180 |
+
history,
|
| 181 |
+
history_md_stable,
|
| 182 |
+
history_md_stream,
|
| 183 |
+
tips,
|
| 184 |
+
file_row,
|
| 185 |
+
],
|
| 186 |
+
)
|
| 187 |
+
stop_btn.click(
|
| 188 |
+
fn=None,
|
| 189 |
+
inputs=[],
|
| 190 |
+
outputs=[],
|
| 191 |
+
cancels=[start_event],
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
if __name__ == "__main__":
|
| 196 |
+
demo.queue(concurrency_count=200).launch()
|