Spaces:
Runtime error
Runtime error
File size: 4,611 Bytes
f590842 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
"""
utils.py - Utility functions for the project.
"""
import logging
import re
def postprocess(text: str):
"""
postprocess - remove common values in scraped dataset
Args:
text (str): the text to postprocess
"""
replacements = {
"ENA": "COMPANY",
"Enron": "COMPANY",
"Enron Corporation": "COMPANY",
"Sony Pictures Entertainment": "COMPANY",
"Columbia Pictures": "COMPANY",
"Sony": "COMPANY",
"Columbia": "COMPANY",
"Hillary": "Jane",
"Clinton": "Smith",
"Amy": "Jane",
"Sara": "Jane",
"Harambe": "Jane",
"Pascal": "PERSON",
}
# replace common values, also check lowercase
for k, v in replacements.items():
text = text.replace(k, v)
text = text.replace(k.lower(), v)
return text
def clear(text, verbose=False, **kwargs):
"""for use with buttons"""
if verbose:
logging.info(f"Clearing text: {text}")
return ""
def make_email_link(
subject: str = "Email subject - This was generated by Postbot",
link_text: str = "click to open in your email client",
body: str = None,
tag_placeholder: str = "PLACEHOLDER",
):
"""
email_link - generate an email link
Args:
subject (str, optional): the subject of the email. Defaults to "Email subject - This was generated by Postbot".
link_text (str, optional): the text of the link. Defaults to "click to open in your email client".
body (str, optional): the body of the email. Defaults to None.
tag_placeholder (str, optional): the placeholder for the tag. Defaults to "PLACEHOLDER".
Returns:
str: the email link, in the form of an html link
"""
if body is None:
body = "hmm - no body. replace me"
# strip brackets and other HTML-tag characters from body with regex
body = re.sub(r"<[^>]*>", tag_placeholder, body)
# replace all newline chars with a whitespace
body = body.replace("\n", " ")
nice_html_button = f"""<!DOCTYPE html>
<html>
<head>
<title>Generated Email</title>
<style>
body {{
font-family: sans-serif;
font-size: 1.2em;
}}
.button {{
background-color: #6CCEC6;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
value: "Send Email";
}}
</style>
<button class="button" onclick="window.location.href='mailto:?subject={subject}&body={body}'">{link_text} value="Open in Email client"</button>
</html>"""
# return f'<a href="mailto:%20?subject={subject}&body={body}">{link_text}</a>'
return nice_html_button
def make_mailto_form(
body: str = None,
subject: str = "This email was generated by Postbot with AI!",
cc_email: str = "",
):
"""Returns a mailto link with the given parameters"""
if body is None:
body = "hmm - no body. Replace me or try rerunning the model."
template = f"""<!DOCTYPE html>
<html>
<head>
<title>Generated Email</title>
<style>
body {{
font-family: sans-serif;
font-size: 1.2em;
}}
.button {{
background-color: #6CCEC6;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
value: "Send Email";
}}
</style>
</head>
<body>
<h1>Adjust and Open in your mail client:</h1>
<form action="mailto:" method="get" enctype="text/plain">
<div>
<label for="cc">CC Email:
<input type="text" name="cc" id="cc" value="{cc_email}"/>
</label>
</div>
<div>
<label for="subject">Subject:
<input type="text" name="subject" id="subject" value="{subject}"/>
</label>
</div>
<div>
<label>Email Body:</label>
<br />
<textarea name="body" id="body" rows="12" cols="35">{body}</textarea>
</div>
<div>
<input type="submit" name="submit" value="Open in Email App" class="button"/>
</div>
</form>
</body>
</html>"""
return template
|