Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Add fallback mode for textractor, closes #145

davidmezzettib085b325421e
diff --git a/setup.py b/setup.py
index 2318810e..dd1bd233 100644
--- a/setup.py
+++ b/setup.py
@@ -13,7 +13,7 @@ extras = {}
extras["dev"] = ["black", "coverage", "pre-commit", "pylint"]
extras["api"] = [
- "aiohttp>=3.7.4",
+ "aiohttp>=3.8.1",
"fastapi>=0.61.1",
"PyYAML>=5.3",
"uvicorn>=0.12.1",
@@ -22,6 +22,7 @@ extras["api"] = [
extras["model"] = ["onnxruntime>=1.8.1"]
extras["pipeline"] = [
+ "beautifulsoup4>=4.9.3",
"fasttext>=0.9.2",
"nltk>=3.5",
"onnx>=1.10.1",
@@ -42,7 +43,7 @@ extras["similarity"] = [
"sentence-transformers>=2.0.0",
]
-extras["workflow"] = ["apache-libcloud>=3.3.1", "pillow>=7.2.0", "requests>=2.24.0", "xmltodict>=0.12.0"]
+extras["workflow"] = ["apache-libcloud>=3.3.1", "pillow>=7.2.0", "requests>=2.26.0", "xmltodict>=0.12.0"]
extras["all"] = extras["api"] + extras["model"] + extras["pipeline"] + extras["similarity"] + extras["workflow"]
diff --git a/src/python/txtai/pipeline/textractor.py b/src/python/txtai/pipeline/textractor.py
index c0a501d5..8af4a0f2 100644
--- a/src/python/txtai/pipeline/textractor.py
+++ b/src/python/txtai/pipeline/textractor.py
@@ -2,8 +2,15 @@
Textractor module
"""
+import contextlib
+import os
+
+from subprocess import Popen
+from urllib.request import urlopen
+
# Conditional import
try:
+ from bs4 import BeautifulSoup
from tika import parser
TIKA = True
@@ -18,13 +25,51 @@ class Textractor(Segmentation):
Extracts text from files.
"""
- def __init__(self, sentences=False, lines=False, paragraphs=False, minlength=None, join=False):
+ def __init__(self, sentences=False, lines=False, paragraphs=False, minlength=None, join=False, tika=True):
if not TIKA:
raise ImportError('Textractor pipeline is not available - install "pipeline" extra to enable')
super().__init__(sentences, lines, paragraphs, minlength, join)
+ # Determine if Tika (default if Java is available) or Beautiful Soup should be used
+ # Beautiful Soup only supports HTML, Tika supports a wide variety of file formats, including HTML.
+ self.tika = self.checkjava() if tika else False
+
def text(self, text):
- # text is a path to a file
- parsed = parser.from_file(text)
- return parsed["content"]
+ # Use Tika if available
+ if self.tika:
+ # text is a path to a file
+ parsed = parser.from_file(text)
+ return parsed["content"]
+
+ # Fallback to Beautiful Soup
+ text = "file://%s" % text if os.path.exists(text) else text
+ with contextlib.closing(urlopen(text)) as connection:
+ text = connection.read()
+
+ soup = BeautifulSoup(text, features="html.parser")
+ return soup.get_text()
+
+ def checkjava(self, path=None):
+ """
+ Checks if a Java executable is available for Tika.
+
+ Args:
+ path: path to java executable
+
+ Returns:
+ True if Java is available, False otherwise
+ """
+
+ # Get path to java executable if path not set
+ if not path:
+ path = os.getenv("TIKA_JAVA", "java")
+
+ # pylint: disable=W0702
+ # Check if java binary is available on path
+ try:
+ _ = Popen(path, stdout=open(os.devnull, "w"), stderr=open(os.devnull, "w"))
+ except:
+ return False
+
+ return True
diff --git a/test/python/testtextractor.py b/test/python/testtextractor.py
index 1a3f3220..4420a6c9 100644
--- a/test/python/testtextractor.py
+++ b/test/python/testtextractor.py
@@ -15,6 +15,23 @@ class TestTextractor(unittest.TestCase):
Textractor tests
"""
+ def testBeautifulSoup(self):
+ """
+ Tests text extraction using Beautiful Soup
+ """
+
+ textractor = Textractor(tika=False)
+ text = textractor(Utils.PATH + "/tabular.csv")
+ self.assertEqual(len(text), 125)
+
+ def testCheckJava(self):
+ """
+ Tests the checkjava method
+ """
+
+ textractor = Textractor()
+ self.assertFalse(textractor.checkjava("1112444abc"))
+
def testParagraphs(self):
"""
Tests extraction to paragraphs