File size: 1,350 Bytes
623c9e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()