Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
import os
|
| 4 |
import gc
|
| 5 |
import shutil
|
| 6 |
import requests
|
|
@@ -15,33 +55,6 @@ from huggingface_hub import HfApi, hf_hub_download, list_repo_files, login
|
|
| 15 |
from safetensors.torch import load_file, save_file
|
| 16 |
from tqdm import tqdm
|
| 17 |
|
| 18 |
-
import pydantic
|
| 19 |
-
import torch
|
| 20 |
-
import pydantic_core
|
| 21 |
-
|
| 22 |
-
# Critical fix for torch.Tensor Pydantic schema
|
| 23 |
-
@classmethod
|
| 24 |
-
def _tensor_pydantic_schema(cls, handler):
|
| 25 |
-
from pydantic_core import core_schema
|
| 26 |
-
return core_schema.any_schema()
|
| 27 |
-
|
| 28 |
-
if not hasattr(torch.Tensor, '__get_pydantic_core_schema__'):
|
| 29 |
-
torch.Tensor.__get_pydantic_core_schema__ = _tensor_pydantic_schema
|
| 30 |
-
|
| 31 |
-
# Patch pydantic.create_model to always allow arbitrary types
|
| 32 |
-
_original_create_model = pydantic.create_model
|
| 33 |
-
|
| 34 |
-
def _patched_create_model(__model_name, __config__=None, **kwargs):
|
| 35 |
-
if __config__ is None:
|
| 36 |
-
__config__ = pydantic.ConfigDict(arbitrary_types_allowed=True)
|
| 37 |
-
elif isinstance(__config__, dict):
|
| 38 |
-
__config__ = {**__config__, "arbitrary_types_allowed": True}
|
| 39 |
-
elif hasattr(__config__, "arbitrary_types_allowed"):
|
| 40 |
-
__config__.arbitrary_types_allowed = True
|
| 41 |
-
|
| 42 |
-
return _original_create_model(__model_name, __config__=__config__, **kwargs)
|
| 43 |
-
|
| 44 |
-
pydantic.create_model = _patched_create_model
|
| 45 |
|
| 46 |
# --- Memory Efficient Safetensors ---
|
| 47 |
class MemoryEfficientSafeOpen:
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import warnings
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
warnings.filterwarnings("ignore", category=UserWarning, module="pydantic")
|
| 6 |
+
|
| 7 |
+
def _monkey_patch_for_mergekit():
|
| 8 |
+
|
| 9 |
+
import pydantic
|
| 10 |
+
import pydantic_core
|
| 11 |
+
import torch
|
| 12 |
+
|
| 13 |
+
original_validate = pydantic_core.core_schema.is_instance_schema
|
| 14 |
+
|
| 15 |
+
def patched_is_instance_schema(cls, *args, **kwargs):
|
| 16 |
+
if cls is torch.Tensor:
|
| 17 |
+
# Return a simple any_schema for torch.Tensor
|
| 18 |
+
return pydantic_core.core_schema.any_schema()
|
| 19 |
+
return original_validate(cls, *args, **kwargs)
|
| 20 |
+
|
| 21 |
+
pydantic_core.core_schema.is_instance_schema = patched_is_instance_schema
|
| 22 |
+
|
| 23 |
+
original_create_model = pydantic.create_model
|
| 24 |
+
|
| 25 |
+
def patched_create_model(__model_name, __config__=None, **kwargs):
|
| 26 |
+
if __config__ is None:
|
| 27 |
+
__config__ = pydantic.ConfigDict(arbitrary_types_allowed=True)
|
| 28 |
+
elif isinstance(__config__, dict):
|
| 29 |
+
__config__["arbitrary_types_allowed"] = True
|
| 30 |
+
elif hasattr(__config__, "arbitrary_types_allowed"):
|
| 31 |
+
__config__.arbitrary_types_allowed = True
|
| 32 |
+
|
| 33 |
+
return original_create_model(__model_name, __config__=__config__, **kwargs)
|
| 34 |
+
|
| 35 |
+
pydantic.create_model = patched_create_model
|
| 36 |
+
|
| 37 |
+
return True
|
| 38 |
+
|
| 39 |
+
if _monkey_patch_for_mergekit():
|
| 40 |
+
print("Applied monkey patches for MergeKit compatibility")
|
| 41 |
+
|
| 42 |
import gradio as gr
|
| 43 |
import torch
|
|
|
|
| 44 |
import gc
|
| 45 |
import shutil
|
| 46 |
import requests
|
|
|
|
| 55 |
from safetensors.torch import load_file, save_file
|
| 56 |
from tqdm import tqdm
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# --- Memory Efficient Safetensors ---
|
| 60 |
class MemoryEfficientSafeOpen:
|