Spaces:
Runtime error
Runtime error
File size: 4,960 Bytes
f115855 74ce6d9 f115855 74ce6d9 f115855 74ce6d9 f115855 ce82379 f115855 c5fa69d f115855 74ce6d9 f115855 c5fa69d 74ce6d9 77a1cda 74ce6d9 f115855 74ce6d9 |
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 |
import os
import json
import time
import base64
import requests
import gradio as gr
username = os.environ.get("CLIENT_ID")
passwd = os.environ.get("CLIENT_SECRETS")
def authorization():
url = "https://stage.id.trimblecloud.com/oauth/token"
credential_pair = f"{username}:{passwd}"
payload = "grant_type=client_credentials&scope=DLPointCloudSegmentation"
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": f"Basic {base64.b64encode(credential_pair.encode('utf-8')).decode('utf-8')}",
}
response = requests.request("POST", url, headers=headers, data=payload)
response.raise_for_status()
print("Succesfully authenticated")
auth_token = json.loads(response.text)["access_token"]
return auth_token
def create_file(auth_token, input_filename):
url = "https://cloud.stage.api.trimblecloud.com/dataocean/api/3.0/api/files"
payload = json.dumps({"file": {"path": input_filename, "regions": ["us1"]}})
headers = {
"Authorization": f"Bearer {auth_token}",
"Content-Type": "application/json",
}
response = requests.request("POST", url, headers=headers, data=payload)
response.raise_for_status()
print("File created successfully")
file_upload_url = json.loads(response.text)["file"]["upload"]["url"]
return file_upload_url
def upload_file(url, file):
with open(file.name, "rb") as lasFile:
payload = lasFile.read()
headers = {"Content-Type": "application/octet-stream"}
response = requests.request("PUT", url, headers=headers, data=payload)
response.raise_for_status()
print("Upload was successful")
def start_execution(auth_token, input_filename, output_filename="output.las"):
url = "https://cloud.stage.api.trimblecloud.com/Processing/api/1/api/executions"
payload = json.dumps(
{
"execution": {
"procedure_id": "a7c4f9c3-b21a-4c9c-b4df-3dc6ba8934d9",
"region": "aws-us1",
"parameters": {
"source_path": input_filename,
"regions": ["us1"],
"output_path": output_filename,
},
}
}
)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {auth_token}",
}
response = requests.request("POST", url, headers=headers, data=payload)
response.raise_for_status()
print("Execution has started")
execution_id = json.loads(response.text)["execution"]["id"]
return execution_id
def track_execution(auth_token, execution_id, output_filename):
url = "https://cloud.stage.api.trimblecloud.com/Processing/api/1/api/executions"
payload = {}
headers = {
"Authorization": f"Bearer {auth_token}",
}
response = requests.request("GET", url, headers=headers, data=payload)
status = json.loads(response.text)["items"][0]["execution_status"]
while status != "FINISHED":
response = requests.request("GET", url, headers=headers, data=payload)
status = json.loads(response.text)["items"][0]["execution_status"]
time.sleep(1)
return download_output(auth_token, output_filename)
def download_output(auth_token, output_filename):
url = f"https://cloud.stage.api.trimblecloud.com/dataocean/api/3.0/api/files?path={output_filename}"
payload = ""
headers = {
"Authorization": f"Bearer {auth_token}",
"Content-Type": "application/json",
}
response = requests.request("GET", url, headers=headers, data=payload)
response.raise_for_status()
print("File downloading")
response = json.loads(response.text)
download_url = response["file"]["download"]["url"]
return download_url
def predict(input_file):
input_filename = "input.las"
output_filename = "output.las"
auth_token = authorization()
file_upload_url = create_file(auth_token, input_filename)
upload_file(file_upload_url, input_file)
execution_id = start_execution(auth_token, input_filename, output_filename)
download_url = track_execution(auth_token, execution_id, output_filename)
html_content = f'<a href="{download_url}" download><button type="button" style="height:100px; width:200px; background-color:blue; border-color:black; color:white">Download</button></a>'
return html_content
demo = gr.Interface(
title="Point Cloud Segmentation-Trimble Cloud",
fn=predict,
inputs=gr.File(file_types=[".las"], file_count="single"),
outputs=["html"],
examples=["test.las"],
cache_examples=False,
description="This is a technology demonstration of Trimble AI's 3D Point Cloud Segmentation running on Trimble Cloud Core's Pegasus Processing Framework. The point cloud is uploaded on behalf of the user into Pegasus, then the result is offered as a downloadable link.",
)
demo.queue(concurrency_count=512, max_size=512).launch()
|