| |
| """app.ipynb |
| |
| Automatically generated by Colaboratory. |
| |
| Original file is located at |
| https://colab.research.google.com/drive/1DdebZU7Zx9pG1X7pVUgNNo20fsgAuFm0 |
| """ |
|
|
| import gradio as gr |
| import tensorflow as tf |
| from transformers import DistilBertTokenizer, TFDistilBertForSequenceClassification |
|
|
| |
| tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased") |
| model = TFDistilBertForSequenceClassification.from_pretrained("Elegbede-Distilbert_FInetuned_For_Text_Classification") |
| |
| def predict(texts): |
|
|
| preprocessed_texts = [] |
| for text in texts: |
| text = str(text).lower() |
| text = th.cont_exp(text) |
| text = th.remove_special_chars(text) |
| text = th.remove_accented_chars(text) |
| preprocessed_texts.append(text) |
|
|
| |
| preprocessed_text = ' '.join(preprocessed_texts) |
|
|
| |
| new_encodings = tokenizer(preprocessed_text, truncation=True, padding=True, max_length=70, return_tensors='tf') |
|
|
| |
| new_predictions = model(new_encodings) |
| new_labels_pred = tf.argmax(new_predictions.logits, axis=1) |
|
|
| labels_dict = {0: 'Sadness π', 1: 'Joy π', 2: 'Love π', 3: 'Anger π ', 4: 'Fear π¨', 5: 'Surprise π²'} |
|
|
| |
| predicted_emotion = labels_dict[new_labels_pred[0].numpy()] |
| return predicted_emotion |
|
|
| iface = gr.Interface( |
| fn=predict, |
| inputs="text", |
| outputs="text", |
| title="Emotion Classifier", |
| description="Predict the emotion from text using a fine-tuned DistilBERT model ", |
| ) |
| |
| iface.launch() |