Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,37 +3,39 @@ import os
|
|
| 3 |
import gradio as gr
|
| 4 |
import json
|
| 5 |
import torch
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 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
|
| 53 |
-
description="
|
| 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 ---")
|