File size: 1,296 Bytes
4d5e59d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfabe63
4d5e59d
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import gradio as gr
from transformers import pipeline

# Load the pre-trained model
classifier = pipeline("text-classification", model='kanad13/emotion_detection_model', return_all_scores=True)

def classify_emotion(text):
    # Get the predictions from the model
    predictions = classifier(text)

    # Find the emotion with the highest score
    highest_score_emotion = max(predictions[0], key=lambda x: x['score'])
    result = highest_score_emotion['label']

    return result

# Link to my blog post
blog_link = "For more details about this project, visit my [blog post](https://www.kunal-pathak.com/blog/Emotion-Detection-App/)."

# Create a Gradio interface
interface = gr.Interface(
    fn=classify_emotion,  # The function to call for predictions
    inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),  # Input component
    outputs=gr.Textbox(),  # Output component
    title="Emotion Detection in Text",
    description="Enter a sentence, and the model will predict one of the following **6 emotions: anger, fear, joy, love, sadness, or surprise.** <br> If the sentence contains an emotion not in this list of 6 emotions, the model will output the closest matching emotion.",
    article=blog_link,
    allow_flagging="never"
)

# Launch the interface
interface.launch()