Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import logging
|
| 2 |
import os
|
| 3 |
import boto3
|
|
@@ -17,6 +21,9 @@ from functools import partial
|
|
| 17 |
import io
|
| 18 |
from io import BytesIO
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
subprocess.run(shlex.split('pip install wheel/torchmcubes-0.1.0-cp310-cp310-linux_x86_64.whl'))
|
| 22 |
|
|
@@ -165,7 +172,7 @@ def preprocess(input_image, do_remove_background, foreground_ratio):
|
|
| 165 |
image = fill_background(image)
|
| 166 |
return image
|
| 167 |
|
| 168 |
-
@spaces.GPU
|
| 169 |
def generate(image, mc_resolution, formats=["obj", "glb"]):
|
| 170 |
scene_codes = model(image, device=device)
|
| 171 |
mesh = model.extract_mesh(scene_codes, resolution=mc_resolution)[0]
|
|
@@ -180,73 +187,32 @@ def generate(image, mc_resolution, formats=["obj", "glb"]):
|
|
| 180 |
|
| 181 |
return mesh_path_obj.name, mesh_path_glb.name
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
preprocessed = preprocess(image_pil, do_remove_background, foreground_ratio)
|
| 186 |
-
mesh_name_obj, mesh_name_glb = generate(preprocessed,
|
| 187 |
-
return preprocessed, mesh_name_obj, mesh_name_glb
|
| 188 |
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
label="Generated Image",
|
| 198 |
-
image_mode="RGBA",
|
| 199 |
-
sources="upload",
|
| 200 |
-
type="pil",
|
| 201 |
-
elem_id="content_image"
|
| 202 |
-
)
|
| 203 |
-
text_prompt = gr.Textbox(
|
| 204 |
-
label="Text Prompt",
|
| 205 |
-
placeholder="Enter Positive Prompt"
|
| 206 |
-
)
|
| 207 |
-
seed = gr.Textbox(label="Random Seed", value=0)
|
| 208 |
-
processed_image = gr.Image(label="Processed Image", interactive=False, visible=False)
|
| 209 |
-
with gr.Row():
|
| 210 |
-
with gr.Group():
|
| 211 |
-
do_remove_background = gr.Checkbox(
|
| 212 |
-
label="Remove Background", value=True
|
| 213 |
-
)
|
| 214 |
-
foreground_ratio = gr.Slider(
|
| 215 |
-
label="Foreground Ratio",
|
| 216 |
-
minimum=0.5,
|
| 217 |
-
maximum=1.0,
|
| 218 |
-
value=0.85,
|
| 219 |
-
step=0.05,
|
| 220 |
-
)
|
| 221 |
-
mc_resolution = gr.Slider(
|
| 222 |
-
label="Marching Cubes Resolution",
|
| 223 |
-
minimum=32,
|
| 224 |
-
maximum=320,
|
| 225 |
-
value=256,
|
| 226 |
-
step=32
|
| 227 |
-
)
|
| 228 |
-
with gr.Row():
|
| 229 |
-
submit = gr.Button("Generate", elem_id="generate", variant="primary")
|
| 230 |
-
with gr.Column():
|
| 231 |
-
with gr.Tab("OBJ"):
|
| 232 |
-
output_model_obj = gr.Model3D(
|
| 233 |
-
label="Output Model (OBJ Format)",
|
| 234 |
-
interactive=False,
|
| 235 |
-
)
|
| 236 |
-
gr.Markdown("Note: Downloaded object will be flipped in case of .obj export. Export .glb instead or manually flip it before usage.")
|
| 237 |
-
with gr.Tab("GLB"):
|
| 238 |
-
output_model_glb = gr.Model3D(
|
| 239 |
-
label="Output Model (GLB Format)",
|
| 240 |
-
interactive=False,
|
| 241 |
-
)
|
| 242 |
-
gr.Markdown("Note: The model shown here has a darker appearance. Download to get correct results.")
|
| 243 |
-
|
| 244 |
-
submit.click(fn=check_input_image, inputs=[input_image]).success(
|
| 245 |
-
fn=run_example,
|
| 246 |
-
inputs=[input_image, seed, do_remove_background, foreground_ratio, mc_resolution, text_prompt],
|
| 247 |
-
outputs=[processed_image, output_model_obj, output_model_glb],
|
| 248 |
-
# outputs=[output_model_obj, output_model_glb],
|
| 249 |
-
)
|
| 250 |
-
|
| 251 |
-
demo.queue(max_size=10)
|
| 252 |
-
demo.launch()
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
| 2 |
+
from fastapi.responses import StreamingResponse
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import Optional
|
| 5 |
import logging
|
| 6 |
import os
|
| 7 |
import boto3
|
|
|
|
| 21 |
import io
|
| 22 |
from io import BytesIO
|
| 23 |
|
| 24 |
+
app = FastAPI()
|
| 25 |
+
|
| 26 |
+
torch.cuda.empty_cache()
|
| 27 |
|
| 28 |
subprocess.run(shlex.split('pip install wheel/torchmcubes-0.1.0-cp310-cp310-linux_x86_64.whl'))
|
| 29 |
|
|
|
|
| 172 |
image = fill_background(image)
|
| 173 |
return image
|
| 174 |
|
| 175 |
+
# @spaces.GPU
|
| 176 |
def generate(image, mc_resolution, formats=["obj", "glb"]):
|
| 177 |
scene_codes = model(image, device=device)
|
| 178 |
mesh = model.extract_mesh(scene_codes, resolution=mc_resolution)[0]
|
|
|
|
| 187 |
|
| 188 |
return mesh_path_obj.name, mesh_path_glb.name
|
| 189 |
|
| 190 |
+
@app.post("/process_image/")
|
| 191 |
+
async def process_image(
|
| 192 |
+
file: UploadFile = File(...),
|
| 193 |
+
seed: int = Form(...),
|
| 194 |
+
use_image: bool = Form(...),
|
| 195 |
+
do_remove_background: bool = Form(...),
|
| 196 |
+
foreground_ratio: float = Form(...),
|
| 197 |
+
mc_resolution: int = Form(...),
|
| 198 |
+
text_prompt: Optional[str] = Form(None)
|
| 199 |
+
):
|
| 200 |
+
image_bytes = await file.read()
|
| 201 |
+
input_image = Image.open(BytesIO(image_bytes))
|
| 202 |
+
|
| 203 |
+
if use_image:
|
| 204 |
+
image_pil = generate_image_from_text(encoded_image=input_image, seed=seed, pos_prompt=text_prompt)
|
| 205 |
+
else:
|
| 206 |
+
image_pil = input_image
|
| 207 |
+
|
| 208 |
preprocessed = preprocess(image_pil, do_remove_background, foreground_ratio)
|
| 209 |
+
mesh_name_obj, mesh_name_glb = generate(preprocessed, mc_resolution)
|
|
|
|
| 210 |
|
| 211 |
+
return {
|
| 212 |
+
"obj_path": mesh_name_obj,
|
| 213 |
+
"glb_path": mesh_name_glb
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
if __name__ == "__main__":
|
| 217 |
+
import uvicorn
|
| 218 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|