performPartition static method

Future<void> performPartition(
  1. String inputPath,
  2. String destPath
)

Implementation

static Future<void> performPartition(
  String inputPath,
  String destPath,
) async {
  if (!File(inputPath).existsSync()) {
    throw Exception('Input file not found at $inputPath');
  }

  String pythonCode = '''
import sys
from unstructured.partition.auto import partition

filename = sys.argv[1]
elements = partition(filename=filename, strategy="hi_res", languages=["eng"])
print("\\n\\n".join([str(el) for el in elements]))
''';

  final partitionProcess = await ProcessUtil.start(_pythonPath, [
    '-c',
    pythonCode,
    inputPath,
  ]);

  StreamSubscription<String> stderrSubscription = partitionProcess.stderr
      .transform(utf8.decoder)
      .listen((data) {
        error('Unstructured stderr: $data');
      });

  IOSink sink = File(destPath).openWrite();
  await partitionProcess.stdout.pipe(sink);

  final exitCode = await partitionProcess.exitCode;
  await sink.close();
  await stderrSubscription.cancel();

  if (exitCode != 0) {
    throw Exception('Partition failed with exit code $exitCode');
  }
}