-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/Querent-ai/querent-ai into …
…ayush
- Loading branch information
Showing
9 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from enum import Enum | ||
from typing import Optional | ||
from pydantic import BaseModel, Field | ||
|
||
|
||
class IngestorBackend(str, Enum): | ||
PDF = "pdf" | ||
TEXT = "txt" | ||
DOCX = "docx" | ||
CSV = "csv" | ||
XLSX = "xlsx" | ||
JSON = "json" | ||
XML = "xml" | ||
HTML = "html" | ||
YAML = "yaml" | ||
MARKDOWN = "markdown" | ||
IMG = "image" | ||
PNG = "png" | ||
JPG = "jpg" | ||
GIF = "gif" | ||
WEBRTC = "webrtc" | ||
MP3 = "mp3" | ||
MP4 = "mp4" | ||
MOV = "mov" | ||
AVI = "avi" | ||
WAV = "wav" | ||
Unsupported = "unsupported" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import List | ||
from querent.processors.async_processor import AsyncProcessor | ||
|
||
|
||
class BaseIngestor: | ||
def __init__(self, processors: List[AsyncProcessor]): | ||
self.processors = processors | ||
|
||
async def process_data(self, text): | ||
# Your common data processing logic here | ||
pass | ||
|
||
async def extract_text_from_file(self, file_path: str) -> str: | ||
# Your common file extraction logic here | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
|
||
from abc import ABC, abstractmethod | ||
from typing import List, Optional | ||
from querent.ingestors.base_ingestor import BaseIngestor | ||
|
||
from querent.processors.async_processor import AsyncProcessor | ||
|
||
|
||
class IngestorFactory(ABC): | ||
@abstractmethod | ||
async def supports(self, file_extension: str) -> bool: | ||
pass | ||
|
||
@abstractmethod | ||
async def create(self, file_extension: str, processors: List[AsyncProcessor]) -> BaseIngestor: | ||
pass | ||
|
||
class UnsupportedIngestor(IngestorFactory): | ||
def __init__(self, message: str): | ||
self.message = message | ||
|
||
async def supports(self, file_extension: str) -> bool: | ||
return False | ||
|
||
async def create(self, file_extension: str) -> Optional[BaseIngestor]: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
from typing import Optional | ||
from querent.config.ingestor_config import IngestorBackend | ||
from querent.ingestors.base_ingestor import BaseIngestor | ||
from querent.ingestors.ingestor_factory import IngestorFactory, UnsupportedIngestor | ||
from querent.ingestors.pdfs.pdf_ingestor_v1 import PdfIngestorFactory | ||
|
||
|
||
class IngestorFactoryManager: | ||
def __init__(self): | ||
self.ingestor_factories = { | ||
IngestorBackend.PDF.value: PdfIngestorFactory(), | ||
#Ingestor.TEXT.value: TextIngestor(), | ||
# Add more mappings as needed | ||
} | ||
|
||
async def get_factory(self, file_extension: str) -> IngestorFactory: | ||
return self.ingestor_factories.get(file_extension.lower(), UnsupportedIngestor("Unsupported file extension")) | ||
|
||
async def get_ingestor(self, file_extension: str) -> Optional[BaseIngestor]: | ||
factory = self.get_factory(file_extension) | ||
return factory.create(file_extension) | ||
|
||
async def supports(self, file_extension: str) -> bool: | ||
factory = self.get_factory(file_extension) | ||
return factory.supports(file_extension) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from typing import AsyncGenerator, List | ||
import fitz # PyMuPDF | ||
from querent.common.types.collected_bytes import CollectedBytes | ||
from querent.config.ingestor_config import IngestorBackend | ||
from querent.ingestors.base_ingestor import BaseIngestor | ||
from querent.ingestors.ingestor_factory import IngestorFactory | ||
from querent.processors.async_processor import AsyncProcessor | ||
|
||
|
||
class PdfIngestorFactory(IngestorFactory): | ||
SUPPORTED_EXTENSIONS = {"pdf"} | ||
|
||
async def supports(self, file_extension: str) -> bool: | ||
return file_extension.lower() in self.SUPPORTED_EXTENSIONS | ||
|
||
async def create(self, file_extension: str, processors: List[AsyncProcessor]) -> BaseIngestor: | ||
if not self.supports(file_extension): | ||
return None | ||
return PdfIngestor(processors) | ||
|
||
|
||
class PdfIngestor(BaseIngestor): | ||
def __init__(self, processors: List[AsyncProcessor]): | ||
super().__init__(IngestorBackend.PDF) | ||
self.processors = processors | ||
|
||
async def ingest( | ||
self, poll_function: AsyncGenerator[CollectedBytes, None] | ||
) -> AsyncGenerator[List[str], None]: | ||
try: | ||
collected_bytes = b"" # Initialize an empty byte string | ||
current_file = None | ||
|
||
async for chunk_bytes in poll_function: | ||
if chunk_bytes.is_error(): | ||
continue # Skip error bytes | ||
|
||
# If it's a new file, start collecting bytes for it | ||
if chunk_bytes.file != current_file: | ||
if current_file: | ||
# Process the collected bytes of the previous file | ||
text = await self.extract_and_process_pdf( | ||
CollectedBytes(file=current_file, data=collected_bytes) | ||
) | ||
yield text | ||
collected_bytes = b"" # Reset collected bytes for the new file | ||
current_file = chunk_bytes.file | ||
|
||
collected_bytes += chunk_bytes.data # Collect the bytes | ||
|
||
# Process the collected bytes of the last file | ||
if current_file: | ||
text = await self.extract_and_process_pdf( | ||
CollectedBytes(file=current_file, data=collected_bytes) | ||
) | ||
yield text | ||
|
||
except Exception as e: | ||
yield [] | ||
|
||
async def extract_and_process_pdf(self, collected_bytes: CollectedBytes) -> List[str]: | ||
text = await self.extract_text_from_pdf(collected_bytes) | ||
return await self.process_data(text) | ||
|
||
async def extract_text_from_pdf(self, collected_bytes: CollectedBytes) -> str: | ||
pdf = fitz.open(stream=collected_bytes.data, filetype="pdf") | ||
text = "" | ||
for page in pdf: | ||
text += page.getText() | ||
return text | ||
|
||
async def process_data(self, text: str) -> List[str]: | ||
processed_data = text | ||
for processor in self.processors: | ||
processed_data = await processor.process(processed_data) | ||
return processed_data |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
class AsyncProcessor(ABC): | ||
@abstractmethod | ||
async def process(self, data): | ||
pass |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import asyncio | ||
from pathlib import Path | ||
from querent.collectors.fs.fs_collector import FSCollectorFactory | ||
from querent.config.collector_config import FSCollectorConfig | ||
from querent.common.uri import Uri | ||
from querent.ingestors.ingestor_manager import IngestorFactoryManager | ||
import pytest | ||
|
||
@pytest.mark.asyncio | ||
async def test_collect_and_ingest_pdf(): | ||
# Set up the collector | ||
collector_factory = FSCollectorFactory() | ||
uri = Uri("file://" + str(Path("./tests/data/pdf/").resolve())) | ||
config = FSCollectorConfig(root_path=uri.path) | ||
collector = collector_factory.resolve(uri, config) | ||
|
||
# Set up the ingestor | ||
ingestor_factory_manager = IngestorFactoryManager() | ||
ingestor_factory = await ingestor_factory_manager.get_factory("pdf") # Notice the use of await here | ||
ingestor = await ingestor_factory.create("pdf", []) | ||
|
||
# Collect and ingest the PDF | ||
ingested_call = ingestor.ingest(collector.poll()) | ||
counter = 0 | ||
async def poll_and_print(): | ||
counter = 0 | ||
async for ingested in ingested_call: | ||
assert ingested is not None | ||
if len(ingested) == 0: | ||
counter += 1 | ||
assert counter == 1 | ||
|
||
await poll_and_print() # Notice the use of await here | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(test_collect_and_ingest_pdf()) |