CoolFace
Apppublic

iridescentX/openui

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
task.py128 linesDownload Raw Back to utils
1import re2import math3 4from datetime import datetime5from typing import Optional6 7 8def prompt_template(9    template: str, user_name: str = None, user_location: str = None10) -> str:11    # Get the current date12    current_date = datetime.now()13 14    # Format the date to YYYY-MM-DD15    formatted_date = current_date.strftime("%Y-%m-%d")16    formatted_time = current_date.strftime("%I:%M:%S %p")17 18    template = template.replace("{{CURRENT_DATE}}", formatted_date)19    template = template.replace("{{CURRENT_TIME}}", formatted_time)20    template = template.replace(21        "{{CURRENT_DATETIME}}", f"{formatted_date} {formatted_time}"22    )23 24    if user_name:25        # Replace {{USER_NAME}} in the template with the user's name26        template = template.replace("{{USER_NAME}}", user_name)27    else:28        # Replace {{USER_NAME}} in the template with "Unknown"29        template = template.replace("{{USER_NAME}}", "Unknown")30 31    if user_location:32        # Replace {{USER_LOCATION}} in the template with the current location33        template = template.replace("{{USER_LOCATION}}", user_location)34    else:35        # Replace {{USER_LOCATION}} in the template with "Unknown"36        template = template.replace("{{USER_LOCATION}}", "Unknown")37 38    return template39 40 41def title_generation_template(42    template: str, prompt: str, user: Optional[dict] = None43) -> str:44    def replacement_function(match):45        full_match = match.group(0)46        start_length = match.group(1)47        end_length = match.group(2)48        middle_length = match.group(3)49 50        if full_match == "{{prompt}}":51            return prompt52        elif start_length is not None:53            return prompt[: int(start_length)]54        elif end_length is not None:55            return prompt[-int(end_length) :]56        elif middle_length is not None:57            middle_length = int(middle_length)58            if len(prompt) <= middle_length:59                return prompt60            start = prompt[: math.ceil(middle_length / 2)]61            end = prompt[-math.floor(middle_length / 2) :]62            return f"{start}...{end}"63        return ""64 65    template = re.sub(66        r"{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}",67        replacement_function,68        template,69    )70 71    template = prompt_template(72        template,73        **(74            {"user_name": user.get("name"), "user_location": user.get("location")}75            if user76            else {}77        ),78    )79 80    return template81 82 83def search_query_generation_template(84    template: str, prompt: str, user: Optional[dict] = None85) -> str:86 87    def replacement_function(match):88        full_match = match.group(0)89        start_length = match.group(1)90        end_length = match.group(2)91        middle_length = match.group(3)92 93        if full_match == "{{prompt}}":94            return prompt95        elif start_length is not None:96            return prompt[: int(start_length)]97        elif end_length is not None:98            return prompt[-int(end_length) :]99        elif middle_length is not None:100            middle_length = int(middle_length)101            if len(prompt) <= middle_length:102                return prompt103            start = prompt[: math.ceil(middle_length / 2)]104            end = prompt[-math.floor(middle_length / 2) :]105            return f"{start}...{end}"106        return ""107 108    template = re.sub(109        r"{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}",110        replacement_function,111        template,112    )113 114    template = prompt_template(115        template,116        **(117            {"user_name": user.get("name"), "user_location": user.get("location")}118            if user119            else {}120        ),121    )122    return template123 124 125def tools_function_calling_generation_template(template: str, tools_specs: str) -> str:126    template = template.replace("{{TOOLS}}", tools_specs)127    return template128