Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,180 +1,224 @@
|
|
1 |
import streamlit as st
|
2 |
-
from PIL import Image,
|
3 |
import io
|
4 |
import time
|
5 |
import os
|
6 |
-
import random
|
|
|
|
|
|
|
7 |
|
8 |
# ------------------------------------------------------------------------------
|
9 |
# Page Configuration
|
10 |
# ------------------------------------------------------------------------------
|
11 |
st.set_page_config(
|
12 |
-
page_title="AI Real Estate Visualization Suite",
|
13 |
layout="wide",
|
14 |
-
page_icon="
|
15 |
initial_sidebar_state="expanded"
|
16 |
)
|
17 |
|
18 |
# ------------------------------------------------------------------------------
|
19 |
-
#
|
20 |
# ------------------------------------------------------------------------------
|
21 |
-
#
|
22 |
-
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
output_mode: str, # 'staging', 'renovation', 'material_swap', 'layout_variation'
|
29 |
-
room_type: str,
|
30 |
-
style: str,
|
31 |
-
furniture_prefs: str = "",
|
32 |
-
materials: dict = {}, # e.g., {'wall_color': '#FFFFFF', 'floor_type': 'Oak Wood'}
|
33 |
-
lighting_time: float = 0.5, # 0.0 (dawn) to 1.0 (dusk)
|
34 |
-
camera_angle: str = "Eye-Level",
|
35 |
-
remove_objects: bool = False,
|
36 |
-
renovation_instructions: str = "" # e.g., "Remove wall between kitchen and living room"
|
37 |
-
) -> Image.Image:
|
38 |
"""
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
"""
|
41 |
-
st.info(
|
42 |
-
print(f"
|
43 |
-
print(
|
44 |
-
print(f"
|
45 |
-
print(f"
|
46 |
-
print(f"
|
47 |
-
|
48 |
-
print(
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
# --- Simulation Logic ---
|
61 |
-
# Determine the placeholder image based on mode
|
62 |
-
base_placeholder = "assets/staged_result_placeholder.png"
|
63 |
-
if output_mode == 'renovation':
|
64 |
-
base_placeholder = "assets/renovation_placeholder.png" # Need another dummy image
|
65 |
-
elif output_mode == 'material_swap':
|
66 |
-
base_placeholder = "assets/material_swap_placeholder.png" # Need another dummy image
|
67 |
-
elif output_mode == 'layout_variation':
|
68 |
-
base_placeholder = f"assets/layout_{random.randint(1,2)}_placeholder.png" # Need layout_1/2 dummies
|
69 |
-
|
70 |
-
if isinstance(input_data, Image.Image):
|
71 |
-
base_image = input_data # Use original if input was image
|
72 |
else:
|
73 |
-
|
|
|
|
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
print(f"SIMULATING AI: Loaded placeholder: {base_placeholder}")
|
81 |
-
except Exception as e:
|
82 |
-
print(f"Error loading placeholder '{base_placeholder}': {e}")
|
83 |
-
staged_image = None
|
84 |
-
|
85 |
-
# Fallback if placeholder failed or input wasn't an image for overlay
|
86 |
-
if staged_image is None:
|
87 |
-
if base_image:
|
88 |
-
staged_image = base_image.copy()
|
89 |
-
print("SIMULATING AI: Placeholder failed, using copy of original image.")
|
90 |
-
else:
|
91 |
-
# Create a generic image if no input image and placeholder failed
|
92 |
-
staged_image = Image.new('RGB', (800, 600), color = (200, 200, 200))
|
93 |
-
print("SIMULATING AI: Placeholder failed and no base image, using blank canvas.")
|
94 |
-
|
95 |
-
# Add text overlay summarizing the simulation
|
96 |
-
draw = ImageDraw.Draw(staged_image)
|
97 |
-
try:
|
98 |
-
font_path = "arial.ttf" # Check system fonts or provide path
|
99 |
-
font_size = 20
|
100 |
-
font = ImageFont.truetype(font_path, font_size)
|
101 |
-
except IOError:
|
102 |
-
font = ImageFont.load_default()
|
103 |
-
|
104 |
-
text_lines = [
|
105 |
-
f"AI Sim Result: {output_mode.replace('_', ' ').title()}",
|
106 |
-
f"Style: {style}, Room: {room_type}",
|
107 |
-
f"Lighting: {lighting_time:.2f}, Camera: {camera_angle}",
|
108 |
-
]
|
109 |
-
if furniture_prefs: text_lines.append(f"Prefs: {furniture_prefs[:30]}...")
|
110 |
-
if materials.get('wall_color'): text_lines.append(f"Wall: {materials['wall_color']}")
|
111 |
-
if materials.get('floor_type'): text_lines.append(f"Floor: {materials['floor_type']}")
|
112 |
-
if remove_objects: text_lines.append(f"[Removed Objects]")
|
113 |
-
if renovation_instructions: text_lines.append(f"Reno: {renovation_instructions[:30]}...")
|
114 |
-
|
115 |
-
y_pos = 10
|
116 |
-
for line in text_lines:
|
117 |
-
try:
|
118 |
-
# Simple background box
|
119 |
-
text_bbox = draw.textbbox((10, y_pos), line, font=font)
|
120 |
-
# Add padding to bbox
|
121 |
-
padded_bbox = (text_bbox[0]-2, text_bbox[1]-2, text_bbox[2]+2, text_bbox[3]+2)
|
122 |
-
draw.rectangle(padded_bbox, fill="rgba(0, 0, 0, 0.6)")
|
123 |
-
draw.text((10, y_pos), line, fill="white", font=font)
|
124 |
-
y_pos += font_size + 4 # Move down for next line
|
125 |
-
except Exception as draw_err:
|
126 |
-
print(f"Error drawing text overlay: {draw_err}") # Log error but continue
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
return staged_image
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
# ------------------------------------------------------------------------------
|
134 |
-
#
|
135 |
# ------------------------------------------------------------------------------
|
136 |
-
|
137 |
-
st.
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
if
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
# ------------------------------------------------------------------------------
|
148 |
-
#
|
149 |
# ------------------------------------------------------------------------------
|
150 |
-
def
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
# ------------------------------------------------------------------------------
|
162 |
-
# Main Application
|
163 |
# ------------------------------------------------------------------------------
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
st.markdown("---")
|
167 |
|
168 |
# --- Sidebar ---
|
169 |
with st.sidebar:
|
170 |
st.header("⚙️ Input & Configuration")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
|
172 |
input_file = st.file_uploader(
|
173 |
"1. Upload File",
|
174 |
-
type=["png", "jpg", "jpeg", "webp", "dxf", "dwg", "obj", "fbx"], #
|
175 |
key="file_uploader",
|
176 |
accept_multiple_files=False,
|
177 |
-
help="Upload Room Image
|
|
|
178 |
)
|
179 |
|
180 |
# --- Input Processing ---
|
@@ -184,203 +228,214 @@ with st.sidebar:
|
|
184 |
file_ext = os.path.splitext(input_file.name)[1].lower()
|
185 |
file_bytes = input_file.getvalue()
|
186 |
processed = False
|
|
|
187 |
|
188 |
-
|
189 |
-
|
190 |
-
try:
|
191 |
image = Image.open(io.BytesIO(file_bytes)).convert("RGB")
|
192 |
-
max_size = (1024, 1024)
|
193 |
image.thumbnail(max_size, Image.Resampling.LANCZOS)
|
194 |
-
|
195 |
-
|
196 |
processed = True
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
st.session_state.input_type = None
|
222 |
st.session_state.uploaded_filename = None
|
223 |
|
224 |
-
elif st.session_state.uploaded_filename: # File was removed by user
|
225 |
-
st.session_state.input_data = None
|
226 |
-
st.session_state.input_type = None
|
227 |
-
st.session_state.ai_result_image = None
|
228 |
-
st.session_state.uploaded_filename = None
|
229 |
-
st.session_state.last_run_params = {}
|
230 |
-
|
231 |
|
232 |
-
# --- Configuration Options
|
233 |
-
if st.session_state.
|
234 |
st.markdown("---")
|
235 |
st.subheader("2. Visualization Mode")
|
236 |
output_mode = st.selectbox(
|
237 |
"Select Mode:",
|
238 |
options=['Virtual Staging', 'Renovation Preview', 'Material Swap', 'Layout Variation'],
|
239 |
key='output_mode_select',
|
240 |
-
|
241 |
-
).lower().replace(' ', '_')
|
242 |
|
243 |
st.markdown("---")
|
244 |
st.subheader("3. Scene Parameters")
|
245 |
-
room_type = st.selectbox(
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
)
|
250 |
-
|
251 |
-
"
|
252 |
-
|
253 |
-
key="
|
254 |
-
|
255 |
-
|
256 |
-
# --- Advanced Controls Expander ---
|
257 |
-
with st.expander("✨ Advanced Controls"):
|
258 |
-
furniture_prefs = st.text_input(
|
259 |
-
"Furniture Preferences (Optional)",
|
260 |
-
placeholder="e.g., 'Large velvet green sofa', 'minimalist oak desk'",
|
261 |
-
key="furniture_input",
|
262 |
-
help="Describe specific furniture items or characteristics."
|
263 |
-
)
|
264 |
-
|
265 |
-
st.markdown("**Material Customization:**")
|
266 |
-
wall_color = st.color_picker("Wall Color (Approx.)", value="#FFFFFF", key="wall_color_picker")
|
267 |
-
floor_type = st.selectbox(
|
268 |
-
"Floor Type",
|
269 |
-
["(Auto)", "Oak Wood", "Dark Wood", "Light Wood", "Carpet (Neutral)", "Concrete", "Tile (Light)", "Tile (Dark)"],
|
270 |
-
key="floor_select"
|
271 |
-
)
|
272 |
-
materials_dict = {'wall_color': wall_color, 'floor_type': floor_type}
|
273 |
-
|
274 |
-
st.markdown("**Lighting & Camera:**")
|
275 |
-
lighting_time = st.slider(
|
276 |
-
"Time of Day (Simulated)",
|
277 |
-
min_value=0.0, max_value=1.0, value=0.5, step=0.1, format="%.1f",
|
278 |
-
key="lighting_slider",
|
279 |
-
help="0.0 = Dawn/Sunrise, 0.5 = Midday, 1.0 = Dusk/Sunset"
|
280 |
-
)
|
281 |
-
camera_angle = st.selectbox(
|
282 |
-
"Camera Angle", ["Eye-Level", "High Angle", "Low Angle", "Wide Angle (Simulated)"],
|
283 |
-
key="camera_select"
|
284 |
-
)
|
285 |
-
|
286 |
-
st.markdown("**AI Assist Features:**")
|
287 |
-
remove_objects = st.checkbox(
|
288 |
-
"Attempt to Remove Existing Objects/Furniture", value=False, key="remove_obj_check",
|
289 |
-
help="If staging an image that isn't empty, AI will try to remove existing items first (Simulated)."
|
290 |
-
)
|
291 |
-
|
292 |
renovation_instructions = ""
|
293 |
-
if output_mode == 'renovation_preview':
|
294 |
-
renovation_instructions = st.text_input(
|
295 |
-
"Renovation Instructions (Simulated)",
|
296 |
-
placeholder="e.g., 'Remove center wall', 'add window on left wall'",
|
297 |
-
key="renovation_input"
|
298 |
-
)
|
299 |
|
300 |
st.markdown("---")
|
301 |
st.subheader("4. Generate Visualization")
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
324 |
|
325 |
else:
|
326 |
st.info("⬆️ Upload a file to begin.")
|
327 |
|
328 |
-
|
329 |
# --- Main Display Area ---
|
330 |
col1, col2 = st.columns(2)
|
331 |
|
332 |
with col1:
|
333 |
st.subheader("Input")
|
334 |
-
if st.session_state.
|
335 |
-
if st.session_state.input_type == 'image':
|
336 |
-
st.image(st.session_state.
|
|
|
|
|
|
|
|
|
337 |
else:
|
338 |
-
|
339 |
-
display_input_placeholder(st.session_state.input_type, st.session_state.uploaded_filename)
|
340 |
else:
|
341 |
-
st.markdown("<div style='height: 400px; border: 2px dashed #ccc;
|
|
|
342 |
|
343 |
with col2:
|
344 |
st.subheader("AI Visualization Result")
|
345 |
-
if st.session_state.ai_result_image is not None:
|
346 |
-
# Display the resulting image
|
347 |
-
run_mode_display = st.session_state.last_run_params.get('output_mode', 'N/A').replace('_', ' ').title()
|
348 |
-
st.image(st.session_state.ai_result_image, caption=f"Result ({run_mode_display} Simulation)", use_column_width=True)
|
349 |
-
|
350 |
-
# Display parameters used for this run
|
351 |
-
with st.expander("View Parameters Used for This Result", expanded=False):
|
352 |
-
st.json(st.session_state.last_run_params, expanded=True) # Cannot directly display image data in json
|
353 |
-
|
354 |
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
#
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
379 |
|
380 |
|
381 |
st.markdown("---")
|
382 |
st.warning("""
|
383 |
-
**Disclaimer:** This is
|
384 |
-
|
385 |
-
|
386 |
""")
|
|
|
1 |
import streamlit as st
|
2 |
+
from PIL import Image, UnidentifiedImageError
|
3 |
import io
|
4 |
import time
|
5 |
import os
|
6 |
+
import random
|
7 |
+
import json # For API simulation
|
8 |
+
import requests # To simulate API calls (though we won't make real ones here)
|
9 |
+
import uuid # For job IDs
|
10 |
|
11 |
# ------------------------------------------------------------------------------
|
12 |
# Page Configuration
|
13 |
# ------------------------------------------------------------------------------
|
14 |
st.set_page_config(
|
15 |
+
page_title="AI Real Estate Visualization Suite [PRO]",
|
16 |
layout="wide",
|
17 |
+
page_icon="🚀",
|
18 |
initial_sidebar_state="expanded"
|
19 |
)
|
20 |
|
21 |
# ------------------------------------------------------------------------------
|
22 |
+
# Simulated Backend API Interaction
|
23 |
# ------------------------------------------------------------------------------
|
24 |
+
# Replace with your actual backend API endpoint
|
25 |
+
BACKEND_API_URL = "http://your-production-backend.com/api/v1/visualize"
|
26 |
+
# Replace with your actual status check endpoint
|
27 |
+
STATUS_API_URL = "http://your-production-backend.com/api/v1/status/{job_id}"
|
28 |
+
|
29 |
+
# --- Simulate API Call ---
|
30 |
+
def submit_visualization_job(payload: dict) -> tuple[str | None, str | None]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
"""
|
32 |
+
Simulates submitting a job to the backend API.
|
33 |
+
In reality, this would use 'requests.post'.
|
34 |
+
|
35 |
+
Args:
|
36 |
+
payload (dict): Data to send (params, input reference, etc.)
|
37 |
+
|
38 |
+
Returns:
|
39 |
+
tuple[str | None, str | None]: (job_id, error_message)
|
40 |
"""
|
41 |
+
st.info("Submitting job to backend simulation...")
|
42 |
+
print(f"SIMULATING API SUBMIT to {BACKEND_API_URL}")
|
43 |
+
print("Payload (summary):")
|
44 |
+
print(f" Mode: {payload.get('output_mode')}")
|
45 |
+
print(f" Style: {payload.get('style')}")
|
46 |
+
print(f" Input Type: {payload.get('input_type')}")
|
47 |
+
# Omit potentially large data like image bytes from console log in real scenario
|
48 |
+
# print(json.dumps(payload, indent=2)) # Don't print potentially large data
|
49 |
+
|
50 |
+
# Simulate network latency & backend processing start
|
51 |
+
time.sleep(1.5)
|
52 |
+
|
53 |
+
# Simulate success/failure
|
54 |
+
if random.random() < 0.95: # 95% success rate
|
55 |
+
job_id = f"job_{uuid.uuid4()}"
|
56 |
+
print(f"API Submit Simulation SUCCESS: Job ID = {job_id}")
|
57 |
+
# In a real app, store job_id associated with user/session
|
58 |
+
return job_id, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
else:
|
60 |
+
error_msg = "Simulated API Error: Failed to submit job (e.g., server busy, invalid params)."
|
61 |
+
print(f"API Submit Simulation FAILED: {error_msg}")
|
62 |
+
return None, error_msg
|
63 |
|
64 |
+
# --- Simulate Status Check ---
|
65 |
+
def check_job_status(job_id: str) -> tuple[str, str | None, str | None]:
|
66 |
+
"""
|
67 |
+
Simulates checking the status of a job via the backend API.
|
68 |
+
In reality, this would use 'requests.get'.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
Args:
|
71 |
+
job_id (str): The ID of the job to check.
|
|
|
72 |
|
73 |
+
Returns:
|
74 |
+
tuple[str, str | None, str | None]: (status, result_url, error_message)
|
75 |
+
status: 'PENDING', 'PROCESSING', 'COMPLETED', 'FAILED'
|
76 |
+
"""
|
77 |
+
status_url = STATUS_API_URL.format(job_id=job_id)
|
78 |
+
print(f"SIMULATING API STATUS CHECK for {job_id} at {status_url}")
|
79 |
+
time.sleep(0.5) # Simulate network latency
|
80 |
+
|
81 |
+
# Simulate different states based on time or random chance
|
82 |
+
# This is highly simplified; a real backend manages state.
|
83 |
+
if job_id not in st.session_state.job_progress:
|
84 |
+
st.session_state.job_progress[job_id] = 0 # Start progress counter
|
85 |
+
|
86 |
+
st.session_state.job_progress[job_id] += random.uniform(0.1, 0.3) # Increment progress
|
87 |
+
|
88 |
+
if st.session_state.job_progress[job_id] < 0.2:
|
89 |
+
status = "PENDING"
|
90 |
+
elif st.session_state.job_progress[job_id] < 0.8:
|
91 |
+
status = "PROCESSING"
|
92 |
+
elif st.session_state.job_progress[job_id] < 1.0:
|
93 |
+
# Simulate occasional failure during processing
|
94 |
+
if random.random() < 0.05: # 5% chance of failure during processing
|
95 |
+
print(f"API Status Simulation: Job {job_id} FAILED during processing.")
|
96 |
+
st.session_state.job_progress[job_id] = 99 # Mark as failed state
|
97 |
+
return "FAILED", None, "Simulated AI Processing Error."
|
98 |
+
else:
|
99 |
+
status = "PROCESSING" # Still processing
|
100 |
+
else:
|
101 |
+
status = "COMPLETED"
|
102 |
+
|
103 |
+
if status == "COMPLETED":
|
104 |
+
# Simulate getting a result URL (e.g., to a generated image in cloud storage)
|
105 |
+
# Use a *real placeholder path* accessible by the Streamlit app for the demo
|
106 |
+
result_placeholder = "assets/staged_result_placeholder.png" # Make sure this exists!
|
107 |
+
print(f"API Status Simulation: Job {job_id} COMPLETED. Result URL (simulated): {result_placeholder}")
|
108 |
+
return "COMPLETED", result_placeholder, None
|
109 |
+
elif status == "FAILED":
|
110 |
+
print(f"API Status Simulation: Job {job_id} FAILED.")
|
111 |
+
# Error message might have been set earlier
|
112 |
+
error = st.session_state.job_errors.get(job_id, "Unknown processing error.")
|
113 |
+
return "FAILED", None, error
|
114 |
+
else:
|
115 |
+
print(f"API Status Simulation: Job {job_id} is {status}.")
|
116 |
+
return status, None, None
|
117 |
+
|
118 |
+
# --- Simulate Fetching Result Image ---
|
119 |
+
def fetch_result_image(image_path_or_url: str) -> Image.Image | None:
|
120 |
+
"""
|
121 |
+
Simulates fetching the result image from a URL or path.
|
122 |
+
In reality, this would use requests.get for a URL.
|
123 |
+
For this demo, we just load from the local placeholder path.
|
124 |
+
"""
|
125 |
+
print(f"SIMULATING Fetching result image from: {image_path_or_url}")
|
126 |
+
if os.path.exists(image_path_or_url):
|
127 |
+
try:
|
128 |
+
img = Image.open(image_path_or_url).convert("RGB")
|
129 |
+
return img
|
130 |
+
except UnidentifiedImageError:
|
131 |
+
print(f"ERROR: Placeholder at '{image_path_or_url}' is not a valid image.")
|
132 |
+
return None
|
133 |
+
except Exception as e:
|
134 |
+
print(f"ERROR: Failed to load placeholder '{image_path_or_url}': {e}")
|
135 |
+
return None
|
136 |
+
else:
|
137 |
+
print(f"ERROR: Placeholder image not found at '{image_path_or_url}'.")
|
138 |
+
return None
|
139 |
|
140 |
# ------------------------------------------------------------------------------
|
141 |
+
# Authentication Simulation
|
142 |
# ------------------------------------------------------------------------------
|
143 |
+
def show_login_form():
|
144 |
+
st.warning("Please log in to use the Visualization Suite.")
|
145 |
+
with st.form("login_form"):
|
146 |
+
username = st.text_input("Username")
|
147 |
+
password = st.text_input("Password", type="password")
|
148 |
+
submitted = st.form_submit_button("Login")
|
149 |
+
if submitted:
|
150 |
+
# --- !!! WARNING: NEVER use hardcoded passwords in production !!! ---
|
151 |
+
# --- This is purely for demonstration. Use secure auth libraries ---
|
152 |
+
# --- like streamlit-authenticator or integrate with OAuth/etc. ---
|
153 |
+
if username == "admin" and password == "password123":
|
154 |
+
st.session_state.logged_in = True
|
155 |
+
st.session_state.username = username
|
156 |
+
st.success("Login successful!")
|
157 |
+
time.sleep(1) # Give user time to see success message
|
158 |
+
st.rerun() # Rerun to show the main app
|
159 |
+
else:
|
160 |
+
st.error("Invalid username or password.")
|
161 |
|
162 |
# ------------------------------------------------------------------------------
|
163 |
+
# Initialize Session State (More Robust)
|
164 |
# ------------------------------------------------------------------------------
|
165 |
+
def initialize_state():
|
166 |
+
defaults = {
|
167 |
+
'logged_in': False,
|
168 |
+
'username': None,
|
169 |
+
'input_data_bytes': None, # Store raw bytes
|
170 |
+
'input_image_preview': None, # Store PIL for display if image
|
171 |
+
'input_type': None,
|
172 |
+
'uploaded_filename': None,
|
173 |
+
'current_job_id': None,
|
174 |
+
'job_status': None, # PENDING, PROCESSING, COMPLETED, FAILED
|
175 |
+
'job_progress': {}, # Dict to track progress simulation per job_id
|
176 |
+
'job_errors': {}, # Dict to store errors per job_id
|
177 |
+
'ai_result_image': None,
|
178 |
+
'last_run_params': {}
|
179 |
+
}
|
180 |
+
for key, value in defaults.items():
|
181 |
+
if key not in st.session_state:
|
182 |
+
st.session_state[key] = value
|
183 |
+
|
184 |
+
initialize_state()
|
185 |
|
186 |
# ------------------------------------------------------------------------------
|
187 |
+
# Main Application Logic
|
188 |
# ------------------------------------------------------------------------------
|
189 |
+
|
190 |
+
# --- Authentication Gate ---
|
191 |
+
if not st.session_state.logged_in:
|
192 |
+
show_login_form()
|
193 |
+
st.stop() # Stop execution if not logged in
|
194 |
+
|
195 |
+
# --- Main App UI (if logged in) ---
|
196 |
+
st.title("🚀 AI Real Estate Visualization Suite [PRO]")
|
197 |
+
st.caption(f"Welcome, {st.session_state.username}! Advanced AI tools at your fingertips.")
|
198 |
st.markdown("---")
|
199 |
|
200 |
# --- Sidebar ---
|
201 |
with st.sidebar:
|
202 |
st.header("⚙️ Input & Configuration")
|
203 |
+
st.caption(f"User: {st.session_state.username}")
|
204 |
+
if st.button("Logout"):
|
205 |
+
for key in list(st.session_state.keys()): # Clear state on logout
|
206 |
+
del st.session_state[key]
|
207 |
+
initialize_state() # Re-init default state
|
208 |
+
st.rerun()
|
209 |
+
|
210 |
+
st.markdown("---")
|
211 |
+
|
212 |
+
# Prevent changing input while a job is running
|
213 |
+
input_disabled = st.session_state.job_status in ["PENDING", "PROCESSING"]
|
214 |
|
215 |
input_file = st.file_uploader(
|
216 |
"1. Upload File",
|
217 |
+
type=["png", "jpg", "jpeg", "webp", "dxf", "dwg", "obj", "fbx"], # Add more as needed
|
218 |
key="file_uploader",
|
219 |
accept_multiple_files=False,
|
220 |
+
help="Upload Room Image, Floor Plan (DXF - simulated), or 3D Model (OBJ - simulated).",
|
221 |
+
disabled=input_disabled
|
222 |
)
|
223 |
|
224 |
# --- Input Processing ---
|
|
|
228 |
file_ext = os.path.splitext(input_file.name)[1].lower()
|
229 |
file_bytes = input_file.getvalue()
|
230 |
processed = False
|
231 |
+
input_image_preview = None
|
232 |
|
233 |
+
try:
|
234 |
+
if file_ext in ['.png', '.jpg', '.jpeg', '.webp']:
|
|
|
235 |
image = Image.open(io.BytesIO(file_bytes)).convert("RGB")
|
236 |
+
max_size = (1024, 1024)
|
237 |
image.thumbnail(max_size, Image.Resampling.LANCZOS)
|
238 |
+
input_image_preview = image # Store PIL for preview
|
239 |
+
input_type = 'image'
|
240 |
processed = True
|
241 |
+
elif file_ext in ['.dxf', '.dwg']:
|
242 |
+
input_type = 'floorplan'
|
243 |
+
processed = True
|
244 |
+
st.warning("Floor plan processing is simulated.")
|
245 |
+
elif file_ext in ['.obj', '.fbx']:
|
246 |
+
input_type = '3dmodel'
|
247 |
+
processed = True
|
248 |
+
st.warning("3D model processing is simulated.")
|
249 |
+
else:
|
250 |
+
st.error(f"Unsupported file type: {file_ext}")
|
251 |
+
|
252 |
+
if processed:
|
253 |
+
st.session_state.input_data_bytes = file_bytes # Store bytes for API
|
254 |
+
st.session_state.input_image_preview = input_image_preview
|
255 |
+
st.session_state.input_type = input_type
|
256 |
+
st.session_state.uploaded_filename = input_file.name
|
257 |
+
# Reset job state
|
258 |
+
st.session_state.current_job_id = None
|
259 |
+
st.session_state.job_status = None
|
260 |
+
st.session_state.ai_result_image = None
|
261 |
+
st.session_state.last_run_params = {}
|
262 |
+
st.success(f"File '{input_file.name}' ({input_type}) loaded.")
|
263 |
+
st.rerun() # Rerun to reflect loaded state
|
264 |
+
else:
|
265 |
+
st.session_state.input_data_bytes = None
|
266 |
+
st.session_state.input_image_preview = None
|
267 |
+
st.session_state.input_type = None
|
268 |
+
st.session_state.uploaded_filename = None
|
269 |
+
|
270 |
+
except UnidentifiedImageError:
|
271 |
+
st.error("Uploaded file is not a valid image or is corrupted.")
|
272 |
+
except Exception as e:
|
273 |
+
st.error(f"Error processing file: {e}")
|
274 |
+
st.session_state.input_data_bytes = None
|
275 |
+
st.session_state.input_image_preview = None
|
276 |
st.session_state.input_type = None
|
277 |
st.session_state.uploaded_filename = None
|
278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
|
280 |
+
# --- Configuration Options ---
|
281 |
+
if st.session_state.input_type is not None:
|
282 |
st.markdown("---")
|
283 |
st.subheader("2. Visualization Mode")
|
284 |
output_mode = st.selectbox(
|
285 |
"Select Mode:",
|
286 |
options=['Virtual Staging', 'Renovation Preview', 'Material Swap', 'Layout Variation'],
|
287 |
key='output_mode_select',
|
288 |
+
disabled=input_disabled
|
289 |
+
).lower().replace(' ', '_')
|
290 |
|
291 |
st.markdown("---")
|
292 |
st.subheader("3. Scene Parameters")
|
293 |
+
room_type = st.selectbox("Room Type:", ["Living Room", "Bedroom", "Kitchen", "Dining Room", "Office", "Bathroom", "Other"], key="room_select", disabled=input_disabled)
|
294 |
+
style = st.selectbox("Primary Style:", ["Modern", "Contemporary", "Minimalist", "Scandinavian", "Industrial", "Traditional", "Coastal", "Farmhouse", "Bohemian"], key="style_select", disabled=input_disabled)
|
295 |
+
|
296 |
+
# Advanced Controls
|
297 |
+
with st.expander("✨ Advanced Controls", expanded=False):
|
298 |
+
furniture_prefs = st.text_input("Furniture Preferences", placeholder="e.g., 'Large velvet green sofa'", key="furniture_input", disabled=input_disabled)
|
299 |
+
wall_color = st.color_picker("Wall Color (Approx.)", value="#FFFFFF", key="wall_color_picker", disabled=input_disabled)
|
300 |
+
floor_type = st.selectbox("Floor Type", ["(Auto)", "Oak Wood", "Dark Wood", "Light Wood", "Carpet (Neutral)", "Concrete", "Tile (Light)", "Tile (Dark)"], key="floor_select", disabled=input_disabled)
|
301 |
+
lighting_time = st.slider("Time of Day", 0.0, 1.0, 0.5, 0.1, "%.1f", key="lighting_slider", help="0.0=Dawn, 0.5=Midday, 1.0=Dusk", disabled=input_disabled)
|
302 |
+
camera_angle = st.selectbox("Camera Angle", ["Eye-Level", "High Angle", "Low Angle", "Wide (Simulated)"], key="camera_select", disabled=input_disabled)
|
303 |
+
remove_objects = st.checkbox("Attempt Remove Existing Objects", value=False, key="remove_obj_check", disabled=input_disabled)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
renovation_instructions = ""
|
305 |
+
if output_mode == 'renovation_preview':
|
306 |
+
renovation_instructions = st.text_input("Renovation Instructions", placeholder="e.g., 'Remove center wall'", key="renovation_input", disabled=input_disabled)
|
|
|
|
|
|
|
|
|
307 |
|
308 |
st.markdown("---")
|
309 |
st.subheader("4. Generate Visualization")
|
310 |
+
|
311 |
+
# Disable button if no input or job already running
|
312 |
+
disable_generate = st.session_state.input_type is None or st.session_state.job_status in ["PENDING", "PROCESSING"]
|
313 |
+
if st.button("🚀 Submit Visualization Job", key="generate_button", use_container_width=True, disabled=disable_generate):
|
314 |
+
# Prepare payload for the backend
|
315 |
+
# In production, you'd likely upload the file to S3 first and send a URL/key
|
316 |
+
# For image data, you might send base64 encoded string or upload separately
|
317 |
+
payload = {
|
318 |
+
"user_id": st.session_state.username, # Identify the user
|
319 |
+
# "input_reference": "s3://bucket/user_uploads/filename.jpg", # Example
|
320 |
+
"input_filename": st.session_state.uploaded_filename, # For info
|
321 |
+
"input_type": st.session_state.input_type,
|
322 |
+
"output_mode": output_mode,
|
323 |
+
"room_type": room_type,
|
324 |
+
"style": style,
|
325 |
+
"furniture_prefs": furniture_prefs,
|
326 |
+
"materials": {'wall_color': wall_color, 'floor_type': floor_type},
|
327 |
+
"lighting_time": lighting_time,
|
328 |
+
"camera_angle": camera_angle,
|
329 |
+
"remove_objects": remove_objects,
|
330 |
+
"renovation_instructions": renovation_instructions,
|
331 |
+
}
|
332 |
+
# Attach input data - simplistic approach for demo, REAL API would handle uploads better
|
333 |
+
# Never send large files directly in JSON payload in production!
|
334 |
+
# payload['input_data_b64'] = base64.b64encode(st.session_state.input_data_bytes).decode()
|
335 |
+
|
336 |
+
job_id, error = submit_visualization_job(payload)
|
337 |
+
|
338 |
+
if job_id:
|
339 |
+
st.session_state.current_job_id = job_id
|
340 |
+
st.session_state.job_status = "PENDING"
|
341 |
+
st.session_state.ai_result_image = None # Clear previous result
|
342 |
+
st.session_state.last_run_params = payload # Store params for display
|
343 |
+
st.session_state.job_progress = {job_id: 0} # Reset progress for new job
|
344 |
+
st.session_state.job_errors = {} # Clear old errors
|
345 |
+
st.success(f"Job submitted successfully! Job ID: {job_id}")
|
346 |
+
st.rerun() # Start polling
|
347 |
+
else:
|
348 |
+
st.error(f"Failed to submit job: {error}")
|
349 |
+
st.session_state.current_job_id = None
|
350 |
+
st.session_state.job_status = "FAILED" # Mark status
|
351 |
|
352 |
else:
|
353 |
st.info("⬆️ Upload a file to begin.")
|
354 |
|
|
|
355 |
# --- Main Display Area ---
|
356 |
col1, col2 = st.columns(2)
|
357 |
|
358 |
with col1:
|
359 |
st.subheader("Input")
|
360 |
+
if st.session_state.input_type is not None:
|
361 |
+
if st.session_state.input_type == 'image' and st.session_state.input_image_preview:
|
362 |
+
st.image(st.session_state.input_image_preview, caption=f"Input: {st.session_state.uploaded_filename}", use_column_width=True)
|
363 |
+
elif st.session_state.input_type != 'image':
|
364 |
+
# Display info for non-image types
|
365 |
+
st.info(f"{st.session_state.input_type.capitalize()} File:\n**{st.session_state.uploaded_filename}**")
|
366 |
+
st.caption("(Preview not available for non-image inputs in this demo)")
|
367 |
else:
|
368 |
+
st.warning("Input loaded, but preview unavailable.")
|
|
|
369 |
else:
|
370 |
+
st.markdown("<div style='height: 400px; border: 2px dashed #ccc; ...'>Upload Input File</div>", unsafe_allow_html=True)
|
371 |
+
|
372 |
|
373 |
with col2:
|
374 |
st.subheader("AI Visualization Result")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
375 |
|
376 |
+
job_id = st.session_state.current_job_id
|
377 |
+
status = st.session_state.job_status
|
378 |
+
|
379 |
+
if job_id:
|
380 |
+
# --- Job Status Display ---
|
381 |
+
if status == "PENDING":
|
382 |
+
st.info(f"Job Status: Pending... (ID: {job_id})")
|
383 |
+
# Trigger periodic rerun for polling
|
384 |
+
time.sleep(5) # Poll every 5 seconds (adjust as needed)
|
385 |
+
st.rerun()
|
386 |
+
elif status == "PROCESSING":
|
387 |
+
progress_value = st.session_state.job_progress.get(job_id, 0)
|
388 |
+
st.progress(min(progress_value, 1.0), text=f"Job Status: Processing... ({int(min(progress_value, 1.0)*100)}%) (ID: {job_id})")
|
389 |
+
# Trigger periodic rerun for polling
|
390 |
+
time.sleep(3) # Poll faster while processing
|
391 |
+
st.rerun()
|
392 |
+
elif status == "COMPLETED":
|
393 |
+
st.success(f"Job Status: Completed! (ID: {job_id})")
|
394 |
+
if st.session_state.ai_result_image:
|
395 |
+
run_mode_display = st.session_state.last_run_params.get('output_mode', 'N/A').replace('_', ' ').title()
|
396 |
+
st.image(st.session_state.ai_result_image, caption=f"Result ({run_mode_display})", use_column_width=True)
|
397 |
+
# Download Button logic (similar to before)
|
398 |
+
# ... (Add download button code here) ...
|
399 |
+
else:
|
400 |
+
st.error("Job completed, but failed to load the result image.")
|
401 |
+
with st.expander("View Parameters Used", expanded=False):
|
402 |
+
# Display JSON but remove potentially large data fields first
|
403 |
+
params_to_display = {k:v for k,v in st.session_state.last_run_params.items()} # if k != 'input_data_b64'}
|
404 |
+
st.json(params_to_display, expanded=True)
|
405 |
+
|
406 |
+
elif status == "FAILED":
|
407 |
+
error_msg = st.session_state.job_errors.get(job_id, "An unknown error occurred.")
|
408 |
+
st.error(f"Job Status: Failed! (ID: {job_id})\nError: {error_msg}")
|
409 |
+
with st.expander("View Parameters Used", expanded=False):
|
410 |
+
params_to_display = {k:v for k,v in st.session_state.last_run_params.items()} # if k != 'input_data_b64'}
|
411 |
+
st.json(params_to_display, expanded=True)
|
412 |
+
|
413 |
+
# --- Logic to update status (if job is active) ---
|
414 |
+
if status in ["PENDING", "PROCESSING"]:
|
415 |
+
new_status, result_url, error = check_job_status(job_id)
|
416 |
+
st.session_state.job_status = new_status
|
417 |
+
if error:
|
418 |
+
st.session_state.job_errors[job_id] = error
|
419 |
+
if new_status == "COMPLETED" and result_url:
|
420 |
+
# Fetch the result image only when completed
|
421 |
+
st.session_state.ai_result_image = fetch_result_image(result_url)
|
422 |
+
if st.session_state.ai_result_image is None:
|
423 |
+
st.session_state.job_status = "FAILED" # Mark as failed if image fetch fails
|
424 |
+
st.session_state.job_errors[job_id] = "Failed to retrieve/load result image."
|
425 |
+
elif new_status == "FAILED" and not error: # If status is failed but no error stored yet
|
426 |
+
st.session_state.job_errors[job_id] = "Job failed for an unknown reason."
|
427 |
+
|
428 |
+
# Rerun immediately if status changed significantly to update UI
|
429 |
+
if status != new_status and new_status in ["COMPLETED", "FAILED"]:
|
430 |
+
st.rerun()
|
431 |
+
|
432 |
+
else: # No active job
|
433 |
+
st.markdown("<div style='height: 400px; border: 2px dashed #ccc; ...'>Result will appear here</div>", unsafe_allow_html=True)
|
434 |
|
435 |
|
436 |
st.markdown("---")
|
437 |
st.warning("""
|
438 |
+
**Disclaimer:** This is a **conceptual blueprint** for a production front-end.
|
439 |
+
User authentication is **not secure**. Backend API calls, job handling, status polling, and AI processing are **simulated**.
|
440 |
+
Building the actual backend infrastructure and state-of-the-art AI models requires significant resources and expertise.
|
441 |
""")
|