trustgraph/test-api/test-load-document

104 lines
3.5 KiB
Text
Raw Normal View History

#!/usr/bin/env python3
import requests
import json
import sys
import base64
url = "http://localhost:8088/api/v1/"
############################################################################
text = open("../sources/Challenger-Report-Vol1.pdf", "rb").read()
# Some random identifiers. The doc ID is important, as extracted knowledge
# is linked back to this identifier
org_id = "https://trustgraph.ai/org/1dd51ece-8bd3-48b8-98ce-1ac9164c5214"
doc_id = "https://trustgraph.ai/doc/72ef3374-af7a-40c4-8c7b-45050aef5b90"
pub_id = "https://trustgraph.ai/pubev/59012ae1-65d4-441f-8288-b6f3c6c15333"
# Organization metadata
org_facts = [
[org_id, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"https://schema.org/Organization"],
[org_id, "http://www.w3.org/2000/01/rdf-schema#label", "NASA"],
[org_id, "https://schema.org/name", "NASA"]
]
# Publication metadata. Note how it links to the Organization
pub_facts = [
[pub_id, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"https://schema.org/PublicationEvent"],
[pub_id, "https://schema.org/description", "Uploading to Github"],
[pub_id, "https://schema.org/endDate", "1986-06-06"],
[pub_id, "https://schema.org/publishedBy", org_id],
[pub_id, "https://schema.org/startDate", "1986-06-06"]
]
# Document metadata. Note how it links to the publication event
doc_facts = [
[doc_id, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"https://schema.org/DigitalDocument"],
[doc_id, "http://www.w3.org/2000/01/rdf-schema#label",
"Challenger Report Volume 1"],
[doc_id, "https://schema.org/copyrightHolder", "US Government"],
[doc_id, "https://schema.org/copyrightNotice",
"Work of the US Gov. Public Use Permitted"],
[doc_id, "https://schema.org/copyrightYear", "1986"],
[doc_id, "https://schema.org/description",
"The findings of the Presidential Commission regarding the circumstances surrounding the Challenger accident are reported and recommendations for corrective action are outlined"
],
[doc_id, "https://schema.org/keywords", "nasa"],
[doc_id, "https://schema.org/keywords", "challenger"],
[doc_id, "https://schema.org/keywords", "space-shuttle"],
[doc_id, "https://schema.org/keywords", "shuttle"],
[doc_id, "https://schema.org/keywords", "orbiter"],
[doc_id, "https://schema.org/name", "Challenger Report Volume 1"],
[doc_id, "https://schema.org/publication", pub_id],
[doc_id, "https://schema.org/url",
"https://ntrs.nasa.gov/citations/19860015255"]
]
def to_value(x):
if x.startswith("https://"):
return { "v": x, "e": True }
if x.startswith("http://"):
return { "v": x, "e": True }
return { "v": x, "e": False }
# Convert the above metadata into the right form
metadata = [
{ "s": to_value(t[0]), "p": to_value(t[1]), "o": to_value(t[2]) }
for t in org_facts + pub_facts + doc_facts
]
input = {
# Document identifer. Knowledge derived by TrustGraph is linked to this
# identifier, so the additional metadata specified above is linked to the
# derived knowledge and users of the knowledge graph could see
# information about the source of knowledge
"id": doc_id,
# Additional metadata in the form of RDF triples
"metadata": metadata,
# The PDF document, is presented as a base64 encoded document.
"data": base64.b64encode(text).decode("utf-8")
}
resp = requests.post(
f"{url}load/document",
json=input,
)
resp = resp.json()
if "error" in resp:
print(f"Error: {resp['error']}")
sys.exit(1)
print(resp)