Khaledmd12's picture
Update app.py
27bb954 verified
raw
history blame
2.12 kB
# -*- 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()