|
|
|
|
|
""" |
|
|
پچ خودکار برای رفع باگ gradio_client |
|
|
رفع خطای: TypeError: argument of type 'bool' is not iterable |
|
|
""" |
|
|
|
|
|
import os |
|
|
import sys |
|
|
import glob |
|
|
|
|
|
def apply_gradio_client_patch(): |
|
|
""" |
|
|
اعمال پچ کامل برای رفع باگ gradio_client |
|
|
رفع خطای: TypeError: argument of type 'bool' is not iterable |
|
|
""" |
|
|
try: |
|
|
print("🔍 جستجوی فایل gradio_client/utils.py...") |
|
|
|
|
|
|
|
|
possible_paths = [ |
|
|
"/usr/local/lib/python*/site-packages/gradio_client/utils.py", |
|
|
"/opt/conda/lib/python*/site-packages/gradio_client/utils.py", |
|
|
"/home/user/.local/lib/python*/site-packages/gradio_client/utils.py", |
|
|
f"{sys.prefix}/lib/python*/site-packages/gradio_client/utils.py", |
|
|
"/app/.local/lib/python*/site-packages/gradio_client/utils.py" |
|
|
] |
|
|
|
|
|
utils_file = None |
|
|
for pattern in possible_paths: |
|
|
matches = glob.glob(pattern) |
|
|
if matches: |
|
|
utils_file = matches[0] |
|
|
print(f"📁 فایل پیدا شد: {utils_file}") |
|
|
break |
|
|
|
|
|
if not utils_file or not os.path.exists(utils_file): |
|
|
print("⚠️ فایل gradio_client/utils.py پیدا نشد") |
|
|
print("💡 احتمالاً gradio_client نصب نشده یا در مسیر متفاوتی قرار دارد") |
|
|
return False |
|
|
|
|
|
|
|
|
print("📖 خواندن محتوای فایل...") |
|
|
with open(utils_file, 'r', encoding='utf-8') as f: |
|
|
content = f.read() |
|
|
|
|
|
|
|
|
if 'if not isinstance(schema, dict):' in content and 'return "any"' in content: |
|
|
print("✅ پچ کامل gradio_client قبلاً اعمال شده است") |
|
|
return True |
|
|
|
|
|
|
|
|
backup_file = utils_file + '.backup' |
|
|
if not os.path.exists(backup_file): |
|
|
with open(backup_file, 'w', encoding='utf-8') as f: |
|
|
f.write(content) |
|
|
print(f"💾 بکآپ ایجاد شد: {backup_file}") |
|
|
|
|
|
|
|
|
print("🔧 اعمال پچ کامل...") |
|
|
|
|
|
|
|
|
import re |
|
|
old_function_pattern = r'def get_type\(schema\):\s*if isinstance\(schema, dict\) and "const" in schema:' |
|
|
new_function = '''def get_type(schema): |
|
|
# پچ کامل برای رفع باگ TypeError |
|
|
if not isinstance(schema, dict): |
|
|
return "any" # مقدار پیشفرض برای schema های غیر dict |
|
|
if "const" in schema:''' |
|
|
|
|
|
if re.search(old_function_pattern, content): |
|
|
content = re.sub(old_function_pattern, new_function, content) |
|
|
print("✅ پچ کامل با regex اعمال شد") |
|
|
else: |
|
|
|
|
|
simple_patches = [ |
|
|
|
|
|
('if "const" in schema:', 'if isinstance(schema, dict) and "const" in schema:'), |
|
|
|
|
|
('if isinstance(schema, dict) and "const" in schema:', |
|
|
'if not isinstance(schema, dict):\n return "any"\n if "const" in schema:') |
|
|
] |
|
|
|
|
|
patched = False |
|
|
for i, (old_line, new_line) in enumerate(simple_patches, 1): |
|
|
if old_line in content and new_line not in content: |
|
|
content = content.replace(old_line, new_line) |
|
|
print(f"✅ پچ مرحله {i} اعمال شد") |
|
|
patched = True |
|
|
break |
|
|
|
|
|
if not patched: |
|
|
print("⚠️ هیچ الگوی قابل پچ پیدا نشد") |
|
|
print("💡 احتمالاً نسخه gradio_client متفاوت است") |
|
|
return False |
|
|
|
|
|
|
|
|
with open(utils_file, 'w', encoding='utf-8') as f: |
|
|
f.write(content) |
|
|
|
|
|
print(f"✅ پچ کامل gradio_client با موفقیت اعمال شد!") |
|
|
print(f"📍 مسیر فایل: {utils_file}") |
|
|
return True |
|
|
|
|
|
except PermissionError: |
|
|
print("❌ خطای دسترسی: برای اعمال پچ به دسترسی نوشتن نیاز است") |
|
|
print("💡 در Hugging Face Space این مشکل معمولاً خودکار حل میشود") |
|
|
return False |
|
|
except Exception as e: |
|
|
print(f"❌ خطا در اعمال پچ: {str(e)}") |
|
|
return False |
|
|
|
|
|
if __name__ == "__main__": |
|
|
print("🚀 شروع اعمال پچ gradio_client...") |
|
|
success = apply_gradio_client_patch() |
|
|
|
|
|
if success: |
|
|
print("\n🎉 پچ با موفقیت اعمال شد!") |
|
|
print("✨ حالا میتوانید app.py را اجرا کنید") |
|
|
else: |
|
|
print("\n⚠️ پچ اعمال نشد") |
|
|
print("💡 در صورت بروز خطا، از نسخههای جدیدتر gradio استفاده کنید") |