I am trying to upload a file but I am facing a problem.
Future fileToJsonLines({required File file, required String fileType}) async {
String read = '';
String jsonLinesStr = '';
String filePath = await removeWhitespace(file.path);
log('File : ${filePath}');
try {
if (file.path.endsWith('.txt') || fileType.contains('text/plain')) {
read = await file.readAsString();
} else if(file.path.endsWith('.pdf') || fileType.contains('application/pdf')) {
Uint8List pdfBytes = file.readAsBytesSync();
PdfDocument document = PdfDocument(inputBytes: pdfBytes);
PdfTextExtractor extractor = PdfTextExtractor(document);
read = extractor.extractText();
//log('read of .pdf: ${read.length}');
//log('read of .pdf: \n $read');
}
List<String> paragraphs = read.split('\n');
List<String> jsonLines = [];
for (var paragraph in paragraphs) {
while (paragraph.isNotEmpty) {
String prompt = paragraph;
paragraph = ''; // Remove the processed portion
Map<String, dynamic> jsonData = {
'prompt': prompt,
'completion': '',
};
String json = jsonEncode(jsonData);
jsonLines.add(json);
}
}
jsonLinesStr = jsonLines.join('\n');
log('final jsonLines: \n $jsonLinesStr');
} catch (error) {
log('final jsonLines: \n$error');
}
return jsonLinesStr;
}