Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import text_hammer as th
|
4 |
+
from transformers import DistilBertTokenizer, TFDistilBertForSequenceClassification
|
5 |
+
|
6 |
+
tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased")
|
7 |
+
model = TFDistilBertForSequenceClassification.from_pretrained("Elegbede/Distilbert_FInetuned_For_Text_Classification")
|
8 |
+
# Define a function to make predictions
|
9 |
+
def predict(texts):
|
10 |
+
# Tokenize and preprocess the new text
|
11 |
+
new_encodings = tokenizer(texts, truncation=True, padding=True, max_length=70, return_tensors='tf')
|
12 |
+
new_predictions = model(new_encodings)
|
13 |
+
# Make predictions
|
14 |
+
new_predictions = model(new_encodings)
|
15 |
+
new_labels_pred = tf.argmax(new_predictions.logits, axis=1)
|
16 |
+
new_labels_pred = new_labels_pred.numpy()[0]
|
17 |
+
|
18 |
+
labels_list = ["Sadness π", "Joy π", "Love π", "Anger π ", "Fear π¨", "Surprise π²"]
|
19 |
+
emotion = labels_list[new_labels_pred]
|
20 |
+
return emotion
|
21 |
+
|
22 |
+
# Create a Gradio interface
|
23 |
+
iface = gr.Interface(
|
24 |
+
fn=predict,
|
25 |
+
inputs="text",
|
26 |
+
outputs=gr.outputs.Label(num_top_classes = 6), # Corrected output type
|
27 |
+
examples=[["Tears welled up in her eyes as she gazed at the old family photo."],
|
28 |
+
["Laughter filled the room as they reminisced about their adventures."],
|
29 |
+
["A handwritten note awaited her on the kitchen table, a reminder of his affection."],
|
30 |
+
["Harsh words were exchanged in the heated argument."],
|
31 |
+
["The eerie silence of the abandoned building sent shivers down her spine."],
|
32 |
+
["She opened the box to find a rare antique hidden inside, a total shock."]
|
33 |
+
],
|
34 |
+
title="Emotion Classification",
|
35 |
+
description="Predict the emotion associated with a text using my fine-tuned DistilBERT model."
|
36 |
+
)
|
37 |
+
# Launch the interfac
|
38 |
+
iface.launch()
|