anabury commited on
Commit
ef50856
·
verified ·
1 Parent(s): 8af3682

Create app.py

Browse files

New sentiment analysis

Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
3
+
4
+ # Load model
5
+ MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest"
6
+ tokenizer = AutoTokenizer.from_pretrained(MODEL)
7
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL)
8
+ sentiment_model = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
9
+
10
+ # Function for Gradio
11
+ def analyze_sentiment(text):
12
+ result = sentiment_model(text)[0]
13
+ return {
14
+ "Sentiment": result["label"],
15
+ "Confidence": f"{result['score']:.2f}"
16
+ }
17
+
18
+ # Example texts
19
+ examples = [
20
+ ["I absolutely love this new phone, the camera is stunning!"],
21
+ ["I hate the way this app keeps crashing."],
22
+ ["It’s fine, nothing special but not terrible either."],
23
+ ]
24
+
25
+ # Gradio UI
26
+ demo = gr.Interface(
27
+ fn=analyze_sentiment,
28
+ inputs=gr.Textbox(lines=3, placeholder="Type a sentence here..."),
29
+ outputs="label",
30
+ examples=examples,
31
+ title="Tweet Sentiment Analyzer",
32
+ description=""
33
+ )
34
+
35
+ if _name_ == "_main_":
36
+ demo.launch()