Meckyhugging commited on
Commit
e282368
·
verified ·
1 Parent(s): 47c203d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -26
app.py CHANGED
@@ -1,42 +1,41 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
3
 
4
- # Load sentiment analysis model
5
- sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
 
 
6
 
7
- # Custom label mapping for multi-level output
8
- label_map = {
9
- "LABEL_0": "Very Negative",
10
- "LABEL_1": "Negative",
11
- "LABEL_2": "Neutral",
12
- "LABEL_3": "Positive",
13
- "LABEL_4": "Very Positive"
14
- }
15
 
16
  def advanced_sentiment_analysis(text):
17
- # Predict sentiment
18
- result = sentiment_pipeline(text, top_k=None)[0]
19
 
20
- # Sum total scores for normalization (if needed)
21
- total_score = sum([entry['score'] for entry in result])
 
22
 
23
- # Build formatted output
24
- formatted_output = ""
25
- for entry in result:
26
- label = label_map.get(entry['label'], entry['label'])
27
- percentage = (entry['score'] / total_score) * 100
28
- formatted_output += f"{label}: {percentage:.2f}%\n"
29
 
30
- return formatted_output.strip()
 
 
 
 
 
31
 
32
  # Gradio UI
33
  with gr.Blocks() as demo:
34
- gr.Markdown("### Welcome, please enter a sample of what you may respond or tell a customer,let's tell you how cool it is")
35
- with gr.Row():
36
- text_input = gr.Textbox(lines=4, placeholder="Type your message here...", label="Customer Message")
37
  output = gr.Textbox(label="Sentiment Analysis Result")
38
  analyze_button = gr.Button("Analyze Sentiment")
39
-
40
  analyze_button.click(fn=advanced_sentiment_analysis, inputs=text_input, outputs=output)
41
 
42
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+ import torch.nn.functional as F
5
 
6
+ # Load model and tokenizer
7
+ model_name = "cardiffnlp/twitter-roberta-base-sentiment"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
10
 
11
+ # Label map for 3-level sentiment
12
+ labels = ['Negative', 'Neutral', 'Positive']
 
 
 
 
 
 
13
 
14
  def advanced_sentiment_analysis(text):
15
+ # Tokenize input
16
+ inputs = tokenizer(text, return_tensors="pt", truncation=True)
17
 
18
+ # Get model logits
19
+ with torch.no_grad():
20
+ logits = model(**inputs).logits
21
 
22
+ # Convert logits to probabilities
23
+ probs = F.softmax(logits, dim=1)[0]
 
 
 
 
24
 
25
+ # Format result
26
+ results = ""
27
+ for i, prob in enumerate(probs):
28
+ results += f"{labels[i]}: {prob.item() * 100:.2f}%\n"
29
+
30
+ return results.strip()
31
 
32
  # Gradio UI
33
  with gr.Blocks() as demo:
34
+ gr.Markdown("### Welcome, please enter a sample of what you may respond or tell a customer, let's tell you how cool it is")
35
+ text_input = gr.Textbox(lines=4, placeholder="Type your message here...", label="Customer Message")
 
36
  output = gr.Textbox(label="Sentiment Analysis Result")
37
  analyze_button = gr.Button("Analyze Sentiment")
38
+
39
  analyze_button.click(fn=advanced_sentiment_analysis, inputs=text_input, outputs=output)
40
 
41
  demo.launch()