Spaces:
Runtime error
Runtime error
Upload sentiment_analysis.py
Browse files- sentiment_analysis.py +71 -0
sentiment_analysis.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Sentiment-analysis.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1pmME28f5Z7SNxUoX5va3W-90XP5zPnQu
|
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 |
+
Import the necessary modules :
|
18 |
+
"""
|
19 |
+
|
20 |
+
from transformers import pipeline
|
21 |
+
import gradio as gr
|
22 |
+
|
23 |
+
"""Import the pipeline :"""
|
24 |
+
|
25 |
+
sentiment = pipeline("sentiment-analysis")
|
26 |
+
|
27 |
+
"""Test the pipeline on these reviews (you can also test on your own reviews) :"""
|
28 |
+
|
29 |
+
#"I really enjoyed my stay !"
|
30 |
+
#"Worst rental I ever got"
|
31 |
+
|
32 |
+
sentiment("I really enjoyed my stay !")
|
33 |
+
|
34 |
+
"""What is the format of the output ? How can you get only the sentiment or the confidence score ?"""
|
35 |
+
|
36 |
+
print(sentiment("I really enjoyed my stay !")[0]['label'])
|
37 |
+
print(sentiment("I really enjoyed my stay !")[0]['score'])
|
38 |
+
print(sentiment("Worst rental I ever got")[0]['label'])
|
39 |
+
print(sentiment("Worst rental I ever got")[0]['score'])
|
40 |
+
|
41 |
+
"""Create a function that takes a text in input, and returns a sentiment, and a confidence score as 2 different variables"""
|
42 |
+
|
43 |
+
def get_sentiment(text):
|
44 |
+
return sentiment(text)[0]['label'], sentiment(text)[0]['score']
|
45 |
+
|
46 |
+
"""Build an interface for the app using Gradio.
|
47 |
+
The customer wants this result :
|
48 |
+
|
49 |
+

|
50 |
+
"""
|
51 |
+
|
52 |
+
interface = gr.Interface(fn=get_sentiment,
|
53 |
+
inputs=gr.Textbox(lines=1, label="Enter the review:"),
|
54 |
+
outputs=[gr.Text(label='Sentiment:'),
|
55 |
+
gr.Text(label='Score:')])
|
56 |
+
|
57 |
+
interface.launch()
|
58 |
+
|
59 |
+
ar_sentiment = pipeline("sentiment-analysis", model='CAMeL-Lab/bert-base-arabic-camelbert-da-sentiment')
|
60 |
+
|
61 |
+
ar_sentiment('انا احب ذلك')
|
62 |
+
|
63 |
+
def get_ar_sentiment(text):
|
64 |
+
return ar_sentiment(text)[0]['label'], ar_sentiment(text)[0]['score']
|
65 |
+
|
66 |
+
interface = gr.Interface(fn=get_ar_sentiment,
|
67 |
+
inputs=gr.Textbox(lines=1, label="Enter the review:"),
|
68 |
+
outputs=[gr.Text(label='Sentiment:'),
|
69 |
+
gr.Text(label='Score:')])
|
70 |
+
|
71 |
+
interface.launch()
|