SatireInstruct / scripts /Email Creation Script.py
benni-ben's picture
Upload 4 files
bdfb457 verified
Raw History Blame Contribute Delete
6.31 kB
import ast
import json
import random
import time
import names
from google import genai
# ----------------------------
# Gemini Client
# ----------------------------
client = genai.Client(
api_key="API"
)
# ----------------------------
# Models (fallback order)
# ----------------------------
MODELS = [
"gemini-3.6-flash",
"gemini-3.5-flash",
"gemini-3.1-flash-lite",
"gemini-3.5-flash-lite",
"gemini-2.5-flash",
"gemini-2.5-flash-lite",
"gemini-3.1-flash-lite-preview",
"gemini-2.5-pro",
"gemini-3.1-pro-preview",
"gemini-3-pro-preview",
"gemini-pro-latest",
"gemini-flash-latest"
]
current_model = 0
# ----------------------------
# Generation Settings
# ----------------------------
generation_config = {
"max_output_tokens": 65536,
"thinking_level": "medium",
"temperature": 1.2,
}
# ----------------------------
# Load system instruction
# ----------------------------
with open("EmailPrompt.txt", "r", encoding="utf-8") as f:
inst = f.read().strip()
# ----------------------------
# Load email topics
# ----------------------------
with open("Topics.txt", "r", encoding="utf-8") as f:
topics = ast.literal_eval(f.read())
# ----------------------------
# Name Generator
# ----------------------------
def generate_person_name(role):
"""
Generates names with occasional titles.
Recipients are more likely to have titles because
professional emails often address people formally.
"""
name = names.get_full_name()
if role == "recipient":
title_chance = 0.35
else:
title_chance = 0.10
if random.random() < title_chance:
title = random.choice([
"Mr.",
"Mrs.",
"Ms.",
"Dr.",
"Prof."
])
return f"{title} {name}"
return name
# ----------------------------
# Prompt Generator
# ----------------------------
def create_email_prompt(topic):
sender = generate_person_name("sender")
recipient = generate_person_name("recipient")
templates = [
f"Write an email about {topic}.",
f"Create an email from {sender} to {recipient} about {topic}.",
f"Write an email from {sender} explaining {topic} to {recipient}.",
f"Write an email to {recipient} regarding {topic}.",
f"Write an email where {sender} needs to discuss {topic} with {recipient}.",
f"Generate a professional email from {sender} concerning {topic}.",
f"Write an email from {sender} requesting something related to {topic}.",
f"Create an email that {sender} would send to {recipient} about {topic}.",
f"Write an email addressed to {recipient} explaining the situation involving {topic}.",
f"Generate a workplace-style email from {sender} about {topic}.",
f"Write an email where an employee contacts {recipient} about {topic}.",
f"Write an email where I need to communicate {topic} to someone important.",
f"Create a formal email discussing {topic}.",
f"Write a message that could be sent to a supervisor about {topic}.",
f"Write an email from a person named {sender} about {topic}."
]
return random.choice(templates)
# ----------------------------
# Generate Dataset
# ----------------------------
with open("terrible_emails_dataset.jsonl", "w", encoding="utf-8") as outfile:
for topic in topics:
prompt = create_email_prompt(topic)
print(
f"[{MODELS[current_model]}] Generating: {topic}"
)
while True:
try:
interaction = client.interactions.create(
model=MODELS[current_model],
input=prompt,
system_instruction=inst,
generation_config=generation_config,
)
assistant_response = interaction.output_text
break
except Exception as e:
error = str(e)
error_lower = error.lower()
print(error)
# ----------------------------
# Quota handling
# ----------------------------
if any(x in error_lower for x in (
"resource_exhausted",
"quota",
"daily limit",
"per day",
"429",
"rate limit",
)):
if current_model < len(MODELS) - 1:
current_model += 1
print(
f"Switching to {MODELS[current_model]}"
)
continue
raise RuntimeError(
"All models exhausted quota."
)
# ----------------------------
# Temporary errors
# ----------------------------
if any(x in error_lower for x in (
"500",
"502",
"503",
"504",
"internal",
"timeout",
"connection",
"unavailable",
)):
print(
"Temporary error. Retrying in 15 seconds..."
)
time.sleep(15)
continue
raise
dataset_entry = {
"messages": [
{
"role": "user",
"content": prompt,
},
{
"role": "assistant",
"content": assistant_response,
},
]
}
outfile.write(
json.dumps(
dataset_entry,
ensure_ascii=False
)
)
outfile.write("\n")
outfile.flush()
print("Done!")