File size: 3,824 Bytes
63ce34f
988efc8
 
 
 
 
 
 
 
 
 
63ce34f
988efc8
63ce34f
 
988efc8
63ce34f
bcd68a1
26217ae
63ce34f
3447081
 
63ce34f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
988efc8
63ce34f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
988efc8
3447081
63ce34f
3447081
63ce34f
 
 
3447081
63ce34f
 
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
# In appTrellis_fileErrorsFIx.py

@spaces.GPU
def text_to_3d(
    prompt: str,
    seed: int,
    ss_guidance_strength: float,
    ss_sampling_steps: int,
    slat_guidance_strength: float,
    slat_sampling_steps: int,
    req: gr.Request,
) -> Tuple[dict, str]: # Return type changed for clarity
    """
    Generates a 3D model (Gaussian and Mesh) from text and returns a
    serializable state dictionary and a video preview path.
    """
    print(f"[text_to_3d] Received prompt: '{prompt}', Seed: {seed}")
    user_dir = os.path.join(TMP_DIR, str(req.session_hash))
    os.makedirs(user_dir, exist_ok=True)
    print(f"[text_to_3d] User directory: {user_dir}")

    # --- Generation Pipeline ---
    try:
        print("[text_to_3d] Running Trellis pipeline...")
        outputs = pipeline.run(
            prompt,
            seed=seed,
            formats=["gaussian", "mesh"], # Ensure both are generated
            sparse_structure_sampler_params={
                "steps": int(ss_sampling_steps), # Ensure steps are int
                "cfg_strength": float(ss_guidance_strength),
            },
            slat_sampler_params={
                "steps": int(slat_sampling_steps), # Ensure steps are int
                "cfg_strength": float(slat_guidance_strength),
            },
        )
        print("[text_to_3d] Pipeline run completed.")
    except Exception as e:
        print(f"❌ [text_to_3d] Pipeline error: {e}", file=sys.stderr)
        traceback.print_exc()
        raise gr.Error(f"Trellis pipeline failed: {e}")

    # --- Create Serializable State Dictionary ---
    try:
        state_dict = pack_state(outputs['gaussian'][0], outputs['mesh'][0])
    except Exception as e:
        print(f"❌ [text_to_3d] pack_state error: {e}", file=sys.stderr)
        traceback.print_exc()
        raise gr.Error(f"Failed to pack state: {e}")

    # --- Render Video Preview (TEMPORARILY DISABLED FOR DEBUGGING) ---
    video_path = None # Set path to None
    # try:
    #     print("[text_to_3d] Rendering video preview...")
    #     video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
    #     video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
    #     # Ensure video frames are uint8
    #     video = [np.concatenate([v.astype(np.uint8), vg.astype(np.uint8)], axis=1) for v, vg in zip(video, video_geo)]
    #     video_path = os.path.join(user_dir, 'sample.mp4')
    #     imageio.mimsave(video_path, video, fps=15, quality=8) # Added quality setting
    #     print(f"[text_to_3d] Video saved to: {video_path}")
    # except Exception as e:
    #     print(f"❌ [text_to_3d] Video rendering/saving error: {e}", file=sys.stderr)
    #     traceback.print_exc()
    #     # Still return state_dict, but maybe signal video error? Return None for path.
    #     video_path = None # Indicate video failure
    print("[text_to_3d] Skipping video rendering for debugging.")

    # --- Cleanup and Return ---
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
        print("[text_to_3d] Cleared CUDA cache.")

    # --- Return Serializable Dictionary and None for Video Path ---
    print("[text_to_3d] Returning state dictionary and None video path.")
    return state_dict, video_path # Return dict and None video path

# --- Gradio UI Definition ---
# ... (rest of the file is the same, but you might want to adjust the output mapping if needed)

# In the generate_btn.click handler, adjust the outputs if the video component causes issues:
# Option 1: Keep Video component, it will just show nothing.
# outputs=[output_buf, video_output], # This might be fine

# Option 2: Use a dummy hidden component if video_output causes issues receiving None
# outputs=[output_buf, gr.Textbox(visible=False)], # Example dummy