Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,69 +2,81 @@ import gradio as gr
|
|
| 2 |
import sqlite3
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
-
import requests
|
| 6 |
import os
|
| 7 |
import shutil
|
| 8 |
-
import
|
| 9 |
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
conn = sqlite3.connect('/
|
| 13 |
c = conn.cursor()
|
| 14 |
c.execute('''CREATE TABLE IF NOT EXISTS jobs
|
| 15 |
(id INTEGER PRIMARY KEY, image_path TEXT, job_id TEXT, status TEXT, output_path TEXT)''')
|
| 16 |
conn.commit()
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def upload_image(image_path):
|
| 21 |
-
with open(image_path, 'rb') as f:
|
| 22 |
-
response = requests.post(
|
| 23 |
-
'https://huggingface.co/spaces/jkorstad/TRELLIS/api/upload',
|
| 24 |
-
files={'file': f}
|
| 25 |
-
)
|
| 26 |
-
response.raise_for_status()
|
| 27 |
-
return response.json()['job_id']
|
| 28 |
-
|
| 29 |
-
def check_status(job_id):
|
| 30 |
-
response = requests.get(f'https://huggingface.co/spaces/jkorstad/TRELLIS/api/status/{job_id}')
|
| 31 |
-
response.raise_for_status()
|
| 32 |
-
return response.json()['status']
|
| 33 |
-
|
| 34 |
-
def get_result(job_id):
|
| 35 |
-
response = requests.get(f'https://huggingface.co/spaces/jkorstad/TRELLIS/api/result/{job_id}')
|
| 36 |
-
response.raise_for_status()
|
| 37 |
-
output_path = f'/tmp/outputs/result_{job_id}.glb'
|
| 38 |
-
os.makedirs('/tmp/outputs', exist_ok=True)
|
| 39 |
-
with open(output_path, 'wb') as f:
|
| 40 |
-
f.write(response.content)
|
| 41 |
-
return output_path
|
| 42 |
|
| 43 |
-
# Processing logic
|
| 44 |
def process_job(job_id):
|
| 45 |
try:
|
|
|
|
| 46 |
c.execute("SELECT image_path FROM jobs WHERE id=?", (job_id,))
|
| 47 |
image_path = c.fetchone()[0]
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
conn.commit()
|
| 51 |
-
|
| 52 |
-
status = check_status(api_job_id)
|
| 53 |
-
if status == 'completed':
|
| 54 |
-
output_path = get_result(api_job_id)
|
| 55 |
-
c.execute("UPDATE jobs SET status='completed', output_path=? WHERE id=?", (output_path, job_id))
|
| 56 |
-
conn.commit()
|
| 57 |
-
break
|
| 58 |
-
elif status == 'failed':
|
| 59 |
-
c.execute("UPDATE jobs SET status='failed' WHERE id=?", (job_id,))
|
| 60 |
-
conn.commit()
|
| 61 |
-
break
|
| 62 |
-
time.sleep(5)
|
| 63 |
except Exception as e:
|
| 64 |
c.execute("UPDATE jobs SET status='failed' WHERE id=?", (job_id,))
|
| 65 |
conn.commit()
|
|
|
|
| 66 |
|
| 67 |
-
# Gradio interface
|
| 68 |
def submit_images(files):
|
| 69 |
if not files:
|
| 70 |
return "No files uploaded."
|
|
@@ -72,8 +84,8 @@ def submit_images(files):
|
|
| 72 |
c.execute("INSERT INTO jobs (status) VALUES ('submitted')")
|
| 73 |
job_id = c.lastrowid
|
| 74 |
conn.commit()
|
| 75 |
-
image_path = f'/
|
| 76 |
-
os.makedirs('/
|
| 77 |
shutil.copy(file.name, image_path)
|
| 78 |
c.execute("UPDATE jobs SET image_path=? WHERE id=?", (image_path, job_id))
|
| 79 |
conn.commit()
|
|
|
|
| 2 |
import sqlite3
|
| 3 |
import threading
|
| 4 |
import time
|
|
|
|
| 5 |
import os
|
| 6 |
import shutil
|
| 7 |
+
from gradio_client import Client, handle_file
|
| 8 |
|
| 9 |
+
# Ensure /data/ directory exists
|
| 10 |
+
os.makedirs('/data', exist_ok=True)
|
| 11 |
|
| 12 |
+
# Database setup
|
| 13 |
+
conn = sqlite3.connect('/data/jobs.db', check_same_thread=False)
|
| 14 |
c = conn.cursor()
|
| 15 |
c.execute('''CREATE TABLE IF NOT EXISTS jobs
|
| 16 |
(id INTEGER PRIMARY KEY, image_path TEXT, job_id TEXT, status TEXT, output_path TEXT)''')
|
| 17 |
conn.commit()
|
| 18 |
|
| 19 |
+
# TRELLIS API client
|
| 20 |
+
trellis_client = Client("jkorstad/TRELLIS")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Processing logic with three-step TRELLIS workflow
|
| 23 |
def process_job(job_id):
|
| 24 |
try:
|
| 25 |
+
# Get the uploaded image path
|
| 26 |
c.execute("SELECT image_path FROM jobs WHERE id=?", (job_id,))
|
| 27 |
image_path = c.fetchone()[0]
|
| 28 |
+
|
| 29 |
+
# Step 1: Preprocess the image
|
| 30 |
+
c.execute("UPDATE jobs SET status='preprocessing' WHERE id=?", (job_id,))
|
| 31 |
+
conn.commit()
|
| 32 |
+
preprocessed_image = trellis_client.predict(
|
| 33 |
+
image=handle_file(image_path),
|
| 34 |
+
api_name="/preprocess_image"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Step 2: Generate 3D asset
|
| 38 |
+
c.execute("UPDATE jobs SET status='generating' WHERE id=?", (job_id,))
|
| 39 |
+
conn.commit()
|
| 40 |
+
time.sleep(5) # Wait between steps; adjust based on observed timing
|
| 41 |
+
result_3d = trellis_client.predict(
|
| 42 |
+
image=handle_file(preprocessed_image), # Use preprocessed image
|
| 43 |
+
multiimages=[],
|
| 44 |
+
seed=0, # Default; could make configurable
|
| 45 |
+
ss_guidance_strength=7.5,
|
| 46 |
+
ss_sampling_steps=12,
|
| 47 |
+
slat_guidance_strength=3,
|
| 48 |
+
slat_sampling_steps=12,
|
| 49 |
+
multiimage_algo="stochastic",
|
| 50 |
+
api_name="/image_to_3d"
|
| 51 |
+
)
|
| 52 |
+
video_path = result_3d['video'] # Extract video filepath from dict
|
| 53 |
+
|
| 54 |
+
# Step 3: Extract GLB
|
| 55 |
+
c.execute("UPDATE jobs SET status='extracting' WHERE id=?", (job_id,))
|
| 56 |
+
conn.commit()
|
| 57 |
+
time.sleep(10) # Wait for 3D processing; adjust as needed
|
| 58 |
+
glb_result = trellis_client.predict(
|
| 59 |
+
mesh_simplify=0.95,
|
| 60 |
+
texture_size=1024,
|
| 61 |
+
api_name="/extract_glb"
|
| 62 |
+
)
|
| 63 |
+
glb_path = glb_result[0] # First element is the GLB filepath
|
| 64 |
+
|
| 65 |
+
# Move GLB to persistent storage
|
| 66 |
+
output_path = f'/data/outputs/result_{job_id}.glb'
|
| 67 |
+
os.makedirs('/data/outputs', exist_ok=True)
|
| 68 |
+
shutil.move(glb_path, output_path)
|
| 69 |
+
|
| 70 |
+
# Update job status
|
| 71 |
+
c.execute("UPDATE jobs SET status='completed', output_path=? WHERE id=?", (output_path, job_id))
|
| 72 |
conn.commit()
|
| 73 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
except Exception as e:
|
| 75 |
c.execute("UPDATE jobs SET status='failed' WHERE id=?", (job_id,))
|
| 76 |
conn.commit()
|
| 77 |
+
print(f"Error processing job {job_id}: {e}")
|
| 78 |
|
| 79 |
+
# Gradio interface
|
| 80 |
def submit_images(files):
|
| 81 |
if not files:
|
| 82 |
return "No files uploaded."
|
|
|
|
| 84 |
c.execute("INSERT INTO jobs (status) VALUES ('submitted')")
|
| 85 |
job_id = c.lastrowid
|
| 86 |
conn.commit()
|
| 87 |
+
image_path = f'/data/inputs/input_{job_id}.jpg'
|
| 88 |
+
os.makedirs('/data/inputs', exist_ok=True)
|
| 89 |
shutil.copy(file.name, image_path)
|
| 90 |
c.execute("UPDATE jobs SET image_path=? WHERE id=?", (image_path, job_id))
|
| 91 |
conn.commit()
|