Netrava commited on
Commit
59fa405
Β·
verified Β·
1 Parent(s): 13cc5bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -4,23 +4,43 @@ import gradio as gr
4
  import json
5
  import torch
6
 
7
- sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
8
 
9
- from omnitool.omniparserserver.parser import OmniParser
 
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  print("Initializing...")
12
- parser = OmniParser.from_model_path("omniparser-v2-effnet")
 
13
  print("βœ… OmniParser model loaded successfully.")
14
 
15
  def parse_document(input_file):
16
  if input_file is None: return "Please upload a document.", "{}"
17
  print(f"Processing file: {input_file.name}")
18
  try:
19
- result = parser.parse(input_file.name)
 
20
  markdown_text = result[0].get("text", "Markdown not found.") if isinstance(result, list) and result else "Output format not as expected."
21
  return markdown_text, json.dumps(result, indent=2)
22
  except Exception as e:
23
- return f"An error occurred: {e}", f"Error: {e}"
 
 
24
 
25
  iface = gr.Interface(
26
  fn=parse_document,
@@ -29,7 +49,7 @@ iface = gr.Interface(
29
  gr.Markdown(label="Parsed Markdown Content"),
30
  gr.Code(label="Full JSON Output", language="json")
31
  ],
32
- title="Microsoft OmniParser Demo",
33
- description="A working deployment."
34
  )
35
  iface.launch()
 
4
  import json
5
  import torch
6
 
7
+ # --- START OF WORKAROUND for Microsoft's missing __init__.py files ---
8
 
9
+ # 1. Get the absolute path to the project's root folder.
10
+ project_root = os.path.dirname(os.path.abspath(__file__))
11
 
12
+ # 2. Build the exact path to the folder containing parser.py
13
+ parser_folder_path = os.path.join(project_root, "omnitool", "omniparserserver")
14
+
15
+ # 3. Forcefully add THAT specific folder to the Python path.
16
+ sys.path.insert(0, parser_folder_path)
17
+
18
+ # 4. Now, import the 'parser' module directly, and get the class from it.
19
+ # This bypasses the broken package structure.
20
+ import parser
21
+ OmniParser = parser.OmniParser
22
+
23
+ # --- END OF WORKAROUND ---
24
+
25
+
26
+ # --- The rest of the application ---
27
  print("Initializing...")
28
+ # This line should now work because we have successfully imported OmniParser.
29
+ parser_instance = OmniParser.from_model_path("omniparser-v2-effnet")
30
  print("βœ… OmniParser model loaded successfully.")
31
 
32
  def parse_document(input_file):
33
  if input_file is None: return "Please upload a document.", "{}"
34
  print(f"Processing file: {input_file.name}")
35
  try:
36
+ # Use the instantiated parser
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)
40
  except Exception as e:
41
+ import traceback
42
+ traceback.print_exc()
43
+ return f"An error occurred during parsing: {e}", f"Error: {e}"
44
 
45
  iface = gr.Interface(
46
  fn=parse_document,
 
49
  gr.Markdown(label="Parsed Markdown Content"),
50
  gr.Code(label="Full JSON Output", language="json")
51
  ],
52
+ title="Microsoft OmniParser Demo (Final Workaround)",
53
+ description="This deployment works by directly injecting the path to the parser module."
54
  )
55
  iface.launch()