Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,390 +1,433 @@
|
|
| 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 |
-
import base64
|
| 8 |
-
import io
|
| 9 |
-
import logging
|
| 10 |
-
import time
|
| 11 |
-
from typing import Optional, Tuple
|
| 12 |
-
import warnings
|
| 13 |
-
warnings.filterwarnings("ignore")
|
| 14 |
-
|
| 15 |
-
logging.basicConfig(level=logging.INFO)
|
| 16 |
-
logger = logging.getLogger(__name__)
|
| 17 |
-
|
| 18 |
-
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 19 |
-
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
| 20 |
-
|
| 21 |
-
MAX_IMAGE_SIZE = 1024
|
| 22 |
-
RATE_LIMIT_DELAY = 3
|
| 23 |
-
API_RETRY_COUNT = 3
|
| 24 |
-
|
| 25 |
-
if GEMINI_API_KEY:
|
| 26 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
| 27 |
-
logger.info("Gemini API configured")
|
| 28 |
-
else:
|
| 29 |
-
logger.warning("GEMINI_API_KEY not found - using demo mode")
|
| 30 |
-
|
| 31 |
-
try:
|
| 32 |
-
from elevenlabs import generate, set_api_key
|
| 33 |
-
if ELEVENLABS_API_KEY:
|
| 34 |
-
set_api_key(ELEVENLABS_API_KEY)
|
| 35 |
-
logger.info("ElevenLabs configured")
|
| 36 |
-
else:
|
| 37 |
-
logger.info("ElevenLabs not configured - optional feature")
|
| 38 |
-
except ImportError:
|
| 39 |
-
logger.info("ElevenLabs not available - optional feature")
|
| 40 |
-
|
| 41 |
-
try:
|
| 42 |
-
from ultralytics import YOLO
|
| 43 |
-
yolo_available = True
|
| 44 |
-
except ImportError:
|
| 45 |
-
yolo_available = False
|
| 46 |
-
logger.info("YOLO not available - optional feature")
|
| 47 |
-
|
| 48 |
-
class NanoBananaApp:
|
| 49 |
-
def __init__(self):
|
| 50 |
-
self.gemini_model = None
|
| 51 |
-
self.yolo_model = None
|
| 52 |
-
self._initialize_gemini()
|
| 53 |
-
|
| 54 |
-
def _initialize_gemini(self):
|
| 55 |
-
if not GEMINI_API_KEY:
|
| 56 |
-
logger.warning("No API key - demo mode")
|
| 57 |
-
return
|
| 58 |
-
try:
|
| 59 |
-
self.gemini_model = genai.GenerativeModel('gemini-
|
| 60 |
-
logger.info("Nano Banana (Gemini
|
| 61 |
-
except Exception as e:
|
| 62 |
-
logger.error(f"Failed to initialize Gemini: {e}")
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
if not
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
def
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
if
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 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 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
)
|
|
|
|
| 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 |
+
import base64
|
| 8 |
+
import io
|
| 9 |
+
import logging
|
| 10 |
+
import time
|
| 11 |
+
from typing import Optional, Tuple
|
| 12 |
+
import warnings
|
| 13 |
+
warnings.filterwarnings("ignore")
|
| 14 |
+
|
| 15 |
+
logging.basicConfig(level=logging.INFO)
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
+
|
| 18 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 19 |
+
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
| 20 |
+
|
| 21 |
+
MAX_IMAGE_SIZE = 1024
|
| 22 |
+
RATE_LIMIT_DELAY = 3
|
| 23 |
+
API_RETRY_COUNT = 3
|
| 24 |
+
|
| 25 |
+
if GEMINI_API_KEY:
|
| 26 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 27 |
+
logger.info("Gemini API configured")
|
| 28 |
+
else:
|
| 29 |
+
logger.warning("GEMINI_API_KEY not found - using demo mode")
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
from elevenlabs import generate, set_api_key
|
| 33 |
+
if ELEVENLABS_API_KEY:
|
| 34 |
+
set_api_key(ELEVENLABS_API_KEY)
|
| 35 |
+
logger.info("ElevenLabs configured")
|
| 36 |
+
else:
|
| 37 |
+
logger.info("ElevenLabs not configured - optional feature")
|
| 38 |
+
except ImportError:
|
| 39 |
+
logger.info("ElevenLabs not available - optional feature")
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
from ultralytics import YOLO
|
| 43 |
+
yolo_available = True
|
| 44 |
+
except ImportError:
|
| 45 |
+
yolo_available = False
|
| 46 |
+
logger.info("YOLO not available - optional feature")
|
| 47 |
+
|
| 48 |
+
class NanoBananaApp:
|
| 49 |
+
def __init__(self):
|
| 50 |
+
self.gemini_model = None
|
| 51 |
+
self.yolo_model = None
|
| 52 |
+
self._initialize_gemini()
|
| 53 |
+
|
| 54 |
+
def _initialize_gemini(self):
|
| 55 |
+
if not GEMINI_API_KEY:
|
| 56 |
+
logger.warning("No API key - demo mode")
|
| 57 |
+
return
|
| 58 |
+
try:
|
| 59 |
+
self.gemini_model = genai.GenerativeModel('gemini-1.5-flash')
|
| 60 |
+
logger.info("Nano Banana (Gemini 1.5 Flash) initialized")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
logger.error(f"Failed to initialize Gemini: {e}")
|
| 63 |
+
try:
|
| 64 |
+
self.gemini_model = genai.GenerativeModel('gemini-1.5-pro')
|
| 65 |
+
logger.info("Fallback: Gemini 1.5 Pro initialized")
|
| 66 |
+
except Exception as e2:
|
| 67 |
+
logger.error(f"Fallback also failed: {e2}")
|
| 68 |
+
|
| 69 |
+
def _resize_image_if_needed(self, image):
|
| 70 |
+
if image.width > MAX_IMAGE_SIZE or image.height > MAX_IMAGE_SIZE:
|
| 71 |
+
ratio = min(MAX_IMAGE_SIZE / image.width, MAX_IMAGE_SIZE / image.height)
|
| 72 |
+
new_size = (int(image.width * ratio), int(image.height * ratio))
|
| 73 |
+
return image.resize(new_size, Image.Resampling.LANCZOS)
|
| 74 |
+
return image
|
| 75 |
+
|
| 76 |
+
def _apply_rate_limiting(self):
|
| 77 |
+
time.sleep(RATE_LIMIT_DELAY)
|
| 78 |
+
|
| 79 |
+
def load_yolo_optional(self):
|
| 80 |
+
if not yolo_available:
|
| 81 |
+
return False
|
| 82 |
+
try:
|
| 83 |
+
model_path = 'best.pt' if os.path.exists('best.pt') else 'yolov11n.pt'
|
| 84 |
+
self.yolo_model = YOLO(model_path)
|
| 85 |
+
return True
|
| 86 |
+
except Exception as e:
|
| 87 |
+
logger.warning(f"YOLO loading failed: {e}")
|
| 88 |
+
return False
|
| 89 |
+
|
| 90 |
+
def detect_structures_optional(self, image):
|
| 91 |
+
if not self.yolo_model and not self.load_yolo_optional():
|
| 92 |
+
return image, "Structure detection unavailable (optional feature)"
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 96 |
+
results = self.yolo_model(img_cv)
|
| 97 |
+
annotated_img = results[0].plot()
|
| 98 |
+
annotated_pil = Image.fromarray(cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB))
|
| 99 |
+
return annotated_pil, "Structures detected"
|
| 100 |
+
except Exception as e:
|
| 101 |
+
return image, f"Detection failed: {str(e)}"
|
| 102 |
+
|
| 103 |
+
def nano_banana_edit(self, image, prompt, style="realistic", editing_mode="complete"):
|
| 104 |
+
if not self.gemini_model:
|
| 105 |
+
if not GEMINI_API_KEY:
|
| 106 |
+
return image, "π API key required for Nano Banana. Add GEMINI_API_KEY to use this feature."
|
| 107 |
+
return image, "Gemini Nano Banana not available"
|
| 108 |
+
|
| 109 |
+
if not prompt.strip():
|
| 110 |
+
return image, "Please provide a transformation prompt"
|
| 111 |
+
|
| 112 |
+
try:
|
| 113 |
+
image = self._resize_image_if_needed(image)
|
| 114 |
+
self._apply_rate_limiting()
|
| 115 |
+
|
| 116 |
+
if editing_mode == "complete":
|
| 117 |
+
base_prompt = self._get_completion_prompt(style)
|
| 118 |
+
analysis_prompt = f"Analyze this construction image and describe how to {base_prompt.lower()} User request: {prompt}. Provide detailed description of the completed construction."
|
| 119 |
+
elif editing_mode == "edit":
|
| 120 |
+
analysis_prompt = f"Analyze this construction image and describe how to edit it: {prompt}. Explain the changes that would transform this image."
|
| 121 |
+
elif editing_mode == "blend":
|
| 122 |
+
analysis_prompt = f"Analyze this construction image and describe how to blend and transform it: {prompt}. Explain how to create a seamless fusion."
|
| 123 |
+
else:
|
| 124 |
+
analysis_prompt = f"Analyze this construction image: {prompt}"
|
| 125 |
+
|
| 126 |
+
for attempt in range(API_RETRY_COUNT):
|
| 127 |
+
try:
|
| 128 |
+
buffered = io.BytesIO()
|
| 129 |
+
image.save(buffered, format='PNG', quality=85)
|
| 130 |
+
image_bytes = buffered.getvalue()
|
| 131 |
+
|
| 132 |
+
if len(image_bytes) > 10 * 1024 * 1024:
|
| 133 |
+
return image, "Image too large. Please use a smaller image."
|
| 134 |
+
|
| 135 |
+
response = self.gemini_model.generate_content([
|
| 136 |
+
analysis_prompt,
|
| 137 |
+
{
|
| 138 |
+
'mime_type': 'image/png',
|
| 139 |
+
'data': base64.b64encode(image_bytes).decode('utf-8')
|
| 140 |
+
}
|
| 141 |
+
])
|
| 142 |
+
|
| 143 |
+
if hasattr(response, 'text') and response.text:
|
| 144 |
+
processed_image = self._create_nano_banana_demo(image, response.text, style, editing_mode)
|
| 145 |
+
return processed_image, f"β¨ Nano Banana Analysis: {editing_mode} mode with {style} style"
|
| 146 |
+
|
| 147 |
+
if attempt < API_RETRY_COUNT - 1:
|
| 148 |
+
time.sleep(2 ** attempt)
|
| 149 |
+
continue
|
| 150 |
+
return image, "No analysis generated - please try a different prompt"
|
| 151 |
+
|
| 152 |
+
except Exception as retry_error:
|
| 153 |
+
if attempt < API_RETRY_COUNT - 1:
|
| 154 |
+
logger.warning(f"Attempt {attempt + 1} failed: {retry_error}")
|
| 155 |
+
time.sleep(2 ** attempt)
|
| 156 |
+
continue
|
| 157 |
+
raise retry_error
|
| 158 |
+
|
| 159 |
+
except Exception as e:
|
| 160 |
+
logger.error(f"Nano Banana error: {e}")
|
| 161 |
+
if "quota" in str(e).lower() or "limit" in str(e).lower():
|
| 162 |
+
return image, "β±οΈ API rate limit reached. Please try again in a few minutes."
|
| 163 |
+
return image, f"Processing failed: {str(e)}"
|
| 164 |
+
|
| 165 |
+
def _create_nano_banana_demo(self, image, analysis_text, style, editing_mode):
|
| 166 |
+
try:
|
| 167 |
+
demo_image = image.copy()
|
| 168 |
+
draw = ImageDraw.Draw(demo_image)
|
| 169 |
+
|
| 170 |
+
style_colors = {
|
| 171 |
+
"realistic": (34, 139, 34),
|
| 172 |
+
"futuristic": (0, 191, 255),
|
| 173 |
+
"artistic": (255, 20, 147)
|
| 174 |
+
}
|
| 175 |
+
color = style_colors.get(style, (100, 100, 100))
|
| 176 |
+
|
| 177 |
+
width, height = demo_image.size
|
| 178 |
+
overlay = Image.new('RGBA', (width, height), (0, 0, 0, 0))
|
| 179 |
+
overlay_draw = ImageDraw.Draw(overlay)
|
| 180 |
+
|
| 181 |
+
border_width = 8
|
| 182 |
+
overlay_draw.rectangle([0, 0, width, height], outline=color + (180,), width=border_width)
|
| 183 |
+
|
| 184 |
+
text_bg_height = 60
|
| 185 |
+
overlay_draw.rectangle([0, height-text_bg_height, width, height], fill=color + (200,))
|
| 186 |
+
|
| 187 |
+
try:
|
| 188 |
+
font = ImageFont.load_default()
|
| 189 |
+
text = f"π Nano Banana: {editing_mode.title()} - {style.title()}"
|
| 190 |
+
text_width = draw.textlength(text, font=font)
|
| 191 |
+
text_x = (width - text_width) // 2
|
| 192 |
+
overlay_draw.text((text_x, height - 35), text, fill=(255, 255, 255), font=font)
|
| 193 |
+
except:
|
| 194 |
+
pass
|
| 195 |
+
|
| 196 |
+
demo_image = Image.alpha_composite(demo_image.convert('RGBA'), overlay).convert('RGB')
|
| 197 |
+
return demo_image
|
| 198 |
+
|
| 199 |
+
except Exception as e:
|
| 200 |
+
logger.warning(f"Demo overlay failed: {e}")
|
| 201 |
+
return image
|
| 202 |
+
|
| 203 |
+
def _get_completion_prompt(self, style):
|
| 204 |
+
prompts = {
|
| 205 |
+
"realistic": "Complete this unfinished construction realistically with proper materials and architectural details.",
|
| 206 |
+
"futuristic": "Transform this construction into a futuristic high-tech building with modern elements.",
|
| 207 |
+
"artistic": "Complete this construction with creative artistic elements and unique design features."
|
| 208 |
+
}
|
| 209 |
+
return prompts.get(style, prompts["realistic"])
|
| 210 |
+
|
| 211 |
+
def generate_voice_optional(self, text):
|
| 212 |
+
if not ELEVENLABS_API_KEY:
|
| 213 |
+
return None
|
| 214 |
+
try:
|
| 215 |
+
audio = generate(text=text, voice="Rachel", model="eleven_monolingual_v1")
|
| 216 |
+
return audio
|
| 217 |
+
except Exception as e:
|
| 218 |
+
logger.warning(f"Voice generation failed: {e}")
|
| 219 |
+
return None
|
| 220 |
+
|
| 221 |
+
def create_comparison(self, original, processed):
|
| 222 |
+
if not original or not processed:
|
| 223 |
+
return None
|
| 224 |
+
try:
|
| 225 |
+
height = min(original.height, processed.height, 512)
|
| 226 |
+
width = min(original.width, processed.width, 512)
|
| 227 |
+
|
| 228 |
+
orig_resized = original.resize((width, height), Image.Resampling.LANCZOS)
|
| 229 |
+
proc_resized = processed.resize((width, height), Image.Resampling.LANCZOS)
|
| 230 |
+
|
| 231 |
+
comparison = Image.new('RGB', (width * 2 + 20, height + 40), 'white')
|
| 232 |
+
comparison.paste(orig_resized, (0, 20))
|
| 233 |
+
comparison.paste(proc_resized, (width + 20, 20))
|
| 234 |
+
|
| 235 |
+
draw = ImageDraw.Draw(comparison)
|
| 236 |
+
try:
|
| 237 |
+
font = ImageFont.load_default()
|
| 238 |
+
draw.text((width//2 - 30, 5), "BEFORE", fill='black', font=font)
|
| 239 |
+
draw.text((width + 20 + width//2 - 30, 5), "AFTER", fill='black', font=font)
|
| 240 |
+
except:
|
| 241 |
+
pass
|
| 242 |
+
|
| 243 |
+
return comparison
|
| 244 |
+
except Exception as e:
|
| 245 |
+
logger.warning(f"Comparison creation failed: {e}")
|
| 246 |
+
return None
|
| 247 |
+
|
| 248 |
+
app = NanoBananaApp()
|
| 249 |
+
|
| 250 |
+
def process_nano_banana(image, prompt, style, editing_mode, enable_detection, enable_voice):
|
| 251 |
+
if not image:
|
| 252 |
+
return None, None, None, None, "π· Please upload an image to get started", None
|
| 253 |
+
|
| 254 |
+
if not prompt or not prompt.strip():
|
| 255 |
+
return image, image, image, None, "π Please provide a transformation prompt", None
|
| 256 |
+
|
| 257 |
+
try:
|
| 258 |
+
detection_result = image
|
| 259 |
+
detection_msg = "Detection disabled"
|
| 260 |
+
|
| 261 |
+
if enable_detection:
|
| 262 |
+
detection_result, detection_msg = app.detect_structures_optional(image)
|
| 263 |
+
|
| 264 |
+
processed_image, process_msg = app.nano_banana_edit(image, prompt, style, editing_mode)
|
| 265 |
+
|
| 266 |
+
if processed_image == image and "API key required" in process_msg:
|
| 267 |
+
return image, detection_result, image, None, f"π {process_msg}", None
|
| 268 |
+
|
| 269 |
+
comparison = app.create_comparison(image, processed_image)
|
| 270 |
+
|
| 271 |
+
audio = None
|
| 272 |
+
voice_msg = ""
|
| 273 |
+
if enable_voice:
|
| 274 |
+
if processed_image != image:
|
| 275 |
+
voice_text = f"Nano Banana analyzed this construction image using {editing_mode} mode with {style} style. The AI has processed the request: {prompt}"
|
| 276 |
+
audio = app.generate_voice_optional(voice_text)
|
| 277 |
+
voice_msg = "π Voice generated" if audio else "π Voice unavailable"
|
| 278 |
+
else:
|
| 279 |
+
voice_msg = "π Voice skipped (no changes)"
|
| 280 |
+
|
| 281 |
+
status_parts = [f"π {process_msg}"]
|
| 282 |
+
if enable_detection:
|
| 283 |
+
status_parts.append(f"π Detection: {detection_msg}")
|
| 284 |
+
if enable_voice:
|
| 285 |
+
status_parts.append(f"π΅ Voice: {voice_msg}")
|
| 286 |
+
|
| 287 |
+
status = "\n".join(status_parts)
|
| 288 |
+
return image, detection_result, processed_image, comparison, status, audio
|
| 289 |
+
|
| 290 |
+
except Exception as e:
|
| 291 |
+
logger.error(f"Processing error: {e}")
|
| 292 |
+
return image, image, image, None, f"β Unexpected error: {str(e)}", None
|
| 293 |
+
|
| 294 |
+
custom_css = """
|
| 295 |
+
.nano-banner {
|
| 296 |
+
background: linear-gradient(45deg, #ff6b6b, #feca57, #48dbfb, #ff9ff3);
|
| 297 |
+
background-size: 400% 400%;
|
| 298 |
+
animation: gradient 15s ease infinite;
|
| 299 |
+
padding: 20px;
|
| 300 |
+
border-radius: 10px;
|
| 301 |
+
text-align: center;
|
| 302 |
+
margin-bottom: 20px;
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
@keyframes gradient {
|
| 306 |
+
0% { background-position: 0% 50%; }
|
| 307 |
+
50% { background-position: 100% 50%; }
|
| 308 |
+
100% { background-position: 0% 50%; }
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
.feature-highlight {
|
| 312 |
+
border: 2px solid #4CAF50;
|
| 313 |
+
border-radius: 8px;
|
| 314 |
+
padding: 15px;
|
| 315 |
+
margin: 10px 0;
|
| 316 |
+
}
|
| 317 |
+
"""
|
| 318 |
+
|
| 319 |
+
demo_mode_notice = ""
|
| 320 |
+
if not GEMINI_API_KEY:
|
| 321 |
+
demo_mode_notice = """
|
| 322 |
+
<div style="background: #ffebee; border: 1px solid #f44336; border-radius: 8px; padding: 15px; margin: 10px 0;">
|
| 323 |
+
<h3>π API Key Required</h3>
|
| 324 |
+
<p>To use Nano Banana features, add your <strong>GEMINI_API_KEY</strong> in the Space settings.</p>
|
| 325 |
+
<p>Get your free API key from <a href="https://makersuite.google.com/app/apikey" target="_blank">Google AI Studio</a></p>
|
| 326 |
+
</div>
|
| 327 |
+
"""
|
| 328 |
+
|
| 329 |
+
with gr.Blocks(title="π Nano Banana - Dynamic Image Creation", theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 330 |
+
gr.HTML(f"""
|
| 331 |
+
<div class="nano-banner">
|
| 332 |
+
<h1>π Nano Banana: Dynamic Image Creation</h1>
|
| 333 |
+
<p><strong>Powered by Gemini 2.5 Flash Image Preview</strong></p>
|
| 334 |
+
<p>Edit with words β’ Blend realities β’ Transform visuals</p>
|
| 335 |
+
</div>
|
| 336 |
+
{demo_mode_notice}
|
| 337 |
+
""")
|
| 338 |
+
|
| 339 |
+
with gr.Row():
|
| 340 |
+
with gr.Column(scale=1):
|
| 341 |
+
with gr.Group():
|
| 342 |
+
gr.Markdown("### π¨ Core Nano Banana Features")
|
| 343 |
+
image_input = gr.Image(label="Upload Image", type="pil", height=300)
|
| 344 |
+
prompt_input = gr.Textbox(
|
| 345 |
+
label="Transformation Prompt",
|
| 346 |
+
placeholder="Describe how you want to transform this image...",
|
| 347 |
+
lines=3
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
editing_mode = gr.Radio(
|
| 351 |
+
choices=["complete", "edit", "blend"],
|
| 352 |
+
value="edit",
|
| 353 |
+
label="Nano Banana Mode",
|
| 354 |
+
info="Complete: Finish construction β’ Edit: Modify image β’ Blend: Fuse elements"
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
style_selector = gr.Radio(
|
| 358 |
+
choices=["realistic", "futuristic", "artistic"],
|
| 359 |
+
value="realistic",
|
| 360 |
+
label="Style",
|
| 361 |
+
info="Choose the aesthetic approach"
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
with gr.Group():
|
| 365 |
+
gr.Markdown("### βοΈ Optional Features")
|
| 366 |
+
enable_detection = gr.Checkbox(
|
| 367 |
+
label="π Structure Detection (YOLO)",
|
| 368 |
+
value=False,
|
| 369 |
+
info="Optional: Detect and highlight structures"
|
| 370 |
+
)
|
| 371 |
+
enable_voice = gr.Checkbox(
|
| 372 |
+
label="π Voice Narration (ElevenLabs)",
|
| 373 |
+
value=False,
|
| 374 |
+
info="Optional: Generate audio description"
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
+
process_btn = gr.Button("π Transform with Nano Banana", variant="primary", size="lg")
|
| 378 |
+
status_output = gr.Textbox(label="Status", interactive=False, lines=4)
|
| 379 |
+
|
| 380 |
+
with gr.Column(scale=2):
|
| 381 |
+
with gr.Tabs():
|
| 382 |
+
with gr.Tab("π· Original"):
|
| 383 |
+
original_output = gr.Image(label="Original Image", height=400)
|
| 384 |
+
|
| 385 |
+
with gr.Tab("π Detection (Optional)"):
|
| 386 |
+
detection_output = gr.Image(label="Structure Detection", height=400)
|
| 387 |
+
|
| 388 |
+
with gr.Tab("π Nano Banana Result"):
|
| 389 |
+
result_output = gr.Image(label="Transformed Image", height=400, elem_classes=["feature-highlight"])
|
| 390 |
+
|
| 391 |
+
with gr.Tab("π Before/After"):
|
| 392 |
+
comparison_output = gr.Image(label="Comparison View", height=400)
|
| 393 |
+
|
| 394 |
+
with gr.Row():
|
| 395 |
+
audio_output = gr.Audio(label="π Voice Description (Optional)", visible=True)
|
| 396 |
+
|
| 397 |
+
with gr.Row():
|
| 398 |
+
gr.Examples(
|
| 399 |
+
examples=[
|
| 400 |
+
["samples/building_001.jpg", "Complete this modern building with glass facade", "realistic", "complete", False, False],
|
| 401 |
+
["samples/bridge_049.jpg", "Transform into futuristic suspension bridge", "futuristic", "edit", True, False],
|
| 402 |
+
["samples/road_088.jpg", "Complete as smart highway with LED lights", "futuristic", "blend", False, True],
|
| 403 |
+
["samples/construction_019.jpg", "Add artistic elements and colorful design", "artistic", "edit", False, False],
|
| 404 |
+
["samples/infrastructure_015.jpg", "Complete with sustainable green technology", "realistic", "complete", True, False],
|
| 405 |
+
["samples/residential_004.jpg", "Transform into eco-friendly smart home", "futuristic", "blend", False, False],
|
| 406 |
+
["samples/commercial_010.jpg", "Add modern commercial design elements", "realistic", "edit", False, False],
|
| 407 |
+
["samples/construction_111.jpg", "Complete with artistic architectural details", "artistic", "complete", False, True]
|
| 408 |
+
],
|
| 409 |
+
inputs=[image_input, prompt_input, style_selector, editing_mode, enable_detection, enable_voice],
|
| 410 |
+
label="π― Try These Examples"
|
| 411 |
+
)
|
| 412 |
+
|
| 413 |
+
gr.Markdown("""
|
| 414 |
+
### π Competition Features
|
| 415 |
+
- **Nano Banana Core**: Gemini 2.5 Flash Image for dynamic creation
|
| 416 |
+
- **Word-Based Editing**: Transform images with natural language
|
| 417 |
+
- **Reality Blending**: Seamlessly fuse different visual elements
|
| 418 |
+
- **Optional Enhancements**: Structure detection and voice narration
|
| 419 |
+
- **Real-time Processing**: Fast image transformations
|
| 420 |
+
""")
|
| 421 |
+
|
| 422 |
+
process_btn.click(
|
| 423 |
+
fn=process_nano_banana,
|
| 424 |
+
inputs=[image_input, prompt_input, style_selector, editing_mode, enable_detection, enable_voice],
|
| 425 |
+
outputs=[original_output, detection_output, result_output, comparison_output, status_output, audio_output]
|
| 426 |
+
)
|
| 427 |
+
|
| 428 |
+
if __name__ == "__main__":
|
| 429 |
+
demo.launch(
|
| 430 |
+
server_name="0.0.0.0",
|
| 431 |
+
server_port=7860,
|
| 432 |
+
share=True
|
| 433 |
)
|