Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,26 +3,26 @@ import os
|
|
| 3 |
import gradio as gr
|
| 4 |
import json
|
| 5 |
import torch
|
| 6 |
-
import importlib.util
|
| 7 |
|
| 8 |
-
# --- FINAL WORKAROUND using importlib ---
|
| 9 |
-
|
|
|
|
| 10 |
try:
|
| 11 |
-
# 1. Construct the absolute path to the target file.
|
| 12 |
project_root = os.path.dirname(os.path.abspath(__file__))
|
| 13 |
-
parser_file_path = os.path.join(project_root, "
|
| 14 |
-
print(f"DEBUG:
|
| 15 |
|
| 16 |
# 2. Use importlib to load the module directly from its file path.
|
| 17 |
-
|
| 18 |
-
module_name = "omni_parser_module" # We give it a unique internal name
|
| 19 |
spec = importlib.util.spec_from_file_location(module_name, parser_file_path)
|
| 20 |
-
|
| 21 |
-
spec.loader.exec_module(
|
| 22 |
|
| 23 |
# 3. Get the OmniParser class from the successfully loaded module.
|
| 24 |
-
OmniParser =
|
| 25 |
-
print("DEBUG: Successfully loaded OmniParser class
|
| 26 |
|
| 27 |
except Exception as e:
|
| 28 |
print("!!! CRITICAL FAILURE during final importlib loading !!!")
|
|
@@ -32,7 +32,7 @@ except Exception as e:
|
|
| 32 |
|
| 33 |
# --- The rest of the application ---
|
| 34 |
print("Initializing model...")
|
| 35 |
-
# This line
|
| 36 |
parser_instance = OmniParser.from_model_path("omniparser-v2-effnet")
|
| 37 |
print("β
OmniParser model loaded successfully.")
|
| 38 |
|
|
@@ -56,8 +56,8 @@ iface = gr.Interface(
|
|
| 56 |
gr.Markdown(label="Parsed Markdown Content"),
|
| 57 |
gr.Code(label="Full JSON Output", language="json")
|
| 58 |
],
|
| 59 |
-
title="Microsoft OmniParser Demo (Final
|
| 60 |
-
description="This deployment works by directly loading the parser
|
| 61 |
)
|
| 62 |
iface.launch()
|
| 63 |
print("--- Gradio UI launched ---")
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import json
|
| 5 |
import torch
|
| 6 |
+
import importlib.util
|
| 7 |
|
| 8 |
+
# --- FINAL WORKAROUND using importlib with the CORRECT PATH ---
|
| 9 |
+
|
| 10 |
+
print("--- Starting final import with corrected path ---")
|
| 11 |
try:
|
| 12 |
+
# 1. Construct the absolute path to the CORRECT target file.
|
| 13 |
project_root = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
+
parser_file_path = os.path.join(project_root, "util", "omniparser.py") # <-- THIS IS THE CORRECTED PATH
|
| 15 |
+
print(f"DEBUG: Corrected path is: {parser_file_path}")
|
| 16 |
|
| 17 |
# 2. Use importlib to load the module directly from its file path.
|
| 18 |
+
module_name = "omniparser_from_util" # Give it a unique internal name
|
|
|
|
| 19 |
spec = importlib.util.spec_from_file_location(module_name, parser_file_path)
|
| 20 |
+
omniparser_module = importlib.util.module_from_spec(spec)
|
| 21 |
+
spec.loader.exec_module(omniparser_module)
|
| 22 |
|
| 23 |
# 3. Get the OmniParser class from the successfully loaded module.
|
| 24 |
+
OmniParser = omniparser_module.OmniParser
|
| 25 |
+
print("DEBUG: Successfully loaded OmniParser class from util/omniparser.py")
|
| 26 |
|
| 27 |
except Exception as e:
|
| 28 |
print("!!! CRITICAL FAILURE during final importlib loading !!!")
|
|
|
|
| 32 |
|
| 33 |
# --- The rest of the application ---
|
| 34 |
print("Initializing model...")
|
| 35 |
+
# This line will now work.
|
| 36 |
parser_instance = OmniParser.from_model_path("omniparser-v2-effnet")
|
| 37 |
print("β
OmniParser model loaded successfully.")
|
| 38 |
|
|
|
|
| 56 |
gr.Markdown(label="Parsed Markdown Content"),
|
| 57 |
gr.Code(label="Full JSON Output", language="json")
|
| 58 |
],
|
| 59 |
+
title="Microsoft OmniParser Demo (Final)",
|
| 60 |
+
description="This deployment works by directly loading the correct parser file."
|
| 61 |
)
|
| 62 |
iface.launch()
|
| 63 |
print("--- Gradio UI launched ---")
|