Spaces:
Configuration error
Configuration error
Upload sentiment_analysis_m2_s3_(3).py
Browse files- sentiment_analysis_m2_s3_(3).py +139 -0
sentiment_analysis_m2_s3_(3).py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""sentiment_analysis_M2_S3 (3).ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/122LsK0EllcEargr6R8LmbYYtcnCb5PV6
|
8 |
+
|
9 |
+
Installations
|
10 |
+
"""
|
11 |
+
|
12 |
+
!pip install gradio --quiet
|
13 |
+
!pip install transformers --quiet
|
14 |
+
|
15 |
+
"""#Let's build a demo for a sentiment analysis task !
|
16 |
+
|
17 |
+
---
|
18 |
+
|
19 |
+
Import the necessary modules :
|
20 |
+
"""
|
21 |
+
|
22 |
+
import numpy as np
|
23 |
+
import gradio as gr
|
24 |
+
from transformers import pipeline
|
25 |
+
|
26 |
+
"""Import the pipeline :"""
|
27 |
+
|
28 |
+
sentiment =pipeline("sentiment-analysis", verbose = 0)
|
29 |
+
|
30 |
+
"""Test the pipeline on these reviews (you can also test on your own reviews) :"""
|
31 |
+
|
32 |
+
reviews= ["I really enjoyed my stay !", "Worst rental I ever got"]
|
33 |
+
|
34 |
+
"""What is the format of the output ? How can you get only the sentiment or the confidence score ?"""
|
35 |
+
|
36 |
+
sentiment(reviews)
|
37 |
+
|
38 |
+
"""Create a function that takes a text in input, and returns a sentiment, and a confidence score as 2 different variables"""
|
39 |
+
|
40 |
+
def sentiment(prompt):
|
41 |
+
# This is where you would integrate with an actual sentiment analysis model
|
42 |
+
# For this example, we'll use simple rules to simulate the behavior
|
43 |
+
if "good" in prompt.lower():
|
44 |
+
return [{'label': 'Positive', 'score': 0.9}]
|
45 |
+
elif "bad" in prompt.lower():
|
46 |
+
return [{'label': 'Negative', 'score': 0.9}]
|
47 |
+
else:
|
48 |
+
return [{'label': 'Neutral', 'score': 0.5}]
|
49 |
+
|
50 |
+
def get_sentiment(prompt):
|
51 |
+
result = sentiment(prompt)
|
52 |
+
return result[0]['label'], result[0]['score']
|
53 |
+
|
54 |
+
"""Build an interface for the app using Gradio.
|
55 |
+
The customer wants this result :
|
56 |
+
|
57 |
+

|
58 |
+
"""
|
59 |
+
|
60 |
+
textbox = gr.Textbox(label="Enter the review:")
|
61 |
+
textbox_sen = gr.Textbox(label="Sentiment")
|
62 |
+
textbox_score = gr.Textbox(label="Score")
|
63 |
+
|
64 |
+
interface = gr.Interface(
|
65 |
+
fn=get_sentiment,
|
66 |
+
inputs=textbox,
|
67 |
+
outputs=[textbox_sen, textbox_score],
|
68 |
+
title="Sentiment Analysis Prototype"
|
69 |
+
)
|
70 |
+
|
71 |
+
interface.launch()
|
72 |
+
|
73 |
+
"""## Arabic sentiment analysis"""
|
74 |
+
|
75 |
+
sa = pipeline('text-classification', model='CAMeL-Lab/bert-base-arabic-camelbert-da-sentiment')
|
76 |
+
|
77 |
+
def sentiment(prompt):
|
78 |
+
result = sa(prompt)
|
79 |
+
return result
|
80 |
+
|
81 |
+
def get_sentiment(prompt):
|
82 |
+
result = sentiment(prompt)
|
83 |
+
label = result[0]['label']
|
84 |
+
score = result[0]['score']
|
85 |
+
return label, score
|
86 |
+
|
87 |
+
textbox = gr.Textbox(label="قم بادخال الرأي:")
|
88 |
+
textbox_sen = gr.Textbox(label="الشعور")
|
89 |
+
textbox_score = gr.Textbox(label="النسبة")
|
90 |
+
|
91 |
+
interface = gr.Interface(
|
92 |
+
fn=get_sentiment,
|
93 |
+
inputs=textbox,
|
94 |
+
outputs=[textbox_sen, textbox_score],
|
95 |
+
title="النموذج الأولي لتحليل المشاعر"
|
96 |
+
)
|
97 |
+
|
98 |
+
interface.launch()
|
99 |
+
|
100 |
+
"""## classify sentiments expressed through text or emojis:
|
101 |
+
|
102 |
+
"""
|
103 |
+
|
104 |
+
import gradio as gr
|
105 |
+
from transformers import pipeline
|
106 |
+
|
107 |
+
# Initialize the sentiment analysis pipeline with a potentially better model
|
108 |
+
model_name = "cardiffnlp/twitter-roberta-base-sentiment"
|
109 |
+
sa = pipeline('sentiment-analysis', model=model_name)
|
110 |
+
|
111 |
+
def classify_emoji(prompt):
|
112 |
+
result = sa(prompt)
|
113 |
+
label = result[0]['label']
|
114 |
+
score = result[0]['score']
|
115 |
+
|
116 |
+
# Map the label to a user-friendly sentiment
|
117 |
+
if label == 'LABEL_2': # Assuming LABEL_2 is positive
|
118 |
+
sentiment = "Positive"
|
119 |
+
elif label == 'LABEL_0': # Assuming LABEL_0 is negative
|
120 |
+
sentiment = "Negative"
|
121 |
+
elif label == 'LABEL_1': # Assuming LABEL_1 is neutral
|
122 |
+
sentiment = "Neutral"
|
123 |
+
|
124 |
+
return sentiment, f"{score:.2f}"
|
125 |
+
|
126 |
+
textbox = gr.Textbox(label="Enter the emoji or text:")
|
127 |
+
textbox_sen = gr.Textbox(label="Sentiment")
|
128 |
+
textbox_score = gr.Textbox(label="Confidence Score")
|
129 |
+
|
130 |
+
interface = gr.Interface(
|
131 |
+
fn=classify_emoji,
|
132 |
+
inputs=textbox,
|
133 |
+
outputs=[textbox_sen, textbox_score],
|
134 |
+
title="Emoji Sentiment Classification",
|
135 |
+
description="Enter an emoji or text to classify its sentiment. The model will return the sentiment and a confidence score.",
|
136 |
+
theme="compact"
|
137 |
+
)
|
138 |
+
|
139 |
+
interface.launch()
|