SIGMitch commited on
Commit
3c3979e
·
verified ·
1 Parent(s): 15bad42
Files changed (1) hide show
  1. app.py +155 -0
app.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pathlib
2
+ import shlex
3
+ import subprocess
4
+ import gradio as gr
5
+ import PIL.Image
6
+ import time
7
+ import os
8
+ import requests
9
+ import json
10
+ import ipywidgets as widgets
11
+ from IPython.display import display
12
+ from google.colab import files
13
+ from IPython.display import display, HTML, Javascript
14
+ from google.colab import output
15
+ import gradio as gr
16
+
17
+ # Define the base URL, the API key and Paths
18
+ base_url = "https://hyperhuman.deemos.com/api/v2"
19
+ api_key = os.getenv('API')
20
+ image_path = "/content/sample_data/wooden_chair.jpg"
21
+
22
+ result_path = "/content/"
23
+
24
+
25
+ # Define the headers for the requests
26
+ headers = {
27
+ "Authorization": f"Bearer {api_key}",
28
+ "Content-Type": "application/json"
29
+ }
30
+
31
+ def submit_task(image_path: str):
32
+ url = f"{base_url}/rodin"
33
+ #image_path = "/content/sample_data/wooden_chair.jpg"
34
+ # Get the uploaded file
35
+ #if uploader.value:
36
+ #uploaded_file = next(iter(uploader.value.values()))
37
+ #if 'name' not in uploaded_file:
38
+ #print("Error: 'name' not found in task response")
39
+ #return
40
+
41
+
42
+ #image_path = uploaded_file['metadata']['name']
43
+
44
+ # Save the uploaded file to disk
45
+ # with open(image_path, 'wb') as f:
46
+ # f.write(uploaded_file['content'])
47
+ #else:
48
+ # print("No file uploaded. Please upload an image.")
49
+
50
+ # Read the image file
51
+ with open(image_path, 'rb') as image_file:
52
+ image_data = image_file.read()
53
+
54
+ # Prepare the multipart form data
55
+ files = {
56
+ 'images': (os.path.basename(image_path), image_data, 'image/jpeg')
57
+ }
58
+
59
+ # Set the tier to Rodin Regular
60
+ data = {
61
+ 'tier': 'Regular'
62
+ }
63
+
64
+ # Prepare the headers.
65
+ headers = {
66
+ 'Authorization': f'Bearer {api_key}',
67
+ }
68
+
69
+ # Note that we are not sending the data as JSON, but as form data.
70
+ # This is because we are sending a file as well.
71
+ response = requests.post(url, files=files, data=data, headers=headers)
72
+ print(response)
73
+ return response.json()
74
+
75
+ # Function to check the status of a task
76
+ def check_status(subscription_key):
77
+ url = f"{base_url}/status"
78
+ data = {
79
+ "subscription_key": subscription_key
80
+ }
81
+ response = requests.post(url, headers=headers, json=data)
82
+ return response.json()
83
+
84
+ # Function to download the results of a task
85
+ def download_results(task_uuid):
86
+ url = f"{base_url}/download"
87
+ data = {
88
+ "task_uuid": task_uuid
89
+ }
90
+ response = requests.post(url, headers=headers, json=data)
91
+ return response.json()
92
+
93
+
94
+
95
+ def submit_task2(image_path: str):
96
+ # Submit the task and get the task UUID
97
+ task_response = submit_task(image_path)
98
+ if 'uuid' not in task_response:
99
+ print("Error: 'uuid' not found in task response")
100
+ return
101
+
102
+ task_uuid = task_response['uuid']
103
+ subscription_key = task_response['jobs']['subscription_key']
104
+
105
+ # Poll the status endpoint every 5 seconds until the task is done
106
+ status = []
107
+ while len(status) == 0 or not all(s['status'] in ['Done', 'Failed'] for s in status):
108
+ time.sleep(5)
109
+ status_response = check_status(subscription_key)
110
+ status = status_response['jobs']
111
+ for s in status:
112
+ print(f"job {s['uuid']}: {s['status']}")
113
+
114
+ # Download the results once the task is done
115
+ download_response = download_results(task_uuid)
116
+ download_items = download_response['list']
117
+
118
+ # Print the download URLs and download them locally.
119
+ for item in download_items:
120
+ print(f"File Name: {item['name']}, URL: {item['url']}")
121
+ dest_fname = os.path.join(result_path, item['name'])
122
+ os.makedirs(os.path.dirname(dest_fname), exist_ok=True)
123
+ with open(dest_fname, 'wb') as f:
124
+ response = requests.get(item['url'])
125
+ f.write(response.content)
126
+ print(f"Downloaded {dest_fname}")
127
+ return dest_fname
128
+
129
+
130
+
131
+
132
+
133
+
134
+ def run(image_path: str) -> str:
135
+
136
+ return submit_task2(image_path)
137
+
138
+ with gr.Blocks() as demo:
139
+ with gr.Group():
140
+ image = gr.Image(label="Input image", show_label=False, type="filepath")
141
+ run_button = gr.Button("Run")
142
+ result = gr.Model3D(label="Result", show_label=False)
143
+
144
+ run_button.click(
145
+ fn=run,
146
+ inputs=[
147
+ image
148
+ ],
149
+ outputs=result,
150
+ api_name="image-to-3d",
151
+ concurrency_id="gpu",
152
+ concurrency_limit=1,
153
+ )
154
+
155
+ demo.launch(auth=("user", os.getenv('PASS')),share=True,debug=True)