from transformers import pipeline import gradio as gr # Load sentiment and emotion models sentiment_model = "cardiffnlp/twitter-roberta-base-sentiment" emotion_model = "bhadresh-savani/bert-base-uncased-emotion" sentiment_pipeline = pipeline("sentiment-analysis", model=sentiment_model, tokenizer=sentiment_model) emotion_pipeline = pipeline("text-classification", model=emotion_model, tokenizer=emotion_model) # Function to analyze sentiment and emotion def analyze_text(text): sentiment_result = sentiment_pipeline(text)[0] emotion_result = emotion_pipeline(text)[0] return { "Sentiment": {sentiment_result['label']: sentiment_result['score']}, "Emotion": {emotion_result['label']: emotion_result['score']} } # Gradio interface demo = gr.Interface( fn=analyze_text, inputs=gr.Textbox(placeholder="Enter your text here...", label="Input Text"), outputs=gr.Label(label="Analysis Results"), title="Sentiment and Emotion Analysis", description="Analyze the sentiment and emotion of your text using advanced NLP models.", examples=[ ["I'm thrilled to start this new adventure!"], ["This situation is making me really frustrated."], ["I feel so heartbroken and lost."] ], theme="soft" ) # Deploy the application if __name__ == "__main__": demo.launch()