Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
enzokro committed Sep 29, 2023
1 parent f4badd5 commit 3606e75
Show file tree
Hide file tree
Showing 16 changed files with 613 additions and 145 deletions.
28 changes: 14 additions & 14 deletions Fractal_LLM_Course/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
'doc_host': 'https://enzokro.github.io',
'git_url': 'https://github.com/enzokro/Fractal-LLM-Course',
'lib_path': 'Fractal_LLM_Course'},
'syms': { 'Fractal_LLM_Course.lesson2.simple_pipeline': { 'Fractal_LLM_Course.lesson2.simple_pipeline.SentimentPipeline': ( 'nbdev.html#sentimentpipeline',
'Fractal_LLM_Course/lesson2/simple_pipeline.py'),
'Fractal_LLM_Course.lesson2.simple_pipeline.SentimentPipeline.__call__': ( 'nbdev.html#sentimentpipeline.__call__',
'Fractal_LLM_Course/lesson2/simple_pipeline.py'),
'Fractal_LLM_Course.lesson2.simple_pipeline.SentimentPipeline.__init__': ( 'nbdev.html#sentimentpipeline.__init__',
'Fractal_LLM_Course/lesson2/simple_pipeline.py'),
'Fractal_LLM_Course.lesson2.simple_pipeline.SentimentPipeline.__repr__': ( 'nbdev.html#sentimentpipeline.__repr__',
'Fractal_LLM_Course/lesson2/simple_pipeline.py'),
'Fractal_LLM_Course.lesson2.simple_pipeline.SentimentPipeline.forward': ( 'nbdev.html#sentimentpipeline.forward',
'Fractal_LLM_Course/lesson2/simple_pipeline.py'),
'Fractal_LLM_Course.lesson2.simple_pipeline.SentimentPipeline.preprocess': ( 'nbdev.html#sentimentpipeline.preprocess',
'Fractal_LLM_Course/lesson2/simple_pipeline.py'),
'Fractal_LLM_Course.lesson2.simple_pipeline.SentimentPipeline.process_outputs': ( 'nbdev.html#sentimentpipeline.process_outputs',
'Fractal_LLM_Course/lesson2/simple_pipeline.py')}}}
'syms': { 'Fractal_LLM_Course.lesson_2.simple_pipeline': { 'Fractal_LLM_Course.lesson_2.simple_pipeline.SentimentPipeline': ( 'nbdev.html#sentimentpipeline',
'Fractal_LLM_Course/lesson_2/simple_pipeline.py'),
'Fractal_LLM_Course.lesson_2.simple_pipeline.SentimentPipeline.__call__': ( 'nbdev.html#sentimentpipeline.__call__',
'Fractal_LLM_Course/lesson_2/simple_pipeline.py'),
'Fractal_LLM_Course.lesson_2.simple_pipeline.SentimentPipeline.__init__': ( 'nbdev.html#sentimentpipeline.__init__',
'Fractal_LLM_Course/lesson_2/simple_pipeline.py'),
'Fractal_LLM_Course.lesson_2.simple_pipeline.SentimentPipeline.__repr__': ( 'nbdev.html#sentimentpipeline.__repr__',
'Fractal_LLM_Course/lesson_2/simple_pipeline.py'),
'Fractal_LLM_Course.lesson_2.simple_pipeline.SentimentPipeline.forward': ( 'nbdev.html#sentimentpipeline.forward',
'Fractal_LLM_Course/lesson_2/simple_pipeline.py'),
'Fractal_LLM_Course.lesson_2.simple_pipeline.SentimentPipeline.preprocess': ( 'nbdev.html#sentimentpipeline.preprocess',
'Fractal_LLM_Course/lesson_2/simple_pipeline.py'),
'Fractal_LLM_Course.lesson_2.simple_pipeline.SentimentPipeline.process_outputs': ( 'nbdev.html#sentimentpipeline.process_outputs',
'Fractal_LLM_Course/lesson_2/simple_pipeline.py')}}}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
# %% auto 0
__all__ = ['SentimentPipeline']

# %% ../../nbs/02_nbdev.ipynb 37
# cell where we import the pieces of our pipeline.
# %% ../../nbs/02_nbdev.ipynb 43
# importing the pieces for the pipeline
from transformers import AutoConfig
from transformers import AutoTokenizer
from transformers import AutoModelForSequenceClassification

# %% ../../nbs/02_nbdev.ipynb 40
# %% ../../nbs/02_nbdev.ipynb 46
class SentimentPipeline:
"""
Docstring, inserted on the fly!
"""
def __init__(self, model_name):
"""
Sentiment Analysis pipeline.
Expand All @@ -27,7 +24,7 @@ def __init__(self, model_name):
def preprocess(self, text: str):
"""
Sends `text` through the LLM's tokenizer.
The tokenizers turns words and characters into special inputs for the LLM.
The tokenizer turns words and characters into special inputs for the LLM.
"""
tokenized_inputs = self.tokenizer(text, return_tensors='pt')
return tokenized_inputs
Expand All @@ -45,31 +42,38 @@ def forward(self, text: str):

def process_outputs(self, outs):
"""
Here is where HuggingFace does the most for us via `pipeline`.
Here we mimic the post-processing that HuggingFace automatically does in its `pipeline`.
"""
# grab the raw "scores" that from the model for Positive and Negative labels
# grab the raw scores from the model for Positive and Negative labels
logits = outs.logits

# find the strongest label score, aka the model's decision
pred_idx = logits.argmax(1).item()

# use the `config` object to find the class label
# use the `config` object to find the actual class label
pred_label = self.config.id2label[pred_idx]

# calculate the human-readable number for the score
# calculate the human-readable probability score for this class
pred_score = logits.softmax(-1)[:, pred_idx].item()

# return the predicted label and its score
return {
'label': pred_label,
'score': pred_score,
}


def __call__(self, text: str):
"""
Overriding the call method to easily and intuitively call the pipeline.
"""
model_outs = self.forward(text)
preds = self.process_outputs(model_outs)
return preds


def __repr__(self):
"""
Cleaner representation of the pipeline.
"""
return f"SentimentAnalysis_{self.model_name}"


379 changes: 262 additions & 117 deletions nbs/02_nbdev.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 3606e75

Please sign in to comment.