Meckyhugging commited on
Commit
afc1774
·
verified ·
1 Parent(s): 1b9c0ec

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()