Skip to content

Instantly share code, notes, and snippets.

@juanalonso
Created October 22, 2021 07:53
Show Gist options
  • Save juanalonso/ca5d9a8b32632665b43366108fc68843 to your computer and use it in GitHub Desktop.
Save juanalonso/ca5d9a8b32632665b43366108fc68843 to your computer and use it in GitHub Desktop.
Slow-calls the Delphi model for descriptive ethics, using sentences from inputFile, storing each response in an individual JSON file.
from time import sleep
import urllib.parse
import subprocess
import json
startAt = 0
inputFile = 'original_truisms.txt'
command = ['curl',
'',
'-s',
'-H', 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:93.0) Gecko/20100101 Firefox/93.0',
'-H', 'Accept: application/json, text/plain, */*',
'-H', 'Accept-Language: es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3', '--compressed',
'-H', 'Origin: https://delphi.allenai.org',
'-H', 'Connection: keep-alive',
'-H', 'Referer: https://delphi.allenai.org/',
'-H', 'Sec-Fetch-Dest: empty',
'-H', 'Sec-Fetch-Mode: cors',
'-H', 'Sec-Fetch-Site: same-site',
'-H', 'TE: trailers']
# print(shlex.split(command))
with open(inputFile) as f:
lines = f.readlines()
for index,line in enumerate(lines):
if index<startAt:
continue
l = line.strip().lower()
command[1] = "https://mosaic-api-morality.apps.allenai.org/api/ponder?action1="+\
urllib.parse.quote_plus(l)
process = subprocess.run(command,
stdout=subprocess.PIPE,
universal_newlines=True)
outJSON = json.loads(process.stdout)
with open(f'out_{index:03d}.json', 'w') as file:
json.dump(outJSON, file, indent=4)
print(f"{index+1:3d}/{len(lines)}: {l}\n {outJSON['answer']['text']}")
sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment