Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,38 +4,50 @@ import os
|
|
4 |
import spaces
|
5 |
import glob
|
6 |
from pathlib import Path
|
|
|
7 |
|
8 |
class InfinigenManager:
|
9 |
-
def __init__(self, base_dir="infinigen", output_dir="outputs"):
|
10 |
self.base_dir = Path(base_dir)
|
11 |
self.output_dir = Path(output_dir)
|
|
|
12 |
self.blender_bin = None
|
13 |
self.blender_python = None
|
14 |
self.initialized = False
|
|
|
15 |
self._setup_blender()
|
16 |
self._setup_infinigen()
|
|
|
17 |
|
18 |
def _setup_blender(self):
|
19 |
-
"""
|
|
|
20 |
try:
|
|
|
21 |
blender_bins = glob.glob("/usr/bin/blender")
|
22 |
-
if
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
python_patterns = [
|
33 |
-
f"
|
34 |
-
|
35 |
-
|
36 |
-
"/**/blender/*/python/bin/python*"
|
37 |
]
|
38 |
for pattern in python_patterns:
|
|
|
39 |
python_bins = glob.glob(pattern, recursive=True)
|
40 |
if python_bins:
|
41 |
self.blender_python = python_bins[0]
|
@@ -43,29 +55,52 @@ class InfinigenManager:
|
|
43 |
self.initialized = True
|
44 |
return
|
45 |
|
46 |
-
# Debugging: Vollständige Python-Suche
|
47 |
-
all_python = glob.glob("/**/python", recursive=True)
|
48 |
-
print(f"Alle Python-Pfade: {all_python}")
|
49 |
raise RuntimeError("Blender Python-Interpreter nicht gefunden!")
|
50 |
except Exception as e:
|
51 |
print(f"Fehler bei Blender-Setup: {e}")
|
52 |
self.blender_python = None
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
def _setup_infinigen(self):
|
55 |
"""Installiert Infinigen, falls möglich."""
|
|
|
56 |
if not self.blender_python:
|
57 |
print("Kann Infinigen nicht installieren: Blender Python fehlt.")
|
58 |
return
|
59 |
|
60 |
if not self.base_dir.exists():
|
61 |
print("Klone Infinigen...")
|
62 |
-
subprocess.run(["git", "clone", "https://github.com/princeton-vl/infinigen.git"], check=True)
|
63 |
|
64 |
if not self._is_infinigen_installed():
|
65 |
print("Installiere Infinigen...")
|
66 |
subprocess.run([
|
67 |
self.blender_python, "-m", "pip", "install", "-e", str(self.base_dir) + "[terrain,vis]", "--no-deps", "--user"
|
68 |
-
], check=True)
|
69 |
print("Infinigen installiert.")
|
70 |
|
71 |
def _is_infinigen_installed(self):
|
@@ -73,7 +108,7 @@ class InfinigenManager:
|
|
73 |
if not self.blender_python:
|
74 |
return False
|
75 |
try:
|
76 |
-
subprocess.run([self.blender_python, "-c", "import infinigen"], check=True, capture_output=True)
|
77 |
return True
|
78 |
except subprocess.CalledProcessError:
|
79 |
return False
|
@@ -84,6 +119,7 @@ class InfinigenManager:
|
|
84 |
if not self.initialized or not self.blender_python:
|
85 |
return "Fehler: Infinigen nicht initialisiert (Blender Python fehlt)!"
|
86 |
|
|
|
87 |
self.output_dir.mkdir(exist_ok=True)
|
88 |
configs = configs or ["infinigen_examples/configs/desert.gin", "infinigen_examples/configs/simple.gin"]
|
89 |
pipeline_configs = pipeline_configs or [
|
@@ -100,16 +136,19 @@ class InfinigenManager:
|
|
100 |
] + ["--configs"] + configs + ["--pipeline_configs"] + pipeline_configs
|
101 |
|
102 |
try:
|
103 |
-
result = subprocess.run(command, capture_output=True, text=True, check=True)
|
104 |
print(f"STDOUT: {result.stdout}")
|
105 |
print(f"STDERR: {result.stderr}")
|
106 |
output_path = self.output_dir / "0000000000.png"
|
107 |
return str(output_path) if output_path.exists() else f"Fehler: Bild nicht gefunden. STDERR: {result.stderr}"
|
|
|
|
|
108 |
except subprocess.CalledProcessError as e:
|
109 |
return f"Fehler: {e.stderr}"
|
110 |
|
111 |
# Manager initialisieren
|
112 |
-
|
|
|
113 |
|
114 |
# Gradio-Oberfläche
|
115 |
with gr.Blocks(title="Infinigen Demo") as demo:
|
@@ -120,4 +159,5 @@ with gr.Blocks(title="Infinigen Demo") as demo:
|
|
120 |
|
121 |
generate_button.click(fn=manager.generate_scene, inputs=[seed_input], outputs=[output_image])
|
122 |
|
|
|
123 |
demo.launch()
|
|
|
4 |
import spaces
|
5 |
import glob
|
6 |
from pathlib import Path
|
7 |
+
import torch
|
8 |
|
9 |
class InfinigenManager:
|
10 |
+
def __init__(self, base_dir="infinigen", output_dir="outputs", blender_version="4.3.2"):
|
11 |
self.base_dir = Path(base_dir)
|
12 |
self.output_dir = Path(output_dir)
|
13 |
+
self.blender_version = blender_version
|
14 |
self.blender_bin = None
|
15 |
self.blender_python = None
|
16 |
self.initialized = False
|
17 |
+
print("Initialisiere InfinigenManager...")
|
18 |
self._setup_blender()
|
19 |
self._setup_infinigen()
|
20 |
+
self._check_cuda()
|
21 |
|
22 |
def _setup_blender(self):
|
23 |
+
"""Installiert Blender 4.3.2, falls nötig, und findet den Python-Interpreter."""
|
24 |
+
print("Starte Blender-Setup...")
|
25 |
try:
|
26 |
+
# Prüfe vorhandene Blender-Version
|
27 |
blender_bins = glob.glob("/usr/bin/blender")
|
28 |
+
if blender_bins:
|
29 |
+
self.blender_bin = blender_bins[0]
|
30 |
+
version_result = subprocess.run([self.blender_bin, "-v"], capture_output=True, text=True, timeout=10)
|
31 |
+
current_version = next((line.split()[1] for line in version_result.stdout.split("\n") if "Blender" in line and "." in line), None)
|
32 |
+
print(f"Vorhandene Blender-Version: {current_version or 'unbekannt'}")
|
33 |
+
if current_version and current_version >= self.blender_version:
|
34 |
+
print("Vorhandene Version ist aktuell genug.")
|
35 |
+
else:
|
36 |
+
print("Installiere neueste Blender-Version...")
|
37 |
+
self._install_latest_blender()
|
38 |
+
else:
|
39 |
+
print("Kein Blender vorhanden, installiere neueste Version...")
|
40 |
+
self._install_latest_blender()
|
41 |
+
|
42 |
+
# Finde Blender-Python
|
43 |
+
base_path = f"/home/user/blender-{self.blender_version}-linux-x64" if self.blender_bin == f"/home/user/blender-{self.blender_version}-linux-x64/blender" else "/usr/share/blender"
|
44 |
python_patterns = [
|
45 |
+
f"{base_path}/python/bin/python*",
|
46 |
+
"/usr/share/blender/*/python/bin/python*",
|
47 |
+
"/usr/lib/blender/*/python/bin/python*"
|
|
|
48 |
]
|
49 |
for pattern in python_patterns:
|
50 |
+
print(f"Suche nach Python mit Muster: {pattern}")
|
51 |
python_bins = glob.glob(pattern, recursive=True)
|
52 |
if python_bins:
|
53 |
self.blender_python = python_bins[0]
|
|
|
55 |
self.initialized = True
|
56 |
return
|
57 |
|
|
|
|
|
|
|
58 |
raise RuntimeError("Blender Python-Interpreter nicht gefunden!")
|
59 |
except Exception as e:
|
60 |
print(f"Fehler bei Blender-Setup: {e}")
|
61 |
self.blender_python = None
|
62 |
|
63 |
+
def _install_latest_blender(self):
|
64 |
+
"""Installiert Blender 4.3.2 dynamisch zur Laufzeit."""
|
65 |
+
try:
|
66 |
+
print("Lade Blender 4.3.2 herunter...")
|
67 |
+
subprocess.run([
|
68 |
+
"wget", "https://download.blender.org/release/Blender4.3/blender-4.3.2-linux-x64.tar.xz"
|
69 |
+
], check=True, timeout=60)
|
70 |
+
print("Entpacke Blender 4.3.2...")
|
71 |
+
subprocess.run([
|
72 |
+
"tar", "-xvf", "blender-4.3.2-linux-x64.tar.xz", "-C", "/home/user"
|
73 |
+
], check=True, timeout=60)
|
74 |
+
self.blender_bin = "/home/user/blender-4.3.2-linux-x64/blender"
|
75 |
+
print(f"Blender 4.3.2 installiert unter: {self.blender_bin}")
|
76 |
+
os.remove("blender-4.3.2-linux-x64.tar.xz")
|
77 |
+
except Exception as e:
|
78 |
+
print(f"Fehler bei der Installation von Blender 4.3.2: {e}")
|
79 |
+
raise
|
80 |
+
|
81 |
+
def _check_cuda(self):
|
82 |
+
"""Prüft CUDA-Verfügbarkeit."""
|
83 |
+
if torch.cuda.is_available():
|
84 |
+
print(f"CUDA verfügbar: {torch.cuda.get_device_name(0)}")
|
85 |
+
else:
|
86 |
+
print("CUDA nicht verfügbar!")
|
87 |
+
|
88 |
def _setup_infinigen(self):
|
89 |
"""Installiert Infinigen, falls möglich."""
|
90 |
+
print("Starte Infinigen-Setup...")
|
91 |
if not self.blender_python:
|
92 |
print("Kann Infinigen nicht installieren: Blender Python fehlt.")
|
93 |
return
|
94 |
|
95 |
if not self.base_dir.exists():
|
96 |
print("Klone Infinigen...")
|
97 |
+
subprocess.run(["git", "clone", "https://github.com/princeton-vl/infinigen.git"], check=True, timeout=30)
|
98 |
|
99 |
if not self._is_infinigen_installed():
|
100 |
print("Installiere Infinigen...")
|
101 |
subprocess.run([
|
102 |
self.blender_python, "-m", "pip", "install", "-e", str(self.base_dir) + "[terrain,vis]", "--no-deps", "--user"
|
103 |
+
], check=True, timeout=60)
|
104 |
print("Infinigen installiert.")
|
105 |
|
106 |
def _is_infinigen_installed(self):
|
|
|
108 |
if not self.blender_python:
|
109 |
return False
|
110 |
try:
|
111 |
+
subprocess.run([self.blender_python, "-c", "import infinigen"], check=True, capture_output=True, timeout=10)
|
112 |
return True
|
113 |
except subprocess.CalledProcessError:
|
114 |
return False
|
|
|
119 |
if not self.initialized or not self.blender_python:
|
120 |
return "Fehler: Infinigen nicht initialisiert (Blender Python fehlt)!"
|
121 |
|
122 |
+
print(f"Generiere Szene mit Seed: {seed}")
|
123 |
self.output_dir.mkdir(exist_ok=True)
|
124 |
configs = configs or ["infinigen_examples/configs/desert.gin", "infinigen_examples/configs/simple.gin"]
|
125 |
pipeline_configs = pipeline_configs or [
|
|
|
136 |
] + ["--configs"] + configs + ["--pipeline_configs"] + pipeline_configs
|
137 |
|
138 |
try:
|
139 |
+
result = subprocess.run(command, capture_output=True, text=True, check=True, timeout=300)
|
140 |
print(f"STDOUT: {result.stdout}")
|
141 |
print(f"STDERR: {result.stderr}")
|
142 |
output_path = self.output_dir / "0000000000.png"
|
143 |
return str(output_path) if output_path.exists() else f"Fehler: Bild nicht gefunden. STDERR: {result.stderr}"
|
144 |
+
except subprocess.TimeoutExpired as e:
|
145 |
+
return f"Fehler: Timeout bei Szenengenerierung: {e.stderr}"
|
146 |
except subprocess.CalledProcessError as e:
|
147 |
return f"Fehler: {e.stderr}"
|
148 |
|
149 |
# Manager initialisieren
|
150 |
+
print("Starte Manager-Initialisierung...")
|
151 |
+
manager = InfinigenManager()
|
152 |
|
153 |
# Gradio-Oberfläche
|
154 |
with gr.Blocks(title="Infinigen Demo") as demo:
|
|
|
159 |
|
160 |
generate_button.click(fn=manager.generate_scene, inputs=[seed_input], outputs=[output_image])
|
161 |
|
162 |
+
print("Starte Gradio-Oberfläche...")
|
163 |
demo.launch()
|