Abs6187 commited on
Commit
e6642ee
·
verified ·
1 Parent(s): 468c880

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +338 -762
app.py CHANGED
@@ -1,762 +1,338 @@
1
- import gradio as gr
2
- import google.generativeai as genai
3
- import cv2
4
- import numpy as np
5
- from PIL import Image, ImageDraw, ImageFont
6
- import os
7
- from dotenv import load_dotenv
8
- import requests
9
- import base64
10
- import io
11
- import json
12
- import time
13
- import logging
14
- from typing import Optional, Tuple, List
15
- import warnings
16
- warnings.filterwarnings("ignore")
17
-
18
- # Configure logging
19
- logging.basicConfig(level=logging.INFO)
20
- logger = logging.getLogger(__name__)
21
-
22
- # Load environment variables
23
- load_dotenv()
24
-
25
- # Configure Gemini API
26
- try:
27
- genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
28
- logger.info("Gemini API configured successfully")
29
- except Exception as e:
30
- logger.warning(f"Gemini API configuration failed: {e}")
31
-
32
- # Configure ElevenLabs API
33
- try:
34
- from elevenlabs import generate, set_api_key
35
- if os.getenv("ELEVENLABS_API_KEY"):
36
- set_api_key(os.getenv("ELEVENLABS_API_KEY"))
37
- logger.info("ElevenLabs API configured successfully")
38
- except ImportError:
39
- logger.warning("ElevenLabs not available - voice features disabled")
40
- except Exception as e:
41
- logger.warning(f"ElevenLabs API configuration failed: {e}")
42
-
43
- class BuildTheFuture:
44
- def __init__(self):
45
- self.model = None
46
- self.yolo_model = None
47
- self._initialize_models()
48
-
49
- def _initialize_models(self):
50
- """Initialize AI models with error handling"""
51
- try:
52
- self.model = genai.GenerativeModel('gemini-2.0-flash-exp')
53
- logger.info("Gemini model initialized successfully")
54
- except Exception as e:
55
- logger.error(f"Failed to initialize Gemini model: {e}")
56
- self.model = None
57
-
58
- def load_yolo_model(self):
59
- """Load YOLOv11 model for structural detection"""
60
- try:
61
- from ultralytics import YOLO
62
- # You can replace this with your custom trained model
63
- self.yolo_model = YOLO('yolov11n.pt')
64
- return True
65
- except Exception as e:
66
- print(f"Error loading YOLO model: {e}")
67
- return False
68
-
69
- def detect_structures(self, image):
70
- """Detect structural elements using YOLOv11"""
71
- if self.yolo_model is None:
72
- if not self.load_yolo_model():
73
- return image, "YOLO model not available"
74
-
75
- try:
76
- # Convert PIL to OpenCV format
77
- img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
78
-
79
- # Run YOLO detection
80
- results = self.yolo_model(img_cv)
81
-
82
- # Draw bounding boxes
83
- annotated_img = results[0].plot()
84
-
85
- # Convert back to PIL
86
- annotated_pil = Image.fromarray(cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB))
87
-
88
- return annotated_pil, "Structures detected successfully"
89
- except Exception as e:
90
- return image, f"Detection error: {str(e)}"
91
-
92
- def complete_construction(self, image, style="realistic"):
93
- """Complete the construction using Gemini 2.5 Flash Image"""
94
- if self.model is None:
95
- return image, "Gemini model not available. Please check your API key."
96
-
97
- try:
98
- # Prepare the prompt based on style
99
- style_prompts = {
100
- "realistic": "Complete this unfinished construction site realistically. Fill in the missing parts with appropriate building materials, proper architectural details, and realistic finishing touches. Make it look like a completed, functional building. Ensure the completion looks natural and follows proper construction practices.",
101
- "futuristic": "Transform this unfinished construction site into a futuristic, high-tech building. Add modern architectural elements, glass facades, smart building features, solar panels, and futuristic design elements. Make it look like a building from the year 2050 with advanced technology integration.",
102
- "artistic": "Complete this construction site with artistic and creative architectural elements. Add unique design features, creative materials, colorful elements, and artistic touches that make it visually striking and memorable. Think outside the box with creative architecture."
103
- }
104
-
105
- prompt = style_prompts.get(style, style_prompts["realistic"])
106
-
107
- # Add additional context for better results
108
- enhanced_prompt = f"{prompt} The image should maintain the same perspective and lighting as the original. Focus on completing the unfinished parts while maintaining architectural coherence."
109
-
110
- # Generate the completed image
111
- response = self.model.generate_content([enhanced_prompt, image])
112
-
113
- # Extract the generated image
114
- if response.candidates and response.candidates[0].content.parts:
115
- for part in response.candidates[0].content.parts:
116
- if hasattr(part, 'inline_data') and part.inline_data:
117
- # Convert base64 to image
118
- image_data = base64.b64decode(part.inline_data.data)
119
- completed_image = Image.open(io.BytesIO(image_data))
120
- return completed_image, f"Construction completed successfully with {style} style"
121
-
122
- return image, "No image generated - please try again"
123
-
124
- except Exception as e:
125
- logger.error(f"Construction completion error: {e}")
126
- return image, f"Completion error: {str(e)}"
127
-
128
- def generate_voice_narration(self, style):
129
- """Generate voice narration using ElevenLabs (optional)"""
130
- if not os.getenv("ELEVENLABS_API_KEY"):
131
- logger.info("ElevenLabs API key not found - voice narration disabled")
132
- return None
133
-
134
- try:
135
- narration_texts = {
136
- "realistic": "Here's how your construction project will look when completed with realistic finishing touches and proper architectural details. The AI has filled in the missing parts with appropriate materials and construction techniques.",
137
- "futuristic": "Behold the future! Your construction site has been transformed into a cutting-edge, high-tech building of tomorrow. Notice the modern architectural elements and smart building features.",
138
- "artistic": "Watch as your construction site becomes a masterpiece of creative architecture and artistic design. The AI has added unique design features and creative elements that make it visually striking."
139
- }
140
-
141
- text = narration_texts.get(style, narration_texts["realistic"])
142
- audio = generate(text=text, voice="Rachel", model="eleven_monolingual_v1")
143
- return audio
144
- except Exception as e:
145
- logger.error(f"Voice generation error: {e}")
146
- return None
147
-
148
- def create_comparison_image(self, original, completed):
149
- """Create a side-by-side comparison image"""
150
- if original is None or completed is None:
151
- return None
152
-
153
- try:
154
- # Resize images to same height
155
- height = min(original.height, completed.height)
156
- width = min(original.width, completed.width)
157
-
158
- original_resized = original.resize((width, height), Image.Resampling.LANCZOS)
159
- completed_resized = completed.resize((width, height), Image.Resampling.LANCZOS)
160
-
161
- # Create comparison image
162
- comparison_width = width * 2 + 20 # Add gap between images
163
- comparison_height = height + 60 # Add space for labels
164
-
165
- comparison_img = Image.new('RGB', (comparison_width, comparison_height), 'white')
166
-
167
- # Paste images
168
- comparison_img.paste(original_resized, (0, 30))
169
- comparison_img.paste(completed_resized, (width + 20, 30))
170
-
171
- # Add labels
172
- draw = ImageDraw.Draw(comparison_img)
173
- try:
174
- font = ImageFont.truetype("arial.ttf", 20)
175
- except:
176
- font = ImageFont.load_default()
177
-
178
- draw.text((width//2 - 50, 5), "BEFORE", fill='black', font=font)
179
- draw.text((width + 20 + width//2 - 50, 5), "AFTER", fill='black', font=font)
180
-
181
- return comparison_img
182
- except Exception as e:
183
- logger.error(f"Comparison image creation error: {e}")
184
- return None
185
-
186
- # Initialize the app
187
- app = BuildTheFuture()
188
-
189
- def process_image(uploaded_image, completion_style):
190
- """Main processing function for the Gradio interface"""
191
- if uploaded_image is None:
192
- return None, None, None, None, "Please upload an image first"
193
-
194
- try:
195
- # Step 1: Detect structures
196
- detected_image, detection_msg = app.detect_structures(uploaded_image)
197
-
198
- # Step 2: Complete construction
199
- completed_image, completion_msg = app.complete_construction(uploaded_image, completion_style)
200
-
201
- # Step 3: Create comparison image
202
- comparison_image = app.create_comparison_image(uploaded_image, completed_image)
203
-
204
- # Step 4: Generate voice narration
205
- audio = app.generate_voice_narration(completion_style)
206
-
207
- status_msg = f"✅ Detection: {detection_msg}\n✅ Completion: {completion_msg}"
208
- if audio is None:
209
- status_msg += "\n💡 Voice narration is optional (ElevenLabs API key not configured)"
210
-
211
- return uploaded_image, detected_image, completed_image, comparison_image, status_msg, audio
212
-
213
- except Exception as e:
214
- logger.error(f"Processing error: {e}")
215
- return uploaded_image, uploaded_image, uploaded_image, None, f"❌ Error: {str(e)}", None
216
-
217
- # Create Gradio interface
218
- with gr.Blocks(title="BuildTheFuture: AI-Powered Construction Completion", theme=gr.themes.Soft()) as demo:
219
- gr.Markdown("""
220
- # 🏗️ BuildTheFuture: AI-Powered Completion of Unfinished Constructions
221
-
222
- Upload a photo of an unfinished construction site and watch AI complete it with realistic, futuristic, or artistic finishes!
223
-
224
- **How it works:**
225
- 1. Upload an image of an unfinished building, road, or bridge
226
- 2. Select your preferred completion style
227
- 3. View the original, detected structures, and AI-completed result
228
- 4. Use the comparison slider to see before/after
229
- """)
230
-
231
- with gr.Row():
232
- with gr.Column(scale=1):
233
- image_input = gr.Image(
234
- label="Upload Unfinished Construction Photo",
235
- type="pil",
236
- height=300
237
- )
238
-
239
- style_selector = gr.Radio(
240
- choices=["realistic", "futuristic", "artistic"],
241
- value="realistic",
242
- label="Completion Style",
243
- info="Choose how you want the construction to be completed"
244
- )
245
-
246
- process_btn = gr.Button("🚀 Complete Construction", variant="primary", size="lg")
247
-
248
- status_text = gr.Textbox(
249
- label="Status",
250
- interactive=False,
251
- lines=3
252
- )
253
-
254
- with gr.Column(scale=2):
255
- with gr.Tabs():
256
- with gr.Tab("Original"):
257
- original_output = gr.Image(label="Original Unfinished Site", height=400)
258
-
259
- with gr.Tab("Detected Structures"):
260
- detected_output = gr.Image(label="YOLO Detection Overlay", height=400)
261
-
262
- with gr.Tab("AI Completed"):
263
- completed_output = gr.Image(label="AI-Completed Construction", height=400)
264
-
265
- with gr.Tab("Before vs After"):
266
- gr.Markdown("### Interactive Comparison")
267
- comparison = gr.Image(
268
- label="Drag slider to compare before and after",
269
- height=400,
270
- show_download_button=True
271
- )
272
-
273
- # Voice narration section
274
- with gr.Row():
275
- audio_output = gr.Audio(
276
- label="Voice Narration",
277
- visible=True
278
- )
279
-
280
- # Add examples with all sample images
281
- with gr.Row():
282
- gr.Markdown("### 📸 Try These Sample Construction Images")
283
- gr.Examples(
284
- examples=[
285
- # Renamed construction samples
286
- ["samples/building_001.jpg", "realistic"],
287
- ["samples/building_001.jpg", "futuristic"],
288
- ["samples/building_001.jpg", "artistic"],
289
- ["samples/building_002.jpg", "realistic"],
290
- ["samples/building_002.jpg", "futuristic"],
291
- ["samples/building_002.jpg", "artistic"],
292
- ["samples/building_003.jpg", "realistic"],
293
- ["samples/building_003.jpg", "futuristic"],
294
- ["samples/building_003.jpg", "artistic"],
295
- ["samples/residential_004.jpg", "realistic"],
296
- ["samples/residential_004.jpg", "futuristic"],
297
- ["samples/residential_004.jpg", "artistic"],
298
- ["samples/residential_005.jpg", "realistic"],
299
- ["samples/residential_005.jpg", "futuristic"],
300
- ["samples/residential_005.jpg", "artistic"],
301
- ["samples/residential_006.jpg", "realistic"],
302
- ["samples/residential_006.jpg", "futuristic"],
303
- ["samples/residential_006.jpg", "artistic"],
304
- ["samples/residential_007.jpg", "realistic"],
305
- ["samples/residential_007.jpg", "futuristic"],
306
- ["samples/residential_007.jpg", "artistic"],
307
- ["samples/residential_008.jpg", "realistic"],
308
- ["samples/residential_008.jpg", "futuristic"],
309
- ["samples/residential_008.jpg", "artistic"],
310
- ["samples/residential_009.jpg", "realistic"],
311
- ["samples/residential_009.jpg", "futuristic"],
312
- ["samples/residential_009.jpg", "artistic"],
313
- ["samples/commercial_010.jpg", "realistic"],
314
- ["samples/commercial_010.jpg", "futuristic"],
315
- ["samples/commercial_010.jpg", "artistic"],
316
- ["samples/commercial_011.jpg", "realistic"],
317
- ["samples/commercial_011.jpg", "futuristic"],
318
- ["samples/commercial_011.jpg", "artistic"],
319
- ["samples/commercial_012.jpg", "realistic"],
320
- ["samples/commercial_012.jpg", "futuristic"],
321
- ["samples/commercial_012.jpg", "artistic"],
322
- ["samples/infrastructure_013.jpg", "realistic"],
323
- ["samples/infrastructure_013.jpg", "futuristic"],
324
- ["samples/infrastructure_013.jpg", "artistic"],
325
- ["samples/infrastructure_014.jpg", "realistic"],
326
- ["samples/infrastructure_014.jpg", "futuristic"],
327
- ["samples/infrastructure_014.jpg", "artistic"],
328
- ["samples/infrastructure_015.jpg", "realistic"],
329
- ["samples/infrastructure_015.jpg", "futuristic"],
330
- ["samples/infrastructure_015.jpg", "artistic"],
331
- ["samples/infrastructure_016.jpg", "realistic"],
332
- ["samples/infrastructure_016.jpg", "futuristic"],
333
- ["samples/infrastructure_016.jpg", "artistic"],
334
- ["samples/infrastructure_017.jpg", "realistic"],
335
- ["samples/infrastructure_017.jpg", "futuristic"],
336
- ["samples/infrastructure_017.jpg", "artistic"],
337
- ["samples/infrastructure_018.jpg", "realistic"],
338
- ["samples/infrastructure_018.jpg", "futuristic"],
339
- ["samples/infrastructure_018.jpg", "artistic"],
340
- ["samples/construction_019.jpg", "realistic"],
341
- ["samples/construction_019.jpg", "futuristic"],
342
- ["samples/construction_019.jpg", "artistic"],
343
- ["samples/construction_020.jpg", "realistic"],
344
- ["samples/construction_020.jpg", "futuristic"],
345
- ["samples/construction_020.jpg", "artistic"],
346
- ["samples/construction_021.jpg", "realistic"],
347
- ["samples/construction_021.jpg", "futuristic"],
348
- ["samples/construction_021.jpg", "artistic"],
349
- ["samples/construction_022.jpg", "realistic"],
350
- ["samples/construction_022.jpg", "futuristic"],
351
- ["samples/construction_022.jpg", "artistic"],
352
- ["samples/construction_023.jpg", "realistic"],
353
- ["samples/construction_023.jpg", "futuristic"],
354
- ["samples/construction_023.jpg", "artistic"],
355
- ["samples/construction_024.jpg", "realistic"],
356
- ["samples/construction_024.jpg", "futuristic"],
357
- ["samples/construction_024.jpg", "artistic"],
358
- ["samples/building_025.jpg", "realistic"],
359
- ["samples/building_025.jpg", "futuristic"],
360
- ["samples/building_025.jpg", "artistic"],
361
- ["samples/building_026.jpg", "realistic"],
362
- ["samples/building_026.jpg", "futuristic"],
363
- ["samples/building_026.jpg", "artistic"],
364
- ["samples/building_027.jpg", "realistic"],
365
- ["samples/building_027.jpg", "futuristic"],
366
- ["samples/building_027.jpg", "artistic"],
367
- ["samples/construction_028.jpg", "realistic"],
368
- ["samples/construction_028.jpg", "futuristic"],
369
- ["samples/construction_028.jpg", "artistic"],
370
- ["samples/construction_029.jpg", "realistic"],
371
- ["samples/construction_029.jpg", "futuristic"],
372
- ["samples/construction_029.jpg", "artistic"],
373
- ["samples/construction_030.jpg", "realistic"],
374
- ["samples/construction_030.jpg", "futuristic"],
375
- ["samples/construction_030.jpg", "artistic"],
376
- ["samples/construction_031.jpg", "realistic"],
377
- ["samples/construction_031.jpg", "futuristic"],
378
- ["samples/construction_031.jpg", "artistic"],
379
- ["samples/construction_032.jpg", "realistic"],
380
- ["samples/construction_032.jpg", "futuristic"],
381
- ["samples/construction_032.jpg", "artistic"],
382
- ["samples/construction_033.jpg", "realistic"],
383
- ["samples/construction_033.jpg", "futuristic"],
384
- ["samples/construction_033.jpg", "artistic"],
385
- ["samples/construction_034.jpg", "realistic"],
386
- ["samples/construction_034.jpg", "futuristic"],
387
- ["samples/construction_034.jpg", "artistic"],
388
- ["samples/construction_035.jpg", "realistic"],
389
- ["samples/construction_035.jpg", "futuristic"],
390
- ["samples/construction_035.jpg", "artistic"],
391
- ["samples/construction_036.jpg", "realistic"],
392
- ["samples/construction_036.jpg", "futuristic"],
393
- ["samples/construction_036.jpg", "artistic"],
394
- ["samples/construction_037.jpg", "realistic"],
395
- ["samples/construction_037.jpg", "futuristic"],
396
- ["samples/construction_037.jpg", "artistic"],
397
- ["samples/construction_038.jpg", "realistic"],
398
- ["samples/construction_038.jpg", "futuristic"],
399
- ["samples/construction_038.jpg", "artistic"],
400
- ["samples/construction_039.jpg", "realistic"],
401
- ["samples/construction_039.jpg", "futuristic"],
402
- ["samples/construction_039.jpg", "artistic"],
403
- ["samples/construction_040.jpg", "realistic"],
404
- ["samples/construction_040.jpg", "futuristic"],
405
- ["samples/construction_040.jpg", "artistic"],
406
- ["samples/construction_041.jpg", "realistic"],
407
- ["samples/construction_041.jpg", "futuristic"],
408
- ["samples/construction_041.jpg", "artistic"],
409
- ["samples/construction_042.jpg", "realistic"],
410
- ["samples/construction_042.jpg", "futuristic"],
411
- ["samples/construction_042.jpg", "artistic"],
412
- ["samples/construction_043.jpg", "realistic"],
413
- ["samples/construction_043.jpg", "futuristic"],
414
- ["samples/construction_043.jpg", "artistic"],
415
- ["samples/construction_044.jpg", "realistic"],
416
- ["samples/construction_044.jpg", "futuristic"],
417
- ["samples/construction_044.jpg", "artistic"],
418
- ["samples/construction_045.jpg", "realistic"],
419
- ["samples/construction_045.jpg", "futuristic"],
420
- ["samples/construction_045.jpg", "artistic"],
421
- ["samples/construction_046.jpg", "realistic"],
422
- ["samples/construction_046.jpg", "futuristic"],
423
- ["samples/construction_046.jpg", "artistic"],
424
- ["samples/construction_047.jpg", "realistic"],
425
- ["samples/construction_047.jpg", "futuristic"],
426
- ["samples/construction_047.jpg", "artistic"],
427
- ["samples/construction_048.jpg", "realistic"],
428
- ["samples/construction_048.jpg", "futuristic"],
429
- ["samples/construction_048.jpg", "artistic"],
430
- ["samples/bridge_049.jpg", "realistic"],
431
- ["samples/bridge_049.jpg", "futuristic"],
432
- ["samples/bridge_049.jpg", "artistic"],
433
- ["samples/bridge_050.jpg", "realistic"],
434
- ["samples/bridge_050.jpg", "futuristic"],
435
- ["samples/bridge_050.jpg", "artistic"],
436
- ["samples/bridge_051.jpg", "realistic"],
437
- ["samples/bridge_051.jpg", "futuristic"],
438
- ["samples/bridge_051.jpg", "artistic"],
439
- ["samples/construction_052.jpg", "realistic"],
440
- ["samples/construction_052.jpg", "futuristic"],
441
- ["samples/construction_052.jpg", "artistic"],
442
- ["samples/construction_053.jpg", "realistic"],
443
- ["samples/construction_053.jpg", "futuristic"],
444
- ["samples/construction_053.jpg", "artistic"],
445
- ["samples/construction_054.jpg", "realistic"],
446
- ["samples/construction_054.jpg", "futuristic"],
447
- ["samples/construction_054.jpg", "artistic"],
448
- ["samples/construction_055.jpg", "realistic"],
449
- ["samples/construction_055.jpg", "futuristic"],
450
- ["samples/construction_055.jpg", "artistic"],
451
- ["samples/construction_056.jpg", "realistic"],
452
- ["samples/construction_056.jpg", "futuristic"],
453
- ["samples/construction_056.jpg", "artistic"],
454
- ["samples/construction_057.jpg", "realistic"],
455
- ["samples/construction_057.jpg", "futuristic"],
456
- ["samples/construction_057.jpg", "artistic"],
457
- ["samples/construction_058.jpg", "realistic"],
458
- ["samples/construction_058.jpg", "futuristic"],
459
- ["samples/construction_058.jpg", "artistic"],
460
- ["samples/construction_059.jpg", "realistic"],
461
- ["samples/construction_059.jpg", "futuristic"],
462
- ["samples/construction_059.jpg", "artistic"],
463
- ["samples/construction_060.jpg", "realistic"],
464
- ["samples/construction_060.jpg", "futuristic"],
465
- ["samples/construction_060.jpg", "artistic"],
466
- ["samples/construction_061.jpg", "realistic"],
467
- ["samples/construction_061.jpg", "futuristic"],
468
- ["samples/construction_061.jpg", "artistic"],
469
- ["samples/construction_062.jpg", "realistic"],
470
- ["samples/construction_062.jpg", "futuristic"],
471
- ["samples/construction_062.jpg", "artistic"],
472
- ["samples/construction_063.jpg", "realistic"],
473
- ["samples/construction_063.jpg", "futuristic"],
474
- ["samples/construction_063.jpg", "artistic"],
475
- ["samples/construction_064.jpg", "realistic"],
476
- ["samples/construction_064.jpg", "futuristic"],
477
- ["samples/construction_064.jpg", "artistic"],
478
- ["samples/construction_065.jpg", "realistic"],
479
- ["samples/construction_065.jpg", "futuristic"],
480
- ["samples/construction_065.jpg", "artistic"],
481
- ["samples/construction_066.jpg", "realistic"],
482
- ["samples/construction_066.jpg", "futuristic"],
483
- ["samples/construction_066.jpg", "artistic"],
484
- ["samples/construction_067.jpg", "realistic"],
485
- ["samples/construction_067.jpg", "futuristic"],
486
- ["samples/construction_067.jpg", "artistic"],
487
- ["samples/construction_068.jpg", "realistic"],
488
- ["samples/construction_068.jpg", "futuristic"],
489
- ["samples/construction_068.jpg", "artistic"],
490
- ["samples/construction_069.jpg", "realistic"],
491
- ["samples/construction_069.jpg", "futuristic"],
492
- ["samples/construction_069.jpg", "artistic"],
493
- ["samples/construction_070.jpg", "realistic"],
494
- ["samples/construction_070.jpg", "futuristic"],
495
- ["samples/construction_070.jpg", "artistic"],
496
- ["samples/construction_071.jpg", "realistic"],
497
- ["samples/construction_071.jpg", "futuristic"],
498
- ["samples/construction_071.jpg", "artistic"],
499
- ["samples/construction_072.jpg", "realistic"],
500
- ["samples/construction_072.jpg", "futuristic"],
501
- ["samples/construction_072.jpg", "artistic"],
502
- ["samples/construction_073.jpg", "realistic"],
503
- ["samples/construction_073.jpg", "futuristic"],
504
- ["samples/construction_073.jpg", "artistic"],
505
- ["samples/construction_074.jpg", "realistic"],
506
- ["samples/construction_074.jpg", "futuristic"],
507
- ["samples/construction_074.jpg", "artistic"],
508
- ["samples/construction_075.jpg", "realistic"],
509
- ["samples/construction_075.jpg", "futuristic"],
510
- ["samples/construction_075.jpg", "artistic"],
511
- ["samples/construction_076.jpg", "realistic"],
512
- ["samples/construction_076.jpg", "futuristic"],
513
- ["samples/construction_076.jpg", "artistic"],
514
- ["samples/construction_077.jpg", "realistic"],
515
- ["samples/construction_077.jpg", "futuristic"],
516
- ["samples/construction_077.jpg", "artistic"],
517
- ["samples/construction_078.jpg", "realistic"],
518
- ["samples/construction_078.jpg", "futuristic"],
519
- ["samples/construction_078.jpg", "artistic"],
520
- ["samples/construction_079.jpg", "realistic"],
521
- ["samples/construction_079.jpg", "futuristic"],
522
- ["samples/construction_079.jpg", "artistic"],
523
- ["samples/construction_080.jpg", "realistic"],
524
- ["samples/construction_080.jpg", "futuristic"],
525
- ["samples/construction_080.jpg", "artistic"],
526
- ["samples/construction_081.jpg", "realistic"],
527
- ["samples/construction_081.jpg", "futuristic"],
528
- ["samples/construction_081.jpg", "artistic"],
529
- ["samples/construction_082.jpg", "realistic"],
530
- ["samples/construction_082.jpg", "futuristic"],
531
- ["samples/construction_082.jpg", "artistic"],
532
- ["samples/construction_083.jpg", "realistic"],
533
- ["samples/construction_083.jpg", "futuristic"],
534
- ["samples/construction_083.jpg", "artistic"],
535
- ["samples/construction_084.jpg", "realistic"],
536
- ["samples/construction_084.jpg", "futuristic"],
537
- ["samples/construction_084.jpg", "artistic"],
538
- ["samples/construction_085.jpg", "realistic"],
539
- ["samples/construction_085.jpg", "futuristic"],
540
- ["samples/construction_085.jpg", "artistic"],
541
- ["samples/construction_086.jpg", "realistic"],
542
- ["samples/construction_086.jpg", "futuristic"],
543
- ["samples/construction_086.jpg", "artistic"],
544
- ["samples/construction_087.jpg", "realistic"],
545
- ["samples/construction_087.jpg", "futuristic"],
546
- ["samples/construction_087.jpg", "artistic"],
547
- ["samples/road_088.jpg", "realistic"],
548
- ["samples/road_088.jpg", "futuristic"],
549
- ["samples/road_088.jpg", "artistic"],
550
- ["samples/road_089.jpg", "realistic"],
551
- ["samples/road_089.jpg", "futuristic"],
552
- ["samples/road_089.jpg", "artistic"],
553
- ["samples/road_090.jpg", "realistic"],
554
- ["samples/road_090.jpg", "futuristic"],
555
- ["samples/road_090.jpg", "artistic"],
556
- ["samples/construction_091.jpg", "realistic"],
557
- ["samples/construction_091.jpg", "futuristic"],
558
- ["samples/construction_091.jpg", "artistic"],
559
- ["samples/construction_092.jpg", "realistic"],
560
- ["samples/construction_092.jpg", "futuristic"],
561
- ["samples/construction_092.jpg", "artistic"],
562
- ["samples/construction_093.jpg", "realistic"],
563
- ["samples/construction_093.jpg", "futuristic"],
564
- ["samples/construction_093.jpg", "artistic"],
565
- ["samples/construction_094.jpg", "realistic"],
566
- ["samples/construction_094.jpg", "futuristic"],
567
- ["samples/construction_094.jpg", "artistic"],
568
- ["samples/construction_095.jpg", "realistic"],
569
- ["samples/construction_095.jpg", "futuristic"],
570
- ["samples/construction_095.jpg", "artistic"],
571
- ["samples/construction_096.jpg", "realistic"],
572
- ["samples/construction_096.jpg", "futuristic"],
573
- ["samples/construction_096.jpg", "artistic"],
574
- ["samples/construction_097.jpg", "realistic"],
575
- ["samples/construction_097.jpg", "futuristic"],
576
- ["samples/construction_097.jpg", "artistic"],
577
- ["samples/construction_098.jpg", "realistic"],
578
- ["samples/construction_098.jpg", "futuristic"],
579
- ["samples/construction_098.jpg", "artistic"],
580
- ["samples/construction_099.jpg", "realistic"],
581
- ["samples/construction_099.jpg", "futuristic"],
582
- ["samples/construction_099.jpg", "artistic"],
583
- ["samples/construction_100.jpg", "realistic"],
584
- ["samples/construction_100.jpg", "futuristic"],
585
- ["samples/construction_100.jpg", "artistic"],
586
- ["samples/construction_101.jpg", "realistic"],
587
- ["samples/construction_101.jpg", "futuristic"],
588
- ["samples/construction_101.jpg", "artistic"],
589
- ["samples/construction_102.jpg", "realistic"],
590
- ["samples/construction_102.jpg", "futuristic"],
591
- ["samples/construction_102.jpg", "artistic"],
592
- ["samples/construction_103.jpg", "realistic"],
593
- ["samples/construction_103.jpg", "futuristic"],
594
- ["samples/construction_103.jpg", "artistic"],
595
- ["samples/construction_104.jpg", "realistic"],
596
- ["samples/construction_104.jpg", "futuristic"],
597
- ["samples/construction_104.jpg", "artistic"],
598
- ["samples/construction_105.jpg", "realistic"],
599
- ["samples/construction_105.jpg", "futuristic"],
600
- ["samples/construction_105.jpg", "artistic"],
601
- ["samples/construction_106.jpg", "realistic"],
602
- ["samples/construction_106.jpg", "futuristic"],
603
- ["samples/construction_106.jpg", "artistic"],
604
- ["samples/construction_107.jpg", "realistic"],
605
- ["samples/construction_107.jpg", "futuristic"],
606
- ["samples/construction_107.jpg", "artistic"],
607
- ["samples/construction_108.jpg", "realistic"],
608
- ["samples/construction_108.jpg", "futuristic"],
609
- ["samples/construction_108.jpg", "artistic"],
610
- ["samples/construction_109.jpg", "realistic"],
611
- ["samples/construction_109.jpg", "futuristic"],
612
- ["samples/construction_109.jpg", "artistic"],
613
- ["samples/construction_110.jpg", "realistic"],
614
- ["samples/construction_110.jpg", "futuristic"],
615
- ["samples/construction_110.jpg", "artistic"],
616
- ["samples/construction_111.jpg", "realistic"],
617
- ["samples/construction_111.jpg", "futuristic"],
618
- ["samples/construction_111.jpg", "artistic"],
619
- ["samples/road_112.jpg", "realistic"],
620
- ["samples/road_112.jpg", "futuristic"],
621
- ["samples/road_112.jpg", "artistic"],
622
- ["samples/road_113.jpg", "realistic"],
623
- ["samples/road_113.jpg", "futuristic"],
624
- ["samples/road_113.jpg", "artistic"],
625
- ["samples/road_114.jpg", "realistic"],
626
- ["samples/road_114.jpg", "futuristic"],
627
- ["samples/road_114.jpg", "artistic"],
628
- ["samples/construction_115.jpg", "realistic"],
629
- ["samples/construction_115.jpg", "futuristic"],
630
- ["samples/construction_115.jpg", "artistic"],
631
- ["samples/construction_116.jpg", "realistic"],
632
- ["samples/construction_116.jpg", "futuristic"],
633
- ["samples/construction_116.jpg", "artistic"],
634
- ["samples/construction_117.jpg", "realistic"],
635
- ["samples/construction_117.jpg", "futuristic"],
636
- ["samples/construction_117.jpg", "artistic"],
637
- ],
638
- ["samples/1--9-_jpg.rf.9644d63e3fac251374ff5bcafcd46df6.jpg", "futuristic"],
639
- ["samples/1--9-_jpg.rf.550c50fd8d264a4635d969a3f6e58e20.jpg", "artistic"],
640
- ["samples/1--87-_jpg.rf.d182208e08a09865edf36470a91b59ee.jpg", "realistic"],
641
- ["samples/1--87-_jpg.rf.2f2c1509d7062a1ca7cf2e7f11524025.jpg", "futuristic"],
642
- ["samples/1--87-_jpg.rf.2573bc211e3ebd7dab8a8e2063f04e72.jpg", "artistic"],
643
- ["samples/1--86-_jpg.rf.d9851354896076145479a3255ad28983.jpg", "realistic"],
644
- ["samples/1--86-_jpg.rf.8743c06fb7171470aad8c29347787e02.jpg", "futuristic"],
645
- ["samples/1--86-_jpg.rf.31c349dab6d93647d16b644b5c01a701.jpg", "artistic"],
646
- ["samples/1--83-_jpg.rf.8eb1985273ed7e729ad48bbaa70b0a2c.jpg", "realistic"],
647
- ["samples/1--83-_jpg.rf.1e63ce7732332e644e18b5fc6a5d5b82.jpg", "futuristic"],
648
- ["samples/1--83-_jpg.rf.161813aa9068d2485cdd8fbf5aa235b6.jpg", "artistic"],
649
- ["samples/1--82-_jpg.rf.ed991a1e1ff5c52aae32d2b184e8879b.jpg", "realistic"],
650
- ["samples/1--82-_jpg.rf.afcf78cc953866c2b426d5d29177945b.jpg", "futuristic"],
651
- ["samples/1--82-_jpg.rf.a125202636d34fbf5f68bf9f169f9b45.jpg", "artistic"],
652
- # Road construction samples
653
- ["samples/1--80-_jpg.rf.d645bf21716e1036496ad924ed79ac6d.jpg", "realistic"],
654
- ["samples/1--80-_jpg.rf.87703d83ea49cb7af1cd234f0421d7da.jpg", "futuristic"],
655
- ["samples/1--80-_jpg.rf.75ef960e488a60fb649b2c6901691cc1.jpg", "artistic"],
656
- ["samples/1--76-_jpg.rf.cbf88ab7105bde653dee9be8c2264c5b.jpg", "realistic"],
657
- ["samples/1--76-_jpg.rf.6798acea648c368e35ef43590a374130.jpg", "futuristic"],
658
- ["samples/1--76-_jpg.rf.4e4e22f8d28e0db71791c0b878d4c2ec.jpg", "artistic"],
659
- # Bridge construction samples
660
- ["samples/1--73-_jpg.rf.b3be7aeb6c77ec6179b486044e900687.jpg", "realistic"],
661
- ["samples/1--73-_jpg.rf.56f7393e7b97bd978b02f550c2a8936b.jpg", "futuristic"],
662
- ["samples/1--73-_jpg.rf.067675d59f006f56190edd5939f6cbfb.jpg", "artistic"],
663
- ["samples/1--7-_jpg.rf.c4bca273346172de9cde286557f61e1d.jpg", "realistic"],
664
- ["samples/1--7-_jpg.rf.3f445c7a434d7d73680221c09300ea61.jpg", "futuristic"],
665
- ["samples/1--7-_jpg.rf.14efa5cd7f0a62e8fb7170a595110d57.jpg", "artistic"],
666
- # Additional construction samples
667
- ["samples/1--68-_jpg.rf.f793eec06843dbcd3712155c0c765e39.jpg", "realistic"],
668
- ["samples/1--68-_jpg.rf.84fafd8af20a11a1daca0749141cdff7.jpg", "futuristic"],
669
- ["samples/1--68-_jpg.rf.0df427eb151c1099ce8c1d56777b06e6.jpg", "artistic"],
670
- ["samples/1--67-_jpg.rf.ddd62d64ddd51eeb07c1de41304cd941.jpg", "realistic"],
671
- ["samples/1--67-_jpg.rf.bc2d043da983274e4d16632cdcd55a1e.jpg", "futuristic"],
672
- ["samples/1--67-_jpg.rf.6f758b1739647e24987418df0e3059fd.jpg", "artistic"],
673
- ["samples/1--65-_jpg.rf.a7039542180728c7cbb8bb5b423f9807.jpg", "realistic"],
674
- ["samples/1--65-_jpg.rf.82fc8f90e8d7536ae6c59f9c03141ef7.jpg", "futuristic"],
675
- ["samples/1--65-_jpg.rf.4a2d32c2552f489771be228141b226c7.jpg", "artistic"],
676
- ["samples/1--64-_jpg.rf.fc7a4fae31efc4a6a63879cb36a39c47.jpg", "realistic"],
677
- ["samples/1--64-_jpg.rf.4af108f9cac6e83adfc416659faf7848.jpg", "futuristic"],
678
- ["samples/1--64-_jpg.rf.2dacf22b827fcf1e2ca4e332c2abf9df.jpg", "artistic"],
679
- ["samples/1--63-_jpg.rf.4ecddff8f7fe664fc147931092790bd7.jpg", "realistic"],
680
- ["samples/1--63-_jpg.rf.37e85abf8d048ef9ce75d47210626718.jpg", "futuristic"],
681
- ["samples/1--63-_jpg.rf.32a6ec79dc25a0b176034fed6d2489d5.jpg", "artistic"],
682
- ["samples/1--62-_jpg.rf.c9c05a5fa96047ad20312ab59825f592.jpg", "realistic"],
683
- ["samples/1--62-_jpg.rf.681ae673b6af7ff87056809c706f4f35.jpg", "futuristic"],
684
- ["samples/1--62-_jpg.rf.34bffd3e20b20147dc9fabac7eb08c2a.jpg", "artistic"],
685
- ["samples/1--61-_jpg.rf.cf3a2daa89d567a47d5355bdba9a0198.jpg", "realistic"],
686
- ["samples/1--61-_jpg.rf.7a107c7913b54770d160d61a90f19f25.jpg", "futuristic"],
687
- ["samples/1--61-_jpg.rf.2bab5b0ba52975e1362a83fa06f71ad2.jpg", "artistic"],
688
- ["samples/1--60-_jpg.rf.e3d64315073b5d9bd72f6f9134dd00b6.jpg", "realistic"],
689
- ["samples/1--60-_jpg.rf.6415acdc3ffcd3dae52acb992308b271.jpg", "futuristic"],
690
- ["samples/1--60-_jpg.rf.34994f98c94dc9b6e092ed8d181e21b5.jpg", "artistic"],
691
- ["samples/1--54-_jpg.rf.ff0c9afdc5e574b293416150bd71da56.jpg", "realistic"],
692
- ["samples/1--54-_jpg.rf.d84d03f1614cd1478ef289b9970a7505.jpg", "futuristic"],
693
- ["samples/1--54-_jpg.rf.40f198aa313d81c2339ba76adec7b632.jpg", "artistic"],
694
- ["samples/1--52-_jpg.rf.f5b8f3320c7742a8563c94ef11ffe685.jpg", "realistic"],
695
- ["samples/1--52-_jpg.rf.f1fccdb171be2239a443a2770ed81cad.jpg", "futuristic"],
696
- ["samples/1--52-_jpg.rf.b433758070b56a1bb117913707164f73.jpg", "artistic"],
697
- ["samples/1--46-_jpg.rf.6d6fc570bf6f6c50fed1b0c1c1dc1f48.jpg", "realistic"],
698
- ["samples/1--46-_jpg.rf.497808af9bd186d92da07e3dd6ff339a.jpg", "futuristic"],
699
- ["samples/1--46-_jpg.rf.2edd9681323a6c6df913efe3ad937904.jpg", "artistic"],
700
- ["samples/1--41-_jpg.rf.ae0be14f17ed018288c6b2b0c21dc252.jpg", "realistic"],
701
- ["samples/1--41-_jpg.rf.2fa10765708519bb42363bbd2668d498.jpg", "futuristic"],
702
- ["samples/1--41-_jpg.rf.292b9eda9b4ab1ea6601d8af9a3fe1e6.jpg", "artistic"],
703
- ["samples/1--4-_jpg.rf.db90e4542575b4320aea6423998b87af.jpg", "realistic"],
704
- ["samples/1--4-_jpg.rf.643b5c3d9bde02cdebfb2c8f2d6760c2.jpg", "futuristic"],
705
- ["samples/1--4-_jpg.rf.0d1c2fe6d5869d80dbca50a038d6d2b8.jpg", "artistic"],
706
- ["samples/1--38-_jpg.rf.7ba6249e6836aef42805430984983714.jpg", "realistic"],
707
- ["samples/1--38-_jpg.rf.558bed32097bb8f0604b9ee9cc01c32c.jpg", "futuristic"],
708
- ["samples/1--38-_jpg.rf.40d2c933f09ee7356bc6f6f184f9077b.jpg", "artistic"],
709
- ["samples/1--37-_jpg.rf.5e1be607b6042a1b9fc508f9d6c711bf.jpg", "realistic"],
710
- ["samples/1--37-_jpg.rf.59de51b1dd967795475dc1c5b7926706.jpg", "futuristic"],
711
- ["samples/1--37-_jpg.rf.4ba07f4c07a3e70e420000f9b1c3dad6.jpg", "artistic"],
712
- ["samples/1--35-_jpg.rf.f3cc69fd984b810bd091efa6a4a16021.jpg", "realistic"],
713
- ["samples/1--35-_jpg.rf.3d52db41936513a4ca9b46341660ed0f.jpg", "futuristic"],
714
- ["samples/1--35-_jpg.rf.144b6d87f8b5a9af9a56e062de67b312.jpg", "artistic"],
715
- ["samples/1--33-_jpg.rf.d14e04d6803e79def00b1e030fa591eb.jpg", "realistic"],
716
- ["samples/1--33-_jpg.rf.8d1f8d5887f8796467a87415e214379f.jpg", "futuristic"],
717
- ["samples/1--33-_jpg.rf.156db5e009b3f61d12ff792f62f857b5.jpg", "artistic"],
718
- ["samples/1--32-_jpg.rf.a3da928423c53a107674eff5e93556db.jpg", "realistic"],
719
- ["samples/1--32-_jpg.rf.0ab6901ce392a13967333e0f229244c6.jpg", "futuristic"],
720
- ["samples/1--32-_jpg.rf.0a88170e18d027822d1e62164e197eff.jpg", "artistic"],
721
- ["samples/1--31-_jpg.rf.98ba9bf87ffdbd8ae22e56b3d7dbb2b7.jpg", "realistic"],
722
- ["samples/1--31-_jpg.rf.3a2df09b1b10348259a032a0a663267e.jpg", "futuristic"],
723
- ["samples/1--31-_jpg.rf.14a871bdca9c22f2aea1c697574175b3.jpg", "artistic"],
724
- ["samples/1--30-_jpg.rf.bd468ebdd124667612002bf0fb487905.jpg", "realistic"],
725
- ["samples/1--30-_jpg.rf.4fafec2b0bdbfb7a2c189fcbc0eb0fdd.jpg", "futuristic"],
726
- ["samples/1--30-_jpg.rf.27581192d14af812295a2bb70dd9b7a4.jpg", "artistic"],
727
- ["samples/1--3-_jpg.rf.c3ef6c743430bfd6e9aac8115389fed3.jpg", "realistic"],
728
- ["samples/1--3-_jpg.rf.c17eee911f46916ce26167de42ea5196.jpg", "futuristic"],
729
- ["samples/1--3-_jpg.rf.0e9c606a0bb6ada5bbc1b2fdb4ec8c9b.jpg", "artistic"],
730
- ["samples/1--27-_jpg.rf.a486cf9e45f8c7accc373780422ae975.jpg", "realistic"],
731
- ["samples/1--27-_jpg.rf.1ac5bbbb7bbf55d72b645979a6403a2e.jpg", "futuristic"],
732
- ["samples/1--27-_jpg.rf.0be78005ac20e89d83e76365676aaf3b.jpg", "artistic"],
733
- ["samples/1--24-_jpg.rf.c0b4a42c7f837b0870930f120efd17fe.jpg", "realistic"],
734
- ["samples/1--24-_jpg.rf.8b40905de0a5235d5ee3bf643c4caa2d.jpg", "futuristic"],
735
- ["samples/1--24-_jpg.rf.8861c49c2ab8560b25dcb5a428697485.jpg", "artistic"],
736
- ["samples/1--17-_jpg.rf.d13676a567fb592569d2290f0270f981.jpg", "realistic"],
737
- ["samples/1--17-_jpg.rf.b2cd1a3e657c0fde9a757c385234729c.jpg", "futuristic"],
738
- ["samples/1--17-_jpg.rf.7928e0354112b2c8901f0de35bf2ac65.jpg", "artistic"],
739
- ["samples/1--16-_jpg.rf.96a61df2434fc94c841c8e8c626eb208.jpg", "realistic"],
740
- # Include the original demo samples as well
741
- ["samples/building_construction.jpg", "realistic"],
742
- ["samples/bridge_construction.jpg", "futuristic"],
743
- ["samples/road_construction.jpg", "artistic"],
744
- ],
745
- inputs=[image_input, style_selector],
746
- label="Sample Construction Images - Click any example to try it!"
747
- )
748
-
749
- # Event handlers
750
- process_btn.click(
751
- fn=process_image,
752
- inputs=[image_input, style_selector],
753
- outputs=[original_output, detected_output, completed_output, comparison, status_text, audio_output]
754
- )
755
-
756
- if __name__ == "__main__":
757
- demo.launch(
758
- server_name="0.0.0.0",
759
- server_port=7860,
760
- share=True,
761
- show_error=True
762
- )
 
1
+ import gradio as gr
2
+ import google.generativeai as genai
3
+ import cv2
4
+ import numpy as np
5
+ from PIL import Image, ImageDraw, ImageFont
6
+ import os
7
+ from dotenv import load_dotenv
8
+ import requests
9
+ import base64
10
+ import io
11
+ import json
12
+ import time
13
+ import logging
14
+ from typing import Optional, Tuple, List
15
+ import warnings
16
+ warnings.filterwarnings("ignore")
17
+
18
+ # Configure logging
19
+ logging.basicConfig(level=logging.INFO)
20
+ logger = logging.getLogger(__name__)
21
+
22
+ # Load environment variables
23
+ load_dotenv()
24
+
25
+ # Configure Gemini API
26
+ try:
27
+ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
28
+ logger.info("Gemini API configured successfully")
29
+ except Exception as e:
30
+ logger.warning(f"Gemini API configuration failed: {e}")
31
+
32
+ # Configure ElevenLabs API
33
+ try:
34
+ from elevenlabs import generate, set_api_key
35
+ if os.getenv("ELEVENLABS_API_KEY"):
36
+ set_api_key(os.getenv("ELEVENLABS_API_KEY"))
37
+ logger.info("ElevenLabs API configured successfully")
38
+ except ImportError:
39
+ logger.warning("ElevenLabs not available - voice features disabled")
40
+ except Exception as e:
41
+ logger.warning(f"ElevenLabs API configuration failed: {e}")
42
+
43
+ class BuildTheFuture:
44
+ def __init__(self):
45
+ self.model = None
46
+ self.yolo_model = None
47
+ self._initialize_models()
48
+
49
+ def _initialize_models(self):
50
+ """Initialize AI models with error handling"""
51
+ try:
52
+ self.model = genai.GenerativeModel('gemini-2.0-flash-exp')
53
+ logger.info("Gemini model initialized successfully")
54
+ except Exception as e:
55
+ logger.error(f"Failed to initialize Gemini model: {e}")
56
+ self.model = None
57
+
58
+ def load_yolo_model(self):
59
+ """Load YOLOv11 model for structural detection"""
60
+ try:
61
+ from ultralytics import YOLO
62
+ # You can replace this with your custom trained model
63
+ self.yolo_model = YOLO('yolov11n.pt')
64
+ return True
65
+ except Exception as e:
66
+ print(f"Error loading YOLO model: {e}")
67
+ return False
68
+
69
+ def detect_structures(self, image):
70
+ """Detect structural elements using YOLOv11"""
71
+ if self.yolo_model is None:
72
+ if not self.load_yolo_model():
73
+ return image, "YOLO model not available"
74
+
75
+ try:
76
+ # Convert PIL to OpenCV format
77
+ img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
78
+
79
+ # Run YOLO detection
80
+ results = self.yolo_model(img_cv)
81
+
82
+ # Draw bounding boxes
83
+ annotated_img = results[0].plot()
84
+
85
+ # Convert back to PIL
86
+ annotated_pil = Image.fromarray(cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB))
87
+
88
+ return annotated_pil, "Structures detected successfully"
89
+ except Exception as e:
90
+ return image, f"Detection error: {str(e)}"
91
+
92
+ def complete_construction(self, image, style="realistic"):
93
+ """Complete the construction using Gemini 2.5 Flash Image"""
94
+ if self.model is None:
95
+ return image, "Gemini model not available. Please check your API key."
96
+
97
+ try:
98
+ # Prepare the prompt based on style
99
+ style_prompts = {
100
+ "realistic": "Complete this unfinished construction site realistically. Fill in the missing parts with appropriate building materials, proper architectural details, and realistic finishing touches. Make it look like a completed, functional building. Ensure the completion looks natural and follows proper construction practices.",
101
+ "futuristic": "Transform this unfinished construction site into a futuristic, high-tech building. Add modern architectural elements, glass facades, smart building features, solar panels, and futuristic design elements. Make it look like a building from the year 2050 with advanced technology integration.",
102
+ "artistic": "Complete this construction site with artistic and creative architectural elements. Add unique design features, creative materials, colorful elements, and artistic touches that make it visually striking and memorable. Think outside the box with creative architecture."
103
+ }
104
+
105
+ prompt = style_prompts.get(style, style_prompts["realistic"])
106
+
107
+ # Add additional context for better results
108
+ enhanced_prompt = f"{prompt} The image should maintain the same perspective and lighting as the original. Focus on completing the unfinished parts while maintaining architectural coherence."
109
+
110
+ # Generate the completed image
111
+ response = self.model.generate_content([enhanced_prompt, image])
112
+
113
+ # Extract the generated image
114
+ if response.candidates and response.candidates[0].content.parts:
115
+ for part in response.candidates[0].content.parts:
116
+ if hasattr(part, 'inline_data') and part.inline_data:
117
+ # Convert base64 to image
118
+ image_data = base64.b64decode(part.inline_data.data)
119
+ completed_image = Image.open(io.BytesIO(image_data))
120
+ return completed_image, f"Construction completed successfully with {style} style"
121
+
122
+ return image, "No image generated - please try again"
123
+
124
+ except Exception as e:
125
+ logger.error(f"Construction completion error: {e}")
126
+ return image, f"Completion error: {str(e)}"
127
+
128
+ def generate_voice_narration(self, style):
129
+ """Generate voice narration using ElevenLabs (optional)"""
130
+ if not os.getenv("ELEVENLABS_API_KEY"):
131
+ logger.info("ElevenLabs API key not found - voice narration disabled")
132
+ return None
133
+
134
+ try:
135
+ narration_texts = {
136
+ "realistic": "Here's how your construction project will look when completed with realistic finishing touches and proper architectural details. The AI has filled in the missing parts with appropriate materials and construction techniques.",
137
+ "futuristic": "Behold the future! Your construction site has been transformed into a cutting-edge, high-tech building of tomorrow. Notice the modern architectural elements and smart building features.",
138
+ "artistic": "Watch as your construction site becomes a masterpiece of creative architecture and artistic design. The AI has added unique design features and creative elements that make it visually striking."
139
+ }
140
+
141
+ text = narration_texts.get(style, narration_texts["realistic"])
142
+ audio = generate(text=text, voice="Rachel", model="eleven_monolingual_v1")
143
+ return audio
144
+ except Exception as e:
145
+ logger.error(f"Voice generation error: {e}")
146
+ return None
147
+
148
+ def create_comparison_image(self, original, completed):
149
+ """Create a side-by-side comparison image"""
150
+ if original is None or completed is None:
151
+ return None
152
+
153
+ try:
154
+ # Resize images to same height
155
+ height = min(original.height, completed.height)
156
+ width = min(original.width, completed.width)
157
+
158
+ original_resized = original.resize((width, height), Image.Resampling.LANCZOS)
159
+ completed_resized = completed.resize((width, height), Image.Resampling.LANCZOS)
160
+
161
+ # Create comparison image
162
+ comparison_width = width * 2 + 20 # Add gap between images
163
+ comparison_height = height + 60 # Add space for labels
164
+
165
+ comparison_img = Image.new('RGB', (comparison_width, comparison_height), 'white')
166
+
167
+ # Paste images
168
+ comparison_img.paste(original_resized, (0, 30))
169
+ comparison_img.paste(completed_resized, (width + 20, 30))
170
+
171
+ # Add labels
172
+ draw = ImageDraw.Draw(comparison_img)
173
+ try:
174
+ font = ImageFont.truetype("arial.ttf", 20)
175
+ except:
176
+ font = ImageFont.load_default()
177
+
178
+ draw.text((width//2 - 50, 5), "BEFORE", fill='black', font=font)
179
+ draw.text((width + 20 + width//2 - 50, 5), "AFTER", fill='black', font=font)
180
+
181
+ return comparison_img
182
+ except Exception as e:
183
+ logger.error(f"Comparison image creation error: {e}")
184
+ return None
185
+
186
+ # Initialize the app
187
+ app = BuildTheFuture()
188
+
189
+ def process_image(uploaded_image, completion_style):
190
+ """Main processing function for the Gradio interface"""
191
+ if uploaded_image is None:
192
+ return None, None, None, None, "Please upload an image first", None
193
+
194
+ try:
195
+ # Step 1: Detect structures
196
+ detected_image, detection_msg = app.detect_structures(uploaded_image)
197
+
198
+ # Step 2: Complete construction
199
+ completed_image, completion_msg = app.complete_construction(uploaded_image, completion_style)
200
+
201
+ # Step 3: Create comparison image
202
+ comparison_image = app.create_comparison_image(uploaded_image, completed_image)
203
+
204
+ # Step 4: Generate voice narration
205
+ audio = app.generate_voice_narration(completion_style)
206
+
207
+ status_msg = f"✅ Detection: {detection_msg}\n✅ Completion: {completion_msg}"
208
+ if audio is None:
209
+ status_msg += "\n💡 Voice narration is optional (ElevenLabs API key not configured)"
210
+
211
+ return uploaded_image, detected_image, completed_image, comparison_image, status_msg, audio
212
+
213
+ except Exception as e:
214
+ logger.error(f"Processing error: {e}")
215
+ return uploaded_image, uploaded_image, uploaded_image, None, f"❌ Error: {str(e)}", None
216
+
217
+ # Create Gradio interface
218
+ with gr.Blocks(title="BuildTheFuture: AI-Powered Construction Completion", theme=gr.themes.Soft()) as demo:
219
+ gr.Markdown("""
220
+ # 🏗️ BuildTheFuture: AI-Powered Completion of Unfinished Constructions
221
+
222
+ Upload a photo of an unfinished construction site and watch AI complete it with realistic, futuristic, or artistic finishes!
223
+
224
+ **How it works:**
225
+ 1. Upload an image of an unfinished building, road, or bridge
226
+ 2. Select your preferred completion style
227
+ 3. View the original, detected structures, and AI-completed result
228
+ 4. Use the comparison slider to see before/after
229
+ """)
230
+
231
+ with gr.Row():
232
+ with gr.Column(scale=1):
233
+ image_input = gr.Image(
234
+ label="Upload Unfinished Construction Photo",
235
+ type="pil",
236
+ height=300
237
+ )
238
+
239
+ style_selector = gr.Radio(
240
+ choices=["realistic", "futuristic", "artistic"],
241
+ value="realistic",
242
+ label="Completion Style",
243
+ info="Choose how you want the construction to be completed"
244
+ )
245
+
246
+ process_btn = gr.Button("🚀 Complete Construction", variant="primary", size="lg")
247
+
248
+ status_text = gr.Textbox(
249
+ label="Status",
250
+ interactive=False,
251
+ lines=3
252
+ )
253
+
254
+ with gr.Column(scale=2):
255
+ with gr.Tabs():
256
+ with gr.Tab("Original"):
257
+ original_output = gr.Image(label="Original Unfinished Site", height=400)
258
+
259
+ with gr.Tab("Detected Structures"):
260
+ detected_output = gr.Image(label="YOLO Detection Overlay", height=400)
261
+
262
+ with gr.Tab("AI Completed"):
263
+ completed_output = gr.Image(label="AI-Completed Construction", height=400)
264
+
265
+ with gr.Tab("Before vs After"):
266
+ gr.Markdown("### Interactive Comparison")
267
+ comparison = gr.Image(
268
+ label="Drag slider to compare before and after",
269
+ height=400,
270
+ show_download_button=True
271
+ )
272
+
273
+ # Voice narration section
274
+ with gr.Row():
275
+ audio_output = gr.Audio(
276
+ label="Voice Narration",
277
+ visible=True
278
+ )
279
+
280
+ # Add examples with sample images
281
+ with gr.Row():
282
+ gr.Markdown("### 📸 Try These Sample Construction Images")
283
+ gr.Examples(
284
+ examples=[
285
+ # Building construction samples
286
+ ["samples/building_001.jpg", "realistic"],
287
+ ["samples/building_002.jpg", "futuristic"],
288
+ ["samples/building_003.jpg", "artistic"],
289
+ # Bridge construction samples
290
+ ["samples/bridge_049.jpg", "realistic"],
291
+ ["samples/bridge_050.jpg", "futuristic"],
292
+ ["samples/bridge_051.jpg", "artistic"],
293
+ # Road construction samples
294
+ ["samples/road_088.jpg", "realistic"],
295
+ ["samples/road_089.jpg", "futuristic"],
296
+ ["samples/road_090.jpg", "artistic"],
297
+ # Residential construction samples
298
+ ["samples/residential_004.jpg", "realistic"],
299
+ ["samples/residential_005.jpg", "futuristic"],
300
+ ["samples/residential_006.jpg", "artistic"],
301
+ # Commercial construction samples
302
+ ["samples/commercial_010.jpg", "realistic"],
303
+ ["samples/commercial_011.jpg", "futuristic"],
304
+ ["samples/commercial_012.jpg", "artistic"],
305
+ # Infrastructure construction samples
306
+ ["samples/infrastructure_013.jpg", "realistic"],
307
+ ["samples/infrastructure_014.jpg", "futuristic"],
308
+ ["samples/infrastructure_015.jpg", "artistic"],
309
+ # General construction samples
310
+ ["samples/construction_019.jpg", "realistic"],
311
+ ["samples/construction_020.jpg", "futuristic"],
312
+ ["samples/construction_021.jpg", "artistic"],
313
+ ["samples/construction_022.jpg", "realistic"],
314
+ ["samples/construction_023.jpg", "futuristic"],
315
+ ["samples/construction_024.jpg", "artistic"],
316
+ # Include the original demo samples as well
317
+ ["samples/building_construction.jpg", "realistic"],
318
+ ["samples/bridge_construction.jpg", "futuristic"],
319
+ ["samples/road_construction.jpg", "artistic"],
320
+ ],
321
+ inputs=[image_input, style_selector],
322
+ label="Sample Construction Images - Click any example to try it!"
323
+ )
324
+
325
+ # Event handlers
326
+ process_btn.click(
327
+ fn=process_image,
328
+ inputs=[image_input, style_selector],
329
+ outputs=[original_output, detected_output, completed_output, comparison, status_text, audio_output]
330
+ )
331
+
332
+ if __name__ == "__main__":
333
+ demo.launch(
334
+ server_name="0.0.0.0",
335
+ server_port=7860,
336
+ share=True,
337
+ show_error=True
338
+ )