Amit Kumar commited on
Commit ·
cfde818
1
Parent(s): 5cc802c
initial commit
Browse filesFiles changed (4)
- .gitattributes+1
- BERT_sentiment_analysis.pthAdded+3
- app.pyAdded+65
- requirements.txtAdded+5
32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines3334353632 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textBERT_sentiment_analysis.pth filter=lfs diff=lfs merge=lfs -text
123version https://git-lfs.github.com/spec/v1oid sha256:7336efc4b2f17e373689bf5cef49d7a9c8eee0288c98a7179be8fdacc0297316size 2678846561234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465### 1. Imports and class names setup ###import gradio as grimport osimport torch
from transformers import DistilBertTokenizerFastfrom timeit import default_timer as timer
# Setup class namesclass_names = ["Positive", "Negative",]
### 2. Load the model ###
model = torch.load(f="bert_sentiment_classifier/BERT_sentiment_analysis.pth", map_location=torch.device("cpu")) # load to CPU)
### 3. Predict function ###
# Create predict functiondef predict(text: str): """Transforms and performs a prediction on img and returns prediction and time taken. """ # Start the timer start_time = timer()
tokenizer = DistilBertTokenizerFast.from_pretrained( 'distilbert-base-uncased' )
input = tokenizer(text, return_tensors="pt").to(DEVICE)
model.eval() with torch.inference_mode():
logits = model(**input).logits predicted_class_id = logits.argmax().item()
if predicted_class_id == 1: result = "Positive 😊" else: result = "Negative 🙁"
# Calculate the prediction time pred_time = round(timer() - start_time, 5)
# Return the prediction dictionary and prediction time return result
### 4. Gradio app ###
# Create title, description and article stringstitle = "Sentiment Classifier"description = "A Sentiment Classifier trained by fine-tuning [DistilBert](https://huggingface.co/docs/transformers/v4.42.0/en/model_doc/distilbert#transformers.DistilBertForSequenceClassification) Transformer model using hugging face [transformers](https://huggingface.co/docs/transformers/en/index) library."article = "The model classifies sentiment of an input text (whether the text shows a positive or negative sentiment)."
#Create the Gradio demodemo = gr.Interface(fn=predict, # mapping function from input to output inputs=[gr.Textbox(label="Input")], outputs=[gr.Label(label="Prediction")], title=title, description=description, article=article)
# Launch the demo!demo.launch()12345torchgradiopandastransformerstorchtext.gitattributes+1
BERT_sentiment_analysis.pthAdded+3
app.pyAdded+65
requirements.txtAdded+5