-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_newsletter_groq.py
More file actions
290 lines (253 loc) · 7.37 KB
/
generate_newsletter_groq.py
File metadata and controls
290 lines (253 loc) · 7.37 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import json
import os
import re
from groq import Groq
from dotenv import load_dotenv
# ---------------- CONFIG ----------------
INPUT_JSON = "data/ai_news_digest.json"
OUTPUT_JSON = "data/ai_newsletter.json"
MODEL = "llama-3.1-8b-instant"
load_dotenv()
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
def extract_url_and_summary(digest_text):
"""
Extracts the URL from the end of the digest (if any)
and returns (clean_summary, url).
"""
url_match = re.search(r"(https?://\S+)", digest_text.strip().split()[-1])
if url_match:
url = url_match.group(0)
# Remove the URL from the digest text
clean_text = digest_text.replace(url, "").strip()
else:
url = None
clean_text = digest_text.strip()
return clean_text, url
def sanitize_text(text: str) -> str:
"""Cleans LLM output to make it JSON-safe."""
text = text.strip()
text = re.sub(r"```(?:json)?", "", text)
text = text.strip("` \n")
text = text.replace("\r", "")
text = re.sub(r"[\x00-\x1F\x7F]", "", text) # remove control chars
return text
def safe_parse_json(text: str):
"""Tries to safely parse JSON output, falling back to plain text if invalid."""
text = sanitize_text(text)
try:
return json.loads(text)
except json.JSONDecodeError:
match = re.search(r"\{[\s\S]+\}", text)
if match:
try:
return json.loads(match.group(0))
except Exception:
pass
return {
"subject": "AI Daily Newsletter",
"newsletter_body": f"<html><body><pre>{text}</pre></body></html>",
}
def create_newsletter(digests):
# Extract title, clean digest, and URL properly
structured_articles = []
for d in digests:
summary, url = extract_url_and_summary(d["digest"])
structured_articles.append({
"title": d["title"],
"summary": summary,
"url": url or "No link available"
})
digest_text = "\n\n".join(
[
f"Title: {a['title']}\nDigest: {a['summary']}\nURL: {a['url']}"
for a in structured_articles
]
)
prompt = f"""
You are a professional AI journalist writing for *The Vector Daily*, a credible AI and technology newsletter.
Below are today's AI article digests:
{digest_text}
Generate a **formal, professional HTML newsletter** with this structure:
1. A 5–6 word professional **subject line**.
2. A **newsletter body (HTML)** that includes:
- A 2–3 line introduction.
- One section per article:
- The article title in <b>bold</b>.
- A rewritten summary of 4–6 sentences (clear, professional, not repetitive).
- Then a line:
<i>To read the full article → <a href="ARTICLE_URL">Click here</a></i>
(Replace ARTICLE_URL with the provided link.)
- A **Final Summary paragraph** (4–5 sentences) connecting all major trends.
- A **moderate or difficult AI algorithm riddle/question** at the end.
- Close with: “Stay curious, The Vector Daily Team”.
Tone:
- Professional, informative, slightly conversational.
- No markdown, emojis, or code blocks.
- Keep formatting clean and email-friendly.
Output valid JSON:
{{
"subject": "...",
"newsletter_body": "<html> ... </html>"
}}
"""
try:
response = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": prompt}],
temperature=0.7,
max_tokens=1800,
)
raw_output = response.choices[0].message.content.strip()
clean_output = sanitize_text(raw_output)
return safe_parse_json(clean_output)
except Exception as e:
print(f"❌ Error creating newsletter: {e}")
return None
def main():
with open(INPUT_JSON, "r", encoding="utf-8") as f:
digests = json.load(f)
print("🧠 Generating newsletter summary...")
result = create_newsletter(digests)
if result:
result["newsletter_body"] = style_newsletter(result["newsletter_body"])
os.makedirs("data", exist_ok=True)
with open(OUTPUT_JSON, "w", encoding="utf-8") as f:
json.dump(result, f, ensure_ascii=False, indent=2)
print(f"✅ Newsletter generated and saved to {OUTPUT_JSON}")
with open("data/vector_daily.html", "w", encoding="utf-8") as f:
f.write(result["newsletter_body"])
print("🌐 Styled HTML preview saved to data/vector_daily.html")
else:
print("⚠️ Newsletter generation failed.")
def style_newsletter(html_content: str):
"""Wraps the generated newsletter HTML in a clean, responsive layout."""
styled_html = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The Vector Daily</title>
<style>
/* Base Styles */
body {{
font-family: 'Inter', Arial, sans-serif;
background-color: #f5f7fa;
margin: 0;
padding: 0;
color: #333;
-webkit-font-smoothing: antialiased;
}}
.container {{
max-width: 700px;
margin: 30px auto;
background: #ffffff;
border-radius: 14px;
overflow: hidden;
box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
}}
/* Header */
.header {{
background: linear-gradient(135deg, #0057e7, #003ea3);
color: #fff;
text-align: center;
padding: 50px 20px 40px;
}}
.header img {{
max-height: 70px;
margin-bottom: 20px;
border-radius: 8px;
}}
.header h1 {{
font-size: 36px;
margin: 0;
font-weight: 800;
letter-spacing: 0.5px;
}}
/* Content */
.content {{
padding: 30px 40px;
line-height: 1.8;
}}
.content h2 {{
color: #111;
font-size: 24px;
margin-top: 35px;
font-weight: 700;
}}
.content p {{
margin: 12px 0;
font-size: 17px;
color: #444;
}}
.highlight {{
background: #f0f5ff;
border-left: 4px solid #0057e7;
padding: 12px 16px;
border-radius: 6px;
font-size: 15px;
}}
a {{
color: #0057e7;
font-weight: 600;
text-decoration: none;
}}
a:hover {{
text-decoration: underline;
}}
hr {{
border: 0;
border-top: 1px solid #eaeaea;
margin: 30px 0;
}}
/* Footer */
.footer {{
background-color: #f8f8f8;
padding: 18px 25px;
text-align: center;
font-size: 14px;
color: #666;
border-top: 1px solid #e4e4e4;
}}
.footer a {{
color: #0057e7;
text-decoration: none;
}}
/* Responsive */
@media (max-width: 600px) {{
.content {{
padding: 20px;
}}
.header h1 {{
font-size: 28px;
}}
.content h2 {{
font-size: 20px;
}}
}}
</style>
</head>
<body>
<div class="container">
<div class="header">
<img src="cid:logo" alt="The Vector Daily Logo" style="width:120px; height:auto; border:none; margin-bottom:10px;">
<h1>The Vector Daily</h1>
</div>
<div class="content">
{html_content}
<hr>
<p class="highlight">
Stay curious. Keep exploring AI and technology with <b>The Vector Daily</b> — your trusted digest for innovation, strategy, and breakthroughs.
</p>
</div>
<div class="footer">
You’re receiving this email because you subscribed to <b>The Vector Daily</b>.<br>
<a href="#">Unsubscribe</a> • <a href="#">Privacy Policy</a>
</div>
</div>
</body>
</html>
"""
return styled_html
if __name__ == "__main__":
main()