Netrava commited on
Commit
9e6864d
Β·
verified Β·
1 Parent(s): 13840b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -27
app.py CHANGED
@@ -3,37 +3,39 @@ import os
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
- print("--- Starting final import with corrected path ---")
10
- try:
11
- project_root = os.path.dirname(os.path.abspath(__file__))
12
- parser_file_path = os.path.join(project_root, "util", "omniparser.py")
13
- print(f"DEBUG: Corrected path is: {parser_file_path}")
14
- module_name = "omniparser_from_util"
15
- spec = importlib.util.spec_from_file_location(module_name, parser_file_path)
16
- omniparser_module = importlib.util.module_from_spec(spec)
17
- spec.loader.exec_module(omniparser_module)
18
- OmniParser = omniparser_module.Omniparser
19
- print("DEBUG: Successfully loaded Omniparser class using importlib.")
20
- except Exception as e:
21
- print("!!! CRITICAL FAILURE during final importlib loading !!!")
22
- import traceback
23
- traceback.print_exc()
24
- raise e
25
-
26
- # --- The rest of the application ---
27
- print("Initializing model...")
28
-
29
- # THIS IS THE FINAL FIX: Call the constructor without the keyword argument.
30
- parser_instance = OmniParser("omniparser-v2-effnet")
31
  print("βœ… OmniParser model loaded successfully.")
32
 
 
 
 
33
  def parse_document(input_file):
34
  if input_file is None: return "Please upload a document.", "{}"
35
  print(f"Processing file: {input_file.name}")
36
  try:
 
37
  result = parser_instance.parse(input_file.name)
38
  markdown_text = result[0].get("text", "Markdown not found.") if isinstance(result, list) and result else "Output format not as expected."
39
  return markdown_text, json.dumps(result, indent=2)
@@ -49,8 +51,8 @@ iface = gr.Interface(
49
  gr.Markdown(label="Parsed Markdown Content"),
50
  gr.Code(label="Full JSON Output", language="json")
51
  ],
52
- title="Microsoft OmniParser Demo (Working)",
53
- description="This deployment is now fully functional."
54
  )
55
  iface.launch()
56
  print("--- Gradio UI launched ---")
 
3
  import gradio as gr
4
  import json
5
  import torch
6
+
7
+ # This part is correct and finds the project files.
8
+ # We don't need the complex importlib anymore as we have fixed all dependencies.
9
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
10
+
11
+
12
+ # --- START OF THE FINAL CORRECTED INITIALIZATION ---
13
+
14
+ # 1. Import the necessary class AND the special config helper function.
15
+ from util.omniparser import Omniparser
16
+ from util.utils import get_config
17
+
18
+ # 2. Define the model name.
19
+ model_name = "omniparser-v2-effnet"
20
+
21
+ # 3. Call the helper function to create the required config dictionary.
22
+ print(f"Generating configuration for model: {model_name}...")
23
+ config = get_config(model_name)
24
+ print("βœ… Configuration generated.")
25
+
26
+ # 4. Pass the CONFIG DICTIONARY to the class constructor.
27
+ print("Initializing model with config...")
28
+ parser_instance = Omniparser(config)
 
 
29
  print("βœ… OmniParser model loaded successfully.")
30
 
31
+ # --- END OF THE FINAL CORRECTED INITIALIZATION ---
32
+
33
+
34
  def parse_document(input_file):
35
  if input_file is None: return "Please upload a document.", "{}"
36
  print(f"Processing file: {input_file.name}")
37
  try:
38
+ # Use the instantiated parser
39
  result = parser_instance.parse(input_file.name)
40
  markdown_text = result[0].get("text", "Markdown not found.") if isinstance(result, list) and result else "Output format not as expected."
41
  return markdown_text, json.dumps(result, indent=2)
 
51
  gr.Markdown(label="Parsed Markdown Content"),
52
  gr.Code(label="Full JSON Output", language="json")
53
  ],
54
+ title="Microsoft OmniParser Demo",
55
+ description="A working deployment of the OmniParser model."
56
  )
57
  iface.launch()
58
  print("--- Gradio UI launched ---")