-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
403 additions
and
4 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,82 @@ | ||
<?php | ||
|
||
namespace App\Domains\Documents\Transformers; | ||
|
||
use App\Domains\Collections\CollectionStatusEnum; | ||
use App\Helpers\TextChunker; | ||
use App\Jobs\VectorlizeDataJob; | ||
use App\Models\Document; | ||
use App\Models\DocumentChunk; | ||
use Illuminate\Support\Facades\Log; | ||
use PhpOffice\PhpWord\IOFactory; | ||
|
||
class DocXTransformer | ||
{ | ||
protected Document $document; | ||
|
||
public function handle(Document $document): array | ||
{ | ||
$this->document = $document; | ||
|
||
$filePath = $this->document->pathToFile(); | ||
|
||
$parser = IOFactory::createReader('Word2007'); | ||
|
||
if (! $parser->canRead($filePath)) { | ||
throw new \Exception('Can not read the document '.$filePath); | ||
} | ||
|
||
$document = $parser->load($filePath); | ||
|
||
$sections = $document->getSections(); | ||
|
||
$content = []; | ||
$chunks = []; | ||
|
||
foreach ($sections as $section) { | ||
$elements = $section->getElements(); | ||
foreach ($elements as $element) { | ||
/** | ||
* @TODO | ||
* what type of section | ||
* text is easy | ||
* what about images | ||
* what about tables | ||
* what about lists | ||
*/ | ||
$content[] = $element->getText(); | ||
|
||
} | ||
} | ||
|
||
$content_flattened = implode(' ', $content); | ||
$size = config('llmdriver.chunking.default_size'); | ||
$chunked_chunks = TextChunker::handle($content_flattened, $size); | ||
$page = 1; | ||
|
||
foreach ($chunked_chunks as $chunkSection => $chunkContent) { | ||
$DocumentChunk = DocumentChunk::updateOrCreate( | ||
[ | ||
'document_id' => $this->document->id, | ||
'sort_order' => $page, | ||
'section_number' => $chunkSection, | ||
], | ||
[ | ||
'guid' => md5($chunkContent), | ||
'content' => $chunkContent, | ||
'meta_data' => [$chunkContent], | ||
] | ||
); | ||
|
||
$chunks[] = [ | ||
new VectorlizeDataJob($DocumentChunk), | ||
]; | ||
} | ||
|
||
notify_collection_ui($document->collection, CollectionStatusEnum::PROCESSING, 'Processing Document'); | ||
|
||
Log::info('DocXTransformer:handle', ['chunks' => count($chunks)]); | ||
|
||
return $chunks; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\Document; | ||
use Facades\App\Domains\Documents\Transformers\DocXTransformer; | ||
use Illuminate\Bus\Batchable; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class ParseDocxJob implements ShouldQueue | ||
{ | ||
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* Create a new job instance. | ||
*/ | ||
public function __construct(public Document $document) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
if ($this->batch()->cancelled()) { | ||
// Determine if the batch has been cancelled... | ||
|
||
return; | ||
} | ||
|
||
$chunks = DocXTransformer::handle($this->document); | ||
|
||
foreach ($chunks as $chunk) { | ||
$this->batch()->add($chunk); | ||
} | ||
|
||
} | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Binary file not shown.
Oops, something went wrong.