Spaces:
Runtime error
Runtime error
Upload app (1).py
Browse files- app (1).py +45 -0
app (1).py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the sentiment analysis pipeline from Hugging Face Hub
|
| 5 |
+
classifier = pipeline("sentiment-analysis", model="Arvind111/sentiment_newclassifier")
|
| 6 |
+
|
| 7 |
+
# Define the label mapping from model output to human-readable emotions with emojis
|
| 8 |
+
# Based on previous tests: LABEL_0 -> Neutral, LABEL_1 -> Sad, LABEL_2 -> Happy
|
| 9 |
+
label_mapping = {
|
| 10 |
+
'LABEL_0': 'Neutral \ud83d\ude10',
|
| 11 |
+
'LABEL_1': 'Sad \ud83d\ude1e',
|
| 12 |
+
'LABEL_2': 'Happy \ud83d\ude0a'
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
def predict_emotion(text):
|
| 16 |
+
if not text:
|
| 17 |
+
return "Please enter some text."
|
| 18 |
+
|
| 19 |
+
# Get prediction from the pipeline
|
| 20 |
+
predictions = classifier(text)
|
| 21 |
+
|
| 22 |
+
if predictions:
|
| 23 |
+
predicted_label_id = predictions[0]['label']
|
| 24 |
+
predicted_score = predictions[0]['score']
|
| 25 |
+
|
| 26 |
+
# Map to human-readable label with emoji
|
| 27 |
+
emotion_label = label_mapping.get(predicted_label_id, "Unknown \ud83e\udd14")
|
| 28 |
+
|
| 29 |
+
return f"Prediction: {emotion_label} (Score: {predicted_score:.4f})"
|
| 30 |
+
else:
|
| 31 |
+
return "Could not get prediction."
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Create the Gradio interface
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=predict_emotion,
|
| 37 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
| 38 |
+
outputs="text",
|
| 39 |
+
title="Emotion Prediction with BERT",
|
| 40 |
+
description="Enter a sentence and the model will predict its primary emotion (Happy, Sad, or Neutral)."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Launch the Gradio interface
|
| 44 |
+
if __name__ == '__main__':
|
| 45 |
+
iface.launch(share=False)
|