Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -235,7 +235,11 @@ def infer(
|
|
| 235 |
return result, seed, get_success_html("تصویر با موفقیت ویرایش شد.")
|
| 236 |
|
| 237 |
except Exception as e:
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
@spaces.GPU(duration=30)
|
| 241 |
def infer_example(input_image, prompt, lora_adapter):
|
|
@@ -255,7 +259,6 @@ async (image) => {
|
|
| 255 |
} else if (!fileUrl && image.path) {
|
| 256 |
fileUrl = window.location.origin + "/file=" + image.path;
|
| 257 |
}
|
| 258 |
-
console.log("Sending download request for:", fileUrl);
|
| 259 |
window.parent.postMessage({
|
| 260 |
type: 'DOWNLOAD_REQUEST',
|
| 261 |
url: fileUrl
|
|
@@ -263,8 +266,7 @@ async (image) => {
|
|
| 263 |
}
|
| 264 |
"""
|
| 265 |
|
| 266 |
-
# --- جاوااسکریپت اصلی (شامل مدیریت هوشمند خطا) ---
|
| 267 |
-
# این اسکریپت با دقت هر تغییری در صفحه را رصد میکند و اگر خطای GPU را ببیند، آن را جایگزین میکند.
|
| 268 |
js_global_func = """
|
| 269 |
function() {
|
| 270 |
// 1. اجبار تم روشن
|
|
@@ -280,50 +282,55 @@ function() {
|
|
| 280 |
forceLight();
|
| 281 |
setInterval(forceLight, 1000);
|
| 282 |
|
| 283 |
-
// 2. مدیریت خطای GPU (نس
|
| 284 |
const observer = new MutationObserver((mutations) => {
|
| 285 |
mutations.forEach((mutation) => {
|
| 286 |
if (mutation.addedNodes.length) {
|
| 287 |
mutation.addedNodes.forEach((node) => {
|
| 288 |
-
|
| 289 |
-
|
| 290 |
const textContent = node.innerText || "";
|
| 291 |
if (textContent.includes("exceeded your GPU quota") || textContent.includes("GPU quota")) {
|
| 292 |
|
| 293 |
-
// پیدا کردن کانتینر اصلی ارور (معمولاً کلاس toast-body
|
| 294 |
-
// اگر خودش کانتینر نبود، والدش را پیدا میکنیم یا خودش را تغییر میدهیم
|
| 295 |
let targetNode = node;
|
| 296 |
if (!targetNode.classList.contains('toast-body')) {
|
| 297 |
const parentToast = targetNode.closest('.toast-body');
|
| 298 |
if (parentToast) targetNode = parentToast;
|
| 299 |
}
|
| 300 |
|
|
|
|
| 301 |
if (!targetNode.dataset.fixed) {
|
| 302 |
targetNode.innerHTML = `
|
| 303 |
-
<div style="font-family: 'Vazirmatn', sans-serif; direction: rtl; text-align: right; padding:
|
| 304 |
-
<div style="display: flex; align-items: center; gap:
|
| 305 |
-
<span style="font-size:
|
| 306 |
-
<
|
|
|
|
|
|
|
| 307 |
</div>
|
| 308 |
-
<p style="margin:
|
| 309 |
-
سهمیه رایگان استفاده از سرور تمام شده است.
|
| 310 |
</p>
|
| 311 |
-
<div style="background-color: #
|
| 312 |
-
<p style="margin: 0; color: #
|
| 313 |
-
<p style="margin: 5px 0 0 0; font-size: 13px; color: #
|
| 314 |
-
لطفاً <b>حالت هواپیما (Airplane Mode)</b> گوشی خود را روشن و سپس خاموش کنید
|
| 315 |
</p>
|
| 316 |
</div>
|
| 317 |
</div>
|
| 318 |
`;
|
| 319 |
targetNode.dataset.fixed = "true";
|
| 320 |
-
//
|
|
|
|
| 321 |
targetNode.style.border = "none";
|
| 322 |
-
targetNode.style.boxShadow = "
|
| 323 |
-
|
| 324 |
-
|
|
|
|
|
|
|
|
|
|
| 325 |
targetNode.parentElement.style.border = "none";
|
| 326 |
-
targetNode.parentElement.style.backgroundColor = "transparent";
|
| 327 |
}
|
| 328 |
}
|
| 329 |
}
|
|
@@ -495,7 +502,7 @@ textarea:focus, input[type="text"]:focus {{
|
|
| 495 |
footer {{ display: none !important; }}
|
| 496 |
.flagging {{ display: none !important; }}
|
| 497 |
|
| 498 |
-
/* استایل Toast */
|
| 499 |
.toast-body {{
|
| 500 |
direction: rtl !important;
|
| 501 |
text-align: right !important;
|
|
|
|
| 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():
|
| 241 |
+
raise gr.Error("You have exceeded your GPU quota")
|
| 242 |
+
return None, seed, get_error_html(f"خطا در پردازش: {error_str}")
|
| 243 |
|
| 244 |
@spaces.GPU(duration=30)
|
| 245 |
def infer_example(input_image, prompt, lora_adapter):
|
|
|
|
| 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 |
}
|
| 267 |
"""
|
| 268 |
|
| 269 |
+
# --- جاوااسکریپت اصلی (شامل مدیریت هوشمند خطای GPU و حالت هواپیما) ---
|
|
|
|
| 270 |
js_global_func = """
|
| 271 |
function() {
|
| 272 |
// 1. اجبار تم روشن
|
|
|
|
| 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) {
|
| 289 |
mutation.addedNodes.forEach((node) => {
|
| 290 |
+
if (node.nodeType === 1) {
|
| 291 |
+
// بررسی متن داخل نود برای پیدا کردن خطا
|
| 292 |
const textContent = node.innerText || "";
|
| 293 |
if (textContent.includes("exceeded your GPU quota") || textContent.includes("GPU quota")) {
|
| 294 |
|
| 295 |
+
// پیدا کردن کانتینر اصلی ارور (معمولاً کلاس toast-body دارد)
|
|
|
|
| 296 |
let targetNode = node;
|
| 297 |
if (!targetNode.classList.contains('toast-body')) {
|
| 298 |
const parentToast = targetNode.closest('.toast-body');
|
| 299 |
if (parentToast) targetNode = parentToast;
|
| 300 |
}
|
| 301 |
|
| 302 |
+
// جایگزینی محتوا
|
| 303 |
if (!targetNode.dataset.fixed) {
|
| 304 |
targetNode.innerHTML = `
|
| 305 |
+
<div style="font-family: 'Vazirmatn', sans-serif; direction: rtl; text-align: right; padding: 15px; background: white; border-radius: 12px; border: 2px solid #e74c3c;">
|
| 306 |
+
<div style="display: flex; align-items: center; gap: 12px; margin-bottom: 10px;">
|
| 307 |
+
<span style="font-size: 32px;">✈️</span>
|
| 308 |
+
<div>
|
| 309 |
+
<h3 style="margin: 0; color: #c0392b; font-size: 18px; font-weight: bold;">پایان سهمیه پردازش</h3>
|
| 310 |
+
</div>
|
| 311 |
</div>
|
| 312 |
+
<p style="margin: 0 0 12px 0; color: #555; font-size: 14px;">
|
| 313 |
+
سهمیه رایگان شما برای استفاده از سرور تمام شده است.
|
| 314 |
</p>
|
| 315 |
+
<div style="background-color: #e3f2fd; border: 1px solid #64b5f6; border-radius: 8px; padding: 10px;">
|
| 316 |
+
<p style="margin: 0; color: #1565c0; font-weight: bold; font-size: 13px;">💡 راه حل پیشنهادی:</p>
|
| 317 |
+
<p style="margin: 5px 0 0 0; font-size: 13px; color: #0d47a1;">
|
| 318 |
+
لطفاً <b>حالت هواپیما (Airplane Mode)</b> گوشی خود را روشن و سپس خاموش کنید تا IP شما تغییر کند، سپس دوباره تلاش کنید.
|
| 319 |
</p>
|
| 320 |
</div>
|
| 321 |
</div>
|
| 322 |
`;
|
| 323 |
targetNode.dataset.fixed = "true";
|
| 324 |
+
// حذف استایلهای پیشفرض زشت Gradio
|
| 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 |
}
|
|
|
|
| 502 |
footer {{ display: none !important; }}
|
| 503 |
.flagging {{ display: none !important; }}
|
| 504 |
|
| 505 |
+
/* استایل Toast برای راست چین کردن */
|
| 506 |
.toast-body {{
|
| 507 |
direction: rtl !important;
|
| 508 |
text-align: right !important;
|