File size: 785 Bytes
edabb58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import gradio as gr
from transformers import pipeline

# Initialize sentiment analysis pipeline
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")

def analyze_sentiment(text):
    if not text.strip():
        return "Please enter text to analyze."
    result = sentiment_analyzer(text)[0]
    return f"Sentiment: {result['label']} (Confidence: {result['score']:.2f})"

# Gradio interface
demo = gr.Interface(
    fn=analyze_sentiment,
    inputs=gr.Textbox(label="Enter text", placeholder="Type your text here..."),
    outputs=gr.Textbox(label="Sentiment Result"),
    title="Sentiment Analysis App",
    description="Analyze the sentiment of your text (positive or negative)."
)

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