YiJina / utils.py
Tonic's picture
escape special characters
e7481b0
# utils.py
import re
from dotenv import load_dotenv
import re
import os
from globalvars import tasks
def load_env_variables():
# Load the .env file
load_dotenv()
# Retrieve the environment variables
hf_token = os.getenv('HF_TOKEN')
yi_token = os.getenv('YI_TOKEN')
return hf_token, yi_token
def parse_and_route(example_output: str):
# Regex pattern to match the true task
pattern = r'"(\w+)":\s?true'
# Find the true task
match = re.search(pattern, example_output)
if match:
true_task = match.group(1)
if true_task in tasks:
return {true_task: tasks[true_task]}
else:
return {true_task: "Task description not found"}
else:
return "No true task found in the example output"
import json
def escape_special_characters(text: str) -> str:
"""
Escapes special characters in the given text for JSON and cURL compatibility.
"""
escaped_text = json.dumps(text)[1:-1]
curl_escaped_text = escaped_text.replace(" ", "\\ ")
curl_escaped_text = curl_escaped_text.replace("&", "\\&")
curl_escaped_text = curl_escaped_text.replace(";", "\\;")
curl_escaped_text = curl_escaped_text.replace("(", "\\(")
curl_escaped_text = curl_escaped_text.replace(")", "\\)")
curl_escaped_text = curl_escaped_text.replace("$", "\\$")
curl_escaped_text = curl_escaped_text.replace("`", "\\`")
curl_escaped_text = curl_escaped_text.replace("|", "\\|")
curl_escaped_text = curl_escaped_text.replace("*", "\\*")
curl_escaped_text = curl_escaped_text.replace("?", "\\?")
curl_escaped_text = curl_escaped_text.replace("<", "\\<")
curl_escaped_text = curl_escaped_text.replace(">", "\\>")
curl_escaped_text = curl_escaped_text.replace("!", "\\!")
curl_escaped_text = curl_escaped_text.replace("{", "\\{")
curl_escaped_text = curl_escaped_text.replace("}", "\\}")
curl_escaped_text = curl_escaped_text.replace("[", "\\[")
curl_escaped_text = curl_escaped_text.replace("]", "\\]")
curl_escaped_text = curl_escaped_text.replace("#", "\\#")
curl_escaped_text = curl_escaped_text.replace("%", "\\%")
curl_escaped_text = curl_escaped_text.replace("^", "\\^")
curl_escaped_text = curl_escaped_text.replace("=", "\\=")
curl_escaped_text = curl_escaped_text.replace("~", "\\~")
curl_escaped_text = curl_escaped_text.replace("'", "\\'")
return curl_escaped_text