Update app.py
Browse files
app.py
CHANGED
|
@@ -41,27 +41,33 @@ def patch_file_content(filename):
|
|
| 41 |
try:
|
| 42 |
with open(filename, "r", encoding="utf-8") as f: content = f.read()
|
| 43 |
|
| 44 |
-
#
|
| 45 |
content = content.replace("device = 'cuda'", "device = 'cpu'")
|
| 46 |
content = content.replace('device = "cuda"', 'device = "cpu"')
|
| 47 |
content = content.replace("torch.device('cuda')", "torch.device('cpu')")
|
| 48 |
content = content.replace(".to(device)", "")
|
| 49 |
content = content.replace(".cuda()", ".cpu()")
|
| 50 |
|
| 51 |
-
#
|
| 52 |
content = re.sub(r'.*Found no NVIDIA driver.*', ' pass', content)
|
| 53 |
content = re.sub(r'.*torch.cuda.is_available().*', ' pass', content)
|
| 54 |
content = re.sub(r'.*torch._C._cuda_init().*', ' pass', content)
|
| 55 |
|
| 56 |
-
#
|
| 57 |
old_func = "def on_select_mask_tab(state):"
|
| 58 |
new_func = "def on_select_mask_tab(state):" + NL + " if not state or 'sample' not in state: return None"
|
| 59 |
content = content.replace(old_func, new_func)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
with open(filename, "w", encoding="utf-8") as f: f.write(content)
|
| 67 |
print("Patched: " + filename)
|
|
@@ -70,11 +76,9 @@ def patch_file_content(filename):
|
|
| 70 |
|
| 71 |
def recursive_patch():
|
| 72 |
print("Starting deep patch...")
|
| 73 |
-
# فایلهای اصلی
|
| 74 |
patch_file_content("gradio_app.py")
|
| 75 |
patch_file_content("visualizer_drag_gradio.py")
|
| 76 |
|
| 77 |
-
# تمام فایلهای داخلی
|
| 78 |
for root, dirs, files in os.walk("draggan"):
|
| 79 |
for file in files:
|
| 80 |
if file.endswith(".py"):
|
|
@@ -123,7 +127,13 @@ def load_app(filename):
|
|
| 123 |
current_dir = os.getcwd()
|
| 124 |
if current_dir not in sys.path: sys.path.append(current_dir)
|
| 125 |
|
| 126 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
torch.cuda.is_available = lambda: False
|
| 128 |
torch.backends.cudnn.enabled = False
|
| 129 |
|
|
|
|
| 41 |
try:
|
| 42 |
with open(filename, "r", encoding="utf-8") as f: content = f.read()
|
| 43 |
|
| 44 |
+
# GPU -> CPU
|
| 45 |
content = content.replace("device = 'cuda'", "device = 'cpu'")
|
| 46 |
content = content.replace('device = "cuda"', 'device = "cpu"')
|
| 47 |
content = content.replace("torch.device('cuda')", "torch.device('cpu')")
|
| 48 |
content = content.replace(".to(device)", "")
|
| 49 |
content = content.replace(".cuda()", ".cpu()")
|
| 50 |
|
| 51 |
+
# Disable Driver Checks
|
| 52 |
content = re.sub(r'.*Found no NVIDIA driver.*', ' pass', content)
|
| 53 |
content = re.sub(r'.*torch.cuda.is_available().*', ' pass', content)
|
| 54 |
content = re.sub(r'.*torch._C._cuda_init().*', ' pass', content)
|
| 55 |
|
| 56 |
+
# Fix State Errors
|
| 57 |
old_func = "def on_select_mask_tab(state):"
|
| 58 |
new_func = "def on_select_mask_tab(state):" + NL + " if not state or 'sample' not in state: return None"
|
| 59 |
content = content.replace(old_func, new_func)
|
| 60 |
|
| 61 |
+
# Fix NameError: inverse_image (CORRECTED PATH)
|
| 62 |
+
# The file is likely at draggan/stylegan2/inversion.py, but usually exposed via draggan.web or similiar
|
| 63 |
+
# We will inject the import directly from the file path to be safe
|
| 64 |
+
if "def on_image_change" in content and "inverse_image" not in content:
|
| 65 |
+
# Try importing from the known structure
|
| 66 |
+
import_line = "from draggan.stylegan2.inversion import inverse_image" + NL
|
| 67 |
+
# If the previous attempt failed, it might be because 'draggan' package isn't fully resolvable this way
|
| 68 |
+
# Let's try a relative import fix or just rely on the fact we add cwd to sys.path later
|
| 69 |
+
if "from draggan.web import" not in content:
|
| 70 |
+
content = import_line + content
|
| 71 |
|
| 72 |
with open(filename, "w", encoding="utf-8") as f: f.write(content)
|
| 73 |
print("Patched: " + filename)
|
|
|
|
| 76 |
|
| 77 |
def recursive_patch():
|
| 78 |
print("Starting deep patch...")
|
|
|
|
| 79 |
patch_file_content("gradio_app.py")
|
| 80 |
patch_file_content("visualizer_drag_gradio.py")
|
| 81 |
|
|
|
|
| 82 |
for root, dirs, files in os.walk("draggan"):
|
| 83 |
for file in files:
|
| 84 |
if file.endswith(".py"):
|
|
|
|
| 127 |
current_dir = os.getcwd()
|
| 128 |
if current_dir not in sys.path: sys.path.append(current_dir)
|
| 129 |
|
| 130 |
+
# Manually import the missing module to ensure it exists before app loads
|
| 131 |
+
try:
|
| 132 |
+
import draggan.stylegan2.inversion
|
| 133 |
+
print("Successfully pre-imported inversion module.")
|
| 134 |
+
except ImportError:
|
| 135 |
+
print("Could not pre-import inversion module. Path might be wrong.")
|
| 136 |
+
|
| 137 |
torch.cuda.is_available = lambda: False
|
| 138 |
torch.backends.cudnn.enabled = False
|
| 139 |
|