Team Ai
Modelpublic

Elegbede/Distilbert_FInetuned_For_Text_Classification

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes19downloads
app.py52 linesDownload Raw Back to root
1# -*- coding: utf-8 -*-2"""app.ipynb3 4Automatically generated by Colaboratory.5 6Original file is located at7    https://colab.research.google.com/drive/1DdebZU7Zx9pG1X7pVUgNNo20fsgAuFm08"""9 10import gradio as gr11import tensorflow as tf12from transformers import DistilBertTokenizer, TFDistilBertForSequenceClassification13 14# Initialize the tokenizer and model (assuming you've already fine-tuned it)15tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased")16model = TFDistilBertForSequenceClassification.from_pretrained("Elegbede-Distilbert_FInetuned_For_Text_Classification")17# Define a function to make predictions18def predict(texts):19 20  preprocessed_texts = []21  for text in texts:22    text = str(text).lower()23    text = th.cont_exp(text)  # Apply cont_exp function24    text = th.remove_special_chars(text)  # Remove special characters25    text = th.remove_accented_chars(text)  # Remove accented characters26    preprocessed_texts.append(text)27 28  # Join the list of preprocessed texts back into a single string29  preprocessed_text = ' '.join(preprocessed_texts)30 31  # Tokenize and preprocess the new text32  new_encodings = tokenizer(preprocessed_text, truncation=True, padding=True, max_length=70, return_tensors='tf')33 34  # Make predictions35  new_predictions = model(new_encodings)36  new_labels_pred = tf.argmax(new_predictions.logits, axis=1)37 38  labels_dict = {0: 'Sadness ๐Ÿ˜ญ', 1: 'Joy ๐Ÿ˜‚', 2: 'Love ๐Ÿ˜', 3: 'Anger ๐Ÿ˜ ', 4: 'Fear ๐Ÿ˜จ', 5: 'Surprise ๐Ÿ˜ฒ'}39 40  # Assuming 'new_labels_pred' contains the predicted class index41  predicted_emotion = labels_dict[new_labels_pred[0].numpy()]42  return predicted_emotion43 44iface = gr.Interface(45  fn=predict,46  inputs="text",47  outputs="text",48  title="Emotion Classifier",49  description="Predict the emotion from text using a fine-tuned DistilBERT model ",50)51# Launch the interfac52iface.launch()