Skip to content

Instantly share code, notes, and snippets.

@muyiwexy
Last active November 28, 2023 21:44
Show Gist options
  • Save muyiwexy/a111e87cc1abeaa6a398474768565957 to your computer and use it in GitHub Desktop.
Save muyiwexy/a111e87cc1abeaa6a398474768565957 to your computer and use it in GitHub Desktop.
index_notifier.dart(3)
enum CreateandUploadState { initial, loading, loaded, error }
class IndexNotifier extends ChangeNotifier {
late LangchainService langchainService;
IndexNotifier({required this.langchainService});
String? _filepath;
String? _fileName;
String? get fileName => _fileName;
final _createandUploadState =
ValueNotifier<CreateandUploadState>(CreateandUploadState.initial);
ValueNotifier<CreateandUploadState> get createandUploadState =>
_createandUploadState;
Future<void> createAndUploadNeonIndex() async {
try {
// load the document into the application
final pickedDocument = await _pickedFile();
_createandUploadState.value = CreateandUploadState.loading;
// split the document to different chunks
final splitChunks = langchainService.splitDocToChunks(pickedDocument);
// Embed the chunks
final embededDoc = await langchainService.embedChunks(splitChunks);
_createandUploadState.value = CreateandUploadState.loaded;
} catch (e) {
_createandUploadState.value = CreateandUploadState.error;
print(e);
} finally {
await Future.delayed(const Duration(milliseconds: 2000));
_createandUploadState.value = CreateandUploadState.initial;
}
}
Future<Document> _pickedFile() async {
FilePickerResult? result = await FilePicker.platform
.pickFiles(type: FileType.custom, allowedExtensions: ['pdf']);
if (result != null) {
_filepath = result.files.single.path;
_fileName = result.files.single.name.replaceAll('.pdf', '').toLowerCase();
final textfile =
_filepath!.isNotEmpty ? await _readPDFandConvertToText() : "";
final loader = TextLoader(textfile);
final document = await loader.load();
Document? docs;
for (var doc in document) {
docs = doc;
}
return docs!;
} else {
throw Exception("No file selected");
}
}
Future<String> _readPDFandConvertToText() async {
File file = File(_filepath!);
List<int> bytes = await file.readAsBytes();
final document = PdfDocument(inputBytes: Uint8List.fromList(bytes));
String text = PdfTextExtractor(document).extractText();
final localPath = await _localPath;
File createFile = File('$localPath/output.txt');
final res = await createFile.writeAsString(text);
document.dispose();
return res.path;
}
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment