Spaces:
Runtime error
Runtime error
remove cache
Browse files
Rodin.py
CHANGED
|
@@ -123,7 +123,7 @@ def load_image(img_path):
|
|
| 123 |
return image_bytes
|
| 124 |
|
| 125 |
def log(level, info_text):
|
| 126 |
-
print(f"[ {level} ] - {time.strftime('%Y%m%d_%H
|
| 127 |
|
| 128 |
class Generator:
|
| 129 |
def __init__(self, user_id, password) -> None:
|
|
@@ -131,16 +131,17 @@ class Generator:
|
|
| 131 |
self.user_id = user_id
|
| 132 |
self.password = password
|
| 133 |
self.task_uuid = None
|
|
|
|
| 134 |
|
| 135 |
-
def preprocess(self, prompt, image_path,
|
| 136 |
-
if
|
| 137 |
log("INFO", "Using cached image and prompt...")
|
| 138 |
-
return prompt
|
| 139 |
log("INFO", "Preprocessing image...")
|
| 140 |
success = False
|
| 141 |
while not success:
|
| 142 |
-
log("INFO", "Loading image...")
|
| 143 |
image_file = load_image(image_path)
|
|
|
|
| 144 |
if prompt and task_uuid:
|
| 145 |
preprocess_response = rodin_preprocess_image(generate_prompt=False, image=image_file, name="images.png", token=self.token)
|
| 146 |
else:
|
|
@@ -157,19 +158,19 @@ class Generator:
|
|
| 157 |
try:
|
| 158 |
if not (prompt and task_uuid):
|
| 159 |
prompt = preprocess_response.get('prompt', None)
|
| 160 |
-
processed_image = "data:image/png;base64," + preprocess_response.get('processed_image', None)
|
| 161 |
success = True
|
| 162 |
except Exception as e:
|
| 163 |
log("ERROR", f"Error in image preprocessing: {preprocess_response}")
|
| 164 |
raise e
|
| 165 |
|
| 166 |
-
return prompt
|
| 167 |
|
| 168 |
-
def generate_mesh(self, prompt,
|
| 169 |
log("INFO", "Generating mesh...")
|
| 170 |
if task_uuid == "":
|
| 171 |
settings = {'view_weights': [1]} # Define weights as per your requirements, for multiple images, use multiple values, e,g [0.5, 0.5]
|
| 172 |
-
images = [processed_image] # List of images, all the images should be processed first
|
| 173 |
|
| 174 |
mesh_response = rodin_mesh(prompt=prompt, group_uuid=None, settings=settings, images=images, name="images.jpeg", token=self.token)
|
| 175 |
progress_checker = JobStatusChecker(BASE_URL, mesh_response['job']['subscription_key'])
|
|
@@ -198,8 +199,12 @@ class Generator:
|
|
| 198 |
except Exception as e:
|
| 199 |
log("ERROR", f"Error in updating mesh: {e}")
|
| 200 |
time.sleep(5)
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
response = requests.get(preview_image, stream=True)
|
| 204 |
if response.status_code == 200:
|
| 205 |
# 创建一个PIL Image对象
|
|
|
|
| 123 |
return image_bytes
|
| 124 |
|
| 125 |
def log(level, info_text):
|
| 126 |
+
print(f"[ {level} ] - {time.strftime('%Y%m%d_%H:%M:%S', time.localtime())} - {info_text}")
|
| 127 |
|
| 128 |
class Generator:
|
| 129 |
def __init__(self, user_id, password) -> None:
|
|
|
|
| 131 |
self.user_id = user_id
|
| 132 |
self.password = password
|
| 133 |
self.task_uuid = None
|
| 134 |
+
self.processed_image = None
|
| 135 |
|
| 136 |
+
def preprocess(self, prompt, image_path, task_uuid=""):
|
| 137 |
+
if prompt and task_uuid != "":
|
| 138 |
log("INFO", "Using cached image and prompt...")
|
| 139 |
+
return prompt
|
| 140 |
log("INFO", "Preprocessing image...")
|
| 141 |
success = False
|
| 142 |
while not success:
|
|
|
|
| 143 |
image_file = load_image(image_path)
|
| 144 |
+
log("INFO", "Image loaded, processing...")
|
| 145 |
if prompt and task_uuid:
|
| 146 |
preprocess_response = rodin_preprocess_image(generate_prompt=False, image=image_file, name="images.png", token=self.token)
|
| 147 |
else:
|
|
|
|
| 158 |
try:
|
| 159 |
if not (prompt and task_uuid):
|
| 160 |
prompt = preprocess_response.get('prompt', None)
|
| 161 |
+
self.processed_image = "data:image/png;base64," + preprocess_response.get('processed_image', None)
|
| 162 |
success = True
|
| 163 |
except Exception as e:
|
| 164 |
log("ERROR", f"Error in image preprocessing: {preprocess_response}")
|
| 165 |
raise e
|
| 166 |
|
| 167 |
+
return prompt
|
| 168 |
|
| 169 |
+
def generate_mesh(self, prompt, task_uuid=""):
|
| 170 |
log("INFO", "Generating mesh...")
|
| 171 |
if task_uuid == "":
|
| 172 |
settings = {'view_weights': [1]} # Define weights as per your requirements, for multiple images, use multiple values, e,g [0.5, 0.5]
|
| 173 |
+
images = [self.processed_image] # List of images, all the images should be processed first
|
| 174 |
|
| 175 |
mesh_response = rodin_mesh(prompt=prompt, group_uuid=None, settings=settings, images=images, name="images.jpeg", token=self.token)
|
| 176 |
progress_checker = JobStatusChecker(BASE_URL, mesh_response['job']['subscription_key'])
|
|
|
|
| 199 |
except Exception as e:
|
| 200 |
log("ERROR", f"Error in updating mesh: {e}")
|
| 201 |
time.sleep(5)
|
| 202 |
+
history = rodin_history(task_uuid, self.token)
|
| 203 |
+
try:
|
| 204 |
+
preview_image = next(reversed(history.items()))[1]["preview_image"]
|
| 205 |
+
except Exception as e:
|
| 206 |
+
print(history)
|
| 207 |
+
raise e
|
| 208 |
response = requests.get(preview_image, stream=True)
|
| 209 |
if response.status_code == 200:
|
| 210 |
# 创建一个PIL Image对象
|
app.py
CHANGED
|
@@ -137,7 +137,6 @@ with gr.Blocks() as demo:
|
|
| 137 |
button_more = gr.Button(value="Download", variant="primary", link=rodin_url)
|
| 138 |
|
| 139 |
cache_raw_image = gr.Image(visible=False, type="pil")
|
| 140 |
-
cache_image_base64 = gr.Text(visible=False)
|
| 141 |
cacha_empty = gr.Text(visible=False)
|
| 142 |
cache_task_uuid = gr.Text(value="", visible=False)
|
| 143 |
|
|
@@ -148,12 +147,12 @@ with gr.Blocks() as demo:
|
|
| 148 |
outputs=[cacha_empty]
|
| 149 |
).success(
|
| 150 |
fn=generator.preprocess,
|
| 151 |
-
inputs=[block_prompt, block_image,
|
| 152 |
-
outputs=[block_prompt
|
| 153 |
show_progress="minimal"
|
| 154 |
).success(
|
| 155 |
fn=generator.generate_mesh,
|
| 156 |
-
inputs=[block_prompt,
|
| 157 |
outputs=[cache_raw_image, cache_task_uuid, fake3d],
|
| 158 |
)
|
| 159 |
|
|
@@ -166,12 +165,12 @@ with gr.Blocks() as demo:
|
|
| 166 |
|
| 167 |
button_generate.click(
|
| 168 |
fn=generator.preprocess,
|
| 169 |
-
inputs=[block_prompt, block_image,
|
| 170 |
-
outputs=[block_prompt
|
| 171 |
show_progress="minimal"
|
| 172 |
).success(
|
| 173 |
fn=generator.generate_mesh,
|
| 174 |
-
inputs=[block_prompt,
|
| 175 |
outputs=[cache_raw_image, cache_task_uuid, fake3d],
|
| 176 |
)
|
| 177 |
|
|
|
|
| 137 |
button_more = gr.Button(value="Download", variant="primary", link=rodin_url)
|
| 138 |
|
| 139 |
cache_raw_image = gr.Image(visible=False, type="pil")
|
|
|
|
| 140 |
cacha_empty = gr.Text(visible=False)
|
| 141 |
cache_task_uuid = gr.Text(value="", visible=False)
|
| 142 |
|
|
|
|
| 147 |
outputs=[cacha_empty]
|
| 148 |
).success(
|
| 149 |
fn=generator.preprocess,
|
| 150 |
+
inputs=[block_prompt, block_image, cache_task_uuid],
|
| 151 |
+
outputs=[block_prompt],
|
| 152 |
show_progress="minimal"
|
| 153 |
).success(
|
| 154 |
fn=generator.generate_mesh,
|
| 155 |
+
inputs=[block_prompt, cache_task_uuid],
|
| 156 |
outputs=[cache_raw_image, cache_task_uuid, fake3d],
|
| 157 |
)
|
| 158 |
|
|
|
|
| 165 |
|
| 166 |
button_generate.click(
|
| 167 |
fn=generator.preprocess,
|
| 168 |
+
inputs=[block_prompt, block_image, cache_task_uuid],
|
| 169 |
+
outputs=[block_prompt],
|
| 170 |
show_progress="minimal"
|
| 171 |
).success(
|
| 172 |
fn=generator.generate_mesh,
|
| 173 |
+
inputs=[block_prompt, cache_task_uuid],
|
| 174 |
outputs=[cache_raw_image, cache_task_uuid, fake3d],
|
| 175 |
)
|
| 176 |
|