-
Notifications
You must be signed in to change notification settings - Fork 61
Fix: move import to class __init__ and add RecursiveCharacterSplitter #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
da61704
Fix: move import to init.
SayaZhang b5e69b4
Add recursive character splitter
SayaZhang 26255e7
Merge branch 'main' of https://github.com/CambioML/uniflow into main
SayaZhang f0fb952
Merge branch 'main' of https://github.com/CambioML/uniflow into main
SayaZhang af81106
Merge branch 'main' of https://github.com/CambioML/uniflow into main
SayaZhang b9bfad6
Update recursive splitter
SayaZhang 61a4f3e
Merge branch 'main' of https://github.com/CambioML/uniflow into main
SayaZhang c3e5c15
Update html_op param
SayaZhang 98e32f0
Update extract html example
SayaZhang c441d66
Merge branch 'main' into main
SayaZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
198 changes: 198 additions & 0 deletions
198
example/extract/extract_txt_with_recursive_splitter.ipynb
This file contains hidden or 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,198 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 1, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "%reload_ext autoreload\n", | ||
| "%autoreload 2\n", | ||
| "\n", | ||
| "import sys\n", | ||
| "\n", | ||
| "sys.path.append(\".\")\n", | ||
| "sys.path.append(\"..\")\n", | ||
| "sys.path.append(\"../..\")" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 2, | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stderr", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "/home/ubuntu/anaconda3/envs/uniflow/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", | ||
| " from .autonotebook import tqdm as notebook_tqdm\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "import os\n", | ||
| "import pandas as pd\n", | ||
| "from uniflow.flow.client import ExtractClient, TransformClient\n", | ||
| "from uniflow.flow.config import TransformOpenAIConfig, ExtractPDFConfig\n", | ||
| "from uniflow.op.model.model_config import OpenAIModelConfig, NougatModelConfig\n", | ||
| "from uniflow.op.prompt import PromptTemplate, Context\n", | ||
| "from uniflow.op.extract.split.splitter_factory import SplitterOpsFactory\n", | ||
| "from uniflow.op.extract.split.constants import RECURSIVE_CHARACTER_SPLITTER" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 3, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "dir_cur = os.getcwd()\n", | ||
| "pdf_file = \"1408.5882_page-1.pdf\"\n", | ||
| "input_file = os.path.join(f\"{dir_cur}/data/raw_input/\", pdf_file)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 4, | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "data": { | ||
| "text/plain": [ | ||
| "['ParagraphSplitter', 'MarkdownHeaderSplitter', 'RecursiveCharacterSplitter']" | ||
| ] | ||
| }, | ||
| "execution_count": 4, | ||
| "metadata": {}, | ||
| "output_type": "execute_result" | ||
| } | ||
| ], | ||
| "source": [ | ||
| "SplitterOpsFactory.list()" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 5, | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stderr", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "/home/ubuntu/anaconda3/envs/uniflow/lib/python3.10/site-packages/torch/functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:3526.)\n", | ||
| " return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "data = [\n", | ||
| " {\"filename\": input_file},\n", | ||
| "]\n", | ||
| "\n", | ||
| "config = ExtractPDFConfig(\n", | ||
| " model_config=NougatModelConfig(\n", | ||
| " model_name = \"0.1.0-small\",\n", | ||
| " batch_size = 1 # When batch_size>1, nougat will run on CUDA, otherwise it will run on CPU\n", | ||
| " ),\n", | ||
| " splitter=RECURSIVE_CHARACTER_SPLITTER,\n", | ||
| ")\n", | ||
| "nougat_client = ExtractClient(config)\n", | ||
| "\n" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 6, | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stderr", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| " 0%| | 0/1 [00:00<?, ?it/s]" | ||
| ] | ||
| }, | ||
| { | ||
| "name": "stderr", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "100%|██████████| 1/1 [00:05<00:00, 5.09s/it]\n" | ||
| ] | ||
| }, | ||
| { | ||
| "data": { | ||
| "text/plain": [ | ||
| "[{'output': [{'text': ['# Convolutional Neural Networks for Sentence Classification Yoon KimNew York Universityyhk255@nyu.edu###### AbstractWe report on a series of experiments with convolutional neural networks (CNN) trained on top of pre-trained word vectors for sentence-level classification tasks. We show that a simple CNN with little hyperparameter tuning and static vectors achieves excellent results on multiple benchmarks. Learning task-specific vectors through fine-tuning offers further gains in performance. We additionally propose a simple modification to the architecture to allow for the use of both task-specific and static vectors. The CNN models discussed herein improve upon the state of the art on 4 out of 7 tasks, which include sentiment analysis and question classification.## 1 Introduction',\n", | ||
| " 'Deep learning models have achieved remarkable results in computer vision [11] and speech recognition [1] in recent years. Within natural language processing, much of the work with deep learning methods has involved learning word vector representations through neural language models [1, 1, 2] and performing composition over the learned word vectors for classification [1]. Word vectors, wherein words are projected from a sparse, 1-of-\\\\(V\\\\) encoding (here \\\\(V\\\\) is the vocabulary size) onto a lower dimensional vector space via a hidden layer, are essentially feature extractors that encode semantic features of words in their dimensions. In such dense representations, semantically close words are likewise close--in euclidean or cosine distance--in the lower dimensional vector space.',\n", | ||
| " 'Convolutional neural networks (CNN) utilize layers with convolving filters that are applied to local features [1]. Originally invented for computer vision, CNN models have subsequently been shown to be effective for NLP and have achieved excellent results in semantic parsing [13], search query retrieval [2], sentence modeling [1], and other traditional NLP tasks [1].',\n", | ||
| " \"In the present work, we train a simple CNN with one layer of convolution on top of word vectors obtained from an unsupervised neural language model. These vectors were trained by Mikolov et al. (2013) on 100 billion words of Google News, and are publicly available.1 We initially keep the word vectors static and learn only the other parameters of the model. Despite little tuning of hyperparameters, this simple model achieves excellent results on multiple benchmarks, suggesting that the pre-trained vectors are 'universal' feature extractors that can be utilized for various classification tasks. Learning task-specific vectors through fine-tuning results in further improvements. We finally describe a simple modification to the architecture to allow for the use of both pre-trained and task-specific vectors by having multiple channels.Footnote 1: [https://code.google.com/p/word2vec/](https://code.google.com/p/word2vec/)\",\n", | ||
| " 'Our work is philosophically similar to Razavian et al. (2014) which showed that for image classification, feature extractors obtained from a pre-trained deep learning model perform well on a variety of tasks--including tasks that are very different from the original task for which the feature extractors were trained.## 2 ModelThe model architecture, shown in figure 1, is a slight variant of the CNN architecture of Collobert et al. (2011). Let \\\\(\\\\mathbf{x}_{i}\\\\in\\\\mathbb{R}^{k}\\\\) be the \\\\(k\\\\)-dimensional word vector corresponding to the \\\\(i\\\\)-th word in the sentence. A sentence of length \\\\(n\\\\) (padded where']}],\n", | ||
| " 'root': <uniflow.node.Node at 0x7fc5fbf73af0>}]" | ||
| ] | ||
| }, | ||
| "execution_count": 6, | ||
| "metadata": {}, | ||
| "output_type": "execute_result" | ||
| } | ||
| ], | ||
| "source": [ | ||
| "output = nougat_client.run(data)\n", | ||
| "output" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 7, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "contexts = output[0]['output'][0]['text']" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 8, | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stdout", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "0 # Convolutional Neural Networks for Sentence Classification Yoon KimNew York Universityyhk255@nyu.edu###### AbstractWe report on a series of experiments with convolutional neural networks (CNN) trained on top of pre-trained word vectors for sentence-level classification tasks. We show that a simple CNN with little hyperparameter tuning and static vectors achieves excellent results on multiple benchmarks. Learning task-specific vectors through fine-tuning offers further gains in performance. We additionally propose a simple modification to the architecture to allow for the use of both task-specific and static vectors. The CNN models discussed herein improve upon the state of the art on 4 out of 7 tasks, which include sentiment analysis and question classification.## 1 Introduction\n", | ||
| "1 Deep learning models have achieved remarkable results in computer vision [11] and speech recognition [1] in recent years. Within natural language processing, much of the work with deep learning methods has involved learning word vector representations through neural language models [1, 1, 2] and performing composition over the learned word vectors for classification [1]. Word vectors, wherein words are projected from a sparse, 1-of-\\(V\\) encoding (here \\(V\\) is the vocabulary size) onto a lower dimensional vector space via a hidden layer, are essentially feature extractors that encode semantic features of words in their dimensions. In such dense representations, semantically close words are likewise close--in euclidean or cosine distance--in the lower dimensional vector space.\n", | ||
| "2 Convolutional neural networks (CNN) utilize layers with convolving filters that are applied to local features [1]. Originally invented for computer vision, CNN models have subsequently been shown to be effective for NLP and have achieved excellent results in semantic parsing [13], search query retrieval [2], sentence modeling [1], and other traditional NLP tasks [1].\n", | ||
| "3 In the present work, we train a simple CNN with one layer of convolution on top of word vectors obtained from an unsupervised neural language model. These vectors were trained by Mikolov et al. (2013) on 100 billion words of Google News, and are publicly available.1 We initially keep the word vectors static and learn only the other parameters of the model. Despite little tuning of hyperparameters, this simple model achieves excellent results on multiple benchmarks, suggesting that the pre-trained vectors are 'universal' feature extractors that can be utilized for various classification tasks. Learning task-specific vectors through fine-tuning results in further improvements. We finally describe a simple modification to the architecture to allow for the use of both pre-trained and task-specific vectors by having multiple channels.Footnote 1: [https://code.google.com/p/word2vec/](https://code.google.com/p/word2vec/)\n", | ||
| "4 Our work is philosophically similar to Razavian et al. (2014) which showed that for image classification, feature extractors obtained from a pre-trained deep learning model perform well on a variety of tasks--including tasks that are very different from the original task for which the feature extractors were trained.## 2 ModelThe model architecture, shown in figure 1, is a slight variant of the CNN architecture of Collobert et al. (2011). Let \\(\\mathbf{x}_{i}\\in\\mathbb{R}^{k}\\) be the \\(k\\)-dimensional word vector corresponding to the \\(i\\)-th word in the sentence. A sentence of length \\(n\\) (padded where\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "for i, _s in enumerate(contexts):\n", | ||
| " print(i, _s)" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "uniflow", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.10.13" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 2 | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we update this
elseto checkfilenameand then add a else case to log regarding a specific extract format is not supported. This should help improved the readability.