-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildpost.py
301 lines (258 loc) · 8.13 KB
/
buildpost.py
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
291
292
293
294
295
296
297
298
299
300
301
import os
import openai
from openai import OpenAI
from dotenv import load_dotenv
import shutil
from datetime import datetime
import urllib.request
import argparse
import json
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
postrootfolder=str(os.getenv("POST_ROOT_FOLDER"))
author=str(os.getenv("AUTHOR"))
identifier="default"
title=""
tags=""
imageprompt=""
articleprompt=""
articletext=""
iArticleRevision=0
iImageRevision=0
sourceurl=""
def pusharticle():
global identifier
global title
global iArticleRevision
global sourceurl
filename="./draft/"+identifier+"/index."+str(iArticleRevision)+".md"
print("Creating" + filename)
with open(filename, 'w') as f:
f.write('---\n')
f.write('title: "'+title+'"\n')
now = datetime.now()
currenttime=now.strftime("%Y-%m-%dT%H:%M:%S")
f.write('date: '+ currenttime +'+05:00\n')
f.write('lastmod: '+ currenttime +'+05:00\n')
f.write('draft: false\n')
f.write('tags: ["'+tags+'"]\n')
f.write('author: "'+author+'"\n')
f.write('images: ["/post/'+identifier+'/'+identifier+'.png"]\n')
f.write('autoCollapseToc: false\n')
f.write('\n')
f.write('---\n')
f.write('\n')
endoffirstparagraph=0
if "\n" in articletext[5:]:
endoffirstparagraph = articletext.index("\n", 5)
f.write(articletext[0:endoffirstparagraph])
f.write('{{% center %}}\n')
f.write('![img](./'+identifier+'.png "img")\n')
f.write('{{% /center %}}\n')
#look for the end of the first paragraph
f.write(articletext[endoffirstparagraph:])
f.write('\n')
f.write('\n')
destinationfile=postrootfolder + identifier + "/index.md"
print("Pushing "+destinationfile)
shutil.copyfile(filename,destinationfile)
iArticleRevision=iArticleRevision+1
def setidentifier():
global identifier
print("Enter post identifier (used for folder, etc)")
identifier = input()
print("folder draft/" + identifier)
print("folder " + postrootfolder + identifier)
print("")
print("Proceed y/n")
proceed = input()
if "n" in proceed:
setidentifier()
setupfolders()
def setupfolders():
global identifier
os.mkdir("./draft/"+ identifier)
os.mkdir(postrootfolder+ identifier)
def settitle():
global title
print("Enter post title")
title = input()
print("title: " + title)
print("")
print("Proceed y/n")
proceed = input()
if "n" in proceed:
settitle()
def settags():
global tags
print("Enter comma-separated post tags")
tags = input()
print("tags: " + tags)
print("")
print("Proceed y/n")
proceed = input()
if "n" in proceed:
settags()
def setimageprompt():
global imageprompt
global identifier
global iImageRevision
if len(imageprompt)<1:
print("Enter post image prompt")
imageprompt = input()
print("image prompt: " + imageprompt)
print("")
getimage()
print("Proceed with this image, hit y, to generate a new image, hit n, retry same prompt r")
proceed = input()
if "n" in proceed:
imageprompt=""
setimageprompt()
if "r" in proceed:
setimageprompt()
def getimage():
global imageprompt
global identifier
global iImageRevision
try:
response = client.images.generate(prompt=imageprompt,
model="dall-e-3",
n=1,
size="1024x1024")
image_url = response.data[0].url
versionfilename="./draft/"+identifier+"/"+identifier+"."+str(iImageRevision)+".png"
print("Retrieving "+image_url)
print("Writing to "+versionfilename)
urllib.request.urlretrieve(image_url, versionfilename)
except openai.OpenAIError as e:
print("Error")
print(e.http_status)
print(e.error)
versionfilename="./NoImageGenerated.png"
finalfilename=postrootfolder + identifier + "/" + identifier + ".png"
print("Pushing "+finalfilename)
shutil.copyfile(versionfilename,finalfilename)
iImageRevision=iImageRevision+1
pusharticle()
def setarticleprompt():
global articleprompt
if len(articleprompt)<1:
print("Enter article prompt")
articleprompt = input()
print("article prompt: " + articleprompt)
print("")
getarticle()
print("Proceed with this article, hit y, to generate a new article, hit n, retry same prompt r")
proceed = input()
if "n" in proceed:
articleprompt=""
setarticleprompt()
if "r" in proceed:
setarticleprompt()
def getarticle():
global articletext
global articleprompt
try:
response = client.chat.completions.create(model="gpt-4o",
messages=[
{"role": "system", "content": "You were born in 1975 in Kalamazoo, Michigan. You are an American researcher and hacker, and hold a Ph.D in electrical engineering from MIT. You have written books about reverse engineering. You are a resident advisor and mentor to an early stage hardware accelerator and venture capital firm."},
{"role": "user", "content": articleprompt},
])
except openai.OpenAIError as e:
print("Error")
print(e.http_status)
print(e.error)
exit(1)
#print(response)
print("Finish reason: ")
print(response.choices[0].finish_reason)
#articletext=response.choices[0].text
articletext = response.choices[0].message.content
#get rid of surplus title added by latest version of GPT-3.5-turbo
if articletext[0:7]=='Title: ':
endoffirstparagraph = articletext.index("\n\n", 0)+2
articletext=articletext[endoffirstparagraph:]
pusharticle()
def createtitle():
global articletext
global title
try:
response = client.chat.completions.create(model="gpt-4o",
messages=[
{"role": "system", "content": "You are a journalist"},
{"role": "user", "content": "Create a catchy title for the following blog text:\n" + articletext},
])
except openai.OpenAIError as e:
print("Error")
print(e.http_status)
print(e.error)
exit(1)
title=response.choices[0].message.content.replace('"','')
#latest version of GPT3.5 has annoying habit of putting Title: in the Title - let's get rid of it
title=title.replace('Title: ','')
pusharticle()
def archivesources():
global identifier
global imageprompt
global articleprompt
global sourceurl
global title
archivesources = {
"articleprompt": articleprompt,
"imageprompt": imageprompt,
"sourceurl": sourceurl,
"title": title,
"articletext":articletext
}
jsonarchivesources = json.dumps(archivesources)
draftcopy="./draft/"+ identifier + "/" + identifier + ".json"
destinationfile=postrootfolder + identifier + "/" + identifier + ".json"
print("Archiving Sources: " + draftcopy)
with open(draftcopy, 'w') as f:
f.write(jsonarchivesources)
shutil.copyfile(draftcopy,destinationfile)
parser = argparse.ArgumentParser()
parser.add_argument("--identifier", help="folder and article name", type=str)
parser.add_argument("--title", help="article title", type=str)
parser.add_argument("--tags", help="tags to categorize articles", type=str)
parser.add_argument("--imageprompt", help="prompt to generate image", type=str)
parser.add_argument("--articleprompt", help="prompt to generate text", type=str)
parser.add_argument("--sourceurl", help="source url used to seed article", type=str)
parser.add_argument("--inputfile", help="set identifier and prompt by input", type=str)
parser.add_argument("-ni", "--noninteractive", help="use command line arguments", action='store_true')
parser.add_argument("-f", "--file", help="use ", action='store_true')
args = parser.parse_args()
if args.noninteractive:
print("Building article")
inputfile = args.inputfile
if len(inputfile)>1:
# Opening JSON file
f = open(inputfile)
data = json.load(f)
identifier=data['identifier']
articleprompt=data['prompt']
tags=data['tags']
sourceurl=data['sourceurl']
imageprompt=data['imageprompt']
else:
identifier=args.identifier
articleprompt=args.articleprompt
tags=str(args.tags)
sourceurl=str(args.sourceurl)
imageprompt=str(args.imageprompt)
title=str(args.title)
setupfolders()
getarticle()
if title=="None":
createtitle()
if imageprompt=="None":
imageprompt="photo to accompany a blog post titled: " + title
getimage()
archivesources()
else:
setidentifier()
settags()
settitle()
setimageprompt()
setarticleprompt()
archivesources()