File upload and content retrieve error in flutter

Hi, I am trying to upload file and getting file content. File can be uploaded successfully but i am not able to retrieve its content and get format exception. If i use your package, then i get issue of file is not in proper format which is jsonLines issue. so i have created method to create jsonLines from files. Please check and let me know what is your procedure to upload files and get content. My current method for retrieving content returns the same content that was uploaded to that can’t be good approach. I am new to it. So i am looking your suggestions and help. My purpose behind this code is to upload any file to openai and then ask questions related to file only. Kind regards.

class UpLoadFiles {
static late FileUploadingProvider fileProvider;

static List fileIds = ;

static Future getFileIds() async {
try {
final url = Uri.parse(‘${ApiUrls.gptBaseUrl}${ApiUrls.files}’);
List ids = ;
final response = await http.get(
url,
headers: {‘Authorization’: ‘Bearer ${ApiUrls.gptApi}’},
);
if (response.statusCode == 200) {
final data = await jsonDecode(response.body);
for (var i in data[‘data’]) {
ids.add(i[‘id’]);
}
return ids;
} else {
log(‘got error’);
showToast(msg: ‘Something went wrong’);
log(ids.toString());
return ;
}
} catch (e) {
showToast(msg: ‘Something went wrong’);
return ;
}
}

static Future getFileContent({required String fileId}) async {
try {
final url = Uri.parse(
‘${ApiUrls.gptBaseUrl}${ApiUrls.files}/$fileId${ApiUrls.fileContent}’);
final response = await http.get(
url,
headers: {‘Authorization’: ‘Bearer ${ApiUrls.gptApi}’},
);
if (response.statusCode == ApiConstants.success) {
String jsonString = response.body;
// String actualString = removeWhitespace(jsonString);
log(‘…Actual String:…${jsonString}’);
Map<String, dynamic> data = jsonDecode(response.body);
return data[‘prompt’];
} else {
log(‘got error’);
showToast(msg: ‘Something went wrong’);
return ‘No content provided’;
}
} catch (e) {
showToast(msg: ‘Something went wrong $e’);
log(‘…getFileContentError…${e.toString()}’);
return ‘No content provided’;
}
}

static Future deleteFileId() async {
if (fileIds.isNotEmpty) {
log(fileIds.toString());
for (int i = 0; i < fileIds.length; i++) {
log(‘${fileIds.length}’);
String fileId = fileIds[i];
final Uri url =
Uri.parse(‘${ApiUrls.gptBaseUrl}${ApiUrls.files}/$fileId’);
final response = await http.delete(
url,
headers: {‘Authorization’: ‘Bearer ${ApiUrls.gptApi}’},
);
log(‘$response’);
log(response.body.toString());
if (response.statusCode == 200) {
// final data = await jsonDecode(response.body);
// logg.log(‘$data’);
log(“File $i: deleted”);
showToast(msg: ‘file deleted’);
} else {
log(‘got error deleting fiel: $i’);
showToast(msg: ‘Couldn’t delete file’);
}
}
}
}

upLoadFile(BuildContext context) async {
final result = await DBServices.pickFile();
String filePath = result!.files.first.path!;

File? file = File(filePath);
String? fileType = await detectFileType(file);
log('.................Extension: ${fileType}');
if (filePath.isEmpty) {
  showToast(msg: 'file not picked', toastLength: Toast.LENGTH_LONG);
} else {
  fileProvider = Provider.of<FileUploadingProvider>(context, listen: false);
  fileProvider.setFileStatus = 'upLoading';

  await fileToJsonLines(file: file, fileType: fileType!)
      .then((value) async {
    await uploadFile(context, value);
  });
}

}

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('.....................fileToJsonLinesError........${error.toString()}');
}

return jsonLinesStr;

}

Future uploadFile(BuildContext context, var value) async {
String fileContent = ‘’;

final url = Uri.parse('${ApiUrls.gptBaseUrl}${ApiUrls.files}');
final request = http.MultipartRequest('POST', url);
request.headers['Authorization'] = 'Bearer ${ApiUrls.gptApi}';
request.fields['purpose'] = 'fine-tune';
request.files.add(http.MultipartFile.fromString(
  'file',
  value,
  filename: 'output.jsonl', // Always use this name for the uploaded file
  contentType: MediaType('application', 'jsonl'),
));
final streamedResponse = await request.send();
final response = await http.Response.fromStream(streamedResponse);

log(response.statusCode.toString());
log(response.body.toString());
if (response.statusCode == ApiConstants.success) {
  fileProvider.setFileStatus = 'upLoaded';
  await getFileIds().then((value) async {
    fileIds = value;
    //log('file id : ${value[0]}');
    await getFileContent(fileId: value[0]).then((value) {
      fileProvider.setFileContent = value;
      fileContent = value;
    });
    return fileContent;
  });
} else {
  //log('.................Uploading Error:${response.body}');
  showToast(msg: 'Error uploading file: ${response.body}');
  return '';
}
return fileContent;
}
}