Spaces:
Running
Running
File size: 3,456 Bytes
2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 9c4480b 2156619 | 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 | {
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "6117abdc",
"metadata": {},
"outputs": [],
"source": [
"import os, requests, time\n",
"\n",
"JOB_ID = os.environ[\"JOB_ID\"]\n",
"SPACE_URL = os.environ[\"SPACE_URL\"].rstrip(\"/\")\n",
"\n",
"print(f\"Job: {JOB_ID}\")\n",
"print(f\"Space: {SPACE_URL}\")\n",
"\n",
"# 1. Download image\n",
"print(\"Downloading image...\")\n",
"r = requests.get(f\"{SPACE_URL}/image/{JOB_ID}\", timeout=30)\n",
"r.raise_for_status()\n",
"with open(\"/kaggle/working/input.png\", \"wb\") as f:\n",
" f.write(r.content)\n",
"print(\"Image downloaded!\")\n",
"\n",
"# 2. Remove background\n",
"os.system(\"pip install -q transparent-background gradio_client pillow\")\n",
"from transparent_background import Remover\n",
"from PIL import Image\n",
"\n",
"print(\"Removing background...\")\n",
"remover = Remover()\n",
"img = Image.open(\"/kaggle/working/input.png\").convert(\"RGB\")\n",
"out = remover.process(img, type=\"rgba\")\n",
"out.save(\"/kaggle/working/input_nobg.png\")\n",
"print(\"BG removed!\")\n",
"\n",
"# 3. Run TRELLIS\n",
"from gradio_client import Client, handle_file\n",
"\n",
"MAX_RETRIES = 3\n",
"result = None\n",
"\n",
"for attempt in range(1, MAX_RETRIES + 1):\n",
" try:\n",
" print(f\"Connecting to TRELLIS (attempt {attempt}/{MAX_RETRIES})...\")\n",
" client = Client(\"trellis-community/TRELLIS\")\n",
" client.predict(api_name=\"/start_session\")\n",
" print(\"Session ready! Generating...\")\n",
"\n",
" result = client.predict(\n",
" image=handle_file(\"/kaggle/working/input_nobg.png\"),\n",
" multiimages=[],\n",
" seed=0,\n",
" ss_guidance_strength=7.5,\n",
" ss_sampling_steps=12,\n",
" slat_guidance_strength=3.0,\n",
" slat_sampling_steps=12,\n",
" multiimage_algo=\"stochastic\",\n",
" mesh_simplify=0.95,\n",
" texture_size=1024,\n",
" api_name=\"/generate_and_extract_glb\"\n",
" )\n",
" print(\"Generation done!\")\n",
" break\n",
"\n",
" except Exception as e:\n",
" print(f\"Attempt {attempt} failed: {e}\")\n",
" if attempt < MAX_RETRIES:\n",
" time.sleep(30)\n",
" else:\n",
" raise RuntimeError(f\"TRELLIS failed after {MAX_RETRIES} attempts: {e}\")\n",
"\n",
"# 4. POST GLB back\n",
"glb_src = result[1] or result[2]\n",
"print(f\"Sending GLB ({os.path.getsize(glb_src)/1024/1024:.1f} MB)...\")\n",
"\n",
"with open(glb_src, \"rb\") as f:\n",
" resp = requests.post(\n",
" f\"{SPACE_URL}/receive_glb\",\n",
" params={\"job_id\": JOB_ID},\n",
" files={\"file\": (\"mesh.glb\", f, \"model/gltf-binary\")},\n",
" timeout=120\n",
" )\n",
"\n",
"resp.raise_for_status()\n",
"print(f\"GLB delivered! Job {JOB_ID} complete.\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.12.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
} |