Created
August 24, 2022 19:20
-
-
Save soukron/3ec0b77368269d8c6297e989a7d37e33 to your computer and use it in GitHub Desktop.
Tool for converting Pickle to JSON using this simple Python Command Line program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from time import time | |
from uuid import uuid4 | |
import os | |
from time import time,sleep | |
embedding_service_host=os.getenv('EMBEDDING_SERVICE_HOST', '127.0.0.1') | |
embedding_service_port=os.getenv('EMBEDDING_SERVICE_PORT', '999') | |
nexus_service_host=os.getenv('NEXUS_SERVICE_HOST', '127.0.0.1') | |
nexus_service_port=os.getenv('NEXUS_SERVICE_PORT', '8888') | |
def wait_for_service(service_name, service_port): | |
while True: | |
try: | |
return requests.get('http://' + service_name + ':' + service_port) | |
except requests.exceptions.ConnectionError: | |
print('Waiting for ' + service_name + ' to be reachable...') | |
sleep(10) | |
def open_file(filepath): | |
with open(filepath, 'r', encoding='utf-8') as infile: | |
return infile.read() | |
service_name = 'executive_action' | |
content_prefix = '' | |
def get_embedding(payload): | |
url = 'http://%s:%s' % (embedding_service_host, embedding_service_port) | |
response = requests.request(method='POST', url=url, json=payload) | |
return response.json() | |
def nexus_send(payload): | |
url = 'http://%s:%s/add' % (nexus_service_host, nexus_service_port) | |
payload['time'] = time() | |
payload['uuid'] = str(uuid4()) | |
payload['content'] = content_prefix + payload['content'] | |
embeddings = get_embedding([payload['content']]) | |
payload['vector'] = embeddings[0]['vector'] | |
payload['service'] = service_name | |
response = requests.request(method='POST', url=url, json=payload) | |
print(response.text) | |
if __name__ == '__main__': | |
wait_for_service(embedding_service_host, embedding_service_port) | |
wait_for_service(nexus_service_host, nexus_service_port) | |
action = os.getenv('ACTION', 'Look at the scene without interfering') | |
nexus_send({'content': action}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Pickle2JSON is a simple Python Command Line program for converting Pickle file to JSON file. | |
Arguments: Only one (1) argument is expected which is the pickle file. | |
Usage: python pickle2json.py myfile.pkl | |
Output: The output is a JSON file bearing the same filename containing the JSON document of the converted Pickle file. | |
Author: Romulo Gapuz Jr. (romgapuz) | |
""" | |
# import libraries | |
import pickle | |
import json | |
import sys | |
import os | |
# open pickle file | |
with open(sys.argv[1], 'rb') as infile: | |
obj = pickle.load(infile) | |
# convert pickle object to json object | |
json_obj = json.loads(json.dumps(obj, default=str)) | |
# write the json file | |
with open( | |
os.path.splitext(sys.argv[1])[0] + '.json', | |
'w', | |
encoding='utf-8' | |
) as outfile: | |
json.dump(json_obj, outfile, ensure_ascii=False, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment