pick 部分代码

This commit is contained in:
w_xiaolizu
2023-06-27 13:56:52 +08:00
parent d8ec10e859
commit 670b5b0f5b
6 changed files with 248 additions and 62 deletions

View File

@ -133,7 +133,7 @@ def html_tag_color(tag, color=None, font='black'):
def html_a_blank(__href, name=''):
if not name:
dir_name = __href
name = __href
a = f'<a href="{__href}" target="_blank" class="svelte-xrr240">{name}</a>'
return a
@ -152,6 +152,9 @@ def ipaddr():
if ip[i][0][3]:
return ip[i][0][1]
def html_local_img(__file):
a = f'<div align="center"><img src="file={__file}"></div>'
return a
def encryption_str(txt: str):
"""(关键字)(加密间隔)匹配机制(关键字间隔)"""
@ -300,6 +303,7 @@ def diff_list(txt='', percent=0.70, switch: list = None, lst: dict = None, sp=15
for key in sorted_dict:
# 开始匹配关键字
index = str(key[0]).lower().find(txt.lower())
if index != -1:
# sp=split 用于判断在哪里启动、在哪里断开
if index - sp > 0:
@ -424,7 +428,7 @@ def prompt_save(txt, name, prompt: gr.Dataset, ipaddr: gr.Request):
return txt, name, [], prompt.update(samples=result, visible=True), prompt
def prompt_input(txt: str, index, data: gr.Dataset, tabs_index):
def prompt_input(txt: str, prompt_str, name_str, index, data: gr.Dataset, tabs_index):
"""
点击dataset的值使用Prompt
Args:
@ -436,17 +440,22 @@ def prompt_input(txt: str, index, data: gr.Dataset, tabs_index):
"""
data_str = str(data.samples[index][1])
data_name = str(data.samples[index][0])
rp_str = '"""{v}"""'
if data_str.find(rp_str) != -1:
new_txt = data_str.replace(rp_str, txt)
elif txt and tabs_index == 0:
new_txt = data_str + '\n' + txt
else:
new_txt = data_str
rp_str = '{{{v}}}'
def str_v_handle(__str):
if data_str.find(rp_str) != -1 and __str:
txt_temp = data_str.replace(rp_str, __str)
elif __str:
txt_temp = data_str + '\n' + __str
else:
txt_temp = data_str
return txt_temp
if tabs_index == 1:
new_txt = str_v_handle(prompt_str)
return txt, new_txt, data_name
else:
return new_txt, '', ''
new_txt = str_v_handle(txt)
return new_txt, prompt_str, name_str
def copy_result(history):
@ -498,7 +507,10 @@ def thread_write_chat(chatbot, history):
private_key = toolbox.get_conf('private_key')[0]
chat_title = chatbot[0][1].split()
i_say = pattern_markdown.sub('', chatbot[-1][0])
gpt_result = history
if history:
gpt_result = history
else: # 如果历史对话不存在,那么读取对话框
gpt_result = [pattern_markdown.sub('', v) for i in chatbot for v in i]
if private_key in chat_title:
SqliteHandle(f'ai_private_{chat_title[-2]}').inset_prompt({i_say: gpt_result})
else:
@ -508,11 +520,10 @@ def thread_write_chat(chatbot, history):
base_path = os.path.dirname(__file__)
prompt_path = os.path.join(base_path, 'users_data')
def reuse_chat(result, chatbot, history, pro_numb):
"""复用对话记录"""
if result is None or result == []:
return chatbot, history, gr.update(), gr.update(), ''
return chatbot, history, gr.update(), gr.update(), '', gr.Column.update()
else:
if pro_numb:
chatbot += result
@ -522,7 +533,7 @@ def reuse_chat(result, chatbot, history, pro_numb):
history += [pattern_markdown.sub('', _) for i in result[-2:] for _ in i]
print(chatbot[-1][0])
i_say = pattern_markdown.sub('', chatbot[-1][0])
return chatbot, history, i_say, gr.Tabs.update(selected='chatbot'), ''
return chatbot, history, i_say, gr.Tabs.update(selected='chatbot'), '', gr.Column.update(visible=False)
def num_tokens_from_string(listing: list, encoding_name: str = 'cl100k_base') -> int:
@ -548,7 +559,7 @@ def spinner_chatbot_loading(chatbot):
return loading_msg
def refresh_load_data(chat, history, prompt):
def refresh_load_data(chat, history, prompt, crazy_list):
"""
Args:
chat: 聊天组件
@ -561,7 +572,8 @@ def refresh_load_data(chat, history, prompt):
is_all = toolbox.get_conf('prompt_list')[0]['key'][0]
data = prompt_retrieval(is_all=[is_all])
prompt.samples = data
return prompt.update(samples=data, visible=True), prompt, chat, history
selected = random.sample(crazy_list, 4)
return prompt.update(samples=data, visible=True), prompt, chat, history, gr.Dataset.update(samples=[[i] for i in selected]), selected
@ -579,6 +591,74 @@ def txt_converter_json(input_string):
return input_string
def clean_br_string(s):
s = re.sub('<\s*br\s*/?>', '\n', s) # 使用正则表达式同时匹配<br>、<br/>、<br />、< br>和< br/>
return s
def update_btn(self,
value: str = None,
variant: str = None,
visible: bool = None,
interactive: bool = None,
elem_id: str = None,
label: str = None
):
if not variant: variant = self.variant
if not visible: visible = self.visible
if not value: value = self.value
if not interactive: interactive = self.interactive
if not elem_id: elem_id = self.elem_id
if not elem_id: label = self.label
return {
"variant": variant,
"visible": visible,
"value": value,
"interactive": interactive,
'elem_id': elem_id,
'label': label,
"__type__": "update",
}
def update_txt(self,
value: str = None,
lines: int = None,
max_lines: int = None,
placeholder: str = None,
label: str = None,
show_label: bool = None,
visible: bool = None,
interactive: bool = None,
type: str = None,
elem_id: str = None
):
return {
"lines": self.lines,
"max_lines": self.max_lines,
"placeholder": self.placeholder,
"label": self.label,
"show_label": self.show_label,
"visible": self.visible,
"value": self.value,
"type": self.type,
"interactive": self.interactive,
"elem_id": elem_id,
"__type__": "update",
}
def txtx(f, q):
return f
def git_log_list():
ll = Shell("git log --pretty=format:'%s | %h' -n 10").read()[1].splitlines()
return [i.split('|') for i in ll if 'branch' not in i][:5]
class YamlHandle:
def __init__(self, file=os.path.join(prompt_path, 'ai_common.yaml')):
@ -614,21 +694,14 @@ class YamlHandle:
class JsonHandle:
def __init__(self, file):
if os.path.exists(file):
with open(file=file, mode='r') as self.file_obj:
pass
else:
self.file_obj = io.StringIO() # 创建空白文本对象
self.file_obj.write('{}') # 向文本对象写入有有效 JSON 格式的数据
self.file_obj.seek(0) # 将文本对象的光标重置到开头
self.file = file
def load(self):
data = json.load(self.file_obj)
def load(self) -> object:
with open(self.file, 'r') as f:
data = json.load(f)
return data
if __name__ == '__main__':
result = [['214214', '5657'], ['fasfaf', '41241'],['kkkgh', '1`31`3'],]
ff = [pattern_markdown.sub('', _) for i in result[-2:] for _ in i]
print(ff)
html_local_img("docs/imgs/openai-api-key-billing-paid-account.png")