Update ui/ui_core.py
Browse files- ui/ui_core.py +21 -3
ui/ui_core.py
CHANGED
@@ -6,6 +6,10 @@ import json
|
|
6 |
import gradio as gr
|
7 |
from typing import List
|
8 |
|
|
|
|
|
|
|
|
|
9 |
# ✅ Fix: Add src to Python path
|
10 |
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
|
11 |
|
@@ -32,6 +36,22 @@ def clean_final_response(text: str) -> str:
|
|
32 |
)
|
33 |
return "".join(panels)
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
def convert_file_to_json(file_path: str, file_type: str) -> str:
|
36 |
try:
|
37 |
if file_type == "csv":
|
@@ -42,9 +62,7 @@ def convert_file_to_json(file_path: str, file_type: str) -> str:
|
|
42 |
except:
|
43 |
df = pd.read_excel(file_path, engine="xlrd", header=None, dtype=str)
|
44 |
elif file_type == "pdf":
|
45 |
-
|
46 |
-
text = "\n".join([page.extract_text() or "" for page in pdf.pages])
|
47 |
-
return json.dumps({"filename": os.path.basename(file_path), "content": text.strip()})
|
48 |
else:
|
49 |
return json.dumps({"error": f"Unsupported file type: {file_type}"})
|
50 |
|
|
|
6 |
import gradio as gr
|
7 |
from typing import List
|
8 |
|
9 |
+
from transformers import LayoutLMv3Processor, LayoutLMv3ForTokenClassification
|
10 |
+
from PIL import Image
|
11 |
+
import torch
|
12 |
+
|
13 |
# ✅ Fix: Add src to Python path
|
14 |
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
|
15 |
|
|
|
36 |
)
|
37 |
return "".join(panels)
|
38 |
|
39 |
+
def use_layoutlmv3_on_image(image_path):
|
40 |
+
processor = LayoutLMv3Processor.from_pretrained("microsoft/layoutlmv3-large")
|
41 |
+
model = LayoutLMv3ForTokenClassification.from_pretrained("microsoft/layoutlmv3-large")
|
42 |
+
|
43 |
+
image = Image.open(image_path).convert("RGB")
|
44 |
+
encoding = processor(images=image, return_tensors="pt")
|
45 |
+
with torch.no_grad():
|
46 |
+
outputs = model(**encoding)
|
47 |
+
|
48 |
+
logits = outputs.logits
|
49 |
+
predicted_class = logits.argmax(-1)
|
50 |
+
tokens = processor.tokenizer.convert_ids_to_tokens(encoding['input_ids'][0])
|
51 |
+
|
52 |
+
text = " ".join([tokens[i] for i in range(len(tokens)) if predicted_class[0][i] != -100])
|
53 |
+
return json.dumps({"filename": os.path.basename(image_path), "content": text})
|
54 |
+
|
55 |
def convert_file_to_json(file_path: str, file_type: str) -> str:
|
56 |
try:
|
57 |
if file_type == "csv":
|
|
|
62 |
except:
|
63 |
df = pd.read_excel(file_path, engine="xlrd", header=None, dtype=str)
|
64 |
elif file_type == "pdf":
|
65 |
+
return use_layoutlmv3_on_image(file_path)
|
|
|
|
|
66 |
else:
|
67 |
return json.dumps({"error": f"Unsupported file type: {file_type}"})
|
68 |
|