File size: 2,125 Bytes
ab31676
 
 
7c493ae
 
38509eb
ab31676
7c493ae
a9c47e0
 
 
 
 
7c493ae
 
a9c47e0
 
 
 
74759ef
a9c47e0
 
ab31676
 
a9c47e0
7c493ae
 
 
5ac858c
 
f4adf8a
7c493ae
f4adf8a
6962b0d
2ee729f
5ac858c
 
 
 
a9c47e0
 
 
5ac858c
ab31676
a9c47e0
 
 
 
 
 
 
 
5ac858c
 
a9c47e0
 
2ee729f
 
 
 
 
a9c47e0
 
 
ab31676
7c493ae
74759ef
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# app.py
import gradio as gr
from transformers import pipeline
import logging
import sys
import pandas as pd

# Set up logging
logging.basicConfig(
    level=logging.INFO,
    stream=sys.stdout,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Initialize the pipeline
logger.info("Initializing emotion classification pipeline...")
classifier = pipeline(
    "text-classification",
    model="bhadresh-savani/distilbert-base-uncased-emotion"
)
logger.info("Pipeline initialized successfully")

def predict_emotion(text):
    """Predict emotions from text and return formatted results."""
    try:
        if not text:
            logger.warning("Empty text received")
            return {}

        # Get predictions and handle result structure
        logger.info("Running prediction...")
        predictions = classifier(text)
        logger.debug(f"Raw predictions output: {predictions}")
        
        # Process predictions into a dict for display
        scores = {item['label']: item['score'] for item in predictions}
        logger.info(f"Processed scores: {scores}")
        return scores
        
    except Exception as e:
        logger.error(f"Error in prediction: {str(e)}")
        return {"error": "An error occurred during emotion prediction"}

# Create the Gradio interface
demo = gr.Interface(
    fn=predict_emotion,
    inputs=gr.Textbox(
        placeholder="Enter text to analyze...", 
        label="Input Text",
        lines=4
    ),
    outputs=gr.JSON(),  # Display the scores in a JSON format
    title="CREATIVE MACHINES: Emotion Detection with DistilBERT",
    description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter any text to analyze its emotional content.",
    examples=[
        "I am so happy to see you!",
        "I'm really angry about what happened.",
        "The sunset was absolutely beautiful today.",
        "I'm worried about the upcoming exam.",
        "Fear is the mind-killer. I will face my fear."
    ],
    allow_flagging="never"
)

if __name__ == "__main__":
    demo.launch(debug=True)