Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -235,9 +235,9 @@ def infer(
|
|
| 235 |
return result, seed, get_success_html("تصویر با موفقیت ویرایش شد.")
|
| 236 |
|
| 237 |
except Exception as e:
|
| 238 |
-
#
|
| 239 |
error_str = str(e)
|
| 240 |
-
if "quota" in error_str.lower():
|
| 241 |
raise gr.Error("You have exceeded your GPU quota")
|
| 242 |
return None, seed, get_error_html(f"خطا در پردازش: {error_str}")
|
| 243 |
|
|
@@ -259,6 +259,7 @@ async (image) => {
|
|
| 259 |
} else if (!fileUrl && image.path) {
|
| 260 |
fileUrl = window.location.origin + "/file=" + image.path;
|
| 261 |
}
|
|
|
|
| 262 |
window.parent.postMessage({
|
| 263 |
type: 'DOWNLOAD_REQUEST',
|
| 264 |
url: fileUrl
|
|
@@ -266,7 +267,7 @@ async (image) => {
|
|
| 266 |
}
|
| 267 |
"""
|
| 268 |
|
| 269 |
-
# --- جاوااسکریپت
|
| 270 |
js_global_func = """
|
| 271 |
function() {
|
| 272 |
// 1. اجبار تم روشن
|
|
@@ -282,7 +283,7 @@ function() {
|
|
| 282 |
forceLight();
|
| 283 |
setInterval(forceLight, 1000);
|
| 284 |
|
| 285 |
-
// 2. مدیریت خطای GPU (شناسایی و جایگزینی
|
| 286 |
const observer = new MutationObserver((mutations) => {
|
| 287 |
mutations.forEach((mutation) => {
|
| 288 |
if (mutation.addedNodes.length) {
|
|
@@ -290,48 +291,75 @@ function() {
|
|
| 290 |
if (node.nodeType === 1) {
|
| 291 |
// بررسی متن داخل نود برای پیدا کردن خطا
|
| 292 |
const textContent = node.innerText || "";
|
| 293 |
-
|
| 294 |
-
|
| 295 |
// پیدا کردن کانتینر اصلی ارور (معمولاً کلاس toast-body دارد)
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
| 300 |
}
|
| 301 |
|
| 302 |
-
|
| 303 |
-
|
| 304 |
targetNode.innerHTML = `
|
| 305 |
-
<div style="
|
| 306 |
-
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
<div>
|
| 309 |
-
<h3 style="margin: 0; color: #c0392b; font-size: 18px; font-weight:
|
|
|
|
| 310 |
</div>
|
| 311 |
</div>
|
| 312 |
-
|
| 313 |
-
|
|
|
|
| 314 |
</p>
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
<p style="margin:
|
| 318 |
-
|
|
|
|
|
|
|
|
|
|
| 319 |
</p>
|
| 320 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
</div>
|
| 322 |
`;
|
| 323 |
targetNode.dataset.fixed = "true";
|
| 324 |
-
|
|
|
|
| 325 |
targetNode.style.backgroundColor = "transparent";
|
| 326 |
targetNode.style.border = "none";
|
| 327 |
targetNode.style.boxShadow = "none";
|
| 328 |
targetNode.style.padding = "0";
|
| 329 |
-
|
| 330 |
-
// اگر والدی دارد که استایل مزاحم دارد، آن را هم درست کن
|
| 331 |
-
if(targetNode.parentElement && targetNode.parentElement.classList.contains('toast-wrap')) {
|
| 332 |
-
targetNode.parentElement.style.boxShadow = "none";
|
| 333 |
-
targetNode.parentElement.style.border = "none";
|
| 334 |
-
}
|
| 335 |
}
|
| 336 |
}
|
| 337 |
}
|
|
|
|
| 235 |
return result, seed, get_success_html("تصویر با موفقیت ویرایش شد.")
|
| 236 |
|
| 237 |
except Exception as e:
|
| 238 |
+
# مدیریت خطای GPU: این متن دقیقا باید همین باشد تا JS آن را تشخیص دهد
|
| 239 |
error_str = str(e)
|
| 240 |
+
if "quota" in error_str.lower() or "exceeded" in error_str.lower():
|
| 241 |
raise gr.Error("You have exceeded your GPU quota")
|
| 242 |
return None, seed, get_error_html(f"خطا در پردازش: {error_str}")
|
| 243 |
|
|
|
|
| 259 |
} else if (!fileUrl && image.path) {
|
| 260 |
fileUrl = window.location.origin + "/file=" + image.path;
|
| 261 |
}
|
| 262 |
+
console.log("Sending download request for:", fileUrl);
|
| 263 |
window.parent.postMessage({
|
| 264 |
type: 'DOWNLOAD_REQUEST',
|
| 265 |
url: fileUrl
|
|
|
|
| 267 |
}
|
| 268 |
"""
|
| 269 |
|
| 270 |
+
# --- جاوااسکریپت هوشمند برای جایگزینی پیام خطا ---
|
| 271 |
js_global_func = """
|
| 272 |
function() {
|
| 273 |
// 1. اجبار تم روشن
|
|
|
|
| 283 |
forceLight();
|
| 284 |
setInterval(forceLight, 1000);
|
| 285 |
|
| 286 |
+
// 2. مدیریت خطای GPU (شناسایی و جایگزینی کامل)
|
| 287 |
const observer = new MutationObserver((mutations) => {
|
| 288 |
mutations.forEach((mutation) => {
|
| 289 |
if (mutation.addedNodes.length) {
|
|
|
|
| 291 |
if (node.nodeType === 1) {
|
| 292 |
// بررسی متن داخل نود برای پیدا کردن خطا
|
| 293 |
const textContent = node.innerText || "";
|
| 294 |
+
|
| 295 |
+
if (textContent.includes("exceeded your GPU quota")) {
|
| 296 |
// پیدا کردن کانتینر اصلی ارور (معمولاً کلاس toast-body دارد)
|
| 297 |
+
// ما تا بالاترین سطح ممکن (toast-wrap) بالا میرویم تا کل کادر زشت را حذف کنیم
|
| 298 |
+
|
| 299 |
+
let targetNode = node.classList.contains('toast-wrap') ? node : node.closest('.toast-wrap');
|
| 300 |
+
|
| 301 |
+
// اگر toast-wrap پیدا نشد، toast-body را پیدا کن
|
| 302 |
+
if (!targetNode) {
|
| 303 |
+
targetNode = node.classList.contains('toast-body') ? node : node.closest('.toast-body');
|
| 304 |
}
|
| 305 |
|
| 306 |
+
if (targetNode && !targetNode.dataset.fixed) {
|
| 307 |
+
// جایگزینی کل محتوای HTML با پیام فارسی زیبا
|
| 308 |
targetNode.innerHTML = `
|
| 309 |
+
<div style="
|
| 310 |
+
font-family: 'Vazirmatn', sans-serif;
|
| 311 |
+
direction: rtl;
|
| 312 |
+
text-align: right;
|
| 313 |
+
padding: 20px;
|
| 314 |
+
background: white;
|
| 315 |
+
border-radius: 15px;
|
| 316 |
+
border: 2px solid #e74c3c;
|
| 317 |
+
box-shadow: 0 10px 30px rgba(231, 76, 60, 0.2);
|
| 318 |
+
min-width: 300px;
|
| 319 |
+
">
|
| 320 |
+
<div style="display: flex; align-items: center; gap: 15px; margin-bottom: 15px; border-bottom: 1px solid #eee; padding-bottom: 10px;">
|
| 321 |
+
<span style="font-size: 35px;">✈️</span>
|
| 322 |
<div>
|
| 323 |
+
<h3 style="margin: 0; color: #c0392b; font-size: 18px; font-weight: 800;">ظرفیت سرور تکمیل شد!</h3>
|
| 324 |
+
<span style="font-size: 12px; color: #7f8c8d;">خطای محدودیت GPU</span>
|
| 325 |
</div>
|
| 326 |
</div>
|
| 327 |
+
|
| 328 |
+
<p style="margin: 0 0 15px 0; color: #34495e; font-size: 14px; line-height: 1.6;">
|
| 329 |
+
متأسفانه سهمیه رایگان پردازش شما تمام شده است. برای ادامه باید IP خود را تغییر دهید.
|
| 330 |
</p>
|
| 331 |
+
|
| 332 |
+
<div style="background-color: #e8f6f3; border: 1px dashed #1abc9c; border-radius: 10px; padding: 12px;">
|
| 333 |
+
<p style="margin: 0; color: #16a085; font-weight: bold; font-size: 13px; display: flex; align-items: center; gap: 5px;">
|
| 334 |
+
<span>💡</span> راه حل سریع:
|
| 335 |
+
</p>
|
| 336 |
+
<p style="margin: 8px 0 0 0; font-size: 13px; color: #0e6655; line-height: 1.5;">
|
| 337 |
+
لطفاً <b>حالت هواپیما (Airplane Mode)</b> را روشن/خاموش کنید و صفحه را رفرش نمایید.
|
| 338 |
</p>
|
| 339 |
</div>
|
| 340 |
+
|
| 341 |
+
<button onclick="this.closest('.toast-wrap').remove()" style="
|
| 342 |
+
margin-top: 15px;
|
| 343 |
+
width: 100%;
|
| 344 |
+
padding: 8px;
|
| 345 |
+
background: #ecf0f1;
|
| 346 |
+
border: none;
|
| 347 |
+
border-radius: 8px;
|
| 348 |
+
color: #7f8c8d;
|
| 349 |
+
cursor: pointer;
|
| 350 |
+
font-family: inherit;
|
| 351 |
+
font-size: 12px;
|
| 352 |
+
">بستن پیام</button>
|
| 353 |
</div>
|
| 354 |
`;
|
| 355 |
targetNode.dataset.fixed = "true";
|
| 356 |
+
|
| 357 |
+
// حذف استایلهای مزاحم پیشفرض Gradio از این نود
|
| 358 |
targetNode.style.backgroundColor = "transparent";
|
| 359 |
targetNode.style.border = "none";
|
| 360 |
targetNode.style.boxShadow = "none";
|
| 361 |
targetNode.style.padding = "0";
|
| 362 |
+
targetNode.className = "toast-wrap"; // حذف کلاسهای error و ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
}
|
| 364 |
}
|
| 365 |
}
|