File size: 2,116 Bytes
27bb954
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa6bce2
 
27bb954
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa6bce2
27bb954
 
 
 
fa6bce2
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""Copy of [Student]_Module_2_Session_3.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1pmME28f5Z7SNxUoX5va3W-90XP5zPnQu

Installations
"""

!pip install gradio --quiet
!pip install transformers --quiet

"""#Let's build a demo for a sentiment analysis task !

Import the necessary modules :
"""

from transformers import pipeline
import gradio as gr

"""Import the pipeline :"""

sentiment = pipeline("sentiment-analysis")

"""Test the pipeline on these reviews (you can also test on your own reviews) :"""

#"I really enjoyed my stay !"
#"Worst rental I ever got"

sentiment("I really enjoyed my stay !")

"""What is the format of the output ? How can you get only the sentiment or the confidence score ?"""

print(sentiment("I really enjoyed my stay !")[0]['label'])
print(sentiment("I really enjoyed my stay !")[0]['score'])
print(sentiment("Worst rental I ever got")[0]['label'])
print(sentiment("Worst rental I ever got")[0]['score'])

"""Create a function that takes a text in input, and returns a sentiment, and a confidence score as 2 different variables"""

def get_sentiment(text):
  return sentiment(text)[0]['label'], sentiment(text)[0]['score']

"""Build an interface for the app using Gradio."""
The customer wants this result :

interface = gr.Interface(fn=get_sentiment,
                         inputs=gr.Textbox(lines=1, label="Enter the review:"),
                         outputs=[gr.Text(label='Sentiment:'),
                                  gr.Text(label='Score:')])

interface.launch()

ar_sentiment = pipeline("sentiment-analysis", model='CAMeL-Lab/bert-base-arabic-camelbert-da-sentiment')

ar_sentiment('انا احب ذلك')

def get_ar_sentiment(text):
  return ar_sentiment(text)[0]['label'], ar_sentiment(text)[0]['score']

interface = gr.Interface(fn=get_ar_sentiment,
                         inputs=gr.Textbox(lines=1, label="Enter the review:"),
                         outputs=[gr.Text(label='Sentiment:'),
                                  gr.Text(label='Score:')])

interface.launch()