-
Notifications
You must be signed in to change notification settings - Fork 0
/
LVReddit_runGPT.py
57 lines (43 loc) · 1.49 KB
/
LVReddit_runGPT.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
import json
import random
import re
from collections import Counter
from multiprocessing.pool import ThreadPool
from loguru import logger
from tqdm import tqdm
from GPT_prompter import Prompter
from corpus_stats import flatten_comments_from_posts, get_stats
file = 'data/LVReddit_labeled.json'
def process(d):
while True:
try:
Prompter.process_prompt(d, 15, 'body')
break
except Exception as e:
logger.exception(e)
logger.error(f'Failed to get response for {d["body"]}')
return d
if __name__ == '__main__':
with open(file, 'r', encoding='utf8') as f:
dataset = json.load(f)
# Ctrl-C handler
import signal
running = True
def signal_handler(sig, frame):
global running
running = False
logger.info('Ctrl-C pressed')
signal.signal(signal.SIGINT, signal_handler)
for i in tqdm(list(range(len(dataset)//20+1))):
samples = dataset[i*20:(i+1)*20]
with ThreadPool(20) as pool:
results = pool.map(process, samples)
for j, d in enumerate(results):
dataset[i*20+j] = d
if i % 20 == 0 or not running:
with open('data/LVReddit_labeled.json', 'w', encoding='utf8') as f:
json.dump(dataset, f, indent=4, ensure_ascii=False)
if not running:
break
with open('data/LVReddit_labeled.json', 'w', encoding='utf8') as f:
json.dump(dataset, f, indent=4, ensure_ascii=False)