Update app.py
Browse files
app.py
CHANGED
|
@@ -6,13 +6,26 @@ learn = from_pretrained_fastai("haripriyaram/Text-emotion-Recognizer-Model")
|
|
| 6 |
|
| 7 |
# Prediction function
|
| 8 |
def predict_emotion(text):
|
| 9 |
-
pred_label, _, probs = learn.predict(text)
|
|
|
|
| 10 |
|
| 11 |
-
# Handle nested vocab if needed
|
| 12 |
-
vocab = learn.dls.vocab[0] if isinstance(learn.dls.vocab[
|
| 13 |
-
probs_dict = {label: float(prob) for label, prob in zip(vocab, probs)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
return
|
| 16 |
|
| 17 |
# Gradio UI
|
| 18 |
iface = gr.Interface(
|
|
@@ -20,9 +33,9 @@ iface = gr.Interface(
|
|
| 20 |
inputs=gr.Textbox(lines=2, placeholder="Enter a sentence..."),
|
| 21 |
outputs=[
|
| 22 |
gr.Label(label="Predicted Emotion"),
|
| 23 |
-
|
| 24 |
],
|
| 25 |
-
title="🎭 Emotion Classifier (
|
| 26 |
description="Enter a sentence and the model will predict the corresponding emotion.",
|
| 27 |
allow_flagging="never"
|
| 28 |
)
|
|
|
|
| 6 |
|
| 7 |
# Prediction function
|
| 8 |
def predict_emotion(text):
|
| 9 |
+
#pred_label, _, probs = learn.predict(text)
|
| 10 |
+
# pred, pred_idx, probs = learn.predict(text)
|
| 11 |
|
| 12 |
+
# # Handle nested vocab if needed
|
| 13 |
+
# vocab = learn.dls.vocab[0] if isinstance(learn.dls.vocab[1], list) else learn.dls.vocab
|
| 14 |
+
# probs_dict = {label: float(prob) for label, prob in zip(vocab, probs)}
|
| 15 |
+
# Perform prediction on the input text
|
| 16 |
+
prediction, class_index, probabilities = learn.predict(text)
|
| 17 |
+
|
| 18 |
+
# Get the list of class labels
|
| 19 |
+
class_labels = learn.dls.vocab
|
| 20 |
+
|
| 21 |
+
# Create a dictionary of probabilities
|
| 22 |
+
probs_dict = dict(zip(class_labels, probabilities.tolist()))
|
| 23 |
+
|
| 24 |
+
print(f"Input text: {input_text}")
|
| 25 |
+
print(f"Predicted emotion: {prediction}")
|
| 26 |
+
print(f"Probabilities: {probs_dict}")
|
| 27 |
|
| 28 |
+
return prediction, probs_dict
|
| 29 |
|
| 30 |
# Gradio UI
|
| 31 |
iface = gr.Interface(
|
|
|
|
| 33 |
inputs=gr.Textbox(lines=2, placeholder="Enter a sentence..."),
|
| 34 |
outputs=[
|
| 35 |
gr.Label(label="Predicted Emotion"),
|
| 36 |
+
gr.JSON(label="Confidence Scores")
|
| 37 |
],
|
| 38 |
+
title="🎭 Emotion Classifier (ML Service)",
|
| 39 |
description="Enter a sentence and the model will predict the corresponding emotion.",
|
| 40 |
allow_flagging="never"
|
| 41 |
)
|