Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -59,11 +59,13 @@ class BuildTheFuture:
|
|
| 59 |
"""Load YOLOv11 model for structural detection"""
|
| 60 |
try:
|
| 61 |
from ultralytics import YOLO
|
| 62 |
-
#
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
return True
|
| 65 |
except Exception as e:
|
| 66 |
-
|
| 67 |
return False
|
| 68 |
|
| 69 |
def detect_structures(self, image):
|
|
@@ -90,7 +92,7 @@ class BuildTheFuture:
|
|
| 90 |
return image, f"Detection error: {str(e)}"
|
| 91 |
|
| 92 |
def complete_construction(self, image, style="realistic"):
|
| 93 |
-
"""Complete the construction using Gemini
|
| 94 |
if self.model is None:
|
| 95 |
return image, "Gemini model not available. Please check your API key."
|
| 96 |
|
|
@@ -107,18 +109,29 @@ class BuildTheFuture:
|
|
| 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 |
-
#
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
# Extract the generated image
|
| 114 |
-
if response
|
| 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:
|
|
|
|
| 59 |
"""Load YOLOv11 model for structural detection"""
|
| 60 |
try:
|
| 61 |
from ultralytics import YOLO
|
| 62 |
+
# Prefer local trained model if available, fallback to small public model
|
| 63 |
+
model_path = 'best.pt' if os.path.exists('best.pt') else 'yolov11n.pt'
|
| 64 |
+
self.yolo_model = YOLO(model_path)
|
| 65 |
+
logger.info(f"YOLO model loaded: {model_path}")
|
| 66 |
return True
|
| 67 |
except Exception as e:
|
| 68 |
+
logger.error(f"Error loading YOLO model: {e}")
|
| 69 |
return False
|
| 70 |
|
| 71 |
def detect_structures(self, image):
|
|
|
|
| 92 |
return image, f"Detection error: {str(e)}"
|
| 93 |
|
| 94 |
def complete_construction(self, image, style="realistic"):
|
| 95 |
+
"""Complete the construction using Gemini image generation"""
|
| 96 |
if self.model is None:
|
| 97 |
return image, "Gemini model not available. Please check your API key."
|
| 98 |
|
|
|
|
| 109 |
# Add additional context for better results
|
| 110 |
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."
|
| 111 |
|
| 112 |
+
# Prepare image bytes for Gemini
|
| 113 |
+
buffered = io.BytesIO()
|
| 114 |
+
image.save(buffered, format='PNG')
|
| 115 |
+
image_bytes = buffered.getvalue()
|
| 116 |
+
|
| 117 |
+
# Use a stable Gemini image-capable model
|
| 118 |
+
gemini_image_model = genai.GenerativeModel('gemini-1.5-flash')
|
| 119 |
+
response = gemini_image_model.generate_content([
|
| 120 |
+
enhanced_prompt,
|
| 121 |
+
{
|
| 122 |
+
'mime_type': 'image/png',
|
| 123 |
+
'data': base64.b64encode(image_bytes).decode('utf-8')
|
| 124 |
+
}
|
| 125 |
+
])
|
| 126 |
+
|
| 127 |
# Extract the generated image
|
| 128 |
+
if getattr(response, 'candidates', None):
|
| 129 |
for part in response.candidates[0].content.parts:
|
| 130 |
+
if hasattr(part, 'inline_data') and part.inline_data and getattr(part.inline_data, 'data', None):
|
|
|
|
| 131 |
image_data = base64.b64decode(part.inline_data.data)
|
| 132 |
+
completed_image = Image.open(io.BytesIO(image_data)).convert('RGB')
|
| 133 |
return completed_image, f"Construction completed successfully with {style} style"
|
| 134 |
+
|
| 135 |
return image, "No image generated - please try again"
|
| 136 |
|
| 137 |
except Exception as e:
|