Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,67 +1,81 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
-
"""Hugging Face Spaces
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import importlib.util
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
app_dir = Path("/app")
|
| 9 |
-
|
| 10 |
-
# Add app_dir to sys.path
|
| 11 |
sys.path.insert(0, str(app_dir))
|
| 12 |
|
| 13 |
-
print(f"
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
|
|
|
| 17 |
ui_app_path = None
|
| 18 |
for item in app_dir.iterdir():
|
| 19 |
-
print(f" {item.name}")
|
| 20 |
-
# Look for the literal filename "ui\app.py"
|
| 21 |
if item.name == "ui\\app.py":
|
| 22 |
ui_app_path = item
|
|
|
|
| 23 |
|
| 24 |
if ui_app_path and ui_app_path.exists():
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# Read the file content and fix imports
|
| 28 |
-
with open(str(ui_app_path), 'r') as f:
|
| 29 |
code = f.read()
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
).replace(
|
| 36 |
-
"from src.",
|
| 37 |
-
"from "
|
| 38 |
-
)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
src_files = [f for f in app_dir.iterdir() if str(f).startswith(str(app_dir) + "/src")]
|
| 42 |
-
for src_file in src_files:
|
| 43 |
-
if src_file.name.endswith(".py") and not src_file.name.startswith("_"):
|
| 44 |
-
module_name = src_file.stem
|
| 45 |
-
try:
|
| 46 |
-
spec = importlib.util.spec_from_file_location(module_name, str(src_file))
|
| 47 |
-
module = importlib.util.module_from_spec(spec)
|
| 48 |
-
sys.modules[module_name] = module
|
| 49 |
-
spec.loader.exec_module(module)
|
| 50 |
-
print(f" Loaded {module_name}")
|
| 51 |
-
except Exception as e:
|
| 52 |
-
print(f" Warning loading {module_name}: {e}")
|
| 53 |
|
| 54 |
-
# Execute the
|
| 55 |
-
|
| 56 |
try:
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
-
print(f"
|
| 60 |
import traceback
|
| 61 |
traceback.print_exc()
|
| 62 |
else:
|
| 63 |
-
print(f"
|
| 64 |
-
print(f"Looking for files with backslash in name...")
|
| 65 |
-
for item in app_dir.iterdir():
|
| 66 |
-
if "\\" in item.name:
|
| 67 |
-
print(f" Found: {item.name}")
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
+
"""Hugging Face Spaces - fix backslash filenames"""
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import importlib.util
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
app_dir = Path("/app")
|
|
|
|
|
|
|
| 9 |
sys.path.insert(0, str(app_dir))
|
| 10 |
|
| 11 |
+
print(f"Starting app from {app_dir}\n")
|
| 12 |
+
|
| 13 |
+
# Step 1: Preload ALL src modules with proper names (no backslashes)
|
| 14 |
+
print("Loading src modules:")
|
| 15 |
+
src_modules = {}
|
| 16 |
+
for item in app_dir.iterdir():
|
| 17 |
+
name = item.name
|
| 18 |
+
if name.startswith("src\\") and name.endswith(".py"):
|
| 19 |
+
# Extract module name: "src\models.py" -> "models"
|
| 20 |
+
module_name = name.split("\\")[-1].replace(".py", "")
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
spec = importlib.util.spec_from_file_location(module_name, str(item))
|
| 24 |
+
module = importlib.util.module_from_spec(spec)
|
| 25 |
+
sys.modules[module_name] = module
|
| 26 |
+
src_modules[module_name] = module
|
| 27 |
+
spec.loader.exec_module(module)
|
| 28 |
+
print(f" {module_name}")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f" {module_name}: {e}")
|
| 31 |
|
| 32 |
+
# Step 2: Fix imports in the code
|
| 33 |
+
print("\nPreparing ui/app.py code:")
|
| 34 |
ui_app_path = None
|
| 35 |
for item in app_dir.iterdir():
|
|
|
|
|
|
|
| 36 |
if item.name == "ui\\app.py":
|
| 37 |
ui_app_path = item
|
| 38 |
+
break
|
| 39 |
|
| 40 |
if ui_app_path and ui_app_path.exists():
|
| 41 |
+
with open(str(ui_app_path), 'r', encoding='utf-8', errors='ignore') as f:
|
|
|
|
|
|
|
|
|
|
| 42 |
code = f.read()
|
| 43 |
|
| 44 |
+
# Fix all src imports
|
| 45 |
+
original_code = code
|
| 46 |
+
code = code.replace("from src.", "from ")
|
| 47 |
+
code = code.replace("import src.", "import ")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
print(f" Fixed imports in ui/app.py")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Step 3: Execute the code
|
| 52 |
+
print("\nLaunching Gradio app:")
|
| 53 |
try:
|
| 54 |
+
exec_globals = {
|
| 55 |
+
"__name__": "__main__",
|
| 56 |
+
"__file__": str(ui_app_path),
|
| 57 |
+
"create_gradio_app": None
|
| 58 |
+
}
|
| 59 |
+
exec_locals = {}
|
| 60 |
+
|
| 61 |
+
exec(compile(code, str(ui_app_path), "exec"), exec_globals, exec_locals)
|
| 62 |
+
|
| 63 |
+
# Get the function from locals
|
| 64 |
+
create_gradio_app = exec_globals.get("create_gradio_app") or exec_locals.get("create_gradio_app")
|
| 65 |
+
|
| 66 |
+
if create_gradio_app:
|
| 67 |
+
demo = create_gradio_app()
|
| 68 |
+
demo.launch(
|
| 69 |
+
server_name="0.0.0.0",
|
| 70 |
+
server_port=int(os.environ.get("PORT", 7860)),
|
| 71 |
+
show_error=True
|
| 72 |
+
)
|
| 73 |
+
else:
|
| 74 |
+
print("ERROR: create_gradio_app not found after execution")
|
| 75 |
+
|
| 76 |
except Exception as e:
|
| 77 |
+
print(f"ERROR: {e}")
|
| 78 |
import traceback
|
| 79 |
traceback.print_exc()
|
| 80 |
else:
|
| 81 |
+
print(f"ERROR: ui\\app.py not found")
|
|
|
|
|
|
|
|
|
|
|
|