Vishakaraj commited on
Commit
f115855
·
1 Parent(s): 60cd172

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +3 -9
  2. app.py +151 -0
  3. requirements.txt +1 -0
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Point Cloud Segmentation-Trimble Cloud
3
- emoji: 📊
4
- colorFrom: pink
5
- colorTo: pink
6
- sdk: gradio
7
- sdk_version: 3.47.1
8
  app_file: app.py
9
- pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Point_Cloud_Segmentation-Trimble_Cloud
 
 
 
 
 
3
  app_file: app.py
4
+ sdk: gradio
5
+ sdk_version: 3.43.1
6
  ---
 
 
app.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import time
4
+ import base64
5
+ import requests
6
+ import gradio as gr
7
+
8
+ username = os.environ.get("CLIENT_ID")
9
+ passwd = os.environ.get("CLIENT_SECRETS")
10
+
11
+ def authorization():
12
+ url = "https://stage.id.trimblecloud.com/oauth/token"
13
+ credential_pair = f"{username}:{passwd}"
14
+
15
+ payload = "grant_type=client_credentials&scope=DLPointCloudSegmentation"
16
+ headers = {
17
+ "Content-Type": "application/x-www-form-urlencoded",
18
+ "Authorization": f"Basic {base64.b64encode(credential_pair.encode('utf-8')).decode('utf-8')}",
19
+ }
20
+
21
+ response = requests.request("POST", url, headers=headers, data=payload)
22
+
23
+ response.raise_for_status()
24
+ print("Succesfully authenticated")
25
+ auth_token = json.loads(response.text)["access_token"]
26
+
27
+ return auth_token
28
+
29
+
30
+ def create_file(auth_token, input_filename):
31
+ url = "https://cloud.stage.api.trimblecloud.com/dataocean/api/3.0/api/files"
32
+
33
+ payload = json.dumps({"file": {"path": input_filename, "regions": ["us1"]}})
34
+ headers = {
35
+ "Authorization": f"Bearer {auth_token}",
36
+ "Content-Type": "application/json",
37
+ }
38
+
39
+ response = requests.request("POST", url, headers=headers, data=payload)
40
+
41
+ response.raise_for_status()
42
+ print("File created successfully")
43
+ file_upload_url = json.loads(response.text)["file"]["upload"]["url"]
44
+ return file_upload_url
45
+
46
+
47
+ def upload_file(url, file):
48
+ with open(file.name, "rb") as lasFile:
49
+ payload = lasFile.read()
50
+
51
+ headers = {"Content-Type": "application/octet-stream"}
52
+
53
+ response = requests.request("PUT", url, headers=headers, data=payload)
54
+
55
+ response.raise_for_status()
56
+ print("Upload was successful")
57
+
58
+
59
+ def start_execution(auth_token, input_filename, output_filename="output.las"):
60
+ url = "https://cloud.stage.api.trimblecloud.com/Processing/api/1/api/executions"
61
+
62
+ payload = json.dumps(
63
+ {
64
+ "execution": {
65
+ "procedure_id": "a7c4f9c3-b21a-4c9c-b4df-3dc6ba8934d9",
66
+ "region": "aws-us1",
67
+ "parameters": {
68
+ "source_path": input_filename,
69
+ "regions": ["us1"],
70
+ "output_path": output_filename,
71
+ },
72
+ }
73
+ }
74
+ )
75
+ headers = {
76
+ "Content-Type": "application/json",
77
+ "Authorization": f"Bearer {auth_token}",
78
+ }
79
+
80
+ response = requests.request("POST", url, headers=headers, data=payload)
81
+
82
+ response.raise_for_status()
83
+ print("Execution has started")
84
+
85
+ execution_id = json.loads(response.text)["execution"]["id"]
86
+
87
+ return execution_id
88
+
89
+
90
+ def track_execution(auth_token, execution_id, output_filename):
91
+ url = "https://cloud.stage.api.trimblecloud.com/Processing/api/1/api/executions"
92
+
93
+ payload = {}
94
+ headers = {
95
+ "Authorization": f"Bearer {auth_token}",
96
+ }
97
+
98
+ response = requests.request("GET", url, headers=headers, data=payload)
99
+
100
+ status = json.loads(response.text)["items"][0]["execution_status"]
101
+
102
+ while status != "FINISHED":
103
+ response = requests.request("GET", url, headers=headers, data=payload)
104
+ status = json.loads(response.text)["items"][0]["execution_status"]
105
+ time.sleep(1)
106
+
107
+ return download_output(auth_token, output_filename)
108
+
109
+
110
+ def download_output(auth_token, output_filename):
111
+ url = f"https://cloud.stage.api.trimblecloud.com/dataocean/api/3.0/api/files?path={output_filename}"
112
+
113
+ payload = ""
114
+ headers = {
115
+ "Authorization": f"Bearer {auth_token}",
116
+ "Content-Type": "application/json",
117
+ }
118
+
119
+ response = requests.request("GET", url, headers=headers, data=payload)
120
+
121
+ response.raise_for_status()
122
+ print("File downloading")
123
+ response = json.loads(response.text)
124
+ download_url = response["file"]["download"]["url"]
125
+
126
+ return download_url
127
+
128
+ def predict(input_file):
129
+ input_filename = "input.las"
130
+ output_filename = "output.las"
131
+ auth_token = authorization()
132
+ file_upload_url = create_file(auth_token, input_filename)
133
+ upload_file(file_upload_url, input_file)
134
+ execution_id = start_execution(
135
+ auth_token, input_filename, output_filename
136
+ )
137
+ download_url = track_execution(auth_token, execution_id, output_filename)
138
+
139
+ html_content = f'<a href="{download_url}">Download output file</a>'
140
+
141
+
142
+ return "Inference has finished. Click the download button to access the output file", html_content
143
+
144
+ demo = gr.Interface(
145
+ title="Point Cloud inference on the Trimble Cloud",
146
+ fn=predict,
147
+ inputs=gr.File(file_types=[".las"], file_count="single"),
148
+ outputs=[gr.Textbox(), "html"],
149
+ )
150
+
151
+ demo.queue(default_enabled=False).launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ requests