CoolFace
Apppublic

eveyuyi/ChatGPT_Prompts

sourceHugging Facegpl-3.0updated 3y agoView on Hugging Face
0likes
utils.py435 linesDownload Raw Back to modules
1# -*- coding:utf-8 -*-2from __future__ import annotations3from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple, Type4import logging5import json6import os7import datetime8import hashlib9import csv10import requests11import re12import html13 14import gradio as gr15from pypinyin import lazy_pinyin16import tiktoken17import mdtex2html18from markdown import markdown19from pygments import highlight20from pygments.lexers import get_lexer_by_name21from pygments.formatters import HtmlFormatter22 23from modules.presets import *24import modules.shared as shared25 26logging.basicConfig(27    level=logging.INFO,28    format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s",29)30 31if TYPE_CHECKING:32    from typing import TypedDict33 34    class DataframeData(TypedDict):35        headers: List[str]36        data: List[List[str | int | bool]]37 38 39def count_token(message):40    encoding = tiktoken.get_encoding("cl100k_base")41    input_str = f"role: {message['role']}, content: {message['content']}"42    length = len(encoding.encode(input_str))43    return length44 45 46def markdown_to_html_with_syntax_highlight(md_str):47    def replacer(match):48        lang = match.group(1) or "text"49        code = match.group(2)50 51        try:52            lexer = get_lexer_by_name(lang, stripall=True)53        except ValueError:54            lexer = get_lexer_by_name("text", stripall=True)55 56        formatter = HtmlFormatter()57        highlighted_code = highlight(code, lexer, formatter)58 59        return f'<pre><code class="{lang}">{highlighted_code}</code></pre>'60 61    code_block_pattern = r"```(\w+)?\n([\s\S]+?)\n```"62    md_str = re.sub(code_block_pattern, replacer, md_str, flags=re.MULTILINE)63 64    html_str = markdown(md_str)65    return html_str66 67 68def normalize_markdown(md_text: str) -> str:69    lines = md_text.split("\n")70    normalized_lines = []71    inside_list = False72 73    for i, line in enumerate(lines):74        if re.match(r"^(\d+\.|-|\*|\+)\s", line.strip()):75            if not inside_list and i > 0 and lines[i - 1].strip() != "":76                normalized_lines.append("")77            inside_list = True78            normalized_lines.append(line)79        elif inside_list and line.strip() == "":80            if i < len(lines) - 1 and not re.match(81                r"^(\d+\.|-|\*|\+)\s", lines[i + 1].strip()82            ):83                normalized_lines.append(line)84            continue85        else:86            inside_list = False87            normalized_lines.append(line)88 89    return "\n".join(normalized_lines)90 91 92def convert_mdtext(md_text):93    code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)94    inline_code_pattern = re.compile(r"`(.*?)`", re.DOTALL)95    code_blocks = code_block_pattern.findall(md_text)96    non_code_parts = code_block_pattern.split(md_text)[::2]97 98    result = []99    for non_code, code in zip(non_code_parts, code_blocks + [""]):100        if non_code.strip():101            non_code = normalize_markdown(non_code)102            if inline_code_pattern.search(non_code):103                result.append(markdown(non_code, extensions=["tables"]))104            else:105                result.append(mdtex2html.convert(non_code, extensions=["tables"]))106        if code.strip():107            # _, code = detect_language(code)  # 暂时去除代码高亮功能,因为在大段代码的情况下会出现问题108            # code = code.replace("\n\n", "\n") # 暂时去除代码中的空行,因为在大段代码的情况下会出现问题109            code = f"\n```{code}\n\n```"110            code = markdown_to_html_with_syntax_highlight(code)111            result.append(code)112    result = "".join(result)113    result += ALREADY_CONVERTED_MARK114    return result115 116 117def convert_asis(userinput):118    return f"<p style=\"white-space:pre-wrap;\">{html.escape(userinput)}</p>"+ALREADY_CONVERTED_MARK119 120def detect_converted_mark(userinput):121    if userinput.endswith(ALREADY_CONVERTED_MARK):122        return True123    else:124        return False125 126 127def detect_language(code):128    if code.startswith("\n"):129        first_line = ""130    else:131        first_line = code.strip().split("\n", 1)[0]132    language = first_line.lower() if first_line else ""133    code_without_language = code[len(first_line) :].lstrip() if first_line else code134    return language, code_without_language135 136 137def construct_text(role, text):138    return {"role": role, "content": text}139 140 141def construct_user(text):142    return construct_text("user", text)143 144 145def construct_system(text):146    return construct_text("system", text)147 148 149def construct_assistant(text):150    return construct_text("assistant", text)151 152 153def construct_token_message(token, stream=False):154    return f"Token 计数: {token}"155 156def delete_first_conversation(history, previous_token_count):157    if history:158        del history[:2]159        del previous_token_count[0]160    return (161        history,162        previous_token_count,163        construct_token_message(sum(previous_token_count)),164    )165 166 167def delete_last_conversation(chatbot, history, previous_token_count):168    if len(chatbot) > 0 and standard_error_msg in chatbot[-1][1]:169        logging.info("由于包含报错信息,只删除chatbot记录")170        chatbot.pop()171        return chatbot, history172    if len(history) > 0:173        logging.info("删除了一组对话历史")174        history.pop()175        history.pop()176    if len(chatbot) > 0:177        logging.info("删除了一组chatbot对话")178        chatbot.pop()179    if len(previous_token_count) > 0:180        logging.info("删除了一组对话的token计数记录")181        previous_token_count.pop()182    return (183        chatbot,184        history,185        previous_token_count,186        construct_token_message(sum(previous_token_count)),187    )188 189 190def save_file(filename, system, history, chatbot):191    logging.info("保存对话历史中……")192    os.makedirs(HISTORY_DIR, exist_ok=True)193    if filename.endswith(".json"):194        json_s = {"system": system, "history": history, "chatbot": chatbot}195        print(json_s)196        with open(os.path.join(HISTORY_DIR, filename), "w") as f:197            json.dump(json_s, f)198    elif filename.endswith(".md"):199        md_s = f"system: \n- {system} \n"200        for data in history:201            md_s += f"\n{data['role']}: \n- {data['content']} \n"202        with open(os.path.join(HISTORY_DIR, filename), "w", encoding="utf8") as f:203            f.write(md_s)204    logging.info("保存对话历史完毕")205    return os.path.join(HISTORY_DIR, filename)206 207 208def save_chat_history(filename, system, history, chatbot):209    if filename == "":210        return211    if not filename.endswith(".json"):212        filename += ".json"213    return save_file(filename, system, history, chatbot)214 215 216def export_markdown(filename, system, history, chatbot):217    if filename == "":218        return219    if not filename.endswith(".md"):220        filename += ".md"221    return save_file(filename, system, history, chatbot)222 223 224def load_chat_history(filename, system, history, chatbot):225    logging.info("加载对话历史中……")226    if type(filename) != str:227        filename = filename.name228    try:229        with open(os.path.join(HISTORY_DIR, filename), "r") as f:230            json_s = json.load(f)231        try:232            if type(json_s["history"][0]) == str:233                logging.info("历史记录格式为旧版,正在转换……")234                new_history = []235                for index, item in enumerate(json_s["history"]):236                    if index % 2 == 0:237                        new_history.append(construct_user(item))238                    else:239                        new_history.append(construct_assistant(item))240                json_s["history"] = new_history241                logging.info(new_history)242        except:243            # 没有对话历史244            pass245        logging.info("加载对话历史完毕")246        return filename, json_s["system"], json_s["history"], json_s["chatbot"]247    except FileNotFoundError:248        logging.info("没有找到对话历史文件,不执行任何操作")249        return filename, system, history, chatbot250 251 252def sorted_by_pinyin(list):253    return sorted(list, key=lambda char: lazy_pinyin(char)[0][0])254 255 256def get_file_names(dir, plain=False, filetypes=[".json"]):257    logging.info(f"获取文件名列表,目录为{dir},文件类型为{filetypes},是否为纯文本列表{plain}")258    files = []259    try:260        for type in filetypes:261            files += [f for f in os.listdir(dir) if f.endswith(type)]262    except FileNotFoundError:263        files = []264    files = sorted_by_pinyin(files)265    if files == []:266        files = [""]267    if plain:268        return files269    else:270        return gr.Dropdown.update(choices=files)271 272 273def get_history_names(plain=False):274    logging.info("获取历史记录文件名列表")275    return get_file_names(HISTORY_DIR, plain)276 277 278def load_template(filename, mode=0):279    logging.info(f"加载模板文件{filename},模式为{mode}(0为返回字典和下拉菜单,1为返回下拉菜单,2为返回字典)")280    lines = []281    logging.info("Loading template...")282    if filename.endswith(".json"):283        with open(os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as f:284            lines = json.load(f)285        lines = [[i["act"], i["prompt"]] for i in lines]286    else:287        with open(288            os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8"289        ) as csvfile:290            reader = csv.reader(csvfile)291            lines = list(reader)292        lines = lines[1:]293    if mode == 1:294        return sorted_by_pinyin([row[0] for row in lines])295    elif mode == 2:296        return {row[0]: row[1] for row in lines}297    else:298        choices = sorted_by_pinyin([row[0] for row in lines])299        return {row[0]: row[1] for row in lines}, gr.Dropdown.update(300            choices=choices, value=choices[0]301        )302 303 304def get_template_names(plain=False):305    logging.info("获取模板文件名列表")306    return get_file_names(TEMPLATES_DIR, plain, filetypes=[".csv", "json"])307 308 309def get_template_content(templates, selection, original_system_prompt):310    logging.info(f"应用模板中,选择为{selection},原始系统提示为{original_system_prompt}")311    try:312        return templates[selection]313    except:314        return original_system_prompt315 316 317def reset_state():318    logging.info("重置状态")319    return [], [], [], construct_token_message(0)320 321 322def reset_textbox():323    logging.debug("重置文本框")324    return gr.update(value="")325 326 327def reset_default():328    newurl = shared.state.reset_api_url()329    os.environ.pop("HTTPS_PROXY", None)330    os.environ.pop("https_proxy", None)331    return gr.update(value=newurl), gr.update(value=""), "API URL 和代理已重置"332 333 334def change_api_url(url):335    shared.state.set_api_url(url)336    msg = f"API地址更改为了{url}"337    logging.info(msg)338    return msg339 340 341def change_proxy(proxy):342    os.environ["HTTPS_PROXY"] = proxy343    msg = f"代理更改为了{proxy}"344    logging.info(msg)345    return msg346 347 348def hide_middle_chars(s):349    if len(s) <= 8:350        return s351    else:352        head = s[:4]353        tail = s[-4:]354        hidden = "*" * (len(s) - 8)355        return head + hidden + tail356 357 358def submit_key(key):359    key = key.strip()360    msg = f"API密钥更改为了{hide_middle_chars(key)}"361    logging.info(msg)362    return key, msg363 364 365def sha1sum(filename):366    sha1 = hashlib.sha1()367    sha1.update(filename.encode("utf-8"))368    return sha1.hexdigest()369 370 371def replace_today(prompt):372    today = datetime.datetime.today().strftime("%Y-%m-%d")373    return prompt.replace("{current_date}", today)374 375 376def get_geoip():377    response = requests.get("https://ipapi.co/json/", timeout=5)378    try:379        data = response.json()380    except:381        data = {"error": True, "reason": "连接ipapi失败"}382    if "error" in data.keys():383        logging.warning(f"无法获取IP地址信息。\n{data}")384        if data["reason"] == "RateLimited":385            return (386                f"获取IP地理位置失败,因为达到了检测IP的速率限制。聊天功能可能仍然可用,但请注意,如果您的IP地址在不受支持的地区,您可能会遇到问题。"387            )388        else:389            return f"获取IP地理位置失败。原因:{data['reason']}。你仍然可以使用聊天功能。"390    else:391        country = data["country_name"]392        if country == "China":393            text = "**您的IP区域:中国。请立即检查代理设置,在不受支持的地区使用API可能导致账号被封禁。**"394        else:395            text = f"您的IP区域:{country}。"396        logging.info(text)397        return text398 399 400def find_n(lst, max_num):401    n = len(lst)402    total = sum(lst)403 404    if total < max_num:405        return n406 407    for i in range(len(lst)):408        if total - lst[i] < max_num:409            return n - i - 1410        total = total - lst[i]411    return 1412 413 414def start_outputing():415    logging.debug("显示取消按钮,隐藏发送按钮")416    return gr.Button.update(visible=False), gr.Button.update(visible=True)417 418 419def end_outputing():420    return (421        gr.Button.update(visible=True),422        gr.Button.update(visible=False),423    )424 425 426def cancel_outputing():427    logging.info("中止输出……")428    shared.state.interrupt()429 430def transfer_input(inputs):431    # 一次性返回,降低延迟432    textbox = reset_textbox()433    outputing = start_outputing()434    return inputs, gr.update(value="")435