File size: 1,862 Bytes
65579ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""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

# Initialize the tokenizer and model (assuming you've already fine-tuned it)
tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased")
model = TFDistilBertForSequenceClassification.from_pretrained("Elegbede-Distilbert_FInetuned_For_Text_Classification")
# Define a function to make predictions
def predict(texts):

  preprocessed_texts = []
  for text in texts:
    text = str(text).lower()
    text = th.cont_exp(text)  # Apply cont_exp function
    text = th.remove_special_chars(text)  # Remove special characters
    text = th.remove_accented_chars(text)  # Remove accented characters
    preprocessed_texts.append(text)

  # Join the list of preprocessed texts back into a single string
  preprocessed_text = ' '.join(preprocessed_texts)

  # Tokenize and preprocess the new text
  new_encodings = tokenizer(preprocessed_text, truncation=True, padding=True, max_length=70, return_tensors='tf')

  # Make predictions
  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 ๐Ÿ˜ฒ'}

  # Assuming 'new_labels_pred' contains the predicted class index
  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 ",
)
# Launch the interfac
iface.launch()