Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -10,31 +10,42 @@ model = AutoModelForCausalLM.from_pretrained(model_checkpoint)
|
|
10 |
generator = pipeline('text-generation', model=model, tokenizer=tokenizer)
|
11 |
|
12 |
def clean_generated_text(text):
|
13 |
-
#
|
14 |
-
text = re.sub(r'^(Re:|Fwd:)', '', text)
|
15 |
-
text = re.sub(r'Best regards,.*$', '', text, flags=re.DOTALL)
|
16 |
-
text = re.sub(r'PHONE.*$', '', text, flags=re.DOTALL)
|
17 |
-
text = re.sub(r'Email:.*$', '', text, flags=re.DOTALL)
|
18 |
-
text = re.sub(r'Cc:.*$', '', text, flags=re.DOTALL)
|
19 |
-
text = re.sub(r'\* Attachments:.*', '', text, flags=re.S)
|
20 |
-
text = re.sub(r'©️ .*$', '', text, flags=re.DOTALL)
|
21 |
-
text = re.sub(r'URL
|
22 |
-
text = re.sub(r'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
return text.strip()
|
24 |
|
25 |
def generate_email(product, gender, profession, hobby):
|
26 |
input_text = f"{product} {gender} {profession} {hobby}"
|
27 |
result = generator(
|
28 |
-
input_text,
|
29 |
-
max_length=256,
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
return best_text
|
|
|
|
10 |
generator = pipeline('text-generation', model=model, tokenizer=tokenizer)
|
11 |
|
12 |
def clean_generated_text(text):
|
13 |
+
#Basic cleaning
|
14 |
+
text = re.sub(r'^(Re:|Fwd:)', '', text) # Remove reply and forward marks
|
15 |
+
text = re.sub(r'Best regards,.*$', '', text, flags=re.DOTALL) # Remove signature
|
16 |
+
text = re.sub(r'PHONE.*$', '', text, flags=re.DOTALL) # Remove phone numbers
|
17 |
+
text = re.sub(r'Email:.*$', '', text, flags=re.DOTALL) # Remove email addresses
|
18 |
+
text = re.sub(r'Cc:.*$', '', text, flags=re.DOTALL) # Remove CC list
|
19 |
+
text = re.sub(r'\* Attachments:.*', '', text, flags=re.S) # Remove Attachments
|
20 |
+
text = re.sub(r'©️ .*$', '', text, flags=re.DOTALL) # Remove copyright and ownership statements
|
21 |
+
text = re.sub(r'URL', '', text) # Remove URLs
|
22 |
+
text = re.sub(r'NUMBER', '10', text) # Replace 'NUMBER' with a real number
|
23 |
+
text = re.sub(r'CURRENCYNUMBER', 'USD 100', text) # Replace 'CURRENCYNUMBER' with a real value
|
24 |
+
text = re.sub(r'About Us.*', '', text, flags=re.DOTALL) # Remove 'About Us' and all following text
|
25 |
+
text = re.sub(r'\d+ [^\s]+ St\.?,?.*?\d{5}', '', text) # Remove street
|
26 |
+
text = re.sub(r'\d+ [^\s]+ Ave\.?,?.*?\d{5}', '', text) # Remove avenues
|
27 |
+
text = re.sub(r'\d+ [^\s]+ Rd\.?,?.*?\d{5}', '', text) # Remove roads
|
28 |
+
text = re.sub(r'\d+ [^\s]+ Ln\.?,?.*?\d{5}', '', text) # Remove lanes
|
29 |
+
text = re.sub(r'\d+ [^\s]+ Blvd\.?,?.*?\d{5}', '', text) # Remove boulevards
|
30 |
+
text = re.sub(r'\d+ [^\s]+ Dr\.?,?.*?\d{5}', '', text) # Remove drives
|
31 |
+
text = re.sub(r'\d+ [^\s]+ Ct\.?,?.*?\d{5}', '', text) # Remove courts
|
32 |
return text.strip()
|
33 |
|
34 |
def generate_email(product, gender, profession, hobby):
|
35 |
input_text = f"{product} {gender} {profession} {hobby}"
|
36 |
result = generator(
|
37 |
+
input_text, # The starting text that guides the model on what to generate
|
38 |
+
max_length=256, # Set a suitable maximum length
|
39 |
+
top_k=40, # Consider more top options words
|
40 |
+
top_p=0.6, # Control the probability range for word choices
|
41 |
+
temperature=0.4, # Control the randomness of generation
|
42 |
+
repetition_penalty=1.5, # Reduce content repetition
|
43 |
+
num_return_sequences=2, # Generate three texts
|
44 |
+
do_sample=True
|
45 |
+
)
|
46 |
+
# Clean each generated text
|
47 |
+
cleaned_texts = [clean_generated_text(seq['generated_text']) for seq in result]
|
48 |
+
# Choose the best text based on length and clarity
|
49 |
+
best_text = max(cleaned_texts, key=len)
|
50 |
return best_text
|
51 |
+
|