Update app.py
Browse files
app.py
CHANGED
|
@@ -22,9 +22,9 @@ from lzstring import LZString
|
|
| 22 |
# =========================
|
| 23 |
NEBIUS_BASE_URL = "https://api.studio.nebius.com/v1/"
|
| 24 |
|
| 25 |
-
# Real-time tracking
|
| 26 |
CURRENT_USER = "samsnata"
|
| 27 |
-
CURRENT_DATETIME = "2025-08-21 08:
|
| 28 |
|
| 29 |
def get_current_time():
|
| 30 |
return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
|
|
@@ -34,14 +34,14 @@ UNSPLASH_ACCESS_KEY = os.getenv("UNSPLASH_ACCESS_KEY", "")
|
|
| 34 |
UNSPLASH_API_URL = "https://api.unsplash.com"
|
| 35 |
|
| 36 |
# FASTEST Vision Model - Only use the 7B for speed
|
| 37 |
-
DEFAULT_VISION_MODEL = "Qwen/Qwen2.5-VL-7B-Instruct" #
|
| 38 |
VISION_MODELS = [
|
| 39 |
-
"Qwen/Qwen2.5-VL-7B-Instruct", # FASTEST
|
| 40 |
-
"Qwen/Qwen2.5-VL-72B-Instruct", #
|
| 41 |
]
|
| 42 |
|
| 43 |
# FASTEST Code Models - Prioritized by speed
|
| 44 |
-
DEFAULT_CODE_MODEL = "mistralai/Mistral-7B-Instruct-v0.3" # FASTEST
|
| 45 |
|
| 46 |
# Speed-optimized model list
|
| 47 |
FAST_CODE_MODELS = [
|
|
@@ -56,75 +56,65 @@ FAST_CODE_MODELS = [
|
|
| 56 |
# TIER 3: BALANCED (30-60 seconds)
|
| 57 |
"meta-llama/Meta-Llama-3.1-70B-Instruct", # Good quality - 30-45s
|
| 58 |
"mistralai/Mistral-Nemo-Instruct-2407", # Optimized - 25-40s
|
| 59 |
-
|
| 60 |
-
# TIER 4: SLOWER (60+ seconds) - Only as fallback
|
| 61 |
-
"Qwen/Qwen2.5-72B-Instruct", # Fallback - 45-60s
|
| 62 |
-
"mistralai/Mixtral-8x22B-Instruct-v0.1", # Large - 60-90s
|
| 63 |
]
|
| 64 |
|
| 65 |
# ULTRA-OPTIMIZED Model Configurations
|
| 66 |
SPEED_OPTIMIZED_CONFIGS = {
|
| 67 |
# FASTEST MODELS - Aggressive optimization
|
| 68 |
"mistralai/Mistral-7B-Instruct-v0.3": {
|
| 69 |
-
"max_tokens":
|
| 70 |
"temperature": 0.7,
|
| 71 |
-
"timeout_read":
|
| 72 |
"timeout_connect": 5.0,
|
| 73 |
-
"retry_count":
|
| 74 |
-
"stream": True, # Enable streaming for perceived speed
|
| 75 |
"speed_tier": 1,
|
| 76 |
"estimated_time": "5-10 seconds"
|
| 77 |
},
|
| 78 |
"meta-llama/Meta-Llama-3.1-8B-Instruct": {
|
| 79 |
-
"max_tokens":
|
| 80 |
"temperature": 0.7,
|
| 81 |
-
"timeout_read":
|
| 82 |
"timeout_connect": 5.0,
|
| 83 |
-
"retry_count":
|
| 84 |
-
"stream": True,
|
| 85 |
"speed_tier": 1,
|
| 86 |
"estimated_time": "8-15 seconds"
|
| 87 |
},
|
| 88 |
"mistralai/Mixtral-8x7B-Instruct-v0.1": {
|
| 89 |
-
"max_tokens":
|
| 90 |
"temperature": 0.7,
|
| 91 |
-
"timeout_read":
|
| 92 |
"timeout_connect": 7.0,
|
| 93 |
"retry_count": 1,
|
| 94 |
-
"stream": True,
|
| 95 |
"speed_tier": 2,
|
| 96 |
"estimated_time": "15-25 seconds"
|
| 97 |
},
|
| 98 |
"meta-llama/Meta-Llama-3.1-70B-Instruct": {
|
| 99 |
-
"max_tokens":
|
| 100 |
"temperature": 0.7,
|
| 101 |
"timeout_read": 60.0,
|
| 102 |
"timeout_connect": 10.0,
|
| 103 |
"retry_count": 1,
|
| 104 |
-
"stream": False,
|
| 105 |
"speed_tier": 3,
|
| 106 |
"estimated_time": "30-45 seconds"
|
| 107 |
},
|
| 108 |
# Vision models - optimized
|
| 109 |
"Qwen/Qwen2.5-VL-7B-Instruct": {
|
| 110 |
-
"max_tokens":
|
| 111 |
"temperature": 0.7,
|
| 112 |
-
"timeout_read":
|
| 113 |
"timeout_connect": 5.0,
|
| 114 |
-
"retry_count":
|
| 115 |
-
"stream": False,
|
| 116 |
"speed_tier": 1,
|
| 117 |
-
"estimated_time": "
|
| 118 |
},
|
| 119 |
"Qwen/Qwen2.5-VL-72B-Instruct": {
|
| 120 |
"max_tokens": 1500,
|
| 121 |
"temperature": 0.7,
|
| 122 |
-
"timeout_read":
|
| 123 |
"timeout_connect": 10.0,
|
| 124 |
-
"retry_count":
|
| 125 |
-
"stream": False,
|
| 126 |
"speed_tier": 3,
|
| 127 |
-
"estimated_time": "30
|
| 128 |
}
|
| 129 |
}
|
| 130 |
|
|
@@ -134,21 +124,32 @@ DEFAULT_NEBIUS_API_KEY = (
|
|
| 134 |
)
|
| 135 |
|
| 136 |
# =========================
|
| 137 |
-
# CACHE AND CONNECTION POOLING
|
| 138 |
# =========================
|
| 139 |
|
| 140 |
# Global connection pool for reuse
|
| 141 |
_connection_pool = None
|
|
|
|
| 142 |
|
| 143 |
def get_connection_pool():
|
| 144 |
-
"""Get or create a connection pool for HTTP requests."""
|
| 145 |
-
global _connection_pool
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
return _connection_pool
|
| 153 |
|
| 154 |
# Cache for model configs
|
|
@@ -156,14 +157,13 @@ def get_connection_pool():
|
|
| 156 |
def get_model_config(model: str) -> Dict[str, Any]:
|
| 157 |
"""Get cached model configuration."""
|
| 158 |
default = {
|
| 159 |
-
"max_tokens":
|
| 160 |
"temperature": 0.7,
|
| 161 |
-
"timeout_read":
|
| 162 |
"timeout_connect": 8.0,
|
| 163 |
-
"retry_count":
|
| 164 |
-
"stream": False,
|
| 165 |
"speed_tier": 2,
|
| 166 |
-
"estimated_time": "
|
| 167 |
}
|
| 168 |
return SPEED_OPTIMIZED_CONFIGS.get(model, default)
|
| 169 |
|
|
@@ -171,75 +171,13 @@ def get_model_config(model: str) -> Dict[str, Any]:
|
|
| 171 |
# ULTRA-FAST API CALLS
|
| 172 |
# =========================
|
| 173 |
|
| 174 |
-
async def call_nebius_api_async(
|
| 175 |
-
model: str,
|
| 176 |
-
messages: list,
|
| 177 |
-
api_key: str,
|
| 178 |
-
max_tokens: Optional[int] = None,
|
| 179 |
-
temperature: Optional[float] = None
|
| 180 |
-
) -> str:
|
| 181 |
-
"""Async API call for maximum speed."""
|
| 182 |
-
|
| 183 |
-
config = get_model_config(model)
|
| 184 |
-
actual_max_tokens = min(max_tokens or config["max_tokens"], config["max_tokens"])
|
| 185 |
-
actual_temperature = temperature or config["temperature"]
|
| 186 |
-
|
| 187 |
-
headers = {
|
| 188 |
-
"Authorization": f"Bearer {api_key}",
|
| 189 |
-
"Content-Type": "application/json"
|
| 190 |
-
}
|
| 191 |
-
|
| 192 |
-
payload = {
|
| 193 |
-
"model": model,
|
| 194 |
-
"messages": messages,
|
| 195 |
-
"max_tokens": actual_max_tokens,
|
| 196 |
-
"temperature": actual_temperature,
|
| 197 |
-
"stream": False # Disable streaming for simplicity
|
| 198 |
-
}
|
| 199 |
-
|
| 200 |
-
url = f"{NEBIUS_BASE_URL}chat/completions"
|
| 201 |
-
|
| 202 |
-
async with httpx.AsyncClient(
|
| 203 |
-
timeout=httpx.Timeout(config["timeout_read"], connect=config["timeout_connect"]),
|
| 204 |
-
http2=True
|
| 205 |
-
) as client:
|
| 206 |
-
response = await client.post(url, headers=headers, json=payload)
|
| 207 |
-
|
| 208 |
-
if response.status_code == 200:
|
| 209 |
-
data = response.json()
|
| 210 |
-
choices = data.get("choices", [])
|
| 211 |
-
if choices:
|
| 212 |
-
return choices[0].get("message", {}).get("content", "")
|
| 213 |
-
|
| 214 |
-
raise Exception(f"API error: {response.status_code}")
|
| 215 |
-
|
| 216 |
-
def call_nebius_api_fast(
|
| 217 |
-
model: str,
|
| 218 |
-
messages: list,
|
| 219 |
-
api_key: str,
|
| 220 |
-
max_tokens: Optional[int] = None,
|
| 221 |
-
temperature: Optional[float] = None
|
| 222 |
-
) -> str:
|
| 223 |
-
"""Synchronous wrapper for async API call."""
|
| 224 |
-
try:
|
| 225 |
-
# Run async function in sync context
|
| 226 |
-
loop = asyncio.new_event_loop()
|
| 227 |
-
asyncio.set_event_loop(loop)
|
| 228 |
-
result = loop.run_until_complete(
|
| 229 |
-
call_nebius_api_async(model, messages, api_key, max_tokens, temperature)
|
| 230 |
-
)
|
| 231 |
-
loop.close()
|
| 232 |
-
return result
|
| 233 |
-
except Exception as e:
|
| 234 |
-
# Fallback to sync call
|
| 235 |
-
return call_nebius_api_sync_fast(model, messages, api_key, max_tokens, temperature)
|
| 236 |
-
|
| 237 |
def call_nebius_api_sync_fast(
|
| 238 |
model: str,
|
| 239 |
messages: list,
|
| 240 |
api_key: str,
|
| 241 |
max_tokens: Optional[int] = None,
|
| 242 |
-
temperature: Optional[float] = None
|
|
|
|
| 243 |
) -> str:
|
| 244 |
"""Ultra-fast synchronous API call with minimal overhead."""
|
| 245 |
|
|
@@ -249,11 +187,15 @@ def call_nebius_api_sync_fast(
|
|
| 249 |
config = get_model_config(model)
|
| 250 |
|
| 251 |
# Use minimal tokens for speed
|
| 252 |
-
actual_max_tokens = min(
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
headers = {
|
| 255 |
"Authorization": f"Bearer {api_key}",
|
| 256 |
-
"Content-Type": "application/json"
|
|
|
|
| 257 |
}
|
| 258 |
|
| 259 |
payload = {
|
|
@@ -261,60 +203,104 @@ def call_nebius_api_sync_fast(
|
|
| 261 |
"messages": messages,
|
| 262 |
"max_tokens": actual_max_tokens,
|
| 263 |
"temperature": temperature or config["temperature"],
|
| 264 |
-
"stream": False
|
|
|
|
|
|
|
|
|
|
| 265 |
}
|
| 266 |
|
| 267 |
url = f"{NEBIUS_BASE_URL}chat/completions"
|
| 268 |
|
| 269 |
-
#
|
| 270 |
start_time = time.time()
|
| 271 |
print(f"[{get_current_time()}] {CURRENT_USER} calling {model} (Tier {config.get('speed_tier', 'N/A')})")
|
| 272 |
|
| 273 |
try:
|
| 274 |
-
# Use connection pool
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
choices = data.get("choices", [])
|
| 289 |
-
if choices:
|
| 290 |
-
return choices[0].get("message", {}).get("content", "")
|
| 291 |
|
| 292 |
-
#
|
| 293 |
-
|
| 294 |
-
|
| 295 |
payload["model"] = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 296 |
-
payload["max_tokens"] =
|
| 297 |
-
|
|
|
|
| 298 |
if response.status_code == 200:
|
| 299 |
data = response.json()
|
| 300 |
choices = data.get("choices", [])
|
| 301 |
if choices:
|
| 302 |
return choices[0].get("message", {}).get("content", "")
|
|
|
|
|
|
|
| 303 |
|
| 304 |
-
raise Exception(f"API error: {response.status_code}")
|
| 305 |
-
|
| 306 |
-
except httpx.TimeoutException:
|
| 307 |
-
# On timeout, immediately try fastest model
|
| 308 |
-
print(f" Timeout - switching to fastest model")
|
| 309 |
-
payload["model"] = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 310 |
-
payload["max_tokens"] = 2000
|
| 311 |
-
|
| 312 |
-
response = client.post(url, headers=headers, json=payload, timeout=20)
|
| 313 |
-
if response.status_code == 200:
|
| 314 |
-
data = response.json()
|
| 315 |
-
choices = data.get("choices", [])
|
| 316 |
-
if choices:
|
| 317 |
-
return choices[0].get("message", {}).get("content", "")
|
| 318 |
raise Exception("Timeout on all attempts")
|
| 319 |
|
| 320 |
except Exception as e:
|
|
@@ -328,18 +314,13 @@ def get_api_key(user_key: str = "") -> str:
|
|
| 328 |
"""Get API key."""
|
| 329 |
return (user_key or "").strip() or os.getenv("NEBIUS_API_KEY", "").strip() or DEFAULT_NEBIUS_API_KEY
|
| 330 |
|
| 331 |
-
@lru_cache(maxsize=100)
|
| 332 |
-
def generate_prompt_hash(description: str) -> str:
|
| 333 |
-
"""Generate hash for caching similar prompts."""
|
| 334 |
-
return hashlib.md5(description.encode()).hexdigest()[:8]
|
| 335 |
-
|
| 336 |
def analyze_image_fast(
|
| 337 |
image: Optional[Image.Image],
|
| 338 |
nebius_api_key: str = "",
|
| 339 |
vision_model: str = DEFAULT_VISION_MODEL,
|
| 340 |
turbo_mode: bool = True
|
| 341 |
) -> str:
|
| 342 |
-
"""Ultra-fast image analysis."""
|
| 343 |
|
| 344 |
if image is None:
|
| 345 |
return "Error: No image provided."
|
|
@@ -348,29 +329,46 @@ def analyze_image_fast(
|
|
| 348 |
if not api_key:
|
| 349 |
return "Error: API key required."
|
| 350 |
|
| 351 |
-
#
|
| 352 |
-
if turbo_mode:
|
| 353 |
vision_model = "Qwen/Qwen2.5-VL-7B-Instruct"
|
| 354 |
|
| 355 |
try:
|
| 356 |
-
#
|
| 357 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
| 359 |
|
| 360 |
-
# Convert to
|
| 361 |
buffered = io.BytesIO()
|
| 362 |
-
image.save(buffered, format="JPEG", quality=
|
| 363 |
img_b64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 364 |
|
| 365 |
-
#
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
Be brief
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
|
| 375 |
messages = [{
|
| 376 |
"role": "user",
|
|
@@ -385,8 +383,9 @@ Be brief but specific. Focus on what's needed to recreate it."""
|
|
| 385 |
model=vision_model,
|
| 386 |
messages=messages,
|
| 387 |
api_key=api_key,
|
| 388 |
-
max_tokens=
|
| 389 |
-
temperature=0.7
|
|
|
|
| 390 |
)
|
| 391 |
|
| 392 |
except Exception as e:
|
|
@@ -397,9 +396,9 @@ def generate_html_fast(
|
|
| 397 |
nebius_api_key: str = "",
|
| 398 |
code_model: str = DEFAULT_CODE_MODEL,
|
| 399 |
turbo_mode: bool = True,
|
| 400 |
-
quality_mode: str = "
|
| 401 |
) -> str:
|
| 402 |
-
"""Ultra-fast HTML generation."""
|
| 403 |
|
| 404 |
if not description or description.startswith("Error"):
|
| 405 |
return "Error: Invalid description."
|
|
@@ -408,54 +407,51 @@ def generate_html_fast(
|
|
| 408 |
if not api_key:
|
| 409 |
return "Error: API key required."
|
| 410 |
|
| 411 |
-
#
|
| 412 |
-
if quality_mode == "fast":
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
max_tokens = 5000
|
| 426 |
-
else: # balanced
|
| 427 |
-
models_to_try = [
|
| 428 |
-
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 429 |
-
"mistralai/Mistral-7B-Instruct-v0.3"
|
| 430 |
-
]
|
| 431 |
-
max_tokens = 3500
|
| 432 |
-
|
| 433 |
-
# Optimized prompt - shorter for speed
|
| 434 |
-
prompt = f"""Create a complete HTML webpage:
|
| 435 |
|
| 436 |
{description}
|
| 437 |
|
| 438 |
Requirements:
|
| 439 |
-
-
|
| 440 |
-
-
|
| 441 |
-
- Responsive
|
| 442 |
-
- Modern
|
| 443 |
-
- Semantic HTML5
|
| 444 |
-
- Include smooth animations
|
| 445 |
-
- Dark mode support
|
| 446 |
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
- Optimized for performance
|
| 451 |
|
| 452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
|
| 454 |
-
|
|
|
|
|
|
|
| 455 |
for model in models_to_try:
|
| 456 |
try:
|
| 457 |
start = time.time()
|
| 458 |
-
print(f"[{get_current_time()}] Generating with {model}
|
| 459 |
|
| 460 |
messages = [{"role": "user", "content": prompt}]
|
| 461 |
|
|
@@ -464,41 +460,47 @@ Return ONLY the HTML code."""
|
|
| 464 |
messages=messages,
|
| 465 |
api_key=api_key,
|
| 466 |
max_tokens=max_tokens,
|
| 467 |
-
temperature=0.7
|
|
|
|
| 468 |
)
|
| 469 |
|
| 470 |
# Clean response
|
| 471 |
html_code = content.strip()
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
|
|
|
|
|
|
|
|
|
| 475 |
|
| 476 |
# Quick validation
|
| 477 |
-
|
|
|
|
| 478 |
elapsed = time.time() - start
|
| 479 |
-
print(f"
|
| 480 |
|
| 481 |
# Add metadata
|
|
|
|
| 482 |
html_code = html_code.replace(
|
| 483 |
"<head>",
|
| 484 |
-
f"<head>\n <!-- Generated by {model} for {CURRENT_USER} at {
|
| 485 |
)
|
| 486 |
|
| 487 |
return html_code
|
| 488 |
-
|
| 489 |
except Exception as e:
|
| 490 |
-
print(f" Failed with {model}: {e}")
|
| 491 |
continue
|
| 492 |
|
| 493 |
-
return "Error: Generation failed. Try
|
| 494 |
|
| 495 |
def process_ultra_fast(
|
| 496 |
image: Image.Image,
|
| 497 |
nebius_api_key: str = "",
|
| 498 |
-
quality_mode: str = "
|
| 499 |
turbo_mode: bool = True
|
| 500 |
) -> Tuple[str, str, float]:
|
| 501 |
-
"""Ultra-fast complete pipeline
|
| 502 |
|
| 503 |
start_time = time.time()
|
| 504 |
|
|
@@ -506,24 +508,33 @@ def process_ultra_fast(
|
|
| 506 |
description = analyze_image_fast(
|
| 507 |
image,
|
| 508 |
nebius_api_key,
|
| 509 |
-
vision_model="Qwen/Qwen2.5-VL-7B-Instruct",
|
| 510 |
turbo_mode=turbo_mode
|
| 511 |
)
|
| 512 |
|
| 513 |
if description.startswith("Error"):
|
| 514 |
return description, "Error: Analysis failed", time.time() - start_time
|
| 515 |
|
|
|
|
|
|
|
|
|
|
| 516 |
# Step 2: Fast code generation
|
|
|
|
| 517 |
html_code = generate_html_fast(
|
| 518 |
description,
|
| 519 |
nebius_api_key,
|
| 520 |
-
code_model="mistralai/Mistral-7B-Instruct-v0.3" if
|
| 521 |
turbo_mode=turbo_mode,
|
| 522 |
quality_mode=quality_mode
|
| 523 |
)
|
| 524 |
|
| 525 |
-
|
| 526 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
|
| 528 |
# =========================
|
| 529 |
# GRADIO UI - SPEED OPTIMIZED
|
|
@@ -534,7 +545,7 @@ with gr.Blocks(
|
|
| 534 |
primary_hue="emerald",
|
| 535 |
secondary_hue="blue"
|
| 536 |
),
|
| 537 |
-
title=f" Ultra-Fast Website Generator - {CURRENT_USER}",
|
| 538 |
css="""
|
| 539 |
.header {
|
| 540 |
background: linear-gradient(135deg, #10b981 0%, #3b82f6 100%);
|
|
@@ -544,6 +555,11 @@ with gr.Blocks(
|
|
| 544 |
text-align: center;
|
| 545 |
margin-bottom: 1.5rem;
|
| 546 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 547 |
.speed-badge {
|
| 548 |
display: inline-block;
|
| 549 |
padding: 0.25rem 0.75rem;
|
|
@@ -552,13 +568,15 @@ with gr.Blocks(
|
|
| 552 |
margin: 0.25rem;
|
| 553 |
font-size: 0.875rem;
|
| 554 |
}
|
| 555 |
-
.
|
| 556 |
-
background: #
|
| 557 |
-
color:
|
|
|
|
|
|
|
| 558 |
}
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
}
|
| 563 |
.timer {
|
| 564 |
font-size: 1.5rem;
|
|
@@ -570,24 +588,40 @@ with gr.Blocks(
|
|
| 570 |
border-radius: 8px;
|
| 571 |
margin: 1rem 0;
|
| 572 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 573 |
"""
|
| 574 |
) as app:
|
| 575 |
|
| 576 |
gr.HTML(f"""
|
| 577 |
<div class="header">
|
| 578 |
-
<h1
|
| 579 |
-
<p>
|
| 580 |
<div>
|
| 581 |
<span class="speed-badge">User: {CURRENT_USER}</span>
|
| 582 |
-
<span class="speed-badge">
|
| 583 |
-
<span class="speed-badge
|
| 584 |
</div>
|
| 585 |
</div>
|
| 586 |
""")
|
| 587 |
|
| 588 |
with gr.Row():
|
| 589 |
with gr.Column(scale=1):
|
| 590 |
-
# API
|
| 591 |
nebius_key = gr.Textbox(
|
| 592 |
label="Nebius API Key",
|
| 593 |
type="password",
|
|
@@ -597,23 +631,31 @@ with gr.Blocks(
|
|
| 597 |
|
| 598 |
# Speed Settings
|
| 599 |
with gr.Group():
|
| 600 |
-
gr.Markdown("###
|
| 601 |
|
| 602 |
quality_mode = gr.Radio(
|
| 603 |
-
label="
|
| 604 |
choices=[
|
| 605 |
-
(" Fast (5-
|
| 606 |
-
(" Balanced (15-
|
| 607 |
-
(" Quality (30-
|
| 608 |
],
|
| 609 |
value="fast",
|
| 610 |
elem_classes=["quality-selector"]
|
| 611 |
)
|
| 612 |
|
| 613 |
turbo_mode = gr.Checkbox(
|
| 614 |
-
label="
|
| 615 |
-
value=True
|
|
|
|
| 616 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 617 |
|
| 618 |
# Image Input
|
| 619 |
image_input = gr.Image(
|
|
@@ -624,88 +666,87 @@ with gr.Blocks(
|
|
| 624 |
|
| 625 |
# Generate Button
|
| 626 |
generate_btn = gr.Button(
|
| 627 |
-
"
|
| 628 |
variant="primary",
|
| 629 |
size="lg",
|
| 630 |
-
elem_classes=["fast-
|
| 631 |
)
|
| 632 |
|
| 633 |
-
# Timer
|
| 634 |
timer_display = gr.HTML(
|
| 635 |
-
value='<div class="timer"
|
| 636 |
)
|
| 637 |
|
| 638 |
with gr.Column(scale=2):
|
| 639 |
-
# Results
|
| 640 |
with gr.Tabs():
|
| 641 |
-
with gr.Tab(" Analysis"):
|
| 642 |
description_output = gr.Textbox(
|
| 643 |
label="Quick Analysis",
|
| 644 |
lines=5,
|
| 645 |
interactive=False
|
| 646 |
)
|
| 647 |
|
| 648 |
-
with gr.Tab(" Generated Code"):
|
| 649 |
html_output = gr.Code(
|
| 650 |
label="HTML Code",
|
| 651 |
language="html",
|
| 652 |
lines=20
|
| 653 |
)
|
| 654 |
|
| 655 |
-
with gr.Tab(" Performance"):
|
| 656 |
performance_display = gr.Markdown(
|
| 657 |
value="""### Performance Metrics
|
| 658 |
|
| 659 |
-
|
| 660 |
)
|
| 661 |
|
| 662 |
# Action Buttons
|
| 663 |
with gr.Row():
|
| 664 |
-
deploy_btn = gr.Button(" Deploy
|
| 665 |
-
download_btn = gr.Button(" Download
|
| 666 |
-
copy_btn = gr.Button(" Copy
|
| 667 |
|
| 668 |
output_message = gr.Markdown()
|
| 669 |
|
| 670 |
-
#
|
| 671 |
-
with gr.Accordion("
|
| 672 |
gr.Markdown(f"""
|
| 673 |
-
###
|
| 674 |
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
|
|
|
|
|
|
| 679 |
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
|
| 687 |
-
**
|
| 688 |
""")
|
| 689 |
|
| 690 |
-
# Event
|
| 691 |
def generate_with_timer(img, api_key, quality, turbo):
|
| 692 |
-
"""Generate with live timer
|
| 693 |
if img is None:
|
| 694 |
return (
|
| 695 |
"Please upload an image",
|
| 696 |
"",
|
| 697 |
-
'<div class="timer"
|
| 698 |
-
"###
|
| 699 |
)
|
| 700 |
|
| 701 |
-
#
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
# Update UI to show processing
|
| 705 |
-
timer_html = '<div class="timer"> Generating... Please wait</div>'
|
| 706 |
|
| 707 |
try:
|
| 708 |
-
#
|
| 709 |
description, html_code, elapsed = process_ultra_fast(
|
| 710 |
img,
|
| 711 |
api_key,
|
|
@@ -713,38 +754,30 @@ with gr.Blocks(
|
|
| 713 |
turbo_mode=turbo
|
| 714 |
)
|
| 715 |
|
| 716 |
-
#
|
| 717 |
-
timer_html = f'<div class="timer"
|
| 718 |
-
|
| 719 |
-
# Performance metrics
|
| 720 |
-
perf_text = f"""### Performance Metrics
|
| 721 |
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
**Turbo Mode:** {'Enabled' if turbo else 'Disabled'}
|
| 725 |
-
**Models Used:**
|
| 726 |
-
- Vision: Qwen2.5-VL-7B (Fast)
|
| 727 |
-
- Code: {'Mistral-7B' if quality == 'fast' else 'Mixtral-8x7B' if quality == 'balanced' else 'Llama-70B'}
|
| 728 |
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 735 |
"""
|
| 736 |
|
| 737 |
-
return description, html_code, timer_html,
|
| 738 |
|
| 739 |
except Exception as e:
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
return (
|
| 743 |
-
f"Error: {str(e)}",
|
| 744 |
-
"",
|
| 745 |
-
timer_html,
|
| 746 |
-
f"### Error\n\nFailed after {elapsed:.1f} seconds"
|
| 747 |
-
)
|
| 748 |
|
| 749 |
generate_btn.click(
|
| 750 |
fn=generate_with_timer,
|
|
@@ -752,92 +785,64 @@ with gr.Blocks(
|
|
| 752 |
outputs=[description_output, html_output, timer_display, performance_display]
|
| 753 |
)
|
| 754 |
|
| 755 |
-
#
|
| 756 |
def deploy_fast(html_code):
|
| 757 |
if not html_code or html_code.startswith("Error"):
|
| 758 |
-
return " No
|
| 759 |
|
| 760 |
try:
|
| 761 |
-
#
|
| 762 |
files = {
|
| 763 |
-
"index.html": {"content": html_code, "isBinary": False}
|
| 764 |
-
"package.json": {
|
| 765 |
-
"content": json.dumps({
|
| 766 |
-
"name": "ultra-fast-website",
|
| 767 |
-
"version": "1.0.0",
|
| 768 |
-
"description": f"Generated by {CURRENT_USER} at {get_current_time()}",
|
| 769 |
-
"main": "index.html"
|
| 770 |
-
}, indent=2),
|
| 771 |
-
"isBinary": False
|
| 772 |
-
}
|
| 773 |
}
|
| 774 |
-
|
| 775 |
params = {"files": files, "template": "static"}
|
| 776 |
-
|
|
|
|
|
|
|
| 777 |
|
| 778 |
lz = LZString()
|
| 779 |
-
compressed = lz.compressToBase64(
|
| 780 |
compressed = compressed.replace('+', '-').replace('/', '_').rstrip('=')
|
| 781 |
|
| 782 |
url = f"https://codesandbox.io/api/v1/sandboxes/define?parameters={compressed}"
|
| 783 |
-
|
| 784 |
-
return f""" **CodeSandbox Ready!**
|
| 785 |
-
|
| 786 |
-
[Click here to open]({url})
|
| 787 |
-
|
| 788 |
-
Generated in turbo mode by {CURRENT_USER}"""
|
| 789 |
|
| 790 |
except Exception as e:
|
| 791 |
-
return f"
|
| 792 |
|
| 793 |
-
deploy_btn.click(
|
| 794 |
-
fn=deploy_fast,
|
| 795 |
-
inputs=[html_output],
|
| 796 |
-
outputs=[output_message]
|
| 797 |
-
)
|
| 798 |
-
|
| 799 |
-
# Download handler
|
| 800 |
-
def download_fast(html_code):
|
| 801 |
-
if not html_code or html_code.startswith("Error"):
|
| 802 |
-
return " No code to download"
|
| 803 |
-
|
| 804 |
-
try:
|
| 805 |
-
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode='w')
|
| 806 |
-
tmp.write(html_code)
|
| 807 |
-
tmp.close()
|
| 808 |
-
return f" File saved: {tmp.name}"
|
| 809 |
-
except:
|
| 810 |
-
return " Download failed"
|
| 811 |
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
|
|
|
|
|
|
|
|
|
| 817 |
|
| 818 |
-
|
| 819 |
-
def copy_code(html_code):
|
| 820 |
-
if not html_code or html_code.startswith("Error"):
|
| 821 |
-
return " No code to copy"
|
| 822 |
-
return " Code copied to clipboard! (Use Ctrl+A and Ctrl+C in the code box)"
|
| 823 |
|
|
|
|
| 824 |
copy_btn.click(
|
| 825 |
-
fn=
|
| 826 |
-
inputs=[html_output],
|
| 827 |
outputs=[output_message]
|
| 828 |
)
|
| 829 |
|
| 830 |
-
# Cleanup
|
| 831 |
import atexit
|
| 832 |
|
| 833 |
def cleanup():
|
| 834 |
global _connection_pool
|
| 835 |
if _connection_pool:
|
| 836 |
-
|
|
|
|
|
|
|
|
|
|
| 837 |
|
| 838 |
atexit.register(cleanup)
|
| 839 |
|
| 840 |
if __name__ == "__main__":
|
| 841 |
-
print(f"[{get_current_time()}]
|
| 842 |
-
print(f"[{get_current_time()}] Optimizations
|
| 843 |
app.launch(share=False)
|
|
|
|
| 22 |
# =========================
|
| 23 |
NEBIUS_BASE_URL = "https://api.studio.nebius.com/v1/"
|
| 24 |
|
| 25 |
+
# Real-time tracking - UPDATED
|
| 26 |
CURRENT_USER = "samsnata"
|
| 27 |
+
CURRENT_DATETIME = "2025-08-21 08:16:10"
|
| 28 |
|
| 29 |
def get_current_time():
|
| 30 |
return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
| 34 |
UNSPLASH_API_URL = "https://api.unsplash.com"
|
| 35 |
|
| 36 |
# FASTEST Vision Model - Only use the 7B for speed
|
| 37 |
+
DEFAULT_VISION_MODEL = "Qwen/Qwen2.5-VL-7B-Instruct" # FASTEST
|
| 38 |
VISION_MODELS = [
|
| 39 |
+
"Qwen/Qwen2.5-VL-7B-Instruct", # FASTEST - 5-10s
|
| 40 |
+
"Qwen/Qwen2.5-VL-72B-Instruct", # Slower - 20-30s
|
| 41 |
]
|
| 42 |
|
| 43 |
# FASTEST Code Models - Prioritized by speed
|
| 44 |
+
DEFAULT_CODE_MODEL = "mistralai/Mistral-7B-Instruct-v0.3" # FASTEST
|
| 45 |
|
| 46 |
# Speed-optimized model list
|
| 47 |
FAST_CODE_MODELS = [
|
|
|
|
| 56 |
# TIER 3: BALANCED (30-60 seconds)
|
| 57 |
"meta-llama/Meta-Llama-3.1-70B-Instruct", # Good quality - 30-45s
|
| 58 |
"mistralai/Mistral-Nemo-Instruct-2407", # Optimized - 25-40s
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
]
|
| 60 |
|
| 61 |
# ULTRA-OPTIMIZED Model Configurations
|
| 62 |
SPEED_OPTIMIZED_CONFIGS = {
|
| 63 |
# FASTEST MODELS - Aggressive optimization
|
| 64 |
"mistralai/Mistral-7B-Instruct-v0.3": {
|
| 65 |
+
"max_tokens": 2500, # Reduced for speed
|
| 66 |
"temperature": 0.7,
|
| 67 |
+
"timeout_read": 25.0, # Short timeout
|
| 68 |
"timeout_connect": 5.0,
|
| 69 |
+
"retry_count": 0, # No retries for speed
|
|
|
|
| 70 |
"speed_tier": 1,
|
| 71 |
"estimated_time": "5-10 seconds"
|
| 72 |
},
|
| 73 |
"meta-llama/Meta-Llama-3.1-8B-Instruct": {
|
| 74 |
+
"max_tokens": 3000,
|
| 75 |
"temperature": 0.7,
|
| 76 |
+
"timeout_read": 30.0,
|
| 77 |
"timeout_connect": 5.0,
|
| 78 |
+
"retry_count": 0,
|
|
|
|
| 79 |
"speed_tier": 1,
|
| 80 |
"estimated_time": "8-15 seconds"
|
| 81 |
},
|
| 82 |
"mistralai/Mixtral-8x7B-Instruct-v0.1": {
|
| 83 |
+
"max_tokens": 3500,
|
| 84 |
"temperature": 0.7,
|
| 85 |
+
"timeout_read": 40.0,
|
| 86 |
"timeout_connect": 7.0,
|
| 87 |
"retry_count": 1,
|
|
|
|
| 88 |
"speed_tier": 2,
|
| 89 |
"estimated_time": "15-25 seconds"
|
| 90 |
},
|
| 91 |
"meta-llama/Meta-Llama-3.1-70B-Instruct": {
|
| 92 |
+
"max_tokens": 4000,
|
| 93 |
"temperature": 0.7,
|
| 94 |
"timeout_read": 60.0,
|
| 95 |
"timeout_connect": 10.0,
|
| 96 |
"retry_count": 1,
|
|
|
|
| 97 |
"speed_tier": 3,
|
| 98 |
"estimated_time": "30-45 seconds"
|
| 99 |
},
|
| 100 |
# Vision models - optimized
|
| 101 |
"Qwen/Qwen2.5-VL-7B-Instruct": {
|
| 102 |
+
"max_tokens": 1000, # Minimal for speed
|
| 103 |
"temperature": 0.7,
|
| 104 |
+
"timeout_read": 25.0,
|
| 105 |
"timeout_connect": 5.0,
|
| 106 |
+
"retry_count": 0,
|
|
|
|
| 107 |
"speed_tier": 1,
|
| 108 |
+
"estimated_time": "5-10 seconds"
|
| 109 |
},
|
| 110 |
"Qwen/Qwen2.5-VL-72B-Instruct": {
|
| 111 |
"max_tokens": 1500,
|
| 112 |
"temperature": 0.7,
|
| 113 |
+
"timeout_read": 50.0,
|
| 114 |
"timeout_connect": 10.0,
|
| 115 |
+
"retry_count": 0,
|
|
|
|
| 116 |
"speed_tier": 3,
|
| 117 |
+
"estimated_time": "20-30 seconds"
|
| 118 |
}
|
| 119 |
}
|
| 120 |
|
|
|
|
| 124 |
)
|
| 125 |
|
| 126 |
# =========================
|
| 127 |
+
# CACHE AND CONNECTION POOLING (WITHOUT HTTP/2)
|
| 128 |
# =========================
|
| 129 |
|
| 130 |
# Global connection pool for reuse
|
| 131 |
_connection_pool = None
|
| 132 |
+
_pool_lock = False
|
| 133 |
|
| 134 |
def get_connection_pool():
|
| 135 |
+
"""Get or create a connection pool for HTTP requests (HTTP/1.1 for compatibility)."""
|
| 136 |
+
global _connection_pool, _pool_lock
|
| 137 |
+
|
| 138 |
+
if _connection_pool is None and not _pool_lock:
|
| 139 |
+
_pool_lock = True
|
| 140 |
+
try:
|
| 141 |
+
_connection_pool = httpx.Client(
|
| 142 |
+
limits=httpx.Limits(
|
| 143 |
+
max_keepalive_connections=20,
|
| 144 |
+
max_connections=40,
|
| 145 |
+
keepalive_expiry=30.0
|
| 146 |
+
),
|
| 147 |
+
timeout=httpx.Timeout(30.0, connect=5.0),
|
| 148 |
+
# http2=False # Explicitly disable HTTP/2 to avoid h2 package requirement
|
| 149 |
+
)
|
| 150 |
+
finally:
|
| 151 |
+
_pool_lock = False
|
| 152 |
+
|
| 153 |
return _connection_pool
|
| 154 |
|
| 155 |
# Cache for model configs
|
|
|
|
| 157 |
def get_model_config(model: str) -> Dict[str, Any]:
|
| 158 |
"""Get cached model configuration."""
|
| 159 |
default = {
|
| 160 |
+
"max_tokens": 2500,
|
| 161 |
"temperature": 0.7,
|
| 162 |
+
"timeout_read": 35.0,
|
| 163 |
"timeout_connect": 8.0,
|
| 164 |
+
"retry_count": 0,
|
|
|
|
| 165 |
"speed_tier": 2,
|
| 166 |
+
"estimated_time": "15-30 seconds"
|
| 167 |
}
|
| 168 |
return SPEED_OPTIMIZED_CONFIGS.get(model, default)
|
| 169 |
|
|
|
|
| 171 |
# ULTRA-FAST API CALLS
|
| 172 |
# =========================
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
def call_nebius_api_sync_fast(
|
| 175 |
model: str,
|
| 176 |
messages: list,
|
| 177 |
api_key: str,
|
| 178 |
max_tokens: Optional[int] = None,
|
| 179 |
+
temperature: Optional[float] = None,
|
| 180 |
+
use_pool: bool = True
|
| 181 |
) -> str:
|
| 182 |
"""Ultra-fast synchronous API call with minimal overhead."""
|
| 183 |
|
|
|
|
| 187 |
config = get_model_config(model)
|
| 188 |
|
| 189 |
# Use minimal tokens for speed
|
| 190 |
+
actual_max_tokens = min(
|
| 191 |
+
max_tokens if max_tokens is not None else config["max_tokens"],
|
| 192 |
+
config["max_tokens"]
|
| 193 |
+
)
|
| 194 |
|
| 195 |
headers = {
|
| 196 |
"Authorization": f"Bearer {api_key}",
|
| 197 |
+
"Content-Type": "application/json",
|
| 198 |
+
"Accept": "application/json"
|
| 199 |
}
|
| 200 |
|
| 201 |
payload = {
|
|
|
|
| 203 |
"messages": messages,
|
| 204 |
"max_tokens": actual_max_tokens,
|
| 205 |
"temperature": temperature or config["temperature"],
|
| 206 |
+
"stream": False, # Never stream for speed
|
| 207 |
+
"top_p": 0.95, # Slightly constrain for consistency
|
| 208 |
+
"frequency_penalty": 0.0,
|
| 209 |
+
"presence_penalty": 0.0
|
| 210 |
}
|
| 211 |
|
| 212 |
url = f"{NEBIUS_BASE_URL}chat/completions"
|
| 213 |
|
| 214 |
+
# Log the request
|
| 215 |
start_time = time.time()
|
| 216 |
print(f"[{get_current_time()}] {CURRENT_USER} calling {model} (Tier {config.get('speed_tier', 'N/A')})")
|
| 217 |
|
| 218 |
try:
|
| 219 |
+
# Use connection pool or create new client
|
| 220 |
+
if use_pool:
|
| 221 |
+
client = get_connection_pool()
|
| 222 |
+
if client is None:
|
| 223 |
+
# Fallback to new client if pool failed
|
| 224 |
+
client = httpx.Client(timeout=httpx.Timeout(
|
| 225 |
+
config["timeout_read"],
|
| 226 |
+
connect=config["timeout_connect"]
|
| 227 |
+
))
|
| 228 |
+
use_pool = False
|
| 229 |
+
else:
|
| 230 |
+
client = httpx.Client(timeout=httpx.Timeout(
|
| 231 |
+
config["timeout_read"],
|
| 232 |
+
connect=config["timeout_connect"]
|
| 233 |
+
))
|
| 234 |
|
| 235 |
+
try:
|
| 236 |
+
response = client.post(
|
| 237 |
+
url,
|
| 238 |
+
headers=headers,
|
| 239 |
+
json=payload,
|
| 240 |
+
timeout=config["timeout_read"]
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
elapsed = time.time() - start_time
|
| 244 |
+
print(f" Response in {elapsed:.1f}s - Status: {response.status_code}")
|
| 245 |
+
|
| 246 |
+
if response.status_code == 200:
|
| 247 |
+
data = response.json()
|
| 248 |
+
choices = data.get("choices", [])
|
| 249 |
+
if choices and len(choices) > 0:
|
| 250 |
+
content = choices[0].get("message", {}).get("content", "")
|
| 251 |
+
if content:
|
| 252 |
+
return content
|
| 253 |
+
raise ValueError("Empty response from API")
|
| 254 |
+
|
| 255 |
+
elif response.status_code == 429:
|
| 256 |
+
# Rate limit - try once with smaller payload
|
| 257 |
+
print(f" Rate limited - retrying with reduced tokens")
|
| 258 |
+
payload["max_tokens"] = min(actual_max_tokens // 2, 1500)
|
| 259 |
+
time.sleep(2)
|
| 260 |
+
response = client.post(url, headers=headers, json=payload, timeout=20)
|
| 261 |
+
if response.status_code == 200:
|
| 262 |
+
data = response.json()
|
| 263 |
+
choices = data.get("choices", [])
|
| 264 |
+
if choices:
|
| 265 |
+
return choices[0].get("message", {}).get("content", "")
|
| 266 |
+
|
| 267 |
+
# Try fallback to fastest model on any error
|
| 268 |
+
if elapsed > 10 and model != "mistralai/Mistral-7B-Instruct-v0.3":
|
| 269 |
+
print(f" Slow response - switching to fastest model")
|
| 270 |
+
payload["model"] = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 271 |
+
payload["max_tokens"] = 2000
|
| 272 |
+
response = client.post(url, headers=headers, json=payload, timeout=20)
|
| 273 |
+
if response.status_code == 200:
|
| 274 |
+
data = response.json()
|
| 275 |
+
choices = data.get("choices", [])
|
| 276 |
+
if choices:
|
| 277 |
+
return choices[0].get("message", {}).get("content", "")
|
| 278 |
+
|
| 279 |
+
raise Exception(f"API error: {response.status_code}")
|
| 280 |
+
|
| 281 |
+
finally:
|
| 282 |
+
# Only close if not using pool
|
| 283 |
+
if not use_pool:
|
| 284 |
+
client.close()
|
| 285 |
|
| 286 |
+
except httpx.TimeoutException:
|
| 287 |
+
print(f" Timeout after {config['timeout_read']}s - trying fastest model")
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
+
# On timeout, immediately try fastest model with new client
|
| 290 |
+
fast_client = httpx.Client(timeout=httpx.Timeout(20.0, connect=5.0))
|
| 291 |
+
try:
|
| 292 |
payload["model"] = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 293 |
+
payload["max_tokens"] = 1500
|
| 294 |
+
|
| 295 |
+
response = fast_client.post(url, headers=headers, json=payload)
|
| 296 |
if response.status_code == 200:
|
| 297 |
data = response.json()
|
| 298 |
choices = data.get("choices", [])
|
| 299 |
if choices:
|
| 300 |
return choices[0].get("message", {}).get("content", "")
|
| 301 |
+
finally:
|
| 302 |
+
fast_client.close()
|
| 303 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
raise Exception("Timeout on all attempts")
|
| 305 |
|
| 306 |
except Exception as e:
|
|
|
|
| 314 |
"""Get API key."""
|
| 315 |
return (user_key or "").strip() or os.getenv("NEBIUS_API_KEY", "").strip() or DEFAULT_NEBIUS_API_KEY
|
| 316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
def analyze_image_fast(
|
| 318 |
image: Optional[Image.Image],
|
| 319 |
nebius_api_key: str = "",
|
| 320 |
vision_model: str = DEFAULT_VISION_MODEL,
|
| 321 |
turbo_mode: bool = True
|
| 322 |
) -> str:
|
| 323 |
+
"""Ultra-fast image analysis with optimizations."""
|
| 324 |
|
| 325 |
if image is None:
|
| 326 |
return "Error: No image provided."
|
|
|
|
| 329 |
if not api_key:
|
| 330 |
return "Error: API key required."
|
| 331 |
|
| 332 |
+
# Always use fastest vision model in turbo mode
|
| 333 |
+
if turbo_mode or "72B" in vision_model:
|
| 334 |
vision_model = "Qwen/Qwen2.5-VL-7B-Instruct"
|
| 335 |
|
| 336 |
try:
|
| 337 |
+
# Aggressive image optimization for speed
|
| 338 |
+
if turbo_mode:
|
| 339 |
+
# Very small size for turbo mode
|
| 340 |
+
max_size = 512
|
| 341 |
+
quality = 75
|
| 342 |
+
else:
|
| 343 |
+
max_size = 768
|
| 344 |
+
quality = 85
|
| 345 |
+
|
| 346 |
+
# Resize image
|
| 347 |
image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
| 348 |
|
| 349 |
+
# Convert to JPEG for smaller size
|
| 350 |
buffered = io.BytesIO()
|
| 351 |
+
image.save(buffered, format="JPEG", quality=quality, optimize=True)
|
| 352 |
img_b64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 353 |
|
| 354 |
+
# Ultra-concise prompt for speed
|
| 355 |
+
if turbo_mode:
|
| 356 |
+
prompt = """Quick analysis:
|
| 357 |
+
1. Layout (grid/flex/sidebar)
|
| 358 |
+
2. Colors (2-3 main)
|
| 359 |
+
3. Components (header/nav/content/footer)
|
| 360 |
+
4. Style (modern/minimal/corporate)
|
| 361 |
+
|
| 362 |
+
Be very brief."""
|
| 363 |
+
else:
|
| 364 |
+
prompt = """Analyze this website:
|
| 365 |
+
1. Layout structure
|
| 366 |
+
2. Color scheme (hex codes)
|
| 367 |
+
3. Main components
|
| 368 |
+
4. Design style
|
| 369 |
+
5. Key features
|
| 370 |
+
|
| 371 |
+
Be concise but complete."""
|
| 372 |
|
| 373 |
messages = [{
|
| 374 |
"role": "user",
|
|
|
|
| 383 |
model=vision_model,
|
| 384 |
messages=messages,
|
| 385 |
api_key=api_key,
|
| 386 |
+
max_tokens=800 if turbo_mode else 1200,
|
| 387 |
+
temperature=0.7,
|
| 388 |
+
use_pool=True
|
| 389 |
)
|
| 390 |
|
| 391 |
except Exception as e:
|
|
|
|
| 396 |
nebius_api_key: str = "",
|
| 397 |
code_model: str = DEFAULT_CODE_MODEL,
|
| 398 |
turbo_mode: bool = True,
|
| 399 |
+
quality_mode: str = "fast"
|
| 400 |
) -> str:
|
| 401 |
+
"""Ultra-fast HTML generation with model selection."""
|
| 402 |
|
| 403 |
if not description or description.startswith("Error"):
|
| 404 |
return "Error: Invalid description."
|
|
|
|
| 407 |
if not api_key:
|
| 408 |
return "Error: API key required."
|
| 409 |
|
| 410 |
+
# Model selection based on quality mode
|
| 411 |
+
if quality_mode == "fast" or turbo_mode:
|
| 412 |
+
models_to_try = ["mistralai/Mistral-7B-Instruct-v0.3"]
|
| 413 |
+
max_tokens = 2000 if turbo_mode else 2500
|
| 414 |
+
elif quality_mode == "balanced":
|
| 415 |
+
models_to_try = ["mistralai/Mixtral-8x7B-Instruct-v0.1", "mistralai/Mistral-7B-Instruct-v0.3"]
|
| 416 |
+
max_tokens = 3000
|
| 417 |
+
else: # quality
|
| 418 |
+
models_to_try = ["meta-llama/Meta-Llama-3.1-70B-Instruct", "mistralai/Mixtral-8x7B-Instruct-v0.1"]
|
| 419 |
+
max_tokens = 4000
|
| 420 |
+
|
| 421 |
+
# Ultra-optimized prompt
|
| 422 |
+
if turbo_mode:
|
| 423 |
+
prompt = f"""HTML webpage from description:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
|
| 425 |
{description}
|
| 426 |
|
| 427 |
Requirements:
|
| 428 |
+
- Complete HTML with inline CSS/JS
|
| 429 |
+
- TailwindCSS CDN
|
| 430 |
+
- Responsive
|
| 431 |
+
- Modern design
|
|
|
|
|
|
|
|
|
|
| 432 |
|
| 433 |
+
Return only HTML code."""
|
| 434 |
+
else:
|
| 435 |
+
prompt = f"""Create complete HTML webpage:
|
|
|
|
| 436 |
|
| 437 |
+
{description}
|
| 438 |
+
|
| 439 |
+
Requirements:
|
| 440 |
+
- Single HTML file, inline CSS/JS
|
| 441 |
+
- TailwindCSS CDN v3
|
| 442 |
+
- Fully responsive
|
| 443 |
+
- Modern, clean design
|
| 444 |
+
- Semantic HTML5
|
| 445 |
+
- Smooth animations
|
| 446 |
+
- Dark mode toggle
|
| 447 |
|
| 448 |
+
Return only the HTML code, no explanations."""
|
| 449 |
+
|
| 450 |
+
# Try models in order
|
| 451 |
for model in models_to_try:
|
| 452 |
try:
|
| 453 |
start = time.time()
|
| 454 |
+
print(f"[{get_current_time()}] Generating with {model} ({quality_mode} mode)")
|
| 455 |
|
| 456 |
messages = [{"role": "user", "content": prompt}]
|
| 457 |
|
|
|
|
| 460 |
messages=messages,
|
| 461 |
api_key=api_key,
|
| 462 |
max_tokens=max_tokens,
|
| 463 |
+
temperature=0.7,
|
| 464 |
+
use_pool=True
|
| 465 |
)
|
| 466 |
|
| 467 |
# Clean response
|
| 468 |
html_code = content.strip()
|
| 469 |
+
|
| 470 |
+
# Remove markdown code fences if present
|
| 471 |
+
if "```" in html_code:
|
| 472 |
+
html_code = re.sub(r'^```[a-z]*\n?', '', html_code)
|
| 473 |
+
html_code = re.sub(r'\n?```$', '', html_code)
|
| 474 |
+
html_code = html_code.strip()
|
| 475 |
|
| 476 |
# Quick validation
|
| 477 |
+
html_lower = html_code.lower()
|
| 478 |
+
if "<!doctype" in html_lower and "</html>" in html_lower:
|
| 479 |
elapsed = time.time() - start
|
| 480 |
+
print(f" Success in {elapsed:.1f}s")
|
| 481 |
|
| 482 |
# Add metadata
|
| 483 |
+
timestamp = get_current_time()
|
| 484 |
html_code = html_code.replace(
|
| 485 |
"<head>",
|
| 486 |
+
f"<head>\n <!-- Generated by {model} for {CURRENT_USER} at {timestamp} in {elapsed:.1f}s -->"
|
| 487 |
)
|
| 488 |
|
| 489 |
return html_code
|
| 490 |
+
|
| 491 |
except Exception as e:
|
| 492 |
+
print(f" Failed with {model}: {str(e)[:100]}")
|
| 493 |
continue
|
| 494 |
|
| 495 |
+
return "Error: Generation failed. Try enabling turbo mode or using fast quality setting."
|
| 496 |
|
| 497 |
def process_ultra_fast(
|
| 498 |
image: Image.Image,
|
| 499 |
nebius_api_key: str = "",
|
| 500 |
+
quality_mode: str = "fast",
|
| 501 |
turbo_mode: bool = True
|
| 502 |
) -> Tuple[str, str, float]:
|
| 503 |
+
"""Ultra-fast complete pipeline."""
|
| 504 |
|
| 505 |
start_time = time.time()
|
| 506 |
|
|
|
|
| 508 |
description = analyze_image_fast(
|
| 509 |
image,
|
| 510 |
nebius_api_key,
|
| 511 |
+
vision_model="Qwen/Qwen2.5-VL-7B-Instruct",
|
| 512 |
turbo_mode=turbo_mode
|
| 513 |
)
|
| 514 |
|
| 515 |
if description.startswith("Error"):
|
| 516 |
return description, "Error: Analysis failed", time.time() - start_time
|
| 517 |
|
| 518 |
+
analysis_time = time.time() - start_time
|
| 519 |
+
print(f" Analysis completed in {analysis_time:.1f}s")
|
| 520 |
+
|
| 521 |
# Step 2: Fast code generation
|
| 522 |
+
code_start = time.time()
|
| 523 |
html_code = generate_html_fast(
|
| 524 |
description,
|
| 525 |
nebius_api_key,
|
| 526 |
+
code_model="mistralai/Mistral-7B-Instruct-v0.3" if quality_mode == "fast" else "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 527 |
turbo_mode=turbo_mode,
|
| 528 |
quality_mode=quality_mode
|
| 529 |
)
|
| 530 |
|
| 531 |
+
code_time = time.time() - code_start
|
| 532 |
+
total_time = time.time() - start_time
|
| 533 |
+
|
| 534 |
+
print(f" Code generation in {code_time:.1f}s")
|
| 535 |
+
print(f" Total pipeline in {total_time:.1f}s")
|
| 536 |
+
|
| 537 |
+
return description, html_code, total_time
|
| 538 |
|
| 539 |
# =========================
|
| 540 |
# GRADIO UI - SPEED OPTIMIZED
|
|
|
|
| 545 |
primary_hue="emerald",
|
| 546 |
secondary_hue="blue"
|
| 547 |
),
|
| 548 |
+
title=f"β‘ Ultra-Fast Website Generator - {CURRENT_USER}",
|
| 549 |
css="""
|
| 550 |
.header {
|
| 551 |
background: linear-gradient(135deg, #10b981 0%, #3b82f6 100%);
|
|
|
|
| 555 |
text-align: center;
|
| 556 |
margin-bottom: 1.5rem;
|
| 557 |
}
|
| 558 |
+
.header h1 {
|
| 559 |
+
font-size: 2.25rem;
|
| 560 |
+
font-weight: 800;
|
| 561 |
+
margin-bottom: 0.5rem;
|
| 562 |
+
}
|
| 563 |
.speed-badge {
|
| 564 |
display: inline-block;
|
| 565 |
padding: 0.25rem 0.75rem;
|
|
|
|
| 568 |
margin: 0.25rem;
|
| 569 |
font-size: 0.875rem;
|
| 570 |
}
|
| 571 |
+
.turbo-badge {
|
| 572 |
+
background: #fbbf24 !important;
|
| 573 |
+
color: #78350f !important;
|
| 574 |
+
font-weight: bold;
|
| 575 |
+
animation: pulse 2s infinite;
|
| 576 |
}
|
| 577 |
+
@keyframes pulse {
|
| 578 |
+
0%, 100% { opacity: 1; }
|
| 579 |
+
50% { opacity: 0.7; }
|
| 580 |
}
|
| 581 |
.timer {
|
| 582 |
font-size: 1.5rem;
|
|
|
|
| 588 |
border-radius: 8px;
|
| 589 |
margin: 1rem 0;
|
| 590 |
}
|
| 591 |
+
.fast-btn {
|
| 592 |
+
background: linear-gradient(135deg, #10b981 0%, #059669 100%) !important;
|
| 593 |
+
color: white !important;
|
| 594 |
+
font-weight: bold !important;
|
| 595 |
+
font-size: 1.125rem !important;
|
| 596 |
+
}
|
| 597 |
+
.quality-card {
|
| 598 |
+
padding: 0.75rem;
|
| 599 |
+
border-radius: 8px;
|
| 600 |
+
border: 2px solid transparent;
|
| 601 |
+
transition: all 0.3s;
|
| 602 |
+
}
|
| 603 |
+
.quality-card:hover {
|
| 604 |
+
border-color: #10b981;
|
| 605 |
+
background: #f0fdf4;
|
| 606 |
+
}
|
| 607 |
"""
|
| 608 |
) as app:
|
| 609 |
|
| 610 |
gr.HTML(f"""
|
| 611 |
<div class="header">
|
| 612 |
+
<h1>β‘ Ultra-Fast Website Generator</h1>
|
| 613 |
+
<p>Generate production-ready websites in seconds!</p>
|
| 614 |
<div>
|
| 615 |
<span class="speed-badge">User: {CURRENT_USER}</span>
|
| 616 |
+
<span class="speed-badge">Session: {CURRENT_DATETIME}</span>
|
| 617 |
+
<span class="speed-badge turbo-badge">β‘ TURBO OPTIMIZED</span>
|
| 618 |
</div>
|
| 619 |
</div>
|
| 620 |
""")
|
| 621 |
|
| 622 |
with gr.Row():
|
| 623 |
with gr.Column(scale=1):
|
| 624 |
+
# API Key
|
| 625 |
nebius_key = gr.Textbox(
|
| 626 |
label="Nebius API Key",
|
| 627 |
type="password",
|
|
|
|
| 631 |
|
| 632 |
# Speed Settings
|
| 633 |
with gr.Group():
|
| 634 |
+
gr.Markdown("### β‘ Speed Settings")
|
| 635 |
|
| 636 |
quality_mode = gr.Radio(
|
| 637 |
+
label="Quality Mode",
|
| 638 |
choices=[
|
| 639 |
+
("π Ultra Fast (5-10s)", "fast"),
|
| 640 |
+
("βοΈ Balanced (15-25s)", "balanced"),
|
| 641 |
+
("π High Quality (30-45s)", "quality")
|
| 642 |
],
|
| 643 |
value="fast",
|
| 644 |
elem_classes=["quality-selector"]
|
| 645 |
)
|
| 646 |
|
| 647 |
turbo_mode = gr.Checkbox(
|
| 648 |
+
label="β‘ TURBO MODE - Maximum Speed",
|
| 649 |
+
value=True,
|
| 650 |
+
elem_classes=["turbo-checkbox"]
|
| 651 |
)
|
| 652 |
+
|
| 653 |
+
gr.Markdown("""
|
| 654 |
+
**Speed Tips:**
|
| 655 |
+
- β
Turbo Mode + Ultra Fast = 5-10 seconds
|
| 656 |
+
- β
Smaller images = Faster processing
|
| 657 |
+
- β
Simple designs = Quicker generation
|
| 658 |
+
""")
|
| 659 |
|
| 660 |
# Image Input
|
| 661 |
image_input = gr.Image(
|
|
|
|
| 666 |
|
| 667 |
# Generate Button
|
| 668 |
generate_btn = gr.Button(
|
| 669 |
+
"β‘ GENERATE NOW",
|
| 670 |
variant="primary",
|
| 671 |
size="lg",
|
| 672 |
+
elem_classes=["fast-btn"]
|
| 673 |
)
|
| 674 |
|
| 675 |
+
# Timer
|
| 676 |
timer_display = gr.HTML(
|
| 677 |
+
value='<div class="timer">β‘ Ready - Click Generate!</div>'
|
| 678 |
)
|
| 679 |
|
| 680 |
with gr.Column(scale=2):
|
| 681 |
+
# Results Tabs
|
| 682 |
with gr.Tabs():
|
| 683 |
+
with gr.Tab("π Analysis"):
|
| 684 |
description_output = gr.Textbox(
|
| 685 |
label="Quick Analysis",
|
| 686 |
lines=5,
|
| 687 |
interactive=False
|
| 688 |
)
|
| 689 |
|
| 690 |
+
with gr.Tab("π» Generated Code"):
|
| 691 |
html_output = gr.Code(
|
| 692 |
label="HTML Code",
|
| 693 |
language="html",
|
| 694 |
lines=20
|
| 695 |
)
|
| 696 |
|
| 697 |
+
with gr.Tab("π Performance"):
|
| 698 |
performance_display = gr.Markdown(
|
| 699 |
value="""### Performance Metrics
|
| 700 |
|
| 701 |
+
Waiting for generation..."""
|
| 702 |
)
|
| 703 |
|
| 704 |
# Action Buttons
|
| 705 |
with gr.Row():
|
| 706 |
+
deploy_btn = gr.Button("π Deploy", size="sm")
|
| 707 |
+
download_btn = gr.Button("πΎ Download", size="sm")
|
| 708 |
+
copy_btn = gr.Button("π Copy", size="sm")
|
| 709 |
|
| 710 |
output_message = gr.Markdown()
|
| 711 |
|
| 712 |
+
# Speed Guide
|
| 713 |
+
with gr.Accordion("π Model Speed Guide", open=False):
|
| 714 |
gr.Markdown(f"""
|
| 715 |
+
### Actual Performance (Updated {get_current_time()})
|
| 716 |
|
| 717 |
+
| Mode | Model | Real Speed | Quality |
|
| 718 |
+
|------|-------|------------|---------|
|
| 719 |
+
| **Ultra Fast** | Mistral-7B | β‘ 5-10s | β
β
β
β
|
|
| 720 |
+
| **Fast** | Llama-8B | β‘ 8-15s | β
β
β
β
|
|
| 721 |
+
| **Balanced** | Mixtral-8x7B | β‘ 15-25s | β
β
β
β
β
|
|
| 722 |
+
| **Quality** | Llama-70B | 30-45s | β
β
β
β
β
|
|
| 723 |
|
| 724 |
+
**Current optimizations:**
|
| 725 |
+
- β
Connection pooling (saves 2-3s)
|
| 726 |
+
- β
Image compression (saves 1-2s)
|
| 727 |
+
- β
Reduced tokens (saves 5-10s)
|
| 728 |
+
- β
No retries in turbo mode (saves 10-20s)
|
| 729 |
+
- β
Direct model selection (no fallbacks)
|
| 730 |
|
| 731 |
+
**User:** {CURRENT_USER}
|
| 732 |
""")
|
| 733 |
|
| 734 |
+
# Event Handler
|
| 735 |
def generate_with_timer(img, api_key, quality, turbo):
|
| 736 |
+
"""Generate with live timer."""
|
| 737 |
if img is None:
|
| 738 |
return (
|
| 739 |
"Please upload an image",
|
| 740 |
"",
|
| 741 |
+
'<div class="timer">β No image uploaded</div>',
|
| 742 |
+
"### No generation performed"
|
| 743 |
)
|
| 744 |
|
| 745 |
+
# Show processing
|
| 746 |
+
timer_start = '<div class="timer">β‘ Generating... Please wait</div>'
|
|
|
|
|
|
|
|
|
|
| 747 |
|
| 748 |
try:
|
| 749 |
+
# Process
|
| 750 |
description, html_code, elapsed = process_ultra_fast(
|
| 751 |
img,
|
| 752 |
api_key,
|
|
|
|
| 754 |
turbo_mode=turbo
|
| 755 |
)
|
| 756 |
|
| 757 |
+
# Success timer
|
| 758 |
+
timer_html = f'<div class="timer">β
Complete in {elapsed:.1f} seconds!</div>'
|
|
|
|
|
|
|
|
|
|
| 759 |
|
| 760 |
+
# Performance report
|
| 761 |
+
perf = f"""### Performance Report
|
|
|
|
|
|
|
|
|
|
|
|
|
| 762 |
|
| 763 |
+
**Total Time:** {elapsed:.1f} seconds
|
| 764 |
+
**Mode:** {quality.upper()} {'+ TURBO' if turbo else ''}
|
| 765 |
+
**Vision Model:** Qwen2.5-VL-7B (Fast)
|
| 766 |
+
**Code Model:** {'Mistral-7B' if quality == 'fast' else 'Mixtral-8x7B'}
|
| 767 |
+
|
| 768 |
+
**Breakdown:**
|
| 769 |
+
- Image Analysis: ~{elapsed * 0.3:.1f}s
|
| 770 |
+
- Code Generation: ~{elapsed * 0.6:.1f}s
|
| 771 |
+
- Network/Other: ~{elapsed * 0.1:.1f}s
|
| 772 |
+
|
| 773 |
+
**Session:** {CURRENT_USER} @ {get_current_time()}
|
| 774 |
"""
|
| 775 |
|
| 776 |
+
return description, html_code, timer_html, perf
|
| 777 |
|
| 778 |
except Exception as e:
|
| 779 |
+
timer_html = f'<div class="timer">β Error: {str(e)[:50]}</div>'
|
| 780 |
+
return f"Error: {str(e)}", "", timer_html, f"### Error\n\n{str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 781 |
|
| 782 |
generate_btn.click(
|
| 783 |
fn=generate_with_timer,
|
|
|
|
| 785 |
outputs=[description_output, html_output, timer_display, performance_display]
|
| 786 |
)
|
| 787 |
|
| 788 |
+
# Quick deploy
|
| 789 |
def deploy_fast(html_code):
|
| 790 |
if not html_code or html_code.startswith("Error"):
|
| 791 |
+
return "β No code to deploy"
|
| 792 |
|
| 793 |
try:
|
| 794 |
+
# Minimal CodeSandbox creation
|
| 795 |
files = {
|
| 796 |
+
"index.html": {"content": html_code, "isBinary": False}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 797 |
}
|
|
|
|
| 798 |
params = {"files": files, "template": "static"}
|
| 799 |
+
|
| 800 |
+
import json
|
| 801 |
+
from lzstring import LZString
|
| 802 |
|
| 803 |
lz = LZString()
|
| 804 |
+
compressed = lz.compressToBase64(json.dumps(params))
|
| 805 |
compressed = compressed.replace('+', '-').replace('/', '_').rstrip('=')
|
| 806 |
|
| 807 |
url = f"https://codesandbox.io/api/v1/sandboxes/define?parameters={compressed}"
|
| 808 |
+
return f"β
**[Open in CodeSandbox]({url})**"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 809 |
|
| 810 |
except Exception as e:
|
| 811 |
+
return f"β {str(e)}"
|
| 812 |
|
| 813 |
+
deploy_btn.click(fn=deploy_fast, inputs=[html_output], outputs=[output_message])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 814 |
|
| 815 |
+
# Download
|
| 816 |
+
def download_fast(code):
|
| 817 |
+
if not code or code.startswith("Error"):
|
| 818 |
+
return "β No code"
|
| 819 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode='w')
|
| 820 |
+
tmp.write(code)
|
| 821 |
+
tmp.close()
|
| 822 |
+
return f"β
Saved to {tmp.name}"
|
| 823 |
|
| 824 |
+
download_btn.click(fn=download_fast, inputs=[html_output], outputs=[output_message])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 825 |
|
| 826 |
+
# Copy hint
|
| 827 |
copy_btn.click(
|
| 828 |
+
fn=lambda: "β
Select code and press Ctrl+C",
|
|
|
|
| 829 |
outputs=[output_message]
|
| 830 |
)
|
| 831 |
|
| 832 |
+
# Cleanup
|
| 833 |
import atexit
|
| 834 |
|
| 835 |
def cleanup():
|
| 836 |
global _connection_pool
|
| 837 |
if _connection_pool:
|
| 838 |
+
try:
|
| 839 |
+
_connection_pool.close()
|
| 840 |
+
except:
|
| 841 |
+
pass
|
| 842 |
|
| 843 |
atexit.register(cleanup)
|
| 844 |
|
| 845 |
if __name__ == "__main__":
|
| 846 |
+
print(f"[{get_current_time()}] β‘ Ultra-Fast Generator starting for {CURRENT_USER}")
|
| 847 |
+
print(f"[{get_current_time()}] Optimizations: Connection pooling, Turbo mode, No HTTP/2")
|
| 848 |
app.launch(share=False)
|