File size: 1,053 Bytes
f415673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1266714
f415673
 
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline

# Load model
MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest"
tokenizer = AutoTokenizer.from_pretrained(MODEL)
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
sentiment_model = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)

# Function for Gradio
def analyze_sentiment(text):
    result = sentiment_model(text)[0]
    return {
        "Sentiment": result["label"],
        "Confidence": f"{result['score']:.2f}"
    }

# Example texts
examples = [
    ["I absolutely love this new phone, the camera is stunning!"],
    ["I hate the way this app keeps crashing."],
    ["It’s fine, nothing special but not terrible either."],
]
# Gradio UI
demo = gr.Interface(
    fn=analyze_sentiment,
    inputs=gr.Textbox(lines=3, placeholder="Type a sentence here..."),
    outputs="label",
    examples=examples,
    title="Sentiment Analyzer",
    description=""
)

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