Update patch_gradio.py
Browse files- patch_gradio.py +54 -27
patch_gradio.py
CHANGED
|
@@ -10,7 +10,8 @@ import glob
|
|
| 10 |
|
| 11 |
def apply_gradio_client_patch():
|
| 12 |
"""
|
| 13 |
-
اعمال پچ برای رفع باگ gradio_client
|
|
|
|
| 14 |
"""
|
| 15 |
try:
|
| 16 |
print("🔍 جستجوی فایل gradio_client/utils.py...")
|
|
@@ -42,37 +43,63 @@ def apply_gradio_client_patch():
|
|
| 42 |
with open(utils_file, 'r', encoding='utf-8') as f:
|
| 43 |
content = f.read()
|
| 44 |
|
| 45 |
-
# بررسی اینکه آیا پچ قبلاً اعمال شده یا نه
|
| 46 |
-
if 'isinstance(schema, dict) and "
|
| 47 |
-
print("✅ پچ gradio_client قبلاً اعمال شده است")
|
| 48 |
return True
|
| 49 |
|
| 50 |
-
# ا
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
if
|
| 56 |
-
content =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
|
| 77 |
except PermissionError:
|
| 78 |
print("❌ خطای دسترسی: برای اعمال پچ به دسترسی نوشتن نیاز است")
|
|
|
|
| 10 |
|
| 11 |
def apply_gradio_client_patch():
|
| 12 |
"""
|
| 13 |
+
اعمال پچ کامل برای رفع باگ gradio_client
|
| 14 |
+
رفع خطای: TypeError: argument of type 'bool' is not iterable
|
| 15 |
"""
|
| 16 |
try:
|
| 17 |
print("🔍 جستجوی فایل gradio_client/utils.py...")
|
|
|
|
| 43 |
with open(utils_file, 'r', encoding='utf-8') as f:
|
| 44 |
content = f.read()
|
| 45 |
|
| 46 |
+
# بررسی اینکه آیا پچ کامل قبلاً اعمال شده یا نه
|
| 47 |
+
if 'if not isinstance(schema, dict):' in content and 'return "any"' in content:
|
| 48 |
+
print("✅ پچ کامل gradio_client قبلاً اعمال شده است")
|
| 49 |
return True
|
| 50 |
|
| 51 |
+
# بکآپ فایل اصلی
|
| 52 |
+
backup_file = utils_file + '.backup'
|
| 53 |
+
if not os.path.exists(backup_file):
|
| 54 |
+
with open(backup_file, 'w', encoding='utf-8') as f:
|
| 55 |
+
f.write(content)
|
| 56 |
+
print(f"💾 بکآپ ایجاد شد: {backup_file}")
|
| 57 |
+
|
| 58 |
+
# اعمال پچ کامل
|
| 59 |
+
print("🔧 اعمال پچ کامل...")
|
| 60 |
+
|
| 61 |
+
# پچ کامل - جایگزینی تابع get_type
|
| 62 |
+
import re
|
| 63 |
+
old_function_pattern = r'def get_type\(schema\):\s*if isinstance\(schema, dict\) and "const" in schema:'
|
| 64 |
+
new_function = '''def get_type(schema):
|
| 65 |
+
# پچ کامل برای رفع باگ TypeError
|
| 66 |
+
if not isinstance(schema, dict):
|
| 67 |
+
return "any" # مقدار پیشفرض برای schema های غیر dict
|
| 68 |
+
if "const" in schema:'''
|
| 69 |
|
| 70 |
+
if re.search(old_function_pattern, content):
|
| 71 |
+
content = re.sub(old_function_pattern, new_function, content)
|
| 72 |
+
print("✅ پچ کامل با regex اعمال شد")
|
| 73 |
+
else:
|
| 74 |
+
# پچ مرحلهای اگر الگوی کامل پیدا نشد
|
| 75 |
+
simple_patches = [
|
| 76 |
+
# مرحله 1: پچ ساده
|
| 77 |
+
('if "const" in schema:', 'if isinstance(schema, dict) and "const" in schema:'),
|
| 78 |
+
# مرحله 2: پچ کامل
|
| 79 |
+
('if isinstance(schema, dict) and "const" in schema:',
|
| 80 |
+
'if not isinstance(schema, dict):\n return "any"\n if "const" in schema:')
|
| 81 |
+
]
|
| 82 |
|
| 83 |
+
patched = False
|
| 84 |
+
for i, (old_line, new_line) in enumerate(simple_patches, 1):
|
| 85 |
+
if old_line in content and new_line not in content:
|
| 86 |
+
content = content.replace(old_line, new_line)
|
| 87 |
+
print(f"✅ پچ مرحله {i} اعمال شد")
|
| 88 |
+
patched = True
|
| 89 |
+
break
|
| 90 |
|
| 91 |
+
if not patched:
|
| 92 |
+
print("⚠️ هیچ الگوی قابل پچ پیدا نشد")
|
| 93 |
+
print("💡 احتمالاً نسخه gradio_client متفاوت است")
|
| 94 |
+
return False
|
| 95 |
+
|
| 96 |
+
# نوشتن فایل اصلاح شده
|
| 97 |
+
with open(utils_file, 'w', encoding='utf-8') as f:
|
| 98 |
+
f.write(content)
|
| 99 |
+
|
| 100 |
+
print(f"✅ پچ کامل gradio_client با موفقیت اعمال شد!")
|
| 101 |
+
print(f"📍 مسیر فایل: {utils_file}")
|
| 102 |
+
return True
|
| 103 |
|
| 104 |
except PermissionError:
|
| 105 |
print("❌ خطای دسترسی: برای اعمال پچ به دسترسی نوشتن نیاز است")
|