# 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)